Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
97 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 The Message Pump
 Updating the World
 The Solution
 ALT-TABBING

 Printable version

 


Updating the World

Our next step is to code the UpdateWorld() function. This should be very simple; all your application variables, surfaces and interfaces have already been initialized, and now you just have to update and render them. That’s an easy task.

Yes indeed, it’s an easy task, but only if you plan that your game will have only one screen, no menus, no options, and nothing.

Let’s suppos that we wanted to build a simple DirectX application that shows a splash screen, then goes into the game, and then shows the same splash screen again.

In the most traditional linear programming way, we would do something like this:

void GameMain(void) { Splash(); PlayTheGame(); Splash(); }

Now, with our UpdateWorld() function, we will have use a finite state machine instead:

void UpdateWorld(void) { static int state=0; switch( state ) { case 0: // display a splash screen and return if( FINISHED(Splash()) ) state=1; break; case 1: if( FINISHED(PlayTheGame()) state=2; break; case 2: if( FINISHED(Splash()) ) TerminateApp(); break; } }

And not only this, but we will also have to define our Splash() and PlayTheGame() functions to return a finish state when they have finished. Now suppose that the Splash() function performs a FadeIn() and a FadeOut(). Splash() would look like this:

int Splash( void ) { static int state=0; switch( state ) { case 0: // init things state=1; break; case 1: if( FINISHED(FadeIn()) ) state=2; break; case 2: if( FINISHED(FadeOut()) ) state=0; break; } if( state==0 ) return FINISH_STATE; else return STILL_WORKING; }

Note that we must set the state to 0 when we finish the last step, because we must be able to invoke this function again later in the code. If we are resetting the state, then we must inform our invoker that we have finished, and that he can continue to his next state.

Well, what we have just done is just a very simple application. Now imagine thirty or forty of these functions, each one with a couple dozen states - as I had to do to port Malvinas 2032 from DOS4GW to DirectX - and you will be facing a very big monster. Try to debug, or even follow this code, and you will get really insane. As you can see, this finite state programming model is far more complicated that the simple one achieved by old linear programs.


Next : The Solution