Microsoft's XNA Framework provides a powerful set of tools for building both 2D and 3D games for Windows, the Xbox 360, and the Windows Phone platforms. As an extension of the Visual Studio development environment, XNA provides developers with a set of free tools for these environments.
The XNA project templates include an integrated game loop, easy to use (and fast) methods to display graphics, full support for 3D models, and simple access to multiple types of input devices.
In this introductory chapter, we will do the following:
Review the system requirements for XNA development
Install the Windows Phone Tools SDK, which includes Visual Studio Express and the XNA 4.0 extensions
Examine the basic structure of an XNA game by building a simple 2D game
Explore a fast-paced rundown of 2D techniques that will provide a foundation for moving forward into 3D with XNA
Starting out a book on 3D game development by building a 2D game may seem like an odd approach, but most 3D games use a number of 2D techniques and resources, even if only to display a readable user interface to the player.
If you already have an understanding of 2D game development in XNA, you may want to glance over this chapter and proceed to Chapter 2, Cube Chaser – A Flat 3D World, where we begin building our first 3D game.
In order to develop games using XNA Game Studio, you will need a computer capable of running both Visual Studio 2010 and the XNA Framework extensions. The general requirements are as follows:
Component |
Minimum requirements |
Notes |
---|---|---|
Windows Vista SP2 or Windows 7 (except Starter Edition) |
Windows XP is not supported. | |
Shader Model 1.1 support DirectX 9.0 support |
Microsoft recommends Shader Model 2.0 support as it is required for many of the XNA Starter Kits and code samples. The projects in this book also require Shader Model 2.0 support. | |
Visual Studio 2010 or Visual Studio 2010 Express |
Visual Studio 2010 Express is installed along with the XNA Framework. | |
Optional | ||
Windows Phone Development Tools, DirectX 10 or later, compatible video card |
The Windows Phone SDK includes a Windows Phone emulator for testing. | |
Xbox Live Silver membershipXNA Creator's Club Premium membership |
Xbox Live Silver is free. The XNA Creator's Club Premium membership costs $49 for 4 months or $99 for 1 year. |
Originally developed as a separate product, XNA is now incorporated in the Windows Phone SDK. You can still develop games for Windows and the Xbox 360 using the tools installed by the Windows Phone SDK.
If you have an existing version of Visual Studio 2010 on your PC, the XNA Framework templates and tools will be integrated into that installation as well as the Visual Studio 2010 Express for Windows Phone installation that is part of the Windows Phone SDK, which we are going to install now.
To install Windows Phone SDK , perform the following steps:
1. Visit http://create.msdn.com/en-us/home/getting_started and download the latest version of the Windows Phone SDK package. Run the setup wizard and allow the installation package to complete.
2. Open Visual Studio 2010 Express. Click on the Help menu and select Register Product. Click on the Register Now link to go to the Visual Studio Express registration page. After you have completed the registration process, return to Visual Studio 2010 Express and enter the registration number into the registration dialog box.
3. Close Visual Studio 2010 Express.
4. Launch Visual Studio 2010 Express, and the Integrated Development Environment (IDE) will be displayed as shown in the following screenshot:
If you have never used XNA before, it would be helpful to review a number of concepts before you dive into 3D game design. In most 3D games, there will be at least some 2D content for user interfaces, Heads-up display (HUD) overlays, text alerts, and so on. In addition, many 3D game constructions are really evolutions of 2D game concepts.
In order to provide both an overview of the XNA game template and to build a foundation for moving forward into 3D development, we will construct a simple game called Speller. In Speller, the player controls a small square using the keyboard. During each round we will generate a random set of letters, including the letters needed to spell a particular word. The player's job is to navigate through the forest of letters and hit only the correct ones in the right order to spell the indicated word.
By building this game, we will be:
Performing initialization when our game is executed
Adding graphical assets to the game and loading them at run time
Colorizing images and fonts
Handling keyboard input and calculating player movement adjusted for the frame rate
Bounding box collision detection
Keeping and displaying the score
Generating random numbers
That is quite a bit of ground to cover in a very small game, so we had better get started!
To create an XNA project, perform the following steps:
1. In the Visual Studio window, open the File menu and select New Project....
2. Under Project Type, make sure C# is selected as the language and that the XNA Game Studio 4.0 category is selected.
3. Under Templates, select Windows Game (4.0).
4. Name the project
Speller
(this will automatically update the Solution Name).5. Click on OK.
The Speller game's Game1.cs
file, when opened in Visual Studio, would look like the following screenshot:

Two separate projects get created when you start a new XNA Game Studio project in Visual Studio. The first is your actual game project, and the second is a special type of project called a content project. This is shown in the following screenshot:

Any non-code pieces of your game, including graphical resources, sounds, fonts, and any number of other item types (you can define your own content interpreters to read things such as level maps) are added to the content project. This project gets built along with the code in your primary project and the two are combined into a single location with everything your game needs to run.
When the
content project is built, each item is examined by a content importer—a bit of code that interprets the raw data of the content file, a .jpg
image for example, and converts it into a format that can be passed into a content processor. The content processor's job is to convert this file into a managed code object that can be stored on a disk and read directly into memory by XNA's ContentManager
class. These compiled binary files carry the .xnb
file extension and are located, by default, in a subdirectory of your game's executable folder called Content
.
Note
ContentManager
Though its primary job is to load the content resources into memory at runtime, ContentManager
does more than that. Each instance of ContentManager
maintains a library of all of the content that has been loaded. If multiple requests to load the same content file are sent to a ContentManager
instance, it will only load the resource from the disk the first time. The remaining requests are supplied with a reference to the item that already exists in memory.
Out of the box, XNA contains importers/processors for 3D meshes, images, fonts, audio, shaders, and XML data. We will create the content used for Speller with an image editor and the tools built into XNA Game Studio.
To create content assets, perform the following steps:
1. Open Microsoft Paint, or your favorite image creation program, and create a new 16 x 16 image. Fill the image with white color and save the file to a temporary location as
SQUARE.BMP
.2. Switch back to Visual Studio and right-click on the SpellerContent (Content) project in Solution Explorer.
3. Select Add | Existing Item... from the pop-up menu and browse to the
SQUARE.BMP
file. Select it and click on Add to add it to the content project.4. Again, right-click on the content project in Solution Explorer and this time select Add | New Item....
5. In the Add New Item window, select Sprite Font from the window's center pane.
6. Enter
Segoe14.spritefont
as the name of the file and click on Add.7. Close the XML document that appears after
Sprite Font
has been added to the project.
We have now added both an image and a font to our content project. We will see how we load these assets into the game at runtime and how we can use them during gameplay.
Note
Alternatives when adding content
You can also drag-and-drop files directly from Windows Explorer into the Solution Manager pane in Visual Studio to add them to your content project. If you have the full version of Visual Studio, you can add a new bitmap object by selecting Add | New Item... from the project's pop-up menu and selecting Bitmap as the type. The free version of Visual Studio does not support creating bitmaps from within Visual Studio.
The SpriteFont
file that we created in step 6 and the XML document mentioned in step 7 actually load an XML template that describes how the content pipeline should create the resulting .xnb
file. In this case, the default values for the SpriteFont
template are sufficient for our game. This resulted in the Segoe UI Mono font (added to your system when the Windows Phone SDK is installed), with a value of 14 points being used. As we will only be using the standard A to Z character set, we do not need to make any changes to this template for Speller.