Handling game loops

Started by
3 comments, last by JimPrice 19 years, 4 months ago
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.
--m_nPostCount++
Advertisement
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
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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
Hi,

Similar topic from a few days ago

HTH,
Jim.

This topic is closed to new replies.

Advertisement