Page 1 of 7
XNA Game Studio Express is intended to allow you to create games for the PC, for the XBox 360, Zune devices and now Windows Phone 7. Most of the easy to follow examples of game creation are for 2D sprite-based games - 3D XNA seems hard. However, if you know a little about how 3D works getting started with XNA isn't that difficult.
This project's end result might not seem very impressive if you were hoping for a 3D game but it is exactly what you need to get started on such a project.
The aim is to create a 3D rotating cube.
This is the 3D graphics equivalent of the familiar “hello world” program because once you can create a rotating 3D cube you can create just about any shape and movement you care to attempt.
In most cases when creating any 3D model you would use a 3D graphics design program and export the model to XNA. However, every 3D programmer should know how to create a 3D model from scratch and so in this case we start off by programmatically building the cube. What might surprise you is that even this simple object isn't a simple to create in code.
Once we have the mesh that defines the cube we move on to solve the problem of displaying it.
Before you get started on this project you will need to download and install C# Express 2010 or Visual Studio 2010. After this you can download and install XNA Game Studio - XNA Creators Club Online - downloads
This which provides a new project types within C# Express 2010. It’s worth knowing that you can’t use XNA Express from Visual Studio 2010
You also need a graphics card that supports Shader Model 2.0 or better - which covers most modern machines.
Getting started with XNA
Open C# Express (or Visual Studio) and create a new Windows Game (XNA) project and call it XNACube.
If you take a look at the generated code you will discover that you have a single class called Game1 (or similar). This provides you with the basic framework to create a game, be it in 2D or 3D.
As well as the constructor, Initialise and LoadContent which can be used to initialise anything you need in the game, there are two important methods that are core to the functioning of the game:
protected override void Update(
which is called at regular intervals and is where you place all of the games “logic” and
protected override void Draw(
which is also called at regular intervals and is where you place all of the code that actually draws the game scene. The parameter passed to both methods - gameTime is an object that can be used to discover how long it has been since the last iteration.
The constructor initialises the graphics device manager which looks after the way graphics hardware is used.
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
You can spend time customising the way this works but for this simple example we can simple accept the defaults. There are also some lines of generated code which handle the game pad keys and a few other things - but once again for this simple example we can ignore them.