• 13
• 18
• 19
• 27
• 10

Handling game loops

This topic is 4854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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 on other sites
Quote:
 Original post by DirectXFreakMost 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 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 on other sites
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