200 likes | 354 Views
Game Architecture in a nutshell. Major Games Programming, February 2010 Jan Verhoeven. Contents. Game loop Games in Windows Design Patterns State machine Links. Game Architecture.
E N D
Game Architecturein a nutshell Major Games Programming, February 2010 Jan Verhoeven
Contents • Game loop • Games in Windows • Design Patterns • State machine • Links
Game Architecture Games are time-dependant interactive applications, consisting of a virtual world simulator that feeds real-time data, a presentation module that displays it, and control mechanisms that allow the player to interact with that world. See gamasutra.com: http://www.gamasutra.com/blogs/SimonLim/20100112/4097/Game_Architecture.php
Real-Time loop • A game is a real-time interactive application with tasks running constantly: • The state of the world must be recomputed • The operator (player) must be allowed to interact with it • The resulting state must be presented to the player, using screen data, audio and so on
Games and Windows • In its default for, the message loop model of a Windows program is not one that will lend itself to game performance. The reason is that the design philosophy behind a typical Windows application and a high performance game are totally different
Windows Application Philosophy • Don't get in the way • Only take use of CPU when we are told to • Spend most of our time sleeping till someone wakes us • Be friendly to other applications and step aside • Things will get done when they need too, no hurry
Game Philosophy : • Take all that you can get • We control all that you see and hear.... • Don't let up for a moment • We're on a deadline here - move it! • Only let others have control as it benefits us
SO? (read it by yourself ;-)) • As you can see, these are two very different ways of thinking. So, do we just replace our message loop with a game loop akin to the days of old? The answer is a resounding NO!!! • Taking complete and utter control of the system in the Windows world will not provide the performance we need. Rather, it will bring the system to its knees. The reason for this is simple : we are reliant on other services to provide access to the system, so we must let Windows run on. • So, is there a happy compromise to be found? A way to insure our performance, while allowing the system to take care of things? Yes, there is, but it will require a few changes.
A typical Windows message loop • while( GetMessage( &msg, NULL, 0, 0 ) ) {TranslateMessage( &msg );DispatchMessage( &msg );} • GetMessage : This function is used to retrieve a message from the Message Queue. If there is not a message available, calling this function causes the thread to sleep until a message is received. • TranslateMessage : This function provides translation of keyboard messages, and must be called on all messages before dispatching them. • DispatchMessage : Calls the appropriate handler or sends the message to the appropriate child window
Keeping Awake (draft version) while (true) { // is there a message to process? if (PeekMessage( &mssg, NULL, 0, 0, PM_REMOVE)) { // dispatch the message TranslateMessage(&mssg);DispatchMessage(&mssg); } else { // our game loop stuff will go here!!} }
Game Loop with Timing • We want our game to run regardless of the underlying hardware and software, so with same speed and motions.
Design Patterns -Singleton- Some useful programming patterns: A singletonis a global object for which only one instance exists in the whole application. See: http://gameprogrammingpatterns.com/singleton.html
Design Patterns -Strategy- Imagine that you are developing a role-playing game where you want to move your characters around on the map. You are interested in supplying different algorithms for movement for different types of characters. For instance, a human character needs to avoid collision with trees etc. while a ghost character can move right through buildings and a wizard character can teleport himself.
Design Patterns And many more: • Composite, • Factory, • State, • And so on …… See: http://gpwiki.org/index.php/Category:Design_patterns
State Machines • Very important subject • Chapter 2 in your AI study book (read it!!) and/or see http://www.ai-junkie.com/architecture/state_driven/tut_state1.html and next 2 pages
FSM Example: Ghost in Pac-Man Player re-spawns Time to exit Center Room Move Randomly Chase Player Player dies Player eats Pellet Pellet wears off Player eats Ghost Run from Player Rise Die Eyes reach Center Room
Links • Game Programming Wiki: http://gpwiki.org/index.php/Game_Programming_Wiki • Game Programming Wikipedia: http://en.wikipedia.org/wiki/Game_programming • GameDev.net: http://www.gamedev.net/ • Amit’s Game Programming Information: http://www-cs-students.stanford.edu/~amitp/gameprog.html • University of Houston: http://games.cs.uh.edu/ • And of course …
About Game Design • Read this very interesting presentation: http://www.cs.rutgers.edu/~nealen/game/gameworkshop.pdf • Why should you study game programming? http://esminfo.prenhall.com/computing/PHIT2/mencher.ppt