Monday, October 06, 2008

XNA Part 1 - Program Structure

To begin, we fire up C# 2005 and choose to create a new project. We will choose a Windows Game 2.0 project.

A new project is created for us with the boilerplate for a basic Windows XNA game. The file Game1.cs is opened for us (dbl click it if it is not already open) you will see at the top a block of using statements

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

These import several namespaces from the XNA framework into our current namespace to make coding a little easier.

Now, lets look at some more code.

public class Game1 : Microsoft.Xna.Framework.Game
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

public Game1()
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";

Our primary game class is a derived class of Microsoft.Xna.Framework.Game
We have some private members for our class to work with, a GraphicsDeviceManager called graphics and a SpriteBatch called spriteBatch (imagine that) We will talk about what those are for a little later.

Now we define our default constructor for our Game1 class. The constructor does 2 things, it allocates a GraphicsDeviceManager for our class and sets a root directory for our game assets. You will notice in the "Solution Explorer" a part called
"Content". This is the base folder for our content pipeline. We will talk more about our content pipeline in the future. It makes your life a little easier.

protected override void Initialize()

protected override void LoadContent()
spriteBatch = new SpriteBatch(GraphicsDevice);

protected override void UnloadContent()

protected override void Update(GameTime gameTime)
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==

protected override void Draw(GameTime gameTime)

Now for the instance methods of our game class that we override from the base class. There are only 5 of them, not too tough!

Initialize is called once at the beginning after the constructor, but before LoadContent, so we can set stuff up non content stuff if we need to.

LoadContent is called once for loading content of all things

UnloadContent is called once at the end for unloading content

Update is called with a target rate of 60 times per second, we update our game objects here. We check for keyboard, mouse or gamepad state and update our game accordingly.

Draw is called once after the updates, then waits for the next update. So optimally it is called 60 times a second as well.

Now click your compile button to build and run your game, and you should get a little blue screen "CornflowerBlue" to be exact. That is all your game does at the moment. Click the red X to end it.

If you got an error rather than the game, you may not have a graphics card capable of running XNA games. For this I apologize for your wasted time. If your game ran, than you will be ready for our next installment.
Post a Comment