I'm trying to figure out how to structure my main game loop and I'm wondering how other people have done theirs. If you have one that you are willing to share, could you post your code (or pseudo code) here so that I can see it.

The below game loop is the one I currently use, adapted for a client / server based, predicting logics system, and for multithreaded rendering. No audio currently =( The Tick() method is called in a loop until an exit flag is set (either on processing window messages, or because the user clicked the big shiny exit button).

void CCrescent::Tick()
{
// Process any window messages
GetWinSys().MessagePump();
// Retrieve new local input data
GetInputSys().Update();
// Call all game logics:
// First, let server collect all game events,
// then, have game state react to all events,
// finally, let server distribute resulting events
GetServer().PreTick();
GetGameState().Tick();
GetServer().PostTick();
// Have menu run if currently enabled
GetMenu().Tick();
// Start rendering a new frame if the renderer
// thread currently is idle
if (GetRenderSys().RenderStart())
{
// Make all game modules render as
// they desire to in proper order
GetIdleScene().Render();
GetGameScene().Render();
GetMenu().Render();
// Finish rendering frame, reactivate rendering thread
GetRenderSys().RenderFinish();
}
}

The reason for the IsActive() branch is so that I can Alt+Tab away from my game, it will disable the renderer, and idle wait in the background for me to return to it, at which point it re-enables the renderer. In my object hierarchy, a RenderEnvironment means an OpenGL context or a D3D device (currently I use OpenGL only, but it could be extended in future). A Game object controls logic, input, object and camera movement and so forth; a Renderer object does what you'd think. A single game may have multiple Renderers, for instance one for rendering the initial menu and one for rendering the main game. This is single-threaded.

It appears that both of you only have one main update routine. I read a while ago that it is best to split up the update into two phases. One phase for updating the game physics/states and another update for the rendering.

The idea is that the physics engine can run at a different rate then the rendering engine.

I'm not quite sure if that's such a good idea anymore... because the more that I try to separate the two, the more problems that I create.

I'm not sure what kind of updates you'd need to do that would be render-specific. Generally, you can update based on physics etc. at a higher frequency than your framerate, but whenever you render, you just render the most recent state at which the physics system has arrived.

It appears that both of you only have one main update routine. I read a while ago that it is best to split up the update into two phases. One phase for updating the game physics/states and another update for the rendering.

Depends on where and what you exactly split up.

It is indeed a good idea - considering the next generation dual core processors - to split up rendering operations and game state code at some point, so that you use both cores.

In detail, most DX / OpenGl API calls eat up cycles like jellybears, so they feel quite comfortable on a different processor. Also, note that during the actual rendering operations, you usually just require few informations from your entities (such as meshes, textures, transforms), so this process is easy to parallelize.

Finally, splitting up makes sense if you are running fixed step physics, and don't want rendering to interfere there.

For most simpler applications, f.e. variable time step and lower CPU / graphics load, it is in most cases simpler to just do a single game loop that does rendering and logics in a fixed sequence.

For splitting up to work, however, it is not important to have a seperate rendering state and game state (which easily becomes an utter mess, I tried this one too ). It is by far more important to properly design your renderer so it is easy to parallelize.

This especially means having just few points where your logic needs to wait or test for the renderer mutex object. Adding a mutex to every single of your object's transforms will most likely kill your performance.

In my main loop, for example, there is just a single non-blocking synchronization request in the main loop (the RenderStart() call). Then, the logics thread commits all rendering commands to the renderer, where they are queued up in an internal buffer. After that, a call to RenderFinish() restarts the rendering main thread. This thread also has a main loop, but it acts as a simple worker thread, and does not do any updating (i.e. it just handles all commands queued while RenderStart() and RenderFinish(), by translating them into API calls, and then sits waiting).

One might perhaps consider adding extrapolation to the renderer (whereas you'd again need some kind of coherent renderer side state, but you might go with the previous command setup, and provide additional infos for extrapolation, i.e. speed, acceleration). Yet, there will hardly ever be a situation where your renderer is faster than your logics system (and thus had time to extrapolate).

Another idea for splitting up would be to seperate updates of graphic objects from updates to their physical state (which however is another, unrelated problem). They did this in battlefields 1942, for example. There, they update the "skeleton" of soldiers far away less often than for nearby enemies. This leads to a few glitches, but improves the framerate. However, I guess this is one thing that may be done in the logic thread whenever it is time to commit rendering operations. For example, before committing an object, see if it should get its geometry updated. It is also certainly not the point where you'd want to go with yet another thread - simply consider how ugly it would be to have to synch three threads (renderer, logics, geo update) trying to access your object's transforms.

I have event handlers in Ogre so that system events get sent straight through my event system. These probably only get called once frame rendering begins, but that just means the new position is in 1/60 seconds time.

IsInterfaceSuspended() checks if the system has been suspended for any reason - such things like alt-tabbing, alt-entre etc - so that the engine and game is not updated when it is not in use.

// Calculte the frame time
frameTime = CalculateFrameStatistics();
// Multiply it by the frame time multiplier
frameTime *= frameTimeMultiplier;

The above code calculates the current frame delta (for time based movement), fps, etc. The frameTimeMultiplier allows the user to specifcy a value to multiple the frame delta by, which allows them to slow the game down, or speed it up. Great for testing if everything in the game time based, rather than frame based.

Its possible to change the resolution of the game on the fly, which is what the resolution check is.

Where both desktop and gfx are os dependant implementations of interfaces that provide you with events and allow you to draw etc etc... You attach windows/controls to the desktop which are updated automatically (no need to call blah->Update() every frame). So actual execution of rendering happens somewhere in a control. The implementation of the renderer will be provided by another interface...

I'm very happy to see so many people sharing their code. I'm mostly interested in determining how people get their main loop timing designed. I'm having problems with my program, because the game does not run at a constant rate. I get a large fluctuation in frame rate.

To get preety constant frame rate, in my case (post#14), im calculating how much time frame took by calling GetTickCount() function. GetTickCount() returns just how many miliseconds passed since system started, im subtracting current ticks from ticks before frame started to get how many miliseconds curent frame took, it is stored every time into iLastFrameTime variable.

value of desired frame rate i have in sParamsC.fps (it is in example 60) and im calculating how much time ll take one frame in that rate. if ie. sParamsC.fps is 60 then when i divide 1024 by 60 i ll get this time (curently im not sure if one seconds contain 1000 or 1024 miliseconds), result is stored into dwMaxFrameTime.

if condition iLastFrameTime >= dwMaxFrameTime is false then it means that current frame took less than desired so its needed to suspend current thread to not go to next frame too fast, its done by calling Sleep() function.

my code only handles situations when frames are rendered too fast, currently nothing is done if frame tooks more time than desired

EDIT: here are some articles about timing in games that i found some time ago.

Heh. Actually, the loop that I posted was taken from a member function of a singleton instance called TheEngine, and quitFlag is a member variable. The convention is to use "The" in names of pointers to singleton instances to give some kind of explicit indication that there's only one of them. Those are all globally-accessible pointers to the various managers in the engine.

It's a bit tricky to get accurate timings without using the slow QueryPerfCount. My whole system is event based, so things are updated only if required. I have framerates of up to 10*10\\^6 when nothing is changing and down to 30 when heavy stuff is going on. The resolution of the rather fast timeGetTime() is less than 6ms so you get pretty inaccurate results for things faster than 6ms... To solve that for my fps/time per frame counters I did this:

This guaranties that a decent amount of time compared to the timer's resolution has passed before calculating the time per frame. The timer is not queried every frame to safe some cycles. It doesn't make much of a difference here but when profiling using the HighPerfCounter I use something similar to avoid calling the timer every frame (which is damn slow). Normally it is enough to profile every n-th run of a function (as you will not sample the data faster then your fps when displaying it realtime).

I think it is because the physics stuff takes a long time to process, and then I render .... I'm now trying to modify this so that I will render while I update at the same time to see if it helps to speed things up.

Hmm I guess the problem is that - even with the fixed timestep you are using, and the delta you accumulated - if your physics are too slow to guarantee a fixed timestep on your machine, you won't be able to do anything about it.

Think about it. If your physics already take say 30 ms in a 20 ms frame on the average, you just accumulate overwork, a little more each frame, until you have so much work that you have to wait a long time for the next frame. In this case, it doesn't matter what you render, or where else in code you lose time, since you already missed the timeline after your physics.

This also holds true for physics that come close to the desired framerate - where then rendering breaks over your desired fixed step time.

In your case, there are just few workarounds I can suggest:

First one would be to remove the unnecessary fixed step buffering, and just have the entire physics run a bit slower on slow machines. Another one would be to use a variable timestep, which leads to numerical robustness problems with some physics algorithms.

Finally, optimize your physics. Your program should never actually be CPU bound unless you are sitting on minimum spec hardware.