Jump to content

  • Log In with Google      Sign In   
  • Create Account

Trying to conceptually understand the following code

  • You cannot reply to this topic
2 replies to this topic

#1 TheStudent111   Members   -  Reputation: 104

Like
0Likes
Like

Posted Yesterday, 02:48 PM

I've been reading the following article...

http://www.koonsolo.com/news/dewitters-gameloop/

 

and I have been having trouble mentally conceptualizing how the following code causes the FPS be dependent on the game speed

<pre>
    const int FRAMES_PER_SECOND = 25;
    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!
        }
    }</pre>

How is the display_game function being dependent on update_game()?

 

or how the following code causes the game speed be dependent on FPS

<pre>
  DWORD prev_frame_tick;
    DWORD curr_frame_tick = GetTickCount();

    bool game_is_running = true;
    while( game_is_running ) {
        prev_frame_tick = curr_frame_tick;
        curr_frame_tick = GetTickCount();

        update_game( curr_frame_tick - prev_frame_tick );
        display_game();
    }</pre>
 

How is update()_game dependent on display_game()?

 

What am I missing? or overlooking?

 

I know this is probably a stupid question, but I am having trouble with it.

 



Sponsor:

#2 Samith   Members   -  Reputation: 2308

Like
7Likes
Like

Posted Yesterday, 03:21 PM

In the first example the code is attempting to lock the framerate to 25 frames per second. It does this by determining how long each frame should be (ie 1/25th of a second), then it measures how long it took to do update_game() and display_game(), and sleeps for the remaining amount of time. So, 1/25th of a second is 40ms. If update_game and display_game only take 30ms, then you sleep for the remaining 10ms. If it takes 45ms, then you're running behind and you just start the next frame immediately. Note that Sleep is typically a very bad way to implement this behavior, because you have no control of when execution will be returned to your thread. The operating system is in charge of rescheduling your thread, and it can do whatever it wants (though it'll likely be CLOSE to what you asked for). 

 

The second example runs each frame as quickly as it can. It doesn't sleep at all. So, in order to get a consistent play experience, there needs to be a way for the game to know how long it's been since the last frame. Hence, you see the code passing in the delta time to update_game(). That means that if you're running at, for example, 10ms a frame, update_game() will know that each frame is taking 10ms and only update 10ms worth of time in the simulation. If you have an object with a position and a velocity, then its updated position would be position = position + velocity * 10ms. If the game started to run slower, and was only going at 20ms a frame, then it would be position = position + velocity * 20ms. So if your game runs fast or slow, the simulation should be approximately the same.



#3 TheStudent111   Members   -  Reputation: 104

Like
0Likes
Like

Posted Today, 12:17 AM

Thank you so much Samith. Very clear and concise.

 

I hope I am not asking for much, but could you explain the constant  game speed with maximum FPS

  const int TICKS_PER_SECOND = 50;
    const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
    const int MAX_FRAMESKIP = 10;

    DWORD next_game_tick = GetTickCount();
    int loops;

    bool game_is_running = true;
    while( game_is_running ) {

        loops = 0;
        while( GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) {
            update_game();

            next_game_tick += SKIP_TICKS;
            loops++;
        }

        display_game();
    }

 

 

and the constant game speed with independent variable FPS

   const int TICKS_PER_SECOND = 25;
    const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
    const int MAX_FRAMESKIP = 5;

    DWORD next_game_tick = GetTickCount();
    int loops;
    float interpolation;

    bool game_is_running = true;
    while( game_is_running ) {

        loops = 0;
        while( GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP) {
            update_game();

            next_game_tick += SKIP_TICKS;
            loops++;
        }

        interpolation = float( GetTickCount() + SKIP_TICKS - next_game_tick )
                        / float( SKIP_TICKS );
        display_game( interpolation );
    }

 

Sorry for the stupid questions, I have never had to make an advanced game loop before. Conceptualizing frame rates, and computer time has been a challenge for me for some strange reason.







PARTNERS