Table of Contents
Introduction xi
XNA Framework and Game Studio 1
What Is XNA? 1
What Is the XNA Framework? 2
Game Assembly 2
Game Class 2
GameComponent/DrawableGameComponent Class 5
GameServiceContainer Class 5
GameTime Class 6
GameWindow Class 6
GraphicsDeviceManager 6
Framework Namespace Interfaces 9
Pipeline Assembly 10
Framework Assembly 11
What Is XNA Game Studio? 17
Installing XNA Game Studio 18
Project Types 19
Windows Game 19
Windows Game Library 23
Xbox 360 Game 23
Xbox 360 Game Library 23
Starter Kits 23
Spacewar 24
Marblets 24
Racing Game 25
Ship Game 25
Content Pipeline Overview 26
Summary 28
Game Development Overview 29
Parts of a Game 29
Graphics 30
Input Device(s) 31
Networking 32
AI 34
GUI/User Interface 35
Physics System 35
Sound/Music 36
Game States 37
The Game Loop - XNA Style 41
Calculating Frame Rate 42
Game Components 43
Game Services 46
Creating Your First Application 51
2D Graphics 63
Texture2D 64
SpriteBatch 66
Rotation 69
Scaling 71
Flipping 72
Transparency 73
Coloring 76
Tiling 79
Animating Sprites 84
Multiple Batch Rendering 86
Text Drawing 88
Rendering with Multiple Viewports 92
2D Shooter Game Version 1 96
Player and Enemy Sprites 97
Showing the Score, Frame Rate, and Countdown 105
Summary 110
Input 111
Gamepad, Keyboard, and Mouse Classes 111
Gamepad 112
Keyboard 114
Mouse 114
[left angle bracket]Device[right angle bracket] State Classes 114
GamePadState 115
KeyboardState 116
MouseState 117
Controlling Sprite Movement 118
Collision Detection 118
Rectangle Collision Detection 119
Per-pixel Collision Detection 125
Transformed Sprite Collision Detection 127
Creating a Configurable Control System 133
2D Shooter Game Version 2 142
Running and Gunning 156
Setting Up User-Configurable Controls 182
Sound 189
XACT and Sound Creation 191
Playing 2D Sound 194
Playing 3D Sound 198
2D Shooter Game Version 3 200
Adding Music and Sound 200
Summary 202
I/O 203
The StorageDevice and StorageContainer Classes 204
Standard I/O Files and Operations 206
Data Serialization 209
2D Shooter Game Version 4 213
Basic Effects 223
BasicEffect Class Methods 224
Colors, Textures, and Alpha 227
Implementing Textured Models 228
Fog in XNA 232
Implementing Fog 233
Lighting 237
Emissive and Ambient Light 239
Diffuse Light 241
Specular Light 242
Implementing Lighting 243
Additional Basic Effect Information 246
Summary 248
3D Math Basics 249
Vectors 249
Vector Arithmetic 251
Clamping with Vectors 254
The Cross Product 255
Distance between Vectors 256
The Dot Product 256
The Length of a Vector 257
Interpolating Vectors 257
Finding the Minimum and Maximum Vector 258
Negating Vectors 259
Normalizing Vectors 260
Reflecting Vectors 260
Smooth Interpolation 261
Vector/Matrix Transformation 262
Additional Vector Operations 263
Matrices 267
Matrix Operations 270
Quaternion Rotations 277
Quaternion Fields, Properties, and Functions 278
Rays 280
Ray Properties and Operations 282
Planes 283
Plane Properties and Operations 283
Bounding Geometry 285
Bounding Spheres, Boxes, and Frustums 285
Summary 292
3D Graphics 293
Primitives 293
Points 294
Lines 294
Triangles 295
Indexed Geometry 297
Models and Meshes 299
XSI and XNA 300
Loading and Rendering Models 302
3D Models Implementation 303
Virtual Cameras 305
3D Camera Implementation 307
Animations 314
Straight-Path Animation Implementation 314
Skeleton Animation 322
3D Shooter Game Version 1 329
Game and Menu Screens 332
The Player and Camera 333
Loading the Level and the Game 341
Summary 355
Importers/Converters 357
Standard Importers and Content Processors 357
Autodesk FBX (.fbx) Importer 358
Effect (.fx) Importer and Processor 358
Model Processor 359
No Processing Required 360
Sprite Font Descriptor Importer and Processors 360
Texture Importer and Processor 360
X File Importer 361
XACT Importer and Processor 361
XML Importer 362
Extending the Standard Content Processing 362
A Simple Example of Extending a Standard Processor 362
Writing a Custom Importer and Processor 366
Creating the Importer's Project 368
Storing Imported and Compiled Data 370
Creating the Importer 370
Creating the Processor 371
Creating the Content Writer 373
Creating the Content Reader 374
Testing the Custom Importer in an Application 375
3D Shooter Game Version 2 377
Game Enemies 377
Version 2 of the Game 382
Adding Custom Content 394
Summary 395
Shaders and Custom Effects 397
Shader Models 399
Shader Model 1.0 399
Shader Model 2.0 400
Shader Model 3.0 401
Shader Model 4.0 402
Vertex and Pixel Shaders 402
Vertex Shaders 403
Pixel Shaders 405
Geometry Shaders 407
An Overview of the High-Level Shading Language (HLSL) 408
Data Types and Swizzling 409
Semantics 412
Techniques and Passes 413
Simple Shader Demo 414
Summary 421
Game Distribution, the Creators Club, and the Xbox 360 423
Sharing Your Games 423
Joining the Creators Club 424
Setting Up Your PC and Xbox 360 for XNA Games 425
Programming Considerations for the Xbox 360 428
Safe Regions 429
Aspect Ratio 430
Graphics, Input, Audio, and Storage Differences between Platforms 430
Graphics 430
Input 431
Audio 432
Storage 432
Setting Up Code for Cross-Platform Sharing 433
Debugging 434
XNA Framework Remote Performance Monitor 434
Creating a Difficulty System 437
Types of Difficulty Systems 437
Determining Our Difficulty Settings 438
Determining Levels of Difficulty 440
Storing Difficulty Settings 440
Implementing the Difficulty System 443
Testing the Difficulty System 448
Moving On... 449
Networking 451
Networking Models 452
Peer-to-Peer 452
Client/Server 454
Networking Topologies 455
Bus Topology 456
Star Topology 456
Ring Topology 456
Mesh Topology 456
Hybrid Topology 457
Preparing the Game for Networking 457
Gamer Services 458
Implementing Networking in Ghost Arena 3D 464
Changes to the Game Screens 465
Changes to the Player and Ghost Classes 470
Creating a Networking Session 473
Hosting and Joining Games 476
The Game Lobby 478
Adding Networking to the Game 488
New Variables and Objects 488
Updated Initialization Functions 489
Updated Game Over 493
Updated Draw and Update Functions 495
Updated Firing 505
Future Improvements 506
Summary 508
Creating an Achievement System 509
Our Achievements 510
The Achievement Class 511
Capturing Achievements 520
Sharing Achievements 526
Summary 526
Additional Resources 527
Index 530