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.