Download presentation
Presentation is loading. Please wait.
Published byBranden Strickland Modified over 9 years ago
1
CSCE 552 Fall 2012 Language and Programming By Jijun Tang
2
Data Structures: Array Elements are adjacent in memory (great cache consistency) Requires continuous memory space They never grow or get reallocated Use dynamic incremental array concept GCC has a remalloc function In C++ there's no check for going out of bounds Use vector if possible Keep in mind of checking boundaries Inserting and deleting elements in the middle is expensive
3
Lists
4
Hash Table
5
Stack/Queue/Priority Queue
6
Bit packing Fold all necessary data into a smaller number of bits Bool in C++ may use up to 4 bytes, thus is very expensive Very useful for storing boolean flags: pack 32 in an integer Possible to apply to numerical values if we can give up range or accuracy Very low level trick Use shifts to handle the operation or use assembly Only use when absolutely necessary
7
Bits
8
Inheritance Models “is-a” relationship Extends behavior of existing classes by making minor changes Do not overuse, if possible, use component systerm UML diagram representing inheritance
9
Component Systems Component system organization
10
Factory Pattern
11
Singleton Implements a single instance of a class with global point of creation and access For example, GUI Don't overuse it!!!
12
Adapter Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces Real interface
13
Observer Allows objects to be notified of specific events with minimal coupling to the source of the event Two parts subject and observer
14
Composite Allow a group of objects to be treated as a single object Very useful for GUI elements, hierarchical objects, inventory systems, etc
15
Composite Pattern Example - I Add many more inherited classes
16
Game Architecture
17
Overall Architecture The code for modern games is highly complex The Sims: 3 million lines of code Xbox HD DVD player: 4.7 million lines MS Train Simulator has 1GB installed, with only 10MB executable With code bases exceeding a million lines of code, a well-defined architecture is essential
18
Overall Architecture Main structure Game-specific code Game-engine code Both types of code are often split into modules, which can be static libraries, DLLs, or just subdirectories
19
Overall Architecture Architecture types Ad-hoc (everything accesses everything) Modular DAG (directed acyclic graph) Layered Options for integrating tools into the architecture Separate code bases (if there's no need to share functionality) Partial use of game-engine functionality Full integration
20
Ad-hoc
21
Modular
22
DAG
23
Layered
24
Example
25
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
26
Initialization/Shutdown 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 Optimizations Fast shutdown Warm reboot
27
Overview: Main Game Loop Games are driven by a game loop that performs a series of tasks every frame Some games have separate loops for the front and the game itself Other games have a unified main loop Must finish a loop within 0.017 second
28
Tasks of Main Game Loop Handling time Gathering player input Networking Simulation Collision detection and response Object updates Rendering Other miscellaneous tasks
30
Sample Game Loop
31
private void GameLoop() { frameRate = game.FrameRate(); ticks = Environment.TickCount; lastTime = 0; if (ticks > lastTime + 16) { lastTime = ticks; switch (game.gameState) { case Game.GameStates.STATE_PLAY: game.playScreen.Draw(); break; case Game.GameStates.STATE_LOAD: game.loadScreen.Draw(); break; case Game.GameStates.STATE_PLAYING: doScrolling(); doHero(); doSide(); break; } game.Update(); Application.DoEvents(); } else { System.Threading.Thread.Sleep(1); } }
32
Main Game Loop Structure Hard-coded loops Multiple game loops: for each major game state Consider steps as tasks to be iterated through Coupling Can decouple the rendering step from simulation and update steps Results in higher frame rate, smoother animation, and greater responsiveness Implementation is tricky and can be error-prone
33
Execution Order of Main Loop 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
34
static void Start(UserSettings settings, string[] args) { InitSimulator(settings, args); Simulator.Start(); Viewer = new Viewer3D(Simulator); Viewer.Run(); Simulator.Stop(); } [ThreadName( "Render" )] public void Run() { LoaderProcess = new LoaderProcess( this ); UpdaterProcess = new UpdaterProcess( this ); RenderProcess = new RenderProcess( this ); RenderProcess.Run(); }
35
static void Start(UserSettings settings, string[] args) { InitSimulator(settings, args); Simulator.Start(); Viewer = new Viewer3D(Simulator); Viewer.Run(); Simulator.Stop(); } [ThreadName( "Render" )] public void Run() { LoaderProcess = new LoaderProcess( this ); UpdaterProcess = new UpdaterProcess( this ); RenderProcess = new RenderProcess( this ); RenderProcess.Run(); }
36
public LoaderProcess(Viewer3D viewer) { Threaded = true; Viewer = viewer; if (Threaded) { State = new ProcessState("Loader"); Thread = new Thread(LoaderThread); Thread.Start(); }
37
[ThreadName("Loader")] void LoaderThread() { Profiler.SetThread(); while (Thread.CurrentThread.ThreadState == System.Threading.ThreadState.Running) { // Wait for a new Update() command State.WaitTillStarted(); try { if (!DoLoad()) return; } finally { State.SignalFinish(); }
38
public UpdaterProcess(Viewer3D viewer) { Threaded = System.Environment.ProcessorCount > 1; Viewer = viewer; if (Threaded) { State = new ProcessState("Updater"); Thread = new Thread(UpdaterThread); Thread.Start(); }
39
[ThreadName("Updater")] void UpdaterThread() { Profiler.SetThread(); while (Thread.CurrentThread.ThreadState == System.Threading.ThreadState.Running) { // Wait for a new Update() command State.WaitTillStarted(); try { if (!DoUpdate()) return; } finally { State.SignalFinish(); }
40
public UpdaterProcess(Viewer3D viewer) { Threaded = System.Environment.ProcessorCount > 1; Viewer = viewer; if (Threaded) { State = new ProcessState("Updater"); Thread = new Thread(UpdaterThread); Thread.Start(); }
41
[ThreadName("Updater")] void UpdaterThread() { Profiler.SetThread(); while (Thread.CurrentThread.ThreadState == System.Threading.ThreadState.Running) { // Wait for a new Update() command State.WaitTillStarted(); try { if (!DoUpdate()) return; } finally { State.SignalFinish(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.