Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Can anyone recommend some good tips, articles, sites, etc. for writing the 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 these, but I've never really read up on game loops. I'm sure I could improve them considerably if I read more about them.

share|improve this question

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

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

3  
Evil answer : What about using someones engine to make this loop for You? :) –  user712092 Jul 3 '11 at 15:33
    
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. –  Uğur Gümüşhan Jun 12 '12 at 0:40

3 Answers 3

up vote 73 down vote accepted

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

share|improve this answer
    
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. –  hokiecsgrad Jul 17 '10 at 13:03
    
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. –  bobobobo May 5 '13 at 23:54
    
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. –  bobobobo May 9 '13 at 1:59
    
+1 for deWiTTERS Game Loop –  ymfoi Jul 3 '13 at 3:31

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

share|improve this answer
    
Those are definitely some really good articles. Thanks for posting! –  hokiecsgrad Jul 17 '10 at 13:04
1  
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. –  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.

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

share|improve this answer

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