Introducing the XNA Framework and XNA Game Studio Express
In This Chapter
- What Is the XNA Framework?
- Installing Visual C# Express
- Installing the DirectX Runtime
- Installing XNA Game Studio Express
- Creating Spacewar Windows Project
- Compiling and Running Spacewar
Most developers I know decided to enter the computer field and specifically programming because of computer games. Game development can be one of the most challenging disciplines of software engineering—it can also be the most rewarding!
Never before has it been possible for the masses to create games for a game console, much less a next generation game console. We are coming in on the ground floor of a technology that is going to experience tremendous growth. Microsoft is leading the way into how content will be created for game consoles. Soon other game console manufacturers will be jumping at a way to allow the public to create content for their machines. The great news for the Xbox 360 is that Microsoft has spent so much time over the years creating productive and stable development environments for programmers. We will be installing one of Microsoft's latest integrated development environments (IDEs) in this chapter. Before we get to that, let's take a look at the technology we discuss in this book—XNA.
What Is the XNA Framework?
You have probably heard the statement, "To know where you are going, you need to know where you have been." I am uncertain if that is entirely true, but I do believe it applies here. Before we dig into exactly what XNA is and what it can do for us, let's take a moment to look at DirectX because that is what the XNA Framework is built on.
The Foundation of the XNA Framework
Let's take a journey back to the days of DOS on the PC. When programming games, graphic demos, and the like in DOS, programmers typically had to write low-level code to talk directly to the sound card, graphics cards, and input devices. This was tedious and the resulting code was error prone because different manufacturers would handle different BIOS interrupts, IO ports, and memory banks—well, differently, so the code would work on one system and not another.
Later, Microsoft released the Windows 95 operating system. Many game programmers were skeptical at writing games for Windows—and rightly so—because there was no way to get down to hardware level to do things that required a lot of speed. Windows 95 had a protected memory model that kept developers from directly accessing the low-level interrupts of the hardware.
To solve this problem, Microsoft created a technology called DirectX. It was actually called Windows Game SDK to begin with, but quickly switched names after a reporter poked fun at the API names DirectDraw, DirectSound, and DirectPlay, calling the SDK Direct "X." Microsoft ran with the name and DirectX 1.0 was born a few months after Windows 95 was released. I remember working with DirectDraw for a couple of demos back when this technology first came out.
Because of DirectX, developers had a way to write games with one source that would work on all PCs regardless of their hardware. Hardware vendors were eager to work with Microsoft on standardizing an interface to access their hardware. They created device drivers to which DirectX would map its API, so all of the work that previously had to be done by game programmers was taken care of, and programmers could then spend their time doing what they wanted to—write games! Vendors called this a Hardware Abstraction Layer (HAL). They also developed a Hardware Emulation Layer (HEL), which emulates hardware through software in case hardware isn't present. Of course, this is slower but it allowed certain games to be run on machines with no special hardware.
After a couple of years Microsoft released DirectX 3.0, which ran on Windows NT 4 as well as Windows 95. As part of those upgrades, they introduced Direct3D. This allowed developers to create 3D objects inside of 3D worlds. DirectX 4 was never released, but DirectX 5 was released in 1997 and later had some upgrades to work under Windows 98.
When DirectX 8 came on the scene in 2000, some of the newly available graphics hardware had vertex and pixel shaders. As a result, Microsoft added in a way to pass custom program code to the hardware. Through assembly code, the game developer could manipulate the data the main game passed to the graphics card. This assembly code was consumed directly by the graphics hardware.
When there was no graphics hardware, games were slow, but they were very flexible. Later, as hardware rendering became prominent, the games were faster, but they were not very flexible in that all of the games really started to look the same. Now with shaders, the speed of the hardware is combined with the flexibility for each game to render and light its 3D content differently.
This brings us to present-day DirectX: We are up to DirectX 9 and 10. Before I talk about DirectX 9, I spend some time talking about DirectX 10. DirectX 10 was released at the same time as Microsoft Windows Vista. In fact, DirectX 10 only works on Vista. This is largely due to the fact that Microsoft has made major changes in the driver model for this operating system. DirectX 10 also requires Shader Model 4.0 hardware.
The Xbox 360 runs on DirectX 9 plus some additional partial support for Shader Model 3.0 functionality. DirectX 9 is the foundation for Managed DirectX, an API that exposed the core DirectX functionality to .NET Framework developers. There was a lot of concern about whether this "wrapper" could be as fast as the C++ counterparts. Fortunately, it was almost as fast—about 98 percent was the benchmark touted. I experienced these benchmark speeds firsthand while on the beta team for this technology. I fell in love with Managed DirectX.
The XNA Framework used the lessons learned from Managed DirectX and used that foundation as a launching pad. To be clear, XNA was built from the ground up and was not built on top of Managed DirectX. It didn't use the same namespaces as Managed DirectX and is not simply pointing to the Managed DirectX methods in the background. Although XNA utilizes DirectX 9 in the background, there are no references to DirectX's API like there were in Managed DirectX.
XNA is actually a generic term much like the term .NET. XNA really refers to anything that Microsoft produces that relates to game developers. The XNA Framework is the API we are discussing. The final piece to XNA is the XNA Game Studio Express application, which we discuss in detail later. This is the IDE we use to develop our XNA games.
XNA allows us to do a lot of things. We have easy access to the input devices (keyboard, game pad or controller, mouse). XNA gives us easy access to the graphics hardware. We are able to easily control audio through XNA. XNA provides the ability for us to store information like high scores and even saved games. XNA does not currently have any networking capability. Microsoft wants to use the Xbox Live technology for adding network support to XNA. However, there is more work to be done to make sure Microsoft can provide multiplayer functionality in a secure manner.
To get started using XNA we have to install some software. We need to install the latest version of DirectX 9 as well as have a graphics card that supports DirectX 9.0c and Shader Model 1.1. (You should get a card that supports Shader Model 2.0 as some of the examples, including the starter kit we use in this chapter and the next one, will not run without it.) We also need to install Visual C# Express, the DirectX 9 runtime, and finally XNA Game Studio Express. Fortunately, all of the software is free! If you don't have graphics hardware that can support Shader Model 2.0 you can pick up a card relatively inexpensively for about $35 USD. If possible, you should purchase a graphics card that can support Shader Model 3.0, as a couple of examples at the end of the book require it.
At this point, games cannot be created for commercial use on the Xbox 360 but Microsoft has mentioned they are interested in supporting commercial games in future versions. Fortunately, we can create community games for the Xbox 360 with the Express versions.
XNA Game Studio Express is great for the game hobbyist, a student, or someone just getting started because you do not have to shell out a lot of (any!) money to get up and running. One exception to this is if you actually want to deploy your games on your Xbox 360. To do that, you will need to subscribe to the XNA Creators Club for $99 USD a year (or $49 USD for four months). Remember, writing games for the PC using XNA is totally free!
Oh, in case you are wondering what XNA stands for, XNA's Not Acronymed (or so Microsoft says in the XNA FAQ).