Generally, what are the core things that one should do in the game loop, and what are some things that one shouldn't do in the game loop?

  • \$\begingroup\$ Note to future visitors: although this was an interesting and relevant question when it was asked ~10 years ago when the site was shiny and new, this is not a good representation of the questions we expect today; it is on-topic but does not really present an issue to be solved. \$\endgroup\$ – Vaillancourt Jan 4 at 20:44

The main game loop handles three major tasks:

  1. Get user input
  2. Update the game state
  3. Draw the game

A simple game loop just mushes these three tasks into one while loop. This has some undesired results:

  1. Game runs at different speeds on different computers.
  2. CPU (can be needlessly) pegged at 100% usage.
  3. "Game states"/menus are missing or mixed with game code.
  4. Main game loop is very long and hard to maintain.
  5. Code is difficult to extend/port to other platforms.

Advanced gamed loops address the issues listed above. Here are some useful articles:

For an excellent example game loop, take a look at the Allegro skater demo game:

Game loops often do the same type of work for most games, so I have been thinking of a way to make a generalized game framework. It is better to write one implementation of a game loop and share it between games. It saves work when creating a new game, and improvements to the shared game loop can be shared by all games (for example, adding a FPS counter or screen capture feature).

  • 1
    \$\begingroup\$ Yep, as suspected, I can definitely make improvements. Good reads. Also, thanks for posting the code. It's great to see actual game code with this stuff. \$\endgroup\$ – hokiecsgrad Jul 17 '10 at 13:03
  • 2
    \$\begingroup\$ The CPU should be stuck at 100% usage (or more like 50% if you have at least 2 cores). Think about it. How are you going to get bleeding edge physics, killer graphics, if you're sitting back there saying to yourself "Oh, but I don't want to use all of the CPU." Yes you should use 100% of the CPU if possible (that means multithreading on today's computers though, so it's very difficult to actually use 100% of 4 cores all the time). Get the most out of the machine you're running on that you can. \$\endgroup\$ – bobobobo May 5 '13 at 23:54
  • 6
    \$\begingroup\$ Allow me to append: For desktop programs. On a portable device, you will be burning a lot more battery, so you'd worry about the 100% usage thing. \$\endgroup\$ – bobobobo May 9 '13 at 1:59
  • 1
    \$\begingroup\$ deWiTTERS Game Loop link is dead. \$\endgroup\$ – user7003859 Mar 4 '17 at 3:30
  • 2
    \$\begingroup\$ another useful link: gameprogrammingpatterns.com/game-loop.html \$\endgroup\$ – brita_ Nov 11 '17 at 13:45

I'd recommend Glenn Fiedler's article about robust framerate independence, "Fix Your Timestep!"

(Less relevant to the topic at hand, but the other articles in the series are also quite good - as is everything on his site!)

  • \$\begingroup\$ Those are definitely some really good articles. Thanks for posting! \$\endgroup\$ – hokiecsgrad Jul 17 '10 at 13:04
  • 1
    \$\begingroup\$ I second the recommendation for having a robust framerate independent implementation. Everything in your game should be implemented as a delta value that you periodically update. And that shouldn't be done necessarily at the same time you advance to the next frame. In fact, if you've skipped a frame, you probably really don't want to simply call your update methods with a 2x delta. It would be more consistent to call your update twice with the same delta. Definitely take the time to learn the issues with your framerate and your update cycle. It will save you time later. \$\endgroup\$ – Dennis Munsie Jul 18 '10 at 0:32

If you want to work on game loops, I would advise reading this old article on gamasutra about multithreaded game engine architectures. It deals with a number of different ways to write your game loop.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.