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.