XNA_Framework_Overview

== I will be using the Microsoft XNA 3.0 Framework along with Visual Studios 2008. This paper will demonstrate the abilities I have gained from reading the XNA Game Studio Document file and various web cast from the XNA Creators Club Online. I will also mention a couple things about Microsoft XNA 2.0 framework.   == The follow items will explain how to create your project game:  1.) Launch the XNA Game Studio Environment (i.e. XNA Game Studio Express). You will see a similar development environment to that of every other Microsoft Visual Studios environment but with some extra bells, whistles, and plug-ins needed for the game programming experience. 2.)  Click file menu and select a new project. 3.) Select the “Windows Game” project template. 4.)  Give your project a name and click ok. 5.) Run your game by pressing the play button or F5.  <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">Yes, it is that easy. While you may have no content or logic the setting up communications with the graphics device has been completed for you by our development environment. Granted you see a blue screen, it is not the blue screen of death we are all familiar with. These simple steps eliminate the need of having to manually establish communication with your graphics device and DirectX. Now you the developer can spend more time focusing on game logic. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 28.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-font-family: "Times New Roman"; mso-bidi-font-size: 14.0pt; mso-bidi-font-style: italic; mso-bidi-language: AR-SA; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-US;">

<span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">I feel it is important to talk about the following pre-existing methods for a moment. We are going to talk about 5 of the important essential game methods. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: Arial,sans-serif; font-size: 12pt; line-height: 115%;">1.) <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">initialize <span style="font-family: Arial,sans-serif; font-size: 12pt; line-height: 115%;">2.)  <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">loadContent <span style="font-family: Arial,sans-serif; font-size: 12pt; line-height: 115%;">3.) <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">unloadContent <span style="font-family: Arial,sans-serif; font-size: 12pt; line-height: 115%;">4.)  <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">update(GameTime gameTime) <span style="font-family: Arial,sans-serif; font-size: 12pt; line-height: 115%;">5.) <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">draw(GameTime gameTime) <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">

<span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">The initialize method is called after the game and graphics device has been created and before and graphics resources have been loaded. It is here you will query any graphic services and load any non-graphic services. It is important to call the base class initialize method. The base initialize method enumerates any graphics components that have been registered for the game and calls their initialize methods. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 20.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-font-family: "Times New Roman"; mso-bidi-font-size: 13.0pt; mso-bidi-language: AR-SA; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-US;">

<span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%; mso-no-proof: yes;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">LoadContent is called by the game whenever graphics device needs to be initialized. This occurs when a game is started or reloaded (like when you reload your XBOX). Note: the parameter **loadAllContent** is from the XNA 2.0 framework and is not included in the 3.0 framework. The parameter loadAllContent determines if the content will be loaded automatically or manually. These are defined by an enumerator and are different resource modes. If loadAllContent equals true, both modes are loaded, if it is false, only the manual resources are loaded. Behind the scenes this is where you load sprites from the content pipeline. Microsoft has helped us out here in a big way, for it is here the content pipeline loads for data from backgrounds, models, music, sprite sheets, textures or XML files become more standardized. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">

<span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">This function is the same thing as loadContent but it does the inverse and unloads the resources. The parameter unloadAllContent (2.0 framework) determines if all the content will be unloaded automatically or just the manually pool. <span style="font-family: "Arial","sans-serif"; font-size: 20.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-font-family: "Times New Roman"; mso-bidi-font-size: 13.0pt; mso-bidi-language: AR-SA; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-US;">

<span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%; mso-no-proof: yes;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%; mso-no-proof: yes;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%; mso-no-proof: yes;">The update method is called at a set interval while the game is running and is responable for processing your core game logic. Examples of what update does are move the world around check boundries, check collisions, update sprites and gather input for just a few. Traditional game development prior to XNA Game Studio express had traditional problems. The one we are going to touch base with is the game loop. There have been long and drawn out debates about the game loop and the most effiecient way to handle them. Hours can be spent with the game loop alone. So what is the game loop? A game loop in a nutshell is/can be a delicate algorythim of when to update your logic opposed to when to draw or render the screen. XNA Game Studio has given us the Update method, one of two methods to handle the game loop. The other method is the draw method which we will talk about momentarily. The best thing about these two functions (update and draw) is that they are called automatically on a continous bases during the game’s life. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">The base initialize method enumerates any graphics components that have been registered for the game and call their update methods to insure they are being handled correctly. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 20.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-font-family: "Times New Roman"; mso-bidi-font-size: 13.0pt; mso-bidi-language: AR-SA; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-US;">

<span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">Ah yes, the draw method. The draw method is called when the game determines it is time to draw a frame. The method update is called at a different rate than the draw method if **IsFixedTimeStep** is set to true. The update method will be called at an interval specified by the **TargetElapsedTime** and the draw method will be call as often as possible. The default for TargetElapsedTime is 1/60th of a second. If isFixedTimeStep is false, the method update and the method draw will be called sequentially and as often as possible. The sequence would look like this (update, draw, update, draw, update, draw). This is where the gameTime parameter comes into play. This is known as a “Variable Step Game.” The gameTime parameter will give you a snap shot of when the last update or draw was called. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">*note: The games developed my XNA Game Express can run on an XBOX at 1080p at 30 frames per second. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> == <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">Adding assets is as easy as adding your assets to Content node in the project tree in the solution file. The most common files that can be added as art assets are .x, .fbx or .tga files. When you add an asset file, the content pipeline attempts to identify the asset and build it when the project is built. For .fbx models the content pipeline imports and processes the file when the project is built and it will be turned into a model object we can use in code. The .tga files do not need to be included for 3D projects because the content pipeline will pick them up automatically when it processes a model that will render them. Do not delete the .tga files, only exclude them from the project. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">Microsoft is in the middle of creating a tool convert you files into a matched file format. This tool is supposed to support many, many graphic tools according to project lead of XNA development. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">Note: <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">You can add raw audio files into the project folder and then exclude them as you did with the .tga files. It is a good idea to create an audio folder to house all audio files. Audio files are not loaded directly through the content pipeline. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">

<span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">3D terrain is a collection of connected 3D points called meshes. 3D worlds use a lot of mathematical structures called matrix which are represented in XNA Framework by a class called Matrix. The matrix describes a range of transformations that can be applied to a point, a set of points, or even a vector. The use of matrix allows us to move, rotate and scale 3D models. Matrix also allows us to draw 2D images that can be displayed on the screen through the use of special matrices called view matrix and project matrices. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">Below is a simple image taken from the XNA creators’ website to represent the X, Y and X axis. Every model has an X, Y and X coordinate or collection of coordinates in the 3D world. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">From the model you can see that XNA Studios use a right hand coordinate system. Of course in the 2D world there are only X and Y coordinates. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">

<span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">The model class provided by XNA framework represents a 3D model that is a collection of 3D meshes. A mesh is like a 3D sculpture of an object whether it is a skeleton, bear, plane or a magic flying carpet. A bear model collection may contain a mesh for its skin, eyes, claws and teeth. A car model will have a mesh collection of hood, door, steering wheel, tire, and hood ornaments. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">

<span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">The vector2 and vector3 class provided by XNA framework are a handy way of handling 2d and 3d coordinates. Vector3.zero will represent (0, 0, 0) the direct center of our 3D world. Vector3 used are good for handling camera position and what the camera is looking at. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">

<span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">A matrix is a mathematical construct that defines a 3 dimensional transformation. We use matrices and apply them to vector3 to rotate, move or scale models. The matrix class has allows us to create view matrix (Matrix.CreateLookAt) and perspective fields of view (Matrix.CreatePerspectiveFieldOfView). <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">

<span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">It is a good idea to start with creating two matrixes to maintain the position of your camera and the camera projection. You will also want to create a function to render your models in a generic way. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="background: silver; color: gray; font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%; mso-highlight: silver; mso-no-proof: yes;"> <span style="background: yellow; font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%; mso-highlight: yellow;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="color: blue; font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%; mso-no-proof: yes;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">Now that everything is defined we need to draw the model we will still need to load the model. Look at the code below to see how to load a model. We need to initialize the model as well. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%; mso-no-proof: yes;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">We will load models in the content pipeline. The models in this example will be loaded in as a Model type not a Texture2D type. This is a 3D model; 2D models will be loaded as a Texture2D type. The last thing to do is to call the draw method. You can see in the draw method we call our defined DrawModel method from above which will draw our terrain to screen each time the draw method is called. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> == <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">At this point I would like to move on to player inputs. So far we have gone over the loading of models and game content in the XNA Game Studio. We have loaded our terrain and we have loaded a model, but how do we move those models around in our 3D world? Once again XNA Game Studio provides us with a little class to help us out. This is one of the easier classes to deal with in the XNA Game Studio environment. It is called very easily by typing “Keyboard”, “Keyboard.GetState” for that matter. Below is some sample code. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">The Keyboard object is ready to use on a whims notice, there is no need to create it or initialize the static class for XNA Studios has handled everything we need for the class already. XNA Studios has done similar objects for the X-Box controller or gamepad and the mouse. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> =<span style="mso-bookmark: _Toc222054102;"> = <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">Collision detection taken from Wikipedia is as follows “involves algorithms for checking for [|collision], i.e. intersection, of two given solids. Simulating what happens once a collision is detected is sometimes referred to as "collision response", for which see [|physics engine] and [|ragdoll physics]. Collision detection algorithms are a basic component of 3D video games. Without them, characters could go through walls and other obstacles.” <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">An important thing to remember about collision detection is we have to wait until all updates are done on all models. If all models are not updated, then we cannot rely on the detection to be correct. Detection checked before all models are update may result in a collision that will not be detected due to it not being in the correct place when the collision algorithm was fired or even a collision detected due to a model not being moved before the algorithm was fired. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">We can use Rectangle.Intersect as a rudimentary collision check for 2D games. We will first need to create Rectangles for our models to check the collision against, and use Rectangle.Intersect method. <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">We use sphere or BoundingSphere for 3D games. Spheres are not incredibly accurate unless your model is a sphere such as a round ball or perhaps a force field. When using 3D models it is important to remember to get the bounding spheres center and you will set that to its position. Then get the model sphere radius multiplied by the scale. You will have do that for both or all models you want to detect collision against. The next step is to check if your model objects intercept with another object model by using [model object name]. intercept ([another model object name]). <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;"> <span style="font-family: "Arial","sans-serif"; font-size: 12.0pt; line-height: 115%;">The only thing to worry about then is what our model does when our objects collide. You may need to stop the models from moving in that direction or end the game, subtract health points or make some collision noises.