Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

nsc92

Member Since 05 Jun 2009
Offline Last Active Jun 05 2012 05:47 PM
-----

Topics I've Started

Main game loop design

03 June 2012 - 04:00 PM

So far, I've made a few small simple games in the past using visual c++ and Windows GDI. More recently in the past year, I've been wanting to develop more complex games and have learned through books and online tutorials about AI techniques and I have a decent understanding of how to use DirectX.

My problem is I don't know how to structure my main game loop. Essentially, I want to create my own simple game engine. I know there are plenty of game engines out there, but I want to do this as a learning experience more than anything.

Here are the three main things I know that I need in my loop.
  • Graphics update -- runs more or less as fast as it can
  • Physics update -- from what I understand, this should run every predetermined set period of time to limit the number of odd physics related bugs that I see.
  • AI update -- this takes care of computer "strategy", pathfinding, etc, and should only be run much less often when compared with the graphics or physics updates.
Based on what I've ready, the sudo code way of how I imagine this going looks like this:
#define PHYSICS_DELAY 30
#define AI_DELAY 500
.
.
.
while(!exit)
{
   if (time_since_last_physics_update > PHYSICS_DELAY)
		 run_physics_update();
   if (time_since_last_ai_update > AI_DELAY)
		 run_ai_update();
   run_graphics_update();
}

However, this means that the time required to make it through a single loop is highly variable. It might just run the graphics update (hopefully not because from what I read, the physics should be updating about twice as fast as the graphics) or it might run all three updates.

I'm sure I'm missing something because this doesn't seem right to me at all.

Could someone please explain to me how a game loop should work? Everything I've seen up to this point that tries to explain it leaves out one part or another.

PARTNERS