Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time based loop and Frame based loop

Tags:

c++

Trying to understand the concepts of setting constant speed on game loop. My head hurts. I read the deWiTTERS page, but I can't see the why/how...when I get it...it slips.

while(true)
{
      player->update() ;
      player->draw() ;
}

This will run as fast as possible depending on how fast a processor is...I get that.

To run at the same speed on all computers, the logic is what I don't get. If I am trying to run at 60fps, then it means for every 16ms the objects move by a frame, yeah? What I don't get is how the update() or draw() may be too slow.

deWiTTERS example (I used 60):

const int FRAMES_PER_SECOND = 60;
const int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;

DWORD next_game_tick = GetTickCount();
// GetTickCount() returns the current number of milliseconds
// that have elapsed since the system was started

int sleep_time = 0;

bool game_is_running = true;

while( game_is_running ) {
    update_game();
    display_game();

    next_game_tick += SKIP_TICKS;
    sleep_time = next_game_tick - GetTickCount();
    if( sleep_time >= 0 ) {
        Sleep( sleep_time );
    }
    else {
        // Shit, we are running behind!
    }
}

I don't understand why he gets the current time before the loop starts. And when he increments by SKIP_TICKS I understand he increments to the next 16ms interval. But I don't understand this part as well:

sleep_time = nextgametick  - GetTickCount() 

What does Sleep(sleep_time) mean? The processor leaves the loop and does something else? How does it achieve running 60fps?

like image 304
Lews Therin Avatar asked Sep 09 '11 13:09

Lews Therin


1 Answers

In cases where the update_game() and display_game() functions complete in less time than a single frame interval at 60FPs, the loop tries to ensure that the next frame is not processed until that interval is up, by sleeping (blocking the thread) off the excess frame time. Seems like it is trying to ensure that the frame rate is capped to 60FPS, and no higher.

The processor does not 'leave the loop' but rather the thread in which your loop is running is blocked (prevented from continuing execution of your code) until the sleep time is up. Then it continues onto the next frame. In a multi-threaded game engine, sleeping the thread of the main game loop like this gives the CPU time to execute code in other threads, which may be managing physics, AI, audio mixing etc, depending on set up.

Why is GetTickCount() called before the loop starts? We know from the comment in your code that GetTickCount() returns the milliseconds since system boot.

So lets say that the system has been running for 30 seconds (30,000ms) when you start your program, and let's say that we didn't call GetTickCount() before entering the loop, but instead initialized next_game_tick to 0.

We do the update and draw calls (as an example, they take 6ms) and then:

next_game_tick += SKIP_TICKS;  // next_game_tick is now 16
sleep_time = next_game_tick - GetTickCount();   

// GetTickCount() returns 30000!
// So sleep_time is now 16 - 30000 = -29984 !!!

Since we (sensibly) only sleep when sleep_time is positive, the game loop would run as fast as possible (potentially faster than 60FPS), which is not what you want.

like image 75
Brian Fearon Avatar answered Sep 19 '22 21:09

Brian Fearon