1

I'm writting a game using C++. I wonder how can I optimize my game loop. For example, we have some game with the main loop looks like this:

while ( gameContinue ) {
    if ( SCENE == HELP_SCENE ) {
        renderHelpScene();
    } else if ( SCENE == SCORE_SCENE ) {
        renderScoreScene();
    } else if ( SCENE == MAIN_GAME_SCENE ) {
        renderMainGameScene();
    } // .... and many others scenes
}

I'm thinking on how to make this code faster and lighter. I think about using callbacks so we will not need many if-cases. Something like this:

typedef void (*callback_function)(void);
callback_function renderFunc; 

void renderMainGameScene() {
    renderFunc = renderScoreScene(); // set to another scene if we need this
}
void renderScoreScene() {
    renderFunc = renderAnyAnotherSceneWeNeedNow();
}

renderFunc = renderMainGameScene();
while ( gameContinue ) {
    renderFunc();
}

What do you think about it? How do you organize your main loops?

2
  • 3
    You should be happy if this is a bottleneck in your game. Jul 24, 2013 at 9:44
  • 2
    Why not just have an AbstractScene with a virtual render method ? Also I agree with the above comment.
    – Nbr44
    Jul 24, 2013 at 9:47

3 Answers 3

2

I've personally started using multi-threading. I have a thread for object updates, a thread for objects collision and a thread for drawing. Each thread loops with a while (GetMessage()) and threads send messages from one to another.

At each cycle (frame), my main loop sends a message to each thread to:

  1. Calculate collision for modified objects
  2. Update objects (movement, state etc.)
  3. Draw the updated objects

That's how I do it (at least on my GDI/GDI+ games). Not sure if the best way, but so far it works like a charm :).

4
  • Interesting idea. How many threads to you have, how heavily do the threads interact, and what is the scalability of this approach?
    – DXsmiley
    Jul 24, 2013 at 9:58
  • So far, I've only tested this approach on small games (just to get used with threading). I've got this idea from a CAD software which had 1 thread for calculating/updating and 1 for drawing the objects, so I believe it's pretty scalable. I have 3 threads - draw, collision, update - and when a thread or the main loop has something to share with another, I use PostThreadMessage() by passing the target thread's id and a custom msg struct/class. On the receiving thread() I loop with a while (GetMessage()) and have a switch() that handles my custom message structure. Jul 24, 2013 at 10:03
  • 3
    You may want to make a note about the dangers of this approach (i.e. basic thread safety en.wikipedia.org/wiki/Thread_safety). Also, collisions tend to rely heavily on the position of objects, which you appear to be changing in the update thread.
    – DXsmiley
    Jul 24, 2013 at 10:11
  • Yes, I'm aware that collisions may appear. However the messages in the thread are processed in order. But, yes, one has to be cautious when using multiple threads that are used intensively. Jul 24, 2013 at 10:26
1

Helpful patterns for this kind of problem are the State Pattern and the Strategy Pattern.
As you can see they are both similar. The Strategy pattern is a bit simpler than the State, but in exchange the State Pattern is more powerful and probably better fitted for a game engine like this. You can also easily create a stack with this for example: game start -> menu -> game run -> menu.
If the parent state of the last menu is game run, the menu would look different (e.g. "Return game" instead of "Start game" in the first menu). The states can be popped and you have a easy navigation.

0

Using call-backs should be fine. Just be careful not to have any cyclic dependencies in your headers. Notably it's a bad idea to include the header for your controller loop anywhere other than the .cpp for the controller loop.

As for the runtime benefits, they are very small. The if-else method will not noticeably slow down your game. Alternatively, there is also the switch statement, which is preferable to a series of if-else statements in terms of code readability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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