Sign in to follow this  
DirectXFreak

Handling game loops

Recommended Posts

Most of the authors of the books I read code their game loops like so:
switch(dwGameState) 
{
case GAME_STATE_INIT:
  {
  // blah
  }
case GAME_STATE_RUN:
  {
  // blah
  }
case GAME_STATE_EXIT:
  {
  // blah
  }
}

OK, this works... But it's not optimal. Is there a more efficient method to do it? Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by DirectXFreak
Most of the authors of the books I read code their game loops like so:

*** Source Snippet Removed ***

OK, this works... But it's not optimal. Is there a more efficient method to do it?
Thanks.


maybe you want to go with a kernel and task approach as shown in the Enginuity articles, it will be much more flexible.

HTH

Share this post


Link to post
Share on other sites
If you just want a simpler structure without getting as complicated as a kernel system, you can do this:


void IntroLoop()
{
while(IntroRunning)
DoIntroStuff();
}

void GameLoop()
{
while(GameRunning)
DoGameStuff();
}

void ExitLoop()
{
while(ExitRunning)
DoExitStuff();
}

void MainFunction()
{
InitStuff();
IntroLoop();
GameLoop();
ExitLoop();
CleanUpStuff();
}


This eliminates a conditional for each iteration of the game loop - a minimal performance gain, but if you do such "hoisting" tricks as much as possible, you can get much better execution efficiency.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Here is how I do it. works well enough for me
psudocode:

while not Endgame
select State
case MAIN_GUI
Setup_main_Gui()
Main_Gui_Loop() //loops forever until the 'State' val changes
End_Main_Gui()

case MAIN_GAME
Setup_Main_Game()
Main_Game_Loop() // loops until the state changes
End_Main_Game()

wend

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this