www.it-ebooks.info Microsoft ® XNA ™ UNLEASHED 800 East 96th Street, Indianapolis, Indiana 46240 USA Chad Carter Graphics and Game Programming for Xbox 360 and Windows www.it-ebooks.info Microsoft® XNA™ Unleashed: Graphics and Game Programming for Xbox 360 and Windows Copyright © 2008 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-32964-7 ISBN-10: 0-672-32964-6 Library of Congress Cataloging-in-Publication Data Carter, Chad. Microsoft XNA unleashed : graphics and game programming for Xbox 360 and Windows / Chad Carter. — 1st ed. p. cm. ISBN 0-672-32964-6 1. Microsoft XNA (Computer file) 2. Computer games—Programming. 3. Video games. I. Title. QA76.76.C672C383 2007 794.8’1526—dc22 2007025607 Printed in the United States of America First Printing July 2007 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 Pearson 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@pearsontechgroup.com Editor-in-Chief Karen Gettman Acquisitions Editor Neil Rowe Development Editor Mark Renfrow Managing Editor Gina Kanouse Project Editor Andy Beaster Copy Editor Teresa Horton Indexer Erika Millen Proofreader Eileen Clark Technical Editor Shawn Hargreaves Team Coordinator Cindy Teeters Media Developer Dan Scherf Book Designer Gary Adair Composition Fastpages www.it-ebooks.info Contents at a Glance Introduction 1 Part I Get Up and Running with XNA on Your PC and Xbox 360 1 Introducing XNA and XNA Game Studio Express 7 2 XNA and the Xbox 360 19 3 Performance Considerations 35 Part II Understanding XNA Basics 4 Creating 3D Objects 59 5 Input Devices and Cameras 85 Part III Content Pipeline 6 Loading and Texturing 3D Objects 113 7 Sound and Music 123 8 Extending the Content Pipeline 153 Part IV 2D in XNA 9 2D Basics 171 10 2D Effects 191 11 Creating a 2D Game 217 Part V High Level Shader Language 12 HLSL Basics 255 13 Advanced HLSL 275 Part VI Physics and Artificial Intelligence 14 Physics Basics 289 15 Finite State Machines and Game State Management 307 16 AI Algorithms 337 Part VII 3D Effects 17 Advanced Texturing Techniques 353 18 Special Effects 379 19 Particle System 397 Part VIII Putting It Into Practice 20 Creating a 3D Game 433 21 Improving the Game 463 22 Finishing Touches 479 Index 501 www.it-ebooks.info Table of Contents Introduction 1 Part I Get Up and Running with XNA on Your PC and Xbox 360 1 Introducing XNA and XNA Game Studio Express 7 What Is the XNA Framework? 7 The Foundation of the XNA Framework 8 XNA Today 9 Installing Visual C# Express 10 Installing the DirectX Runtime 14 Installing XNA Game Studio Express 15 Creating Spacewar Windows Project 17 Compiling and Running Spacewar 17 Summary 17 2 XNA and the Xbox 360 19 Creating Spacewar Xbox 360 Project 19 Buying the XNA Creators Club Subscription 20 Connecting the Xbox 360 to the PC 22 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 33 Summary 33 3 Performance Considerations 35 Measure, Measure, Measure 35 The 80–20 Rule 36 Creating a Benchmark 37 Monitoring Performance on the Xbox 360 39 Managing Memory 40 Understanding the Garbage Collector 41 On the .NET Framework (Windows) 41 On the .NET Compact Framework (Xbox 360) 42 Optimization Suggestions 43 Creating a Micro-Benchmark Framework 43 www.it-ebooks.info Sealing Virtual Methods 53 Collections 53 Summary 54 Part II Understanding XNA Basics 4 Creating 3D Objects 59 Vertices 59 Vectors 59 Matrices 60 Transformations 60 Translation 60 Scaling 60 Rotation 60 Transformations Reloaded 60 Creating a Camera 61 Projection 61 View 62 World 63 Vertex Buffers 63 Effects 66 Textures 68 Index Buffers 69 XNA Game Components 72 Checking Performance 75 Transformations Revolutions 79 Summary 83 5 Input Devices and Cameras 85 Creating a Game Service 85 Starting a Library 88 Working with Input Devices 91 Keyboard 91 Game Pad 95 Mouse (Windows Only) 98 Creating a Stationary Camera 100 Creating a First Person Camera 103 Creating a Split Screen 106 Summary 110 Contents v www.it-ebooks.info Part III Content Pipeline 6 Loading and Texturing 3D Objects 113 Understanding the Content Pipeline 113 Loading 3D Models 114 Texturing 3D Models 120 Summary 121 7 Sounds and Music 123 Microsoft Cross-Platform Audio Creation Tool (XACT) 123 Wave Banks 124 Sounds Banks 125 Understanding Variations 125 Updating Our Input Handlers 134 Plugging In Our Sound Manager 140 Creating a Sound Demo 147 Summary 151 8 Extending the Content Pipeline 153 Creating a Skybox 153 Creating the Skybox Content Object 154 Creating the Skybox Processor 154 Creating the Skybox Compiler 162 Creating the Skybox Reader 164 Using the Skybox 165 Debugging the Content Pipeline Extension 167 Summary 168 Part IV 2D in XNA 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 178 Sprite Batch Blend and Sort Mode Demo 179 Progress Bar Demo 184 Using Sprite Fonts 188 Importing Truetype Fonts 188 Microsoft XNA Unleashed vi www.it-ebooks.info Creating Bitmap Fonts 189 Drawing 2D Text 189 Summary 190 10 2D Effects 191 Cel Animation 191 Rotating and Scaling 201 Blending Mode Example 203 Fade to Color 204 Making Fire Old School Style 206 Explosions 214 Summary 216 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 241 Winning and Losing 243 Adding Transitions 244 Adding Explosions 247 Adding Sounds 252 Summary 252 Part V High Level Shader Language 12 HLSL Basics 255 Understanding the Shader Process 257 HLSL Syntax 258 Variable Types 258 Semantics 260 Structs 260 Intrinsic Functions 263 Loops and Conditions 267 Vertext Shaders 267 Pixel Shaders 268 Techniques 269 Passes 269 Passing Application Data to the GPU 270 HLSL Demo 270 Summary 274 Contents vii www.it-ebooks.info 13 Advanced HLSL 275 Vertex Displacement 275 Postprocessing 277 Setting Up Our Game Code 277 Setting Up Our Effect Code 280 More Postprocessing Examples 281 Negative Image 281 Switching RGB Values 281 Sharpening the Image 282 Blurring an Image 282 Embossed 283 Grayscale 283 Chalk 284 Wavy 284 Summary 285 Part VI Physics and Artificial Intelligence 14 Physics Basics 289 Kinesmatics 289 Velocity 290 Acceleration 290 Force 294 Collisions 295 Momentum 295 Impulse 296 Conservation of Momentum 296 Kinetic Energy 296 Coefficient of Restitution 296 Conservation of Kinetic Energy 297 Solving Our Final Velocities 297 Creating a Collision Response Demo 297 Summary 305 15 Finite State Machines and Game State Management 307 Finite State Machine 307 Object-Oriented Design 308 Managing Game States 309 Managing Game States Demo 312 Summary 335 Microsoft XNA Unleashed viii www.it-ebooks.info 16 AI Algorithms 337 Setting Up Our Demo 337 Chase Algorithm 342 A Better Chase Algorithm 343 Evading Algorithm 344 Random Movement 344 Creating a Finite State Machine 346 Summary 349 Part VII 3D Effects 17 Advanced Texturing Techniques 353 3D Lighting 353 Creating a Custom Vertex Format 353 Creating the Demo 356 Ambient Lighting 359 Directional Lighting 361 Bump Mapping 364 Normal Mapping 365 Parallax Mapping 369 Relief Mapping 372 Texture Animation 376 Summary 378 18 Special Effects 379 Transitions 379 Cross-Fade (Dissolve) 384 Directional Wipes 385 Making Fire 390 Summary 396 19 Particle System 397 Particle System Defined 397 Point Sprite Defined 398 Creating the Particle Class 398 Creating the VertexPointerSprite Struct 404 Creating the Particle System Engine 405 Point Sprite Effect File 414 Particle System Demo 417 Creating Particle Effects 420 Rain 420 Contents ix www.it-ebooks.info [...]... DirectX XNA Today 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 TIP In this book, whenever we use the term XNA, we... When running XNA Framework games on the Xbox 360 console, a hard drive must be connected to the console Software Requirements All of the software required to utilize the XNA Framework on Windows is free: Microsoft Visual C# 2005 Express Edition Microsoft XNA Game Studio Express DirectX 9.0c Instructions on installing the software can be found in Chapter 1, “Introducing the XNA Framework and XNA Game Studio... the XNA Framework unless otherwise noted 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. .. 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) Installing Visual C# Express To get started, we must have the software installed Let’s start by installing Visual C# Express Visual C# Express is the IDE that is required to run XNA Game Studio Express XNA requires C# due to how the Content... accompanying CD Any updates to the code can be downloaded via www.samspublishing.com or www.xnaessentials.com /unleashed www.it-ebooks.info 3 This page intentionally left blank www.it-ebooks.info PART I Get Up and Running with XNA on Your PC and Xbox 360 IN THIS PART CHAPTER 1 Introducing XNA and XNA Game Studio Express 7 CHAPTER 2 XNA and the Xbox 360 19 CHAPTER 3 Performance Considerations 35 www.it-ebooks.info... are done “the XNA way.” The book also targets readers who have some programming experience but have not done anything formal The book teaches by example It is written in such a way that if readers are not in front of their computers, they can still get valuable information from the book because the code is listed in the book as it is being discussed www.it-ebooks.info 2 Microsoft XNA Unleashed Hardware... Performance Considerations 35 www.it-ebooks.info This page intentionally left blank www.it-ebooks.info CHAPTER 1 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 Most developers I know decided to enter the computer field and... 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 www.it-ebooks.info... Simply click Finish to exit the setup Now, we can move on to installing XNA Game Studio Express Installing XNA Game Studio Express To use XNA Game Studio Express we must use Visual C# Express We cannot use Visual Studio NET Professional nor can we use any other IDE Although there are people who have successfully been able to run XNA and even get the Content Pipeline (which we talk about in Part III... NET Professional, it is not officially supported by Microsoft and is not covered in this book WARNING You must run the Visual C# Express IDE at least one time before installing XNA Game Studio Express If this is not done, not all of the functionality will be installed If XNA Game Studio Express was installed prematurely, you will need to uninstall XNA Game Studio Express and run Visual C# Express and . www.it-ebooks.info Microsoft ® XNA ™ UNLEASHED 800 East 96th Street, Indianapolis, Indiana 46240 USA Chad Carter Graphics and Game Programming for Xbox 360 and Windows www.it-ebooks.info Microsoft XNA Unleashed: . Get Up and Running with XNA on Your PC and Xbox 360 1 Introducing XNA and XNA Game Studio Express 7 What Is the XNA Framework? 7 The Foundation of the XNA Framework 8 XNA Today 9 Installing. and Running with XNA on Your PC and Xbox 360 1 Introducing XNA and XNA Game Studio Express 7 2 XNA and the Xbox 360 19 3 Performance Considerations 35 Part II Understanding XNA Basics 4 Creating