Game Engine Architecture Game Engine Architecture Jason Gregory A K Peters, Ltd Wellesley, Massachusetts A K Peters/CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2009 by Taylor and Francis Group, LLC A K Peters/CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S Government works Printed in the United States of America on acid-free paper 10 International Standard Book Number-13: 978-1-4398-6526-2 (Ebook-PDF) This book contains information obtained from authentic and highly regarded sources Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint Except as permitted under U.S Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400 CCC is a not-for-profit organization that provides licenses and registration for a variety of users For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the A K Peters Web site at http://www.akpeters.com Dedicated to Trina, Evan and Quinn Gregory, in memory of our heros, Joyce Osterhus and Kenneth Gregory Contents Foreword xiii Preface xvii I Foundations 1 Introduction 1.1 1.2 1.3 1.4 1.5 1.6 1.7 Structure of a Typical Game Team What Is a Game? What Is a Game Engine? Engine Differences Across Genres Game Engine Survey Runtime Engine Architecture Tools and the Asset Pipeline 11 13 25 28 49 Tools of the Trade 57 2.1 2.2 2.3 57 66 85 Version Control Microsoft Visual Studio Profiling Tools vii viii Contents 2.4 2.5 Fundamentals of Software Engineering for Games 3.1 3.2 3.3 Memory Leak and Corruption Detection Other Tools C++ Review and Best Practices Data, Code, and Memory in C/C++ Catching and Handling Errors 87 88 91 91 98 128 3D Math for Games 137 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 137 138 151 169 177 181 185 192 Solving 3D Problems in 2D Points and Vectors Matrices Quaternions Comparison of Rotational Representations Other Useful Mathematical Objects Hardware-Accelerated SIMD Math Random Number Generation II Low-Level Engine Systems 195 Engine Support Systems 197 5.1 5.2 5.3 5.4 5.5 197 205 223 242 252 Subsystem Start-Up and Shut-Down Memory Management Containers Strings Engine Configuration Resources and the File System 261 6.1 6.2 262 272 File System The Resource Manager The Game Loop and Real-Time Simulation 303 7.1 7.2 303 304 The Rendering Loop The Game Loop 14.9 High-Level Game Flow sents a single player objective or encounter and is associated with a particular locale within the virtual game world As the player completes each task, the state machine advances to the next state, and the player is presented with a new set of goals The state machine also defines what should happen in the event of the player’s failure to accomplish the necessary tasks or objectives Often, failure sends the player back to the beginning of the current state, so he or she can try again Sometimes after enough failures, the player has run out of “lives” and will be sent back to the main menu, where he or she can choose to play a new game The flow of the entire game, from the menus to the first “level” to the last, can be controlled through this high-level state machine The task system used in Naughty Dog’s Jak and Daxter and Uncharted franchises is an example of such a state-machine-based system It allows for linear sequences of states (called tasks at Naughty Dog) It also permits parallel tasks, where one task branches out into two or more parallel tasks, which eventually merge back into the main task sequence This parallel task feature sets the Naughty Dog task graph apart from a regular state machine, since state machines typically can only be in one state at a time 817 Part V Conclusion 15 You Mean There’s More? C ongratulations! You’ve reached the end of your journey through the landscape of game engine architecture in one piece (and hopefully none the worse for wear) With any luck, you’ve learned a great deal about the major components that comprise a typical game engine But of course, every journey’s end is another’s beginning There’s a great deal more to be learned within each and every topic area covered within these pages As technology and computing hardware continue to improve, more things will become possible in games—and more engine systems will be invented to support them What’s more, this book’s focus was on the game engine itself We haven’t even begun to discuss the rich world of gameplay programming, a topic that could fill many more volumes In the following brief sections, I’ll identify a few of the engine and gameplay systems we didn’t have room to cover in any depth in this book, and I’ll suggest some resources for those who wish to learn more about them 15.1 Some Engine Systems We Didn’t Cover 15.1.1 Audio I mentioned in Section 1.6.13 that audio often takes a back seat to other aspects of game development, much to the chagrin of the audio engineers, sound de821 822 15 You Mean There’s More? signers, voice actors, and composers who work so hard to add that all-toocritical fourth dimension to the virtual game world And yet, sadly, the same thing has happened in this book—I am out of room and out of time, so a full treatment of audio will have to wait until the second edition (In keeping with a long and painfully unfortunate tradition in game development, once again audio gets the shaft!) Thankfully, a number of other books and online resources provide a wealth of information on audio development First off, I recommend reading the documentation for Microsoft’s XACT sound authoring tool and runtime API, located on the MSDN website under XACT: Tutorials and Samples (http:// msdn.microsoft.com/en-us/library/bb172329(VS.85).aspx) XACT supports virtually every audio feature the average game programmer would want, and its documentation is quite easy to digest The Game Programming Gems book series also includes a plethora of articles on audio—see [7] Section and [40] Section 15.1.2 Movie Player Most games include a movie player for displaying prerendered movies, also known as full-motion video (FMV) The basic components of the movie player are an interface to the streaming file I/O system (see Section 6.1.3), a codec to decode the compressed video stream, and some form of synchronization with the audio playback system for the sound track A number of different video encoding standards and corresponding codecs are available, each one suited to a particular type of application For example, video CDs (VCD) and DVDs use MPEG-1 and MPEG-2 (H.262) codecs, respectively The H.261 and H.263 standards are designed primarily for online video conferencing applications Games often use standards like MPEG-4 part (e.g., DivX), MPEG-4 Part 10 / H.264, Windows Media Video (WMV), or Bink Video (a standard designed specifically for games by Rad Game Tools, Inc.) See http://en.wikipedia.org/wiki/Video_codec and http://www.radgametools.com/bnkmain.htm for more information on video codecs 15.1.3 Multiplayer Networking Although we have touched on a number of aspects of multiplayer game architecture and networking (e.g., Sections 1.6.14, 7.7, and 14.8.3.2), this book’s coverage of the topic is far from complete For an in-depth treatment of multiplayer networking, see [3] 15.2 Gameplay Systems 15.2 Gameplay Systems A game is of course much more than just its engine On top of the gameplay foundation layer (discussed in Chapter 14), you’ll find a rich assortment of genre- and game-specific gameplay systems These systems tie the myriad game engine technologies described in this book together into a cohesive whole, breathing life into the game 15.2.1 Player Mechanics Player mechanics are of course the most important gameplay system Each genre is defined by a general style of player mechanics and gameplay, and of course every game within a genre has its own specific designs As such, player mechanics is a huge topic It involves the integration of human interface device systems, motion simulation, collision detection, animation, and audio, not to mention integration with other gameplay systems like the game camera, weapons, cover, specialized traversal mechanics (ladders, swinging vines, etc.), vehicle systems, puzzle mechanics, and so on Clearly player mechanics are as varied as the games themselves, so there’s no one place you can go to learn all about them It’s best to tackle this topic by studying a single genre at a time Play games and try to reverse-engineer their player mechanics Then try to implement them yourself! And as a very modest start on reading, you can check out [7] Section 4.11 for a discussion of Mario-style platformer player mechanics 15.2.2 Cameras A game’s camera system is almost as important as the player mechanics In fact, the camera can make or break the gameplay experience Each genre tends to have its own camera control style, although of course every game within a particular genre does it a little bit differently (and some very differently!) See [6] Section 4.3 for some basic game camera control techniques In the following paragraphs, I’ll briefly outline some of the most prevalent kinds of cameras in 3D games, but please note that this is far from a complete list • Look-at cameras This type of camera rotates about a target point and can be moved in and out relative to this point • Follow cameras This type of camera is prevalent in platformer, thirdperson shooter, and vehicle-based games It acts much like a look-at camera focused on the player character/avatar/vehicle, but its motion typically lags the player A follow camera alos includes advanced collision detection and avoidance logic and provides the human player 823 824 15 You Mean There’s More? with some degree of control over the camera’s orientation relative to the player avatar • First-person cameras As the player character moves about in the game world, a first-person camera remains affixed to the character’s virtual eyes The player typically has full control over the direction in which the camera should be pointed, either via mouse or joypad control The look direction of the camera also translates directly into the aim direction of the player’s weapon, which is typically indicated by a set of disembodied arms and a gun attached to the bottom of the screen, and a reticle at the center of the screen • RTS cameras Real-time strategy and god games tend to employ a camera that floats above the terrain, looking down at an angle The camera can be panned about over the terrain, but the pitch and yaw of the camera are usually not under direct player control • Cinematic cameras Most three-dimensional games have at least some cinematic moments in which the camera flies about within the scene in a more filmic manner rather than being tethered to an object in the game 15.2.3 Artificial Intelligence Another major component of most character-based games is artificial intelligence (AI) At its lowest level, an AI system is usually founded in technologies like basic path finding (which commonly makes use of the well-known A* algorithm), perception systems (line of sight, vision cones, knowledge of the environment, etc.) and some form of memory On top of these foundations, character control logic is implemented A character control system determines how to make the character perform specific actions like locomoting, navigating unusual terrain features, using weapons, driving vehicles, taking cover, and so on It typically involves complex interfaces to the collision, physics, and animation systems within the engine Character control is discussed in detail in Sections 11.11 and 11.12 Above the character control layer, an AI system typically has goal setting and decision making logic, emotional state, group behaviors (coordination, flanking, crowd and flocking behaviors, etc.), and possibly some advanced features like an ability to learn from past mistakes or adapt to a changing environment Of course, the term “artificial intelligence” is a bit of a misnomer when applied to games Game AI is usually more smoke and mirrors than it is an attempt at true artificial intelligence It’s important to realize that, in a game, 15.2 Gameplay Systems all that really matters is the player’s perception of what is going on A classic example comes from the game Halo When Bungie first implemented their AI system, they included a simple rule that stated that the small “grunt” aliens would all run away when their leader had died In play test after play test, no one realized that this was why the little guys were running away Even after the Bungie team had made various adjustments to the animations and AI behaviors in the game, still no one got the connection Finally, the developers resorted to having one of the grunts say, “Leader dead! Run away!” This just goes to show that all the AI logic in the world doesn’t amount to anything if the player doesn’t perceive the meaning behind it AI programming is a rich topic, and we certainly have not done it justice in this book For more information, see [16], [6] Section 3, [7] Section 3, and [40] Section 15.2.4 Other Gameplay Systems Clearly there’s a lot more to a game than just player mechanics, cameras, and AI Some games have drivable vehicles, implement specialized types of weaponry, allow the player to destroy the environment with the help of a dynamic physics simulation, let the player create his or her own characters, build custom levels, require the player to solve puzzles, or… Of course, the list of genre- and game-specific features, and all of the specialized software systems that implement them, could go on forever Gameplay systems are as rich and varied as games are Perhaps this is where your next journey as a game programmer will begin! 825 References [1] Tomas Akenine-Moller, Eric Haines, and Naty Hoffman Real-Time Rendering (3rd Edition) Wellesley, MA: A K Peters, 2008 [2] Andrei Alexandrescu Modern C++ Design: Generic Programming and Design Patterns Applied Resding, MA: Addison-Wesley, 2001 [3] Grenville Armitage, Mark Claypool and Philip Branch Networking and Online Games: Understanding and Engineering Multiplayer Internet Games New York, NY: John Wiley and Sons, 2006 [4] James Arvo (editor) Graphics Gems II San Diego, CA: Academic Press, 1991 [5] Grady Booch, Robert A Maksimchuk, Michael W Engel, Bobbi J Young, Jim Conallen, and Kelli A Houston Object-Oriented Analysis and Design with Applications, third edition Reading, MA: Addison-Wesley, 2007 [6] Mark DeLoura (editor) Game Programming Gems Hingham, MA: Charles River Media, 2000 [7] Mark DeLoura (editor) Game Programming Gems Hingham, MA: Charles River Media, 2001 [8] Philip Dutré, Kavita Bala and Philippe Bekaert Advanced Global Illumination (Second Edition) Wellesley, MA: A K Peters, 2006 827 828 References [9] David H Eberly 3D Game Engine Design: A Practical Approach to Real-Time Computer Graphics San Francisco, CA: Morgan Kaufmann, 2001 [10] David H Eberly 3D Game Engine Architecture: Engineering Real-Time Applications with Wild Magic San Francisco, CA: Morgan Kaufmann, 2005 [11] David H Eberly Game Physics San Francisco, CA: Morgan Kaufmann, 2003 [12] Christer Ericson Real-Time Collision Detection San Francisco, CA: Morgan Kaufmann, 2005 [13] Randima Fernando (editor) GPU Gems: Programming Techniques, Tips and Tricks for Real-Time Graphics Reading, MA: Addison-Wesley, 2004 [14] James D Foley, Andries van Dam, Steven K Feiner, and John F Hughes Computer Graphics: Principles and Practice in C, second edition Reading, MA: Addison-Wesley, 1995 [15] Grant R Fowles and George L Cassiday Analytical Mechanics (7th Edition) Pacific Grove, CA: Brooks Cole, 2005 [16] John David Funge AI for Games and Animation: A Cognitive Modeling Approach Wellesley, MA: A K Peters, 1999 [17] Erich Gamma, Richard Helm, Ralph Johnson, and John M Vlissiddes Design Patterns: Elements of Reusable Object-Oriented Software Reading, MA: Addison-Wesley, 1994 [18] Andrew S Glassner (editor) Graphics Gems I San Francisco, CA: Morgan Kaufmann, 1990 [19] Paul S Heckbert (editor) Graphics Gems IV San Diego, CA: Academic Press, 1994 [20] Maurice Herlihy, Nir Shavit The Art of Multiprocessor Programming San Francisco, CA: Morgan Kaufmann, 2008 [21] Roberto Ierusalimschy, Luiz Henrique de Figueiredo and Waldemar Celes Lua 5.1 Reference Manual Lua.org, 2006 [22] Roberto Ierusalimschy Programming in Lua, 2nd Edition Lua.org, 2006 [23] Isaac Victor Kerlow The Art of 3-D Computer Animation and Imaging (Second Edition) New York, NY: John Wiley and Sons, 2000 [24] David Kirk (editor) Graphics Gems III San Francisco, CA: Morgan Kaufmann, 1994 [25] Danny Kodicek Mathematics and Physics for Game Programmers Hingham, MA: Charles River Media, 2005 References [26] Raph Koster A Theory of Fun for Game Design Phoenix, AZ: Paraglyph, 2004 [27] John Lakos Large-Scale C++ Software Design Reading, MA: Addison-Wesley, 1995 [28] Eric Lengyel Mathematics for 3D Game Programming and Computer Graphics, 2nd Edition Hingham, MA: Charles River Media, 2003 [29] Tuoc V Luong, James S H Lok, David J Taylor and Kevin Driscoll Internationalization: Developing Software for Global Markets New York, NY: John Wiley & Sons, 1995 [30] Steve Maguire Writing Solid Code: Microsoft’s Techniques for Developing BugFree C Programs Bellevue, WA: Microsoft Press, 1993 [31] Scott Meyers Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd Edition) Reading, MA: Addison-Wesley, 2005 [32] Scott Meyers More Effective C++: 35 New Ways to Improve Your Programs and Designs Reading, MA: Addison-Wesley, 1996 [33] Scott Meyers Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library Reading, MA: Addison-Wesley, 2001 [34] Ian Millington Game Physics Engine Development San Francisco, CA: Morgan Kaufmann, 2007 [35] Hubert Nguyen (editor) GPU Gems Reading, MA: Addison-Wesley, 2007 [36] Alan W Paeth (editor) Graphics Gems V San Francisco, CA: Morgan Kaufmann, 1995 [37] C Michael Pilato, Ben Collins-Sussman, and Brian W Fitzpatrick Version Control with Subversion, second edition Sebastopol , CA: O’Reilly Media, 2008 (Commonly known as “The Subversion Book.” Available online at http://svnbook.red-bean.com.) [38] Matt Pharr (editor) GPU Gems 2: Programming Techniques for High-Performance Graphics and General-Purpose Computation Reading, MA: Addison-Wesley, 2005 [39] Bjarne Stroustrup The C++ Programming Language, special edition (3rd edition) Reading, MA: Addison-Wesley, 2000 [40] Dante Treglia (editor) Game Programming Gems Hingham, MA: Charles River Media, 2002 [41] Gino van den Bergen Collision Detection in Interactive 3D Environments San Francisco, CA: Morgan Kaufmann, 2003 829 830 References [42] Alan Watt 3D Computer Graphics (3rd Edition) Reading, MA: Addison Wesley, 1999 [43] James Whitehead II, Bryan McLemore and Matthew Orlando World of Warcraft Programming: A Guide and Reference for Creating WoW Addons New York, NY: John Wiley & Sons, 2008 [44] Richard Williams The Animator’s Survival Kit London, England: Faber & Faber, 2002 ... 1.4 1.5 1.6 1.7 Structure of a Typical Game Team What Is a Game? What Is a Game Engine? Engine Differences Across Genres Game Engine Survey Runtime Engine Architecture Tools and the Asset Pipeline... games Hydro Thunder Engine Can be “modded” to build any game in a specific genre Unreal Quake III Engine Engine Can be us ed to build any game imaginable Probably impossible Figure 1.1 Game engine. .. featured game engine built on top of OGRE Crystal Space is a game engine with an extensible modular architecture Torque and Irrlicht are also well-known and widely used engines 1.6 Runtime Engine Architecture