Chapter 1: Introducing XNA


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 anyone who already knows the basics.

In the next few chapters you will directly start developing some cool smaller games. This section contains the basic foundation and helper classes you will use later in this book to develop a fullblown graphics engine for more advanced game projects. To keep things simple you start with 2D programming in Part I and then advance to 3D in Part II.

Let’s get started.

Introduction to the XNA Framework

XNA is developed by Microsoft and was started a few years ago, but kept very secret. At the GDC (annually biggest Game Developers Conference) in 2004 Microsoft announced XNA for the first time. XNA is not just a framework like DirectX; it also contains a lot of tools and even a custom IDE derived from Visual Studio to make the game programmer’s life easier. Because no tools or bits were released until 2006, DirectX developers only noticed the “XNA” logo in the upper-right corner of the DirectX SDK documentation from 2004 to 2006 (see Figure 1-1). (XNA just means “XNAs Not Acronymed.”)

image from book
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 happened, and especially small to mid-sized teams don’t really need a complex build management tool.

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 targeted at beginners, hobbyists, and students to allow them to quickly develop their own games for the Windows and Xbox 360 platform. But this does not mean professional game studios cannot use XNA yet. A special XNA Game Studio Professional version targeted to professional game studios will be released in the summer of 2007, but if you want to start developing in Visual Studio 2005 Professional, follow the tricks I describe later in this chapter on how to work with VS and XNA productively.

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 simultaneously for the first time. But if you want to run your XNA game on the Xbox 360 console you have to join the “Creators Club” for an annual fee of $99.

XNA Game Studio Express

Figure 1-2 shows you XNA Game Studio Express. You learn how to install it in a second.

image from book
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 image from book 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 worse: If you try to open a .csproj file you created with XNA Game Studio Express, Visual Studio cannot open the project. It is not supported. Read on for a few tips on how to get XNA working with Visual Studio 2005 in the section “What about Visual Studio 2005 Professional?”

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.

Application Model

The XNA Framework is divided into three essential parts (see Figure 1-3):

  • 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.dlls

image from book
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 simplifies things a little bit. The content pipeline is discussed in a little bit.

image from book
Figure 1-4

Take a look at the Application Model. Each XNA project uses a Game class, which contains all the important game components, the graphic device, the window settings, and the content manager. You can also add the input and sound handling here. Basically everything that your game does on a very high level somehow roots in the game class or at least is in some component that can be accessed from the game class.

The following are the three most important methods in the game class (see Figure 1-5). For the final release Microsoft also added the LoadGraphicsContent and UnloadGraphicsContent helper methods by default to the Game1.cs class, which is created automatically for you when you create a new XNA project, but these methods will never be used in this book because it is much simpler to have all initialization and loading code at one place in the Initialize function.

image from book
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 user could press Alt and Tab or minimize the game, in which case Draw does not need to be called anymore, but maybe you want the game to continue to run in the background. This is especially important for network games to make sure the player is still synchronized.

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 looked cool in the webcasts, but when I started coding my first game projects in XNA I did not find it very useful to work with the designer that was presented in the first beta. In the second beta and the final release, most of the initial game component code was removed as well as the graphical designer component in XNA Game Studio. This happened because Microsoft was not able to find a way to implement the designer and game components idea in a useful way for the Xbox 360 console (it only worked on the Windows platform before). This just confirmed my first feeling that the game component idea is not very useful and may even limit you in certain respects, like having to worry about the drawing yourself (see Chapter 4 for more details).

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 menus are created in every game. You can still extract code very easily and create your own game components if you want to create a new game, and then you can just reuse the code (for example, the SkyCubeMapping class). Some game components like the camera and screenshot capturer classes from Chapter 4 are good examples for components that can be reused in many other games, but almost everything else in the games from this book is implemented without using game components.

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.

Content Pipeline

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 reduces the amount of custom code you have to do to get graphics, 3D data, and shaders into your game. For example, if you drop a model file into your project and it uses two textures and a specific shader, the content pipeline will process your model file and automatically find and add the required textures and shaders. You don’t have to do this for yourself; the content importer “compiles” the data into a binary format and in the process it picks up everything you need.

image from book
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 sprites are just textures too and usually use the uncompressed 32-bit format.

  • 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.




Professional XNA Game Programming
Professional XNA Programming: Building Games for Xbox 360 and Windows with XNA Game Studio 2.0
ISBN: 0470261285
EAN: 2147483647
Year: 2007
Pages: 138

Similar book on Amazon
XNA Game Studio 4.0 Programming: Developing for Windows Phone 7 and Xbox 360 (Developer's Library)
XNA Game Studio 4.0 Programming: Developing for Windows Phone 7 and Xbox 360 (Developer's Library)
Learning XNA 3.0: XNA 3.0 Game Development for the PC, Xbox 360, and Zune
Learning XNA 3.0: XNA 3.0 Game Development for the PC, Xbox 360, and Zune
Beginning XNA 2.0 Game Programming: From Novice to Professional (Expert's Voice in Game Programming)
Beginning XNA 2.0 Game Programming: From Novice to Professional (Expert's Voice in Game Programming)
Microsoftu00ae XNAu00ae Game Studio 3.0: Learn Programming Now! (Pro - Developer)
Microsoftu00ae XNAu00ae Game Studio 3.0: Learn Programming Now! (Pro - Developer)

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net