ptg6022785 Microsoft ® XNA ™ Game Studio 3.0 UNLEASHED 800 East 96th Street, Indianapolis, Indiana 46240 USA Chad Carter From the Library of Neil Preston ptg6022785 Microsoft® XNA™ Game Studio 3.0 Unleashed Copyright © 2009 by Chad Carter All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. ISBN-13: 978-0-672-33022-3 ISBN-10: 0-672-33022-9 Library of Congress Cataloging-in-Publication Data: Carter, Chad. Microsoft XNA game studio 3.0 unleashed / Chad Carter. p. cm. ISBN 978-0-672-33022-3 1. Microsoft XNA (Computer file) 2. Computer games—Programming. 3. Video games. I. Title. QA76.76.C672C425 2009 794.8’1536—dc22 2008054527 Printed in the United States of America Second Printing July 2009 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possi- ble, but no warranty or fitness is implied. The information provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book or from the use of the CD or programs accompanying it. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact International Sales international@pearson.com Editor-in-Chief Karen Gettman Executive Editor Neil Rowe Development Editor Mark Renfrow Technical Editor Chris Williams Managing Editor Kristy Hart Project Editor Anne Goebel Copy Editor Bart Reed Indexer Lisa Stumpf Proofreader Language Logistics Publishing Coordinator Cindy Teeters Multimedia Developer Dan Scherf Cover Designer Gary Adair Compositor Jake McFarland From the Library of Neil Preston ptg6022785 Contents at a Glance Introduction 1 Part I Get Up and Running with XNA Game Studio on Your PC and Xbox 360 1 Introducing the XNA Framework and XNA Game Studio 7 2 XNA Game Studio and the Xbox 360 19 3 Performance Considerations 33 Part II Understanding XNA Framework Basics 4 Creating 3D Objects 55 5 Handling Input to Move Our Camera 83 Part III Content Pipeline 6 Loading and Texturing 3D Objects 113 7 Sound and Music 123 8 Extending the Content Pipeline 151 Part IV 2D in XNA Game Studio 9 2D Basics 171 10 2D Effects 191 11 Creating a 2D Game 217 Part V XNA Game Studio and the Zune 12 Programming for the Zune 255 13 Running the Game on the Zune 273 Part VI High Level Shader Language 14 HLSL Basics 291 15 Advanced HLSL 311 Part VII Physics and Artificial Intelligence 16 Physics Basics 325 17 Finite State Machines and Game State Management 343 18 AI Algorithms 375 From the Library of Neil Preston ptg6022785 Part VIII 3D Effects 19 Advanced Texturing Techniques 391 20 Special Effects 417 21 Particle System 435 Part IX Putting It into Practice 22 Creating a 3D Game 471 23 Improving the Game 501 24 Finishing Touches 525 Part X XNA Framework Networking 25 Networking Basics 549 26 Creating Multiplayer Demos 565 27 Creating a Networking Game Skeleton 583 28 Creating a Turn-based Multiplayer Game 631 29 Creating a Real-time Multiplayer Game 671 Part XI Xbox LIVE Community Games 30 Best Practices for Creating an Xbox LIVE Community Game 719 31 Selling the Game on Xbox LIVE Marketplace 727 Index 739 From the Library of Neil Preston ptg6022785 Table of Contents Introduction 1 Part I Get Up and Running with XNA Game Studio on Your PC and Xbox 360 1 Introducing the XNA Framework and XNA Game Studio 7 What Is the XNA Framework? 7 The Foundation of the XNA Framework 8 XNA Today 9 Installing Visual C# 2008 Express 10 Installing the DirectX Runtime 15 Installing XNA Game Studio 15 Creating the Platformer Projects 17 Compiling and Running Platformer 18 2 XNA Game Studio and the Xbox 360 19 Creating an Xbox 360 Project 19 Buying the XNA Creators Club Subscription 20 Connecting the Xbox 360 to the PC 21 Deploying on the Xbox 360 23 Debugging on the Xbox 360 25 Creating a Test Demo for the Xbox 360 25 Programming for Dual Platforms 28 The .NET Compact Framework on the Xbox 360 31 3 Performance Considerations 33 Measure, Measure, Measure 33 The 80–20 Rule 34 Creating a Benchmark 35 Monitoring Performance on the Xbox 360 37 Managing Memory 38 Understanding the Garbage Collector 39 On the .NET Framework (Windows) 39 On the .NET Compact Framework (Xbox 360 and Zune) 40 Optimization Suggestions 41 Creating a Micro-Benchmark Framework 41 Sealing Virtual Methods 51 Collections 51 From the Library of Neil Preston ptg6022785 Part II Understanding XNA Framework Basics 4 Creating 3D Objects 55 Vertices 55 Vectors 55 Matrices 56 Transformations 56 Translation 56 Scaling 56 Rotation 56 Transformations Reloaded 57 Creating a Camera 57 Projection 57 View 58 World 59 Vertex Buffers 59 Effects 63 Textures 65 Index Buffers 66 XNA Game Components 69 Checking Performance 72 DrawUserIndexedPrimitives versus DrawIndexedPrimitives 74 Transformations Revolutions 76 5 Handling Input to Move Our Camera 83 Creating a Game Service 83 Starting a Library 86 Working with Input Devices 89 Keyboard 89 Game Pad 93 Mouse (Windows Only) 97 Creating a Stationary Camera 98 Creating a First-person Camera 102 Creating a Split Screen 104 Part III Content Pipeline 6 Loading and Texturing 3D Objects 113 Understanding the Content Pipeline 113 Loading 3D Models 114 Texturing 3D Models 119 Microsoft XNA Game Studio 3.0 Unleashed vi From the Library of Neil Preston ptg6022785 7 Sound and Music 123 Direct Access to Sound Files 123 Microsoft Cross-Platform Audio Creation Tool (XACT) 124 Wave Banks 124 Sound Banks 125 Understanding Variations 126 Updating Our Input Handler 134 Plugging In Our Sound Manager 139 Creating a Sound Demo 146 8 Extending the Content Pipeline 151 Creating a Skybox 151 Creating the Skybox Content Object 152 Creating the Skybox Processor 152 Creating the Skybox Content Type Writer 160 Creating the Skybox Reader 162 Using the Skybox 163 Content Pipeline Processor Parameters 165 Debugging the Content Pipeline Extension 167 Part IV 2D in XNA Game Studio 9 2D Basics 171 Sprite Batches 171 Sprite Blend Modes 173 Sprite Sort Modes 173 Save State Modes 174 Practical Sprite Batches 175 Splash or Loading Screen Demo 176 Drawing Multiple Sprites from One Texture Demo 177 Sprite Batch Blend and Sort Mode Demo 179 Progress Bar Demo 183 Using Sprite Fonts 187 Importing TrueType Fonts 188 Creating Bitmap Fonts 188 Drawing 2D Text 189 10 2D Effects 191 Cel Animation 191 Rotating and Scaling 201 Blending Mode Example 203 Contents vii From the Library of Neil Preston ptg6022785 Fade to Color 204 Making Fire, Old-School Style 206 Explosions 214 11 Creating a 2D Game 217 Setting Up the Game Skeleton 217 Creating Parallax Scrolling 220 Switching States 229 Drawing Our Hero 230 Drawing Our Enemies 235 Handling Collision Detection 240 Winning and Losing 243 Adding Transitions 243 Adding Explosions 246 Adding Sounds 251 Part V XNA Game Studio and the Zune 12 Programming for the Zune 255 Display Album Art Demo 255 Deploying to the Zune 258 Updating the XELibrary 258 Fire Demo Zune Edition 260 Creating a Visualization Demo 262 Creating the Visualization 268 13 Running the Game on the Zune 273 Porting the Game to Run on the Zune 273 Running the Game in Landscape Mode 280 Optimizing the Game to Run on the Zune 283 Zune Limitations 286 Part VI High Level Shader Language 14 HLSL Basics 291 Understanding the Shader Process 293 HLSL Syntax 294 Variable Types 294 Semantics 296 Structs 296 Intrinsic Functions 298 Microsoft XNA Game Studio 3.0 Unleashed viii From the Library of Neil Preston ptg6022785 Loops and Conditions 304 Vertex Shaders 304 Pixel Shaders 305 Techniques 305 Passes 306 Passing Application Data to the GPU 306 HLSL Demo 307 15 Advanced HLSL 311 Vertex Displacement 311 Postprocessing 313 Setting Up Our Game Code 313 Setting Up Our Effect Code 316 More Postprocessing Examples 317 Negative Image 318 Switching RGB Values 318 Sharpening the Image 318 Blurring an Image 319 Embossing 319 Grayscale 320 Chalk 321 Wavy 321 Part VII Physics and Artificial Intelligence 16 Physics Basics 325 Kinematics 325 Velocity 326 Acceleration 326 Force 330 Collisions 331 Momentum 331 Impulse 332 Conservation of Momentum 332 Kinetic Energy 332 Coefficient of Restitution 333 Conservation of Kinetic Energy 333 Solving Our Final Velocities 333 Creating a Collision Response Demo 334 Contents ix From the Library of Neil Preston ptg6022785 17 Finite State Machines and Game State Management 343 Finite State Machine 343 Object-Oriented Design 344 Managing Game States 345 Managing Game States Demo 348 18 AI Algorithms 375 Setting Up Our Demo 375 Chase Algorithm 380 A Better Chase Algorithm 381 Evading Algorithm 382 Random Movement 382 Creating a Finite State Machine 384 Part VIII 3D Effects 19 Advanced Texturing Techniques 391 3D Lighting 391 Creating a Custom Vertex Format 391 Creating the Demo 394 Ambient Lighting 397 Directional Lighting 399 Bump Mapping 403 Normal Mapping 403 Parallax Mapping 407 Relief Mapping 410 Texture Animation 414 20 Special Effects 417 Transitions 417 Cross-Fade (Dissolve) 422 Directional Wipes 423 Making Fire 428 21 Particle System 435 Particle System Defined 435 Point Sprite Defined 436 Creating the Particle Class 436 Creating the VertexPointSprite Struct 442 Microsoft XNA Game Studio 3.0 Unleashed x From the Library of Neil Preston [...]... installing XNA Game Studio Installing XNA Game Studio To use XNA Game Studio, you can use any of the Visual Studio SKUs, including Visual C# Express WARNING You must run the Visual C# Express IDE at least one time before installing XNA Game Studio If this is not done, not all the functionality will be installed If XNA Game Studio was installed prematurely, you will need to uninstall XNA Game Studio, run... play the games made by other developers Xbox LIVE Community Games, introduced in version 3.0 of XNA Game Studio, has changed that Through a peer review process, games can be approved and put on Xbox LIVE for the world to download Never before has there been such an easy way for a game to be seen by so many people Not only is XNA Game Studio great for the professional, it is great for the game hobbyist,... I Get Up and Running with XNA Game Studio on Your PC and Xbox 360 IN THIS PART CHAPTER 1 Introducing the XNA Framework and XNA Game Studio CHAPTER 2 XNA Game Studio and the Xbox 360 CHAPTER 3 Performance Considerations From the Library of Neil Preston This page intentionally left blank From the Library of Neil Preston CHAPTER 1 Introducing the XNA Framework and XNA Game Studio M ost developers I know... shown in Figure 1.2, has two installation options you can check Neither of these options is required to utilize XNA Game Studio From the Library of Neil Preston 12 CHAPTER 1 Introducing the XNA Framework and XNA Game Studio FIGURE 1.2 Neither of these options is required to utilize XNA Game Studio 6 Click Next to continue 7 The next screen, shown in Figure 1.3, asks where we would like to install Visual... Practice Creating a 3D Game 471 Creating the Tunnel Vision Game 471 Creating the Game States 471 Adding a Skybox to Our Game 472 Compiling the Game 472 Creating the Game Logic ... Express or Visual Studio IDE From the Library of Neil Preston Creating the Platformer Projects 17 TIP 1 Everything in this book works with all the Visual Studio 2008 SKUs as well as Visual C# 2008 Express From this point on I will simply use the term Visual Studio, regardless of which SKU (including C# Express) is being used When you installed XNA Game Studio, it added properties to Visual Studio to allow... each platform every time For the project you set as your startup project, XNA Game Studio will try to deploy the game to that device Summary In this chapter, I laid the groundwork in getting all the software required installed so you can actually create games on your PC We even compiled a game and played it After getting a game session fix, join me in the next chapter, where we will get this project... 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... because they saw a video game and thought, “How did they do that?” This book helps demystify what is required to make video games Being able to write games on a next-generation console such as the Xbox 360 has never been an option for the masses before Now with the XNA Framework, games can be written for the console By the end of this book, you will have created four complete games and many demos along... is discussed in Part XI, “Xbox LIVE Community Games.” Oh, in case you are wondering what XNA stands for, XNA’s Not Acronymed (or so Microsoft says in the XNA FAQ) Installing Visual C# 2008 Express To get started, you must have the software installed Let’s start by installing Visual C# 2008 Express TIP Any Visual Studio 2008 SKU works with XNA Game Studio 3.0 XNA requires C# due to how the Content Pipeline . herein. ISBN- 13: 978 -0- 672 -33 02 2 -3 ISBN- 10: 0- 672 -33 02 2-9 Library of Congress Cataloging-in-Publication Data: Carter, Chad. Microsoft XNA game studio 3. 0 unleashed / Chad Carter. p. cm. ISBN 978 -0- 672 -33 02 2 -3 1 Conditions 30 4 Vertex Shaders 30 4 Pixel Shaders 30 5 Techniques 30 5 Passes 30 6 Passing Application Data to the GPU 30 6 HLSL Demo 30 7 15 Advanced HLSL 31 1 Vertex Displacement 31 1 Postprocessing 31 3 Setting. 33 1 Momentum 33 1 Impulse 33 2 Conservation of Momentum 33 2 Kinetic Energy 33 2 Coefficient of Restitution 33 3 Conservation of Kinetic Energy 33 3 Solving Our Final Velocities 33 3 Creating a