61286c01.
61286c01.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 3 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 start developing some cool smaller games.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 4 Part I: XNA Framework Basics 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 to the DirectX Framework, they thought, 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.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 5 Chapter 1: Introducing XNA Figure 1-2 XNA Game Studio Express is currently the only available IDE for developing games with the XNA Framework, but once Microsoft ships XNA Game Studio 2.0 (end of 2007) you can also develop with Visual Studio 2005 Standard or Professional. If you have Visual Studio 2005, but try to use XNA 1.0 with it to create an XNA project, there will be no templates in it. Even worse: If you try to open a .
1286c01.qxd:WroxPro 1/21/08 3:44 PM Page 6 Part I: XNA Framework Basics Figure 1-3 All of these dlls are written in C# and are completely managed. This means you can open them up with a tool like Reflector (available from 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 shortly. Figure 1-4 Take a look at the Application Model.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 7 Chapter 1: Introducing XNA 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.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 8 Part I: XNA Framework Basics 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.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 9 Chapter 1: Introducing XNA ❑ 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 .
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 10 Part I: XNA Framework Basics and tricks, such as how to get XNA running in Visual Studio 2005 Professional, which is not supported out of the box, but quite useful if you use Visual Studio plug-ins or tools such as SourceSafe or Team Foundation (to work better in a team, for example). Requirements XNA Game Studio Express targets Windows XP SP2 (SP means Service Pack) and Windows Vista. SP2 is important because of the requirements of the .NET 2.0 Framework.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 11 Chapter 1: Introducing XNA The beta versions and XNA 1.0 still required that you install Visual C# Express and the latest version of DirectX 9.0c first; maybe this will change when the final version of XNA 2.0 is shipped. All of these downloads are completely free and it is really a nice thing to have all these tools available without any costs involved.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 12 Part I: XNA Framework Basics also download the drivers yourself and follow the instructions from the official hardware site of Microsoft products at www.microsoft.com/hardware/gaming/download.mspx. Last but not least, you will do a lot of unit testing in this book, and for that I usually recommend NUnit and TestDriven.NET (and later even xUnit, a newer unit testing framework released in late 2007). NUnit is an external dll and TestDriven.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 13 Chapter 1: Introducing XNA (called x86, by the way). People using the 64-bit operating systems of Windows had the same problem in the past with Managed DirectX, which is available only for 32 bit, too. Using a 64-bit operating system does not mean you cannot use 32-bit applications anymore; quite the contrary.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 14 Part I: XNA Framework Basics Figure 1-8 Figure 1-9 After you have successfully entered the encryption key, you can click OK on both the PC and the console. If this fails or you reinstall XNA Game Studio, you can always get a new encryption key and enter it again here.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 15 Chapter 1: Introducing XNA From now on you can create Xbox 360 projects and deploy them to your console, which can be done in the XNA Studio build menu. If you start to debug a project with F5 it will get deployed, too, and you can directly debug it on your console, which is pretty cool because you can step through code on your PC and immediately see the results on your Xbox 360 (kind of multi-monitor developing for free).
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 16 Part I: XNA Framework Basics The Game1 class contains the Initialize, Update, and Draw methods mentioned a little bit earlier. Initialize does nothing for now and Update just checks if the Back button on the first connected gamepad was pressed. For now you are just interested in the Draw method, which basically just executes the following line to clear the background to a specific color: graphics.GraphicsDevice.Clear(Color.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 17 Chapter 1: Introducing XNA Changing the Code Instead of giving up here, think about some ways you can modify the code in a useful manner. First of all, you should be able to quit your program by pressing Esc. By default, XNA just adds the following lines to the Update method, which quits the program when the Xbox 360 controller’s Back button is being pressed: // Allows the default game to exit on Xbox 360 and Windows if (GamePad.GetState(PlayerIndex.One).
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 18 Part I: XNA Framework Basics To load this texture (CityGroundSmall.jpg) into your game, just drag it into your project on the Solution Explorer. Then click the file in the project and select Properties. You should now see the screen shown in Figure 1-13. Normally you would just see the build action and some options for copying the content file to the output directory (e.g., if you want to include a .dll or .xml file).
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 19 Chapter 1: Introducing XNA protected override void Initialize() { backgroundTexture = content.Load(“CityGroundSmall”); sprites = new SpriteBatch(graphics.GraphicsDevice); base.Initialize(); } // Initialize() To display the background texture, you have to start the sprite batch and render the texture to your sprites in the Draw method: protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.Green); sprites.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 20 Part I: XNA Framework Basics One final thing you are going to add for this first project is the capability to scroll the background with the cursor keys or a gamepad, and then you render the scrollable tiles to the whole background.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 21 Chapter 1: Introducing XNA } // for for sprites.End(); base.Draw(gameTime); } // Draw(gameTime) Now you can start your project and move around with up and down. This is pretty good for your first little application, isn’t it? Notes about XNA To finish this chapter, here are some additional tips and tricks about the XNA Framework and XNA Game Studio Express 1.0 and 2.0.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 22 Part I: XNA Framework Basics ❑ http://XnaProjects.net — Another website created by the author just for hosting XNA games like the ones in this book. It is a completely open website for everyone to use, upload games, and to add your own links to, but there are no articles or tutorials on this site, just the games with installers, source code, screenshots, and videos. Is C# Good for Game Development? There are constantly forum threads on sites like www.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 23 Chapter 1: Introducing XNA away. Most big game studios also can’t just adopt every new technology right away. The studio might be in the middle of a big project and also might have a very big code base, which is not easy to port. In the long run, however, code will get converted and we will move up the ladder of high-level languages. Figure 1-15 is an old picture I did to show the differences between DirectX with C++ and Managed DirectX in C#.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 24 Part I: XNA Framework Basics You might even ask why you should change the way you loaded textures before. If you just want your game to run on the Windows platform, you can certainly load textures and shaders dynamically and without importing them first into your project as XNA content files. This has the advantage that you can change textures or shaders while the game is running (a feature that is not supported by the XNA content pipeline).
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 25 Chapter 1: Introducing XNA learn more about the structure of an application and why some things work so well and others take so much time. For more links on the Internet, check out the great collection of XNA links at http://xnadevelopment .com/links.shtml. There are many more sites about XNA and it seems every day you see a couple of new community sites and more resources. Do a Google search to find out which site is the most popular. Changes in XNA 2.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 26 Part I: XNA Framework Basics use, or peer-to-peer games as role playing or strategy games usually use. It is totally up to you; you could even use a hybrid approach. Because XNA makes great use of the Xbox Live system, you get all the features in there for free, which includes your gamer card, friend management, and chat system (you can even show the keyboard and messages boxes on the Xbox 360 now). You can even enable voice support for your games.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 27 Chapter 1: Introducing XNA Another notable feature in XNA 2.0 is the ability to use XNA inside a window control, which is useful for level editors such as the Dungeon Quest Editor discussed at the very end of this book. The Input classes in XNA now support more devices like the Chatpad, the Big button pad, guitars, drums, or flight sticks. See Chapter 10 for more information.
61286c01.qxd:WroxPro 1/21/08 3:44 PM Page 28 Part I: XNA Framework Basics Chapter 2 provides more detailed coverage of the steps required to get a game running on the Xbox 360, but the sample in this chapter also works on the console out-of-the-box. Just start the project, switch to the Xbox 360 output, and press F5. If you have set up everything correctly, you can see the same output on the Xbox 360.