Wednesday, April 13, 2011

Anatomy of a XNA project

So, you want to do some graphics programming in XNA 4.0 huh? Well first of all you have to create a new project. Yup, this is what this post is all about. Just hit File->New->Project.

Before you start I hope you have installed XNA Game Studio on your computer, you can find the download link here. I also recommend downloading and installing NShader (pretty little VS plug-in to highlight your shader code) and the latest DirectX SDK. Now that we have everything set up just go and start Visual Studio. Hit File->New->Project->Visual C#->XNA Game Studio X.X->Windows Game. Here you can select the name and location of your project.

You will see that Visual Studio created 2 projects for you. The first one has the same name as the solution and is your actual game. The second one has Content attached to the end of the name, this is because it is a Game Content project. This project will have all your game assets (textures, models, shaders etc.). It will use content importers and processors (different for each type of content) to load the files in your game.
Our main project as you can see has 2 code files Program.cs and Game1.cs.

If you open up Program.cs you will find main and see that the only thing it does is run our Game1 class, so we don't have any business here (at least for now). If we open the Game1.cs file we will find a a class with a few useful methods. These methods are called when they need to be called by the framework so we don't have to worry about it. The first of our methods is the constructor, the constructor sets our GraphicsDeviceManager and our ContentManager root folder. The GraphicsDeviceManager class is a class that contains all the settings and references to our video card. So if you ever need to change a render state or call a draw function, you will use an object of this type (we already have one called graphics, I will assume that you didn't change this name in all my tutorials).

XNA simple game loop
XNA simple game loop

After it we will find Initialize, this method is called immediately after the constructor, so you can put all your initialization code here. Further down we can see LoadContent, this method should be used to load all game assets from disk and is called right after Initialize.

After it you will see UnloadContent, this method should be used to unload any files we loaded in LoadContent BUT remember that anything loaded with the XNA content pipeline gets automatically unloaded here (so you don't have to unload them) and C# runs in managed code so the .NET Framework has automatic garbage collection so the you will have to write very little code (or none at all) in this method. The UnloadContent method is run just before you exit the game.

Now you will find Update and Draw, these 2 are called again and again (each frame) as long as the game runs. In update you should put all your game logic (AI, physics, user input, collisions etc.) and Draw should be used for drawing items to the screen. XNA prioritizez Update, what I mean is that when the frame rate drops under the current setting the game will skip a draw call to increase the frame rate, this ensures that our game logic doesn't loose it's consistency while we  still have a decent frame rate.

This is the basic setup of our XNA game. I will go in more detail of each element here when I will need them.

No comments:

Post a Comment