Multiple Game Loops

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.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s