I know it took me quite a bit to go and get me a simple cup of coffee...but now I'm back. I've completed some first small steps understanding the fundamentals of a XNA game and will use this post to document what I've learned.
So let's go.
We're going to create a "Windows Game(4.0)" project which can be found in the standard "create new project" dialog of Visual Studio 2010 (as I described in my last post).
You'll notice that this project template is filled with quite a bit more content from the start compared to
a standard C# project. But no need to worry, it's not too complicated to find your way around when you take a look at the separate components.
*During the following explanation I will be using the standard project and class names generated by Visual Studio 2010 (WindowsGame1, Game1 etc...) so apply your names as needed*
We'll start on the project level of our generated solution:
A standard XNA Windows Game solution consists of two different projects:
WindowsGame1 - this project will be used to write game logic, load textures and models, acquire computer resources - in short this is where the magic happens.
WindowsGame1Content - remember I just mentioned we will be able to load textures, models and other neat stuff? Well this is where we're going to store it. This project is used to refer to all game resources that can be declared as content (sprites, models, music, animations etc.).
Now that wasn't so bad now was it?
If you open the content project, you'll see that aside from a couple of references, the project is completely empty. We'll take a look at how to load and render content a little later.
First we'll take a look at the classes generated in the WindowsGame1 project and find out a XNA Windows Game works.
The following is a class diagram of the WindowsGame1 project (compliments of Visual Studio 2010, just right-click on the project in the solution explorer, select "view class diagram" in the dialog and voila).
So let's get to know our two new classes shall we?
Program - This static class is used as the entry point of the XNA Windows Game. In Program an instance of our Game1 is initialized and told to Run. That's all we need to start our video game.And for now that's all we need to know about Program and until we add anything to it, that's all it does:
It starts our game.
Game1 - This class is where it all comes together, the loading of content, evaluation of game logic, rendering of the video game, reading user inputs....you see where I'm going with this? :) But again, if we take a look at the provided methods of our Game1 class, we'll see it's not as complicated as one would think.
Now that we know what the separate projects are for and what the standard classes are used for, let's open up Game1 and see how a XNA Video Game works.
Here are the 5 basic methods that can be used to manage a whole game:
The first three methods are called once during a XNA Video Game, and are used to manage the content used to construct the video game.
Initialize() - This is the first method called when Game1.Run() is called from the Program class. It's purpose is is to load non-graphical resources (stuff you won't be drawing :P) and to query if needed services are present (Web, Network, etc.).
LoadContent() - Once Initialize() is finished, this method is automatically called to load all graphical content (you guessed it, everything that will be displayed on-screen).
UnloadContent() - This is called as a clean-up method to unload all the content we loaded at the beginning of the game.
The final two methods, are placed inside of a never-ending loop. These are used control and display the Video Game.
Update() - This is where the game logic is evaluated. Movement, collisions, scoring - all that kind of logic is placed in this method.
Draw() - And finally, this method contains all the logic needed to display are game content.
And that's all folks.
I tried to keep this post pretty abstract without going into the code (yet). Before I started on this post, I fought my way through the MSDN documentation. Doing this helped a lot in understanding how the separate parts of XNA Video Game work together.
In my next post we'll be writing our first little game and tackling the following subjects along the way:
- How to load content into a game
- Rendering the loaded game content
- Evaluating user input
I hope this post helped in taking a little bit of the magic out of the inner-workings of a video game. Next time we'll be seeing some first results. I promise :)