Richard Carter

Computer Science & Game Development Student at North Carolina State University

09-22-11

Multiple Game Loops

under Uncategorized

All the game development books and tutorials teach one game loop. Sometimes they implement some sort of finite state machine, but they pretty much always use one main game loop, with some sort of conditional statements to choose what to draw at what time.

But why limit yourself to just one loop? The general format of a game loop is pretty simple, so instead of transitioning with booleans or a state machine object, why not transition with the natural flow of the language, by exiting one loop and entering another? This generally cleans up code. Take for example the following example main loop:

while(!userQuit) {
    clearScreen();
    if(stillLoading()) {
        loadingImage.draw();
    } else {
        stuff.draw();
        gui.draw();
        // etc.
    }
    presentScreen();
}

That’s just a simple example, and that conditional could potentially have more branches for transitions between levels, a pause menu, etc. But why not, for discrete states, do the following?

while(stillLoading()) {
    clearScreen();
    loadingImage.draw();
    presentScreen();
}
while(!userQuit) {
    clearScreen();
    stuff.draw();
    gui.draw();
    // etc.
    presentScreen();
}

This is much cleaner and easier to follow.

Share this article:
  • Print
  • RSS
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
  • Live
  • StumbleUpon

Add A Comment

Sorry for the mess! I recently got hacked and lost my previous theme, so I'm working on reconstructing that. In the meantime, this theme is messy/broken but at least you can read my posts. Sorry!!


Hi, I'm Richard Carter! I use this blog to document particularly difficult-to-solve computer problems. My posts are written for clarity and keywords for search engines to pick up on, so that the next person that runs into the same problem will easily find my solution here and have an easier time than I had! I'm forging a path through the brush, so to speak. So if you came here by search engine, I guess it worked! Enjoy the solution to your problem.