Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With...
-
date post
22-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With...
![Page 1: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/1.jpg)
Chapter 3.4Game Architecture
![Page 2: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/2.jpg)
Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of
code, a well-defined architecture is essential for: Adhering to deadlines
Managing personal
2
![Page 3: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/3.jpg)
Overall Architecture Main structure
Game-specific code Game-engine code Both types of code are often split into modules,
which can be static libraries, dynamic link libraries (DLLs), or just subdirectories
3
![Page 4: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/4.jpg)
Overall Architecture Coupling of code is concern Architecture types
Ad-hoc (everything accesses everything) Modular DAG (directed acyclic graph) Layered
4
![Page 5: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/5.jpg)
Overall Architecture Options for integrating tools into the
architecture Separate code bases (if there's no need to share
functionality) XML data sheets (SVG, X3D)
Partial use of game-engine functionality Level Editors
Full integration Play, pause, edit, resume
5
![Page 6: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/6.jpg)
Overview: Initialization/Shutdown The initialization step prepares everything
that is necessary to start a part of the game
The shutdown step undoes everything the initialization step did, but in reverse order
6
![Page 7: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/7.jpg)
FrontEnd Initialization/Shutdown
{ FrontEnd frontEnd; // FacadefrontEnd.Initialize();frontEnd.loop();frontEnd.shutdown();
}
7
![Page 8: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/8.jpg)
Memory Leaks (I)
char *a = malloc(128*sizeof(char));char *b = malloc(128*sizeof(char));
// ---------------------------------------b = a; // oops! // -----------------------------------------------------
free(a);free(b); // won't work
8
![Page 9: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/9.jpg)
RIAA Resource Acquisition Is Initialization A useful rule to minimalize mismatch errors
in the initialization and shutdown steps Means that creating an object acquires and
initializes all the necessary resources, and destroying it destroys and shuts down all those resources
RIAA is helpful for managing memory but can still result in leaks.
9
![Page 10: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/10.jpg)
FrontEnd Initialization/Shutdown
try { FrontEnd frontEnd; // FacadefrontEnd.loop();
}catch (…) { // handle problems here}
// Destructor ~FrontEnd called before main() ends
10
![Page 11: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/11.jpg)
Memory Leaks (II)
BaseClass* obj_ptr = new DerivedClass; // Allowed due to polymorphism....delete obj_ptr; // calls ~Parent() destructor NOT ~Child()
11
![Page 12: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/12.jpg)
Optimizations Fast shutdown (level change)
Creating and destroying objects is costly Using “memory pool” is faster
Warm reboot Some resources can't be recovered until
main() ends. Restarting machine reinitializes stack Hand-Held gaming devices.
12
![Page 13: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/13.jpg)
Overview:Main Game Loop
Games are driven by a game loop that performs a series of tasks every frame
Game may consist of single main loop Some games have separate loops for the front
and and the game itself Multi-threading may also entail multiple
loops
13
![Page 14: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/14.jpg)
Frames
Don't think of a frame as a picture A “frame” is a logical unit
Game frame (time step) Graphics or rendering frame (screen)
30 fps (30hz) flicker test Also frame rate of Halo 3
Some LCD max of 60fps
14
![Page 15: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/15.jpg)
Overview:Main Game Loop
Tasks Handling time (time stamp, time elapsed) Gathering player input Networking Simulation Collision detection and response Object updates Rendering Other miscellaneous tasks
15
![Page 16: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/16.jpg)
Overview:Main Game Loop
Structure Hard-coded loops Multiple game loops
For each major game state Front-End, Main, etc. For major threads
Consider steps as tasks to be iterated through
16
![Page 17: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/17.jpg)
17
while ( !IsDone() ) {UpdateTime();GetInput();GetNetworkMessages();SimulateWorld();CollisionStep();UpdateObjects();RenderWorld(); // the 'graphics' partMiscTasks();
}
![Page 18: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/18.jpg)
Overview:Main Game Loop
Execution order Most of the time it doesn't matter In some situations, execution order is important Can help keep player interaction seamless Can maximize parallelism Exact ordering depends on hardware
18
![Page 19: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/19.jpg)
19
while ( !IsDone() ) {Tasks::iterator it = m_tasks.begin();for (; it != m_tasks.end(); it ++){
Task* task = *it;it -> update();
}}
![Page 20: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/20.jpg)
Decoupling Rendering Can decouple the rendering step from
simulation and update steps 30fps game loop 100fps graphics capability
Results in higher frame rate, smoother animation, and greater responsiveness
Implementation is tricky and can be error-prone
20
![Page 21: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/21.jpg)
21
while ( !IsDone() ) {
UpdateTime();
if(TimetoRunSimulation())RunSimulation();
if(SimulationNotRun())InterpolateState();
RenderWorld();
}
![Page 22: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/22.jpg)
Multi-threading Allowing multiple operations to happen in
parallel. (requires sharing of data code) Consider that a GPU is a separate processor. Potential 'threads'/'concurrent processes'
Physics Collision calculations Animation processing Agent updates AI pathfinding
22
![Page 23: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/23.jpg)
Game Entities What are game entities?
Basically anything in a game world that can be interacted with
More precisely, a self-contained piece of logical interactive content
Enemy, bullet, fire, menu button Only things we will interact with should become
game entities
23
![Page 24: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/24.jpg)
Game Entities - Organization Basic Choices
Simple list Multiple databases Logical tree Spatial database
Multiple options are often necessary Single Master List Other data structures use pointers to objects in
master list Observer model maintains consistency
24
![Page 25: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/25.jpg)
Game Entities - Updating Updating each entity once per frame can be
too expensive Can use a tree structure to impose a hierarchy
for updating Can use a priority queue to decide which
entities to update every frame Different operations can use different data
structures.
25
![Page 26: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/26.jpg)
Game Entities - Creation Basic object factories
Enum list, switch statement Returns point to object
Extensible object factories Allows registration of new types of objects Using automatic registration Using explicit registration
26
![Page 27: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/27.jpg)
Game Entities – Level Instantiation
Loading a level involves loading both assets and the game state
It is necessary to create the game entities and set the correct state for them
Level/State often stored in file Using instance data vs. template data
Not all object data will be different “Flyweight” concept: one copy of duplicate
data
27
![Page 28: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/28.jpg)
Game Entities - Identification Strings Pointers Unique IDs or handles (UID)
Maps a short unique name to a pointer Hashmap, Key → bucket
Hashmap usually managed by an object
28
![Page 29: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/29.jpg)
Game Entities - Communication Simplest method is function calls
How to know what functions supported? Many games use a full messaging system
Usually a dedicated object (singleton) Need to be careful about passing and allocating messages
Messages need to be small, patterned Classes (and union) are useful for establishing
message types 1000's of messages per frame, new(), delete()
overhead Memory pools key; overriding new/delete to refer
to fixed set of locations: “post office boxes”29
![Page 30: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/30.jpg)
Exercises
Highly recommend you look at questions 2,3 and 4 in 2nd edition book.
2. C++ memory leak utilities 3. Simple game loop, separate rendering
loop (print messages for each part). 4. Task loop that allows tasks to register.
30
![Page 31: Chapter 3.4 Game Architecture. Overall Architecture The code for modern games is highly complex With code bases exceeding a million lines of code, a well-defined.](https://reader035.fdocuments.in/reader035/viewer/2022062715/56649d7d5503460f94a6034f/html5/thumbnails/31.jpg)
The End
31