Welcome to the world of XNA. As a game programmer you probably know about DirectX and maybe even the basics of the XNA Framework. This chapter explains how to install XNA Game Studio Express and how to use it in a productive way. It also contains quite a lot of tips that might even be useful for
In the
Let’s get started.
XNA is developed by Microsoft and was started a few
Figure 1-1
This means Microsoft was working on the XNA Framework for quite a while, but the developers did not really know what to expect. It could be a successor of the DirectX Framework, but when Direct3D 10 Beta for Windows Vista was released in the end of 2005, it seemed that DirectX was still the preferred graphics framework even for this new operating system. Then early in 2006 at the GDC the Microsoft XNA Build March 2006 CTP was released. XNA Build is a tool that allows you to manage complex build processes, similar to Msbuild and tools like Ants, but more complex and powerful. Because Microsoft’s MechCommander 2 was also released as a Shared Source Release, a lot of people downloaded it and tried to rebuild the MechCommander 2 game. But after a while not much
Then it was quiet for a while and only Microsoft personnel and DirectX MVPs (luckily I am one) got to know about the upcoming XNA Framework and XNA Game Studio releases. The rest of the world found out about that at the Gamefest conference in August (a new game developer conference by Microsoft), where Microsoft announced the XNA Game Studio Express beta 1 release on August 30, 2006. The first beta only contained one starter kit, “Space Wars,” and XNA did not include much 3D functionality. Many developers and hobbyists tried out XNA and wrote many small 2D games with the help of the Sprite classes in XNA. Although you could quickly create your Pong clone or some simple shoot-’em-up game, it was very hard to write your own 3D model importer and render code.
XNA Game Studio Express was initially
Gladly, Microsoft released another beta a few months later in November 2006 before the final release of XNA Game Studio Express happened in December 2006, which includes the content pipeline and many new features you will learn about in Chapters 2 and 3.
XNA is completely free and allows developers to create games for both the Windows platform and for the Xbox 360 platform
Figure 1-2 shows you XNA Game Studio Express. You learn how to install it in a second.
Figure 1-2
The screen does not only look similar to Visual C# 2005 Express, it actually is just that. There are only some minor changes to your project settings if you create an XNA project. There is also an extra option in Tools
Options, which allows you to select your Xbox 360 device and enter the encryption key. Additionally, there are some new features inside the IDE; for example, the content pipeline that allows you to import textures, models, and shaders very quickly into your project. More about all that in a little bit.
XNA Game Studio Express is currently the only available IDE for developing games with the XNA Framework, but Microsoft will ship XNA Game Studio Pro sometime in 2007 (summer or later). XNA Game Studio Professional will be based on the Visual Studio 2005 Professional Edition and allows you to use all Visual Studio plugins and features. If you own Visual Studio 2005 and try to create an XNA project, there will be no templates in it. Even
Microsoft also mentions another version of the XNA IDE called just XNA Studio, which is based on the Visual Studio Team System version and is targeted to large AAA studios. Read more about the XNA Framework at http://msdn.microsoft.com/directx/xna/faq.
The XNA Framework is divided into three essential
XNA Graphic Engine in the Microsoft.Xna.Framework.dll
XNA Game Application Model in the Microsoft.Xna.Framework.Game.dll
XNA Content Pipeline
in the Microsoft.Xna.Framework.Content.Pipeline.
Figure 1-3
All of these dlls are written in C# and are completely managed dlls. This means you can open them up with a tool like Reflection (get it from http://www.aisto.com/roeder/dotnet/) and see directly how they work (see Figure 1-4). Most internal functionality just calls to the DirectX dlls and
Figure 1-4
Take a look at the Application Model. Each XNA project uses a
Game
class, which contains all the important game
The following are the three most important
Figure 1-5
Initialize ()
Update (GameTime time)
Draw (GameTime time)
You can probably already guess what all these do.
Initialize
loads all your game content, sets all your startup settings, and initializes everything you need. If you want to follow the design patterns Microsoft provides for XNA you would do all the loading in the
LoadGraphicsContent
method.
Update
is called before each frame is drawn to update your game time, input, sound, and everything else that is not visible on the screen. If your game is GPU limited it can very well happen that
Update
is called more often than
Draw
, but your update code should run separate from the drawing code anyway and none of the samples in this book will need special care for the number of times
Update
and
Draw
are called. And finally,
Draw
is called each frame to draw everything to the screen. The separation of
Update
and
Draw
might not always be important and can almost always be ignored for unit tests, but for the final game it is quite important to make sure the game logic runs independent of the draw code. For example, on the Windows platform the
Additionally, you can add GameComponent classes to your game class, which again have an Update and a Draw method. Both these methods are automatically called from your game Update and Draw methods. The initialization can happen directly in the constructor there. Initially Microsoft wanted the developers to create and add game components with the designer of Visual Studio, which can be seen in the first beta of XNA Game Studio Express (30 August, 2006). The designer feature was later removed because it did not work well, was not supported for the Xbox 360 platform, and not many developers used it anyway.
The idea with the game components is to reuse parts of your code and make it very easy to just plug them into your games. Examples would be a frame counter or maybe a sky cube mapping renderer for the 3D background. In my opinion there are two major drawbacks: No standard game components are shipped with XNA, and it is not really hard to code such an application model yourself and even extend it. I will not use many GameComponent classes in this book, but feel free to plug them in on your own. Read Chapter 4 for more details of the GameComponent class and learn about its advantages and disadvantages. Because the game class has a Components property, it is very easy to add more components.
Don’t get me wrong, the basic idea of game components is really great; there was a small webcast from Mitch Walker, the Program Manager of the XNA Framework at Microsoft, at the time the first XNA beta was released about the game components and how to combine them. At first I was not very sure what to think of the content pipeline and the game components idea; it
This is similar to the fact that every game implements its own UI and menu logic, which is quite a hassle. But if you think about it, just some standard menu system like Windows uses for every app is very boring and it is always a nice experience to see a new way in which
One of the hopes Microsoft has with this application model is that the community of game developers can create and share their game components quite easily and improve the community aspect of XNA. For more information you can check out the eXperience project on www.codeplex.com.
The content pipeline is used to import, compile, and load game assets like textures, 3D models, shaders, and sound files to your game project (see Figure 1-6). It greatly
Figure 1-6
Say the shader you added through the model you dropped into your project contains an error. In the past you would have to start your game project, and then get an exception telling you that the shader could not be compiled and the game would crash. Now the shader is compiled in the build process and you don’t have to start your game to see that it does not work yet. You can quickly fix the error through the line and error message from the build output in XNA Game Studio Express and then rebuild.
The content pipeline does not just consist of one dll; there are five different dlls:
Microsoft.Xna.Framework.Content.Pipeline.dll contains the basic functions for the content pipeline.
Microsoft.Xna.Framework.Content.Pipeline.EffectImporter.dll is used to compile and import shaders.
Microsoft.Xna.Framework.Content.Pipeline.FBXImporter.dll is the biggest of all dlls and contains a lot of code to import .fbx 3D model files and supports many features, for example skinning and bones.
Microsoft.Xna.Framework.Content.Pipeline.TextureImporter.dll
is used to import texture files to your game. These files can be dds files already in the DirectX format (which is the best format for textures and supports hardware compression), but .png, .jpg, .bmp, and .tga files are also supported. 2D
Microsoft.Xna.Framework.Content.Pipeline.XImporter.dll allows you to import .x 3D model files, a format that was used by many DirectX applications and samples.
Your game itself will never require any of these dlls; they are just used to build and compile the content into .xnb (XNA Binary) files in your build process. This makes the distribution easier because you don’t have to worry about the game content files anymore; it is easier to make sure all the content files are there when you start your game. Don’t modify the .xnb files, they are just the output format like .exe files and should not be modified directly. The data can also not be converted back to textures, models, or shaders (well it might be possible, but there are no tools for that). The .xnb files are also very different on the Windows platform and the Xbox 360 platform, whereas the game source code and the content files might be exactly the same.
Additionally you can create your own custom content processors, which allow you to compile any other game asset you have (for example, another model format) into .xnb files. You explore that in Part II when you have to make sure all your 3D models have tangent data for the normal mapping shaders.
Ok, that’s all the basics you need for now. It is time to get going and code your first game.

Microsoft XNA Game Studio Creator's Guide, Second Edition

Learning XNA 3.0: XNA 3.0 Game Development for the PC, Xbox 360, and Zune

XNA Game Studio 4.0 Programming: Developing for Windows Phone 7 and Xbox 360 (Developer's Library)

Microsoftu00ae XNAu00ae Game Studio 3.0: Learn Programming Now! (Pro - Developer)