130
\$\begingroup\$

How should I write a main game loop? What are some things that you should do in the game loop, and what are some things that you shouldn't do in the game loop?

I've written plenty of them, but I've never really read up on game loops. I'm sure I could improve them considerably, but I am unsure how.

\$\endgroup\$

closed as too broad by Josh Jul 3 '13 at 3:42

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    \$\begingroup\$ Evil answer : What about using someones engine to make this loop for You? :) \$\endgroup\$ – user712092 Jul 3 '11 at 15:33
  • \$\begingroup\$ you can ask the user the total number of frames he wants to run and use a for loop to the n times the amount of buffers. \$\endgroup\$ – Uğur Gümüşhan Jun 12 '12 at 0:40
121
\$\begingroup\$

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).

\$\endgroup\$
  • 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
  • 1
    \$\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
  • 5
    \$\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\$ @user6003859: I fixed the link. \$\endgroup\$ – Leftium Mar 6 '17 at 3:59
  • 1
    \$\begingroup\$ another useful link: gameprogrammingpatterns.com/game-loop.html \$\endgroup\$ – brita_ Nov 11 '17 at 13:45
51
\$\begingroup\$

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!)

\$\endgroup\$
  • \$\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
4
\$\begingroup\$

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.

http://www.gamasutra.com/view/feature/1830/multithreaded_game_engine_.php

\$\endgroup\$

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