ebook img

Microsoft XNA Unleashed: Graphics and Game Programming for Xbox 360 and Windows (R) XNA(TM) Unleashed: Graphics and Game Programming for Xbox 360 and Windows PDF

543 Pages·4.305 MB·English
by  CarterChad
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Microsoft XNA Unleashed: Graphics and Game Programming for Xbox 360 and Windows (R) XNA(TM) Unleashed: Graphics and Game Programming for Xbox 360 and Windows

Chad Carter Microsoft ® XNA ™ U N L E A S H E D Graphics and Game Programming for Xbox 360 and Windows 800 East 96th Street,Indianapolis,Indiana 46240 USA Microsoft® XNA™ Unleashed: Graphics and Game Programming for Editor-in-Chief Xbox 360 and Windows Karen Gettman Copyright © 2008 by Chad Carter Acquisitions Editor 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, Neil Rowe 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 Development Editor precaution has been taken in the preparation of this book,the publisher and author Mark Renfrow assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. Managing Editor ISBN-13: 978-0-672-32964-7 Gina Kanouse ISBN-10: 0-672-32964-6 Project Editor Andy Beaster Library of Congress Cataloging-in-Publication Data Copy Editor Carter,Chad. Teresa Horton Microsoft XNA unleashed : graphics and game programming for Xbox 360 and Windows / Chad Carter. — 1st ed. Indexer p. cm. ISBN 0-672-32964-6 Erika Millen 1. Microsoft XNA (Computer file) 2. Computer games—Programming. 3. Video games. I. Title. Proofreader QA76.76.C672C383 2007 Eileen Clark 794.8’1526—dc22 2007025607 Technical Editor Shawn Hargreaves Printed in the United States of America Team Coordinator First Printing July 2007 Cindy Teeters Trademarks All terms mentioned in this book that are known to be trademarks or service marks Media Developer have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of Dan Scherf this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Book Designer Warning and Disclaimer Gary Adair 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” Composition basis. The author and the publisher shall have neither liability nor responsibility to any Fastpages 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 [email protected] For sales outside of the U.S.,please contact: International Sales [email protected] The Safari®Enabled icon on the cover of your favorite technology book means the book is available through Safari Bookshelf. When you buy this book,you get free access to the online edition for 45 days. Safari Bookshelf is an electronic reference library that lets you easily search thousands of technical books,find code samples,download chapters,and access technical information whenever and wherever you need it. To gain 45-day Safari Enabled access to this book: . Go to http://www.samspublishing.com/safarienabled . Complete the brief registration form . Enter the coupon code NIMD-6KVM-A2QA-4GMI-2Z7B If you have difficulty registering on Safari Bookshelf or accessing the online edition,please e-mail [email protected]. 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 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 Contents v 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 vi Microsoft XNA Unleashed 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 Contents vii 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 viii Microsoft XNA Unleashed 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 Contents ix 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 VertexPointerSpriteStruct.........................................404 Creating the Particle System Engine.................................................405 Point Sprite Effect File...................................................................414 Particle System Demo....................................................................417 Creating Particle Effects.................................................................420 Rain...................................................................................420

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.