We write XNA code in C# (C sharp) so you should at least be familiar with the basics of C# before starting in on XNA (there are a lot of introductory tutorials for both C# and XNA on the internet).

Taking it section by section;

Using statements reference other namespaces which are typically linked libraries or assemblies within .NET. They basically enable us to use methods from within those namespaces without having to specify the namespace every time, so instead of typing Microsoft.Xna.Framework.Graphics.Effect we can just type Effect and the compiler will select the correct namespace for us because we have a using reference to Microsoft.Xna.Framework.Graphics.

using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

Here XNA has specified a namespace for our game, WindowsGame1. You may end up using multiple namespaces within your game to help seperate out functional components and to provide another level of encapsulation. You should see that code will only be able to access code within another namespace by fully qualifying the namespace or via the using method.

namespace WindowsGame1

This is the class definition and shows that our Game1 class inherits from the Microsoft.Xna.Framework.Game class, which provides all of the base functionality for our game. Within the definition XNA has provided member variables for the GraphicsDeviceManager which is our graphics device and a SpriteBatch which may be used for drawing sprites.

/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

The class constructor initialises the GraphicsDeviceManager member, graphics, and defines a root directory for the game to find it’s Content (content is also known as the game assets, or the models, textures, sounds, etc that your game will use).

The Initialize method can be used to perform other initialisation actions. It is also important to call base.Initialize(); as this causes the correct creation of any other GameComponents.

/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}

LoadContent can be used to, load your content 🙂 You will see that XNA has initialised the SpriteBatch member, spriteBatch, here.

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
}

UnloadContent, unsurprisingly, unloads your content 🙂

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

This is a key method, Update. Upate and Draw are (usually) called in sequence over and over providing your game loop. The Update step is usually where you would gather user input, and update your game logic. You will see that Update also takes a parameter, gameTime, which allows us to perform time-related calculations. XNA has provided minimal code here, it checks to see if a controller button has been pressed and if so exits the game loop.

Like, Initialize, the call to base.Update(); causes any other GameComponents to update as well.

The other key method within your game loop, Draw. Draw, like Update, takes a gameTime parameter. Here XNA clears the window to a pretty blue, and calls base.Draw(); so that any other DrawableGameComponents can also draw themselves.

I’ve been messing about with XNA3 for about a year now, and recently decided to move up to XNA4. Microsoft have obviously decided to leverage the XNA user base to provide content for the Windows Phone 7 platform, so you’d be forgiven for thinking that XNA4 is only for phone develooment as much of the tools, educational resources, etc are all re-branded and aimed squarely at Windows Phone 7. XNA still supports the PC, Xbox 360 and Zune as target platforms alongside the Windows Phone.

Getting started with XNA is incredibly simple. XNA4 provides us with quite a solid framework to build our game / engine upon. Much of the hard work involved in getting DirectX up and running is handled for us.

For instance, working with C++ and DirectX, the first step is usually to create a windowed application and set up a game loop rendering a blank scene. You would have to, amongst other things;

create a window for your application

provide a windows message handler

intialise Direct3D

create the Direct3D Device

start the message pump / game loop

render to the window

XNA does all of that and a bit more for us without us having to enter a single line of code. The default XNA4 template will generate a window like the image shown and provide us with the framework code listed below to give us the basics that we need to get started.

using System;
using System.Collections.Generic;
using System.Linq;
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.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace WindowsGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}

Within XNA there is an underlying Game class that provides all of the basic functionality to produce the window, graphics device, the message pump / game loop, etc. All of the initial hard work that C++ programmers have to handle is done for us, and this design is mirrored throughout XNA Game Studio.

The framework above, the Game1 class, overrides the underlying Game class and provides us with the ability to write our own code over the top. We add code to the Initialize, LoadContent, Update, and Draw methods and add our own components, methods, content, etc. to build up our own game.

Visual Basic 6 and DirectX 8 are looking very long in the tooth now, pretty much dead, but where to go?

I dabbled briefly with an early version of XNA (Game Studio 2.0 I seem to recall) when I was looking at alternatives for game programming languages/libraries, but wasn’t particularly impressed with it. Yes, it allowed rapid development, and most of the basics were done for you, but it seemed slow, and required doing things in a particular way, and seemed more aimed at the casual 2D game market on the Xbox 360 (although it supports Windows development as well).

With the advent of XNA 4 (due later this year) I thought I’d have another look at it and so installed the latest iteration XNA Game Studio 3.1.

The video tutorials walk you through all aspects of getting started with C#/XNA; setting up a Creators Club account, creating your first simple 2D and then 3D game, and there are ‘Starter Kits’ that help springboard you into different projects.

Bear in mind that you don’t need to purchase the full membership to develop games for both Windows and the Xbox 360, but you will only be able to deploy your games on Windows PCs without the full membership.

Its slicker than I remember, and much more streamlined, I’m actually going to give this another go 🙂