XNA 3.0 Game Programming Recipes A Problem-Solution Approach Riemer Grootjans... XNA 3.0 Game Programming Recipes: A Problem-Solution ApproachCopyright © 2009 by Riemer Grootjans All rig
Trang 2XNA 3.0 Game
Programming Recipes
A Problem-Solution Approach
Riemer Grootjans
Trang 3XNA 3.0 Game Programming Recipes: A Problem-Solution Approach
Copyright © 2009 by Riemer Grootjans
All rights reserved No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher
ISBN-13 (pbk): 978-1-4302-1855-5
ISBN-13 (electronic): 978-1-4302-1856-2
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark
Lead Editor: Ewan Buckingham
Development Editor: Joohn Choe
Technical Reviewer: Fabio Claudio Ferracchiati
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell,
Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper,
Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Richard Dal Porto
Copy Editors: Heather Lang, Kim Wimpsett
Associate Production Director: Kari Brooks-Copony
Production Editor: Elizabeth Berry
Compositor: Linda Weidemann, Wolf Creek Publishing Services
Proofreader: April Eddy
Indexer:Broccoli Information Management
Artist: April Milne
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013 Phone 1-800-SPRINGER, fax 201-348-4505, e-mail kn`ano)ju<olnejcan)o^i*_ki,
or visit dppl6++sss*olnejcankjheja*_ki
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705 Phone 510-549-5930, fax 510-549-5939, e-mail ejbk<]lnaoo*_ki, or visit
dppl6++sss*]lnaoo*_ki
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use eBook versions and licenses are also available for most titles For more information, reference our Special Bulk Sales–eBook Licensing web page at dppl6++sss*]lnaoo*_ki+ejbk+^qhgo]hao
The information in this book is distributed on an “as is” basis, without warranty Although every pre-caution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly
or indirectly by the information contained in this work
The source code for this book is available to readers at dppl6++sss*]lnaoo*_ki You may need to answer questions pertaining to this book in order to successfully download the code
Trang 4To Elisa, the most lovely woman I know
Without your love and support, this would not have been possible.
To my parents and brothers, for giving me each and every opportunity.
To my friends and coworkers, for their support and laughs.
Trang 5Contents at a Glance
About the Author xxiii
About the Technical Reviewer xxv
Acknowledgments xxvii
Introduction xxix
CHAPTER 1 Getting Started with XNA 3.0 1
CHAPTER 2 Setting Up Different Camera Modes in Your 3D World 29
CHAPTER 3 Working with 2D Images/Textures in XNA 3.0 173
CHAPTER 4 Working with Models 281
CHAPTER 5 Getting the Most Out of Vertices 393
CHAPTER 6 Adding Light to Your Scene in XNA 3.0 521
CHAPTER 7 Adding Sounds to Your XNA 3.0 Project 583
CHAPTER 8 Networking in XNA 3.0 599
INDEX 631
Trang 6NI N D E X
648
defining TypeWriter capable of
serial-izing object of MapData class, 463
overview, 460–462
overview, 393–394
redundant, removing using indices,
413–417
rendering multiple lines, 401
rendering multiple points, 401
rendering multiple triangles
using TriangleFan, 402
using TriangleList, 399–400
using TriangleStrip, 400–401
rendering triangle to screen, 397–399
sharing normals between, 528–532
storing in DynamicVertexBuffer, 423–425
storing in memory of graphics card,
417–422
IndexBuffer, 421–422
overview, 417–420
VertexBuffer, 420
in VertexBuffer, automatically calculating
normals for, 429–435
GenerateNormalsForTriangleList
Method, 431–432
making method fail-safe, 434
overview, 429–430
starting from VertexBuffer and
Index-Buffer, 434–435
for structure rendered as TriangleList,
432–433
for structure rendered as TriangleStrip,
433–434
of triangle, 431
writing custom content importer
calculating collision point between
pointer and terrain, 166
Intermediate class, 168
overview, 166
receiving data in content processor,
169–170
vertices array, 418, 433, 435
view frustum, 30, 32, 37
View matrix, 17, 20, 30, 34, 47, 52, 55, 395
viewAngle variable, 576
viewMatrix node, 98
viewMatrix variable, 45–46
ViewProjection matrix, 244, 275, 565
Visual Studio 2008, installing, 2
W
WakeUp method, 199
walls, collision with, 66
.wav files
playing and controlling, 583–585
through XAct, 586–590
WaveBank method, 589 waves, 587
while loop, 101, 394, 402, 439, 440, 444, 455,
457, 459–460, 468–469, 502 WMV audio files, playing, 585–586 world matrices
ensuring correct lighting when using, 541–542
objects in sight of camera, 60–63 setting different world matrices for differ-ent objects, 286–293
combining multiple transformations, 288
order of matrix multiplication, 288–292 overview, 286–288
updating, 99–101 worldMatrix node, 97 worldMatrix property, 93 worldMatrix variable, 92 WorldViewProjection matrix, 272 Wrap method, 410
Wrapping mode, 410 Write method 353, 362, 614 WriteBone method, 314–316 WriteModelMesh method, 314–316 WriteModelStructure method, 314–316
X
.x file, 282 XAct, 586 xAllowedRotDir variable, 244 xAmbient variable, 542 xap file, 588
xBlurSize variable, 158–159 Xbox 360 console, 6 Xbox 360, deploying games on, 5–8 connecting Xbox 360 and PCs, 6 Creators Club licenses, 6 generating projects from existing XNA 2.0 projects, 7–8
installing XNA Game Studio Connect, 6 overview, 5–6
pairing PCs and Xbox 360, 7 Xbox Live accounts, 6 Xbox Live accounts, setting up, 6 Xbox Live Marketplace, 5 xCamUp parameter, 248 xCamUp variable, 247 xConeAngle variable, 552 xConeDecay variable, 553 xConeDirection variable, 552 xgs binary file, 588
XImporter method, 215–216 xLightDirection parameter, 546 xLightStrength value, 557
Trang 7NI N D E X 649
xLightStrength variable, 556
xMirrorTexture variable, 271–275, 278–279
.xml file, 461
XML file, loading data from, 460–465
creating XNA-compatible XML file from
existing object, 464–465
defining custom MapData class, 462
defining TypeReader capable of serializing
object of MapData class, 463
defining TypeWriter capable of serializing
object of MapData class, 463
overview, 460–462
XmlSerializer class, 23, 27
XNA, 491–492, 503–504
XNA 2.0 Framework files, installing, 10
XNA code, Skybox, 84–86
XNA Framework Redistributable 2.0, 10
XNA Game Studio 3.0, 28
deploying games
on PCs, 10
on Xbox 360, 5–8
on Zune, 8–9
.fx HLSL files, 5
game loop timing, 12–14
Draw method frequency, 14
IsRunningSlowly variable, 13
overview, 12
Update method frequency, 13
GameComponent class, 14–23
changing updating order of, 22–23
DrawableGameComponent class, 15–16
making subscribe to interfaces, 20
overview, 14–20
subscribing to ICameraInterface
service, 20–21
using GameComponent class, 17–18
using multiple, 21–22
installing, 1–2
overview, 1
predefined methods, 3–4
Draw method, 4–13
Game1 constructor, 4
Initialize method, 4 LoadContent method, 4 overview, 3
UnloadContent method, 4 Update method, 4 saving/loading data, 23–28 creating StorageDevice method asynchronously, 24–25 loading data from disks, 27–28 overview, 23–24
saving data to disks, 26–27 starting, 3
starting new projects, 3 XNA Game Studio Connect program, 6–8 XNA Game Studio Device Center 7, 9 XNA project, accessing data in, 363–364 XNA-compatible XML file, creating from existing object, 464–465
<XNAContent> tag, 460 XNA-to-HLSL variables, 478, 487–488, 495–496
XNAUtils class, 102 XNAUtils file, 301 xnb binary file, 214–215 xnb file, 215–216, 224 xPreviousShadingMapContents variable, 578 xRelative value, 450
xTexStretch variable, 487 xTexture variable, 335, 577 xTime variable, 151, 257–258, 260, 498 xWaveHeights variable, 495, 497, 499, 504–505
xWaveLengths variable, 495, 497, 499, 504–505
xWaveSpeeds variable, 497
Z
z-buffer (or depth buffer), 36–37, 78, 263 zRelative value, 450
Zune, deploying games on, 8–9