PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Game loop order of events

I'm somewhat of a seasoned developer but this is my first time tackling a somewhat large game project (i.e. more than the basic pong).

My quesion revolves around the order of events within a game loop and how input should be thrown in there. In my comments, I have something sort of like this (note, I am using PocketFrog if that helps)
Code:

GameLoop
{
// get input state

// update objects (AI, PC, static obj's etc...)

// draw background

// draw objects

// update screen(flip)
}


Is this kind of how it should be done, or is it better to let the individual objects themselves determine the input state? PocketFrog has callbacks for ButtonDown and ButtonUp which seems to imply that some sort of controller object notifies the individual objects about the input state.

How do you do it?

-Mark
Your basic loop is fine.

If I were using C++, I'd probably hold a list of objects that responded to input and define a virtual function in an ancestor so that I could provide the objects with their input (sounds like PocketFrog's way of doing things, from your desription - I've not looked it PFrog). Actually, if you didn't have many objects that you throw input at, it might be cleaner to use multiple inheritance for those objects to get their input function.

I wouldn't check the input per object. Just have a loop after the input that throws it at all of the relevant objects...

I probably wouldn't make an "input controller" because the coupling would be too high.
Which is really the best (from a performance perspective) of these two options for the gameloop:

1) Use set a timer that fires every 30 ms or so (to get 33 fps), and then call the game's main loop every WM_TIMER.

2) Have it go into a main loop and there every frame process messages posted by the system. After this it will check if drawing pervious frame was faster then what would be same as 34 fps. If so it pauses a bit and then goes on (so the game doesn't run away at ultraspeed at times on faster devices)

Are 1 or 2 the way to go, or are there better ways? Maybe the same things but with variations.

Take care!
1) is probably not a good idea. I'd go for the method where you're looking after time-keeping, not the OS. Imagine if the even occurs before you've finished processing - you'll get a back-log of WM_TIMER events to deal with (I think - don't quote me on that)

To help performance:
If you use double/triple-buffering, you can start working on the next frame while you're waiting to display the last - that way "expensive" frames don't cause as much stutter as they would if you were waiting until the next (artificial) vsync. The added latency of one frame normally isn't noticable.

When the draw queue is full, you are going to have to "wait"... but if you find you're waiting lots per frame, then do more cool stuff! :)

Cheers,

Ref.
When testing I get a tiny wee bit better performance out of using WM_TIMER. But then again...I really don't know what happens there with overflows...So going with #2 is probably wiser.

As for the other option...the truth is that I'll probably never will need to wait. But it is just an if statement and two GetTickCount(), so it can stay in case there are some parts of the app that uses little overhead, like some menu animation or whatever.
Here is my timer loop. Anyone want to change/add/remove something :)

Code:

int nSpeedTimer;
bool bQuit = false;
while(!bQuit) {
nSpeedTimer = GetTickCount();
while(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
if(Msg.message == WM_QUIT) bQuit = true;
}
Main();
while(GetTickCount()-nSpeedTimer < 30) { }
}