game loop - pilkch/library GitHub Wiki

Constant Update Speed, Variable Rendering Speed

Each time we render with a float to represent our exact time between tick counts, this allows prediction, so for example we can render a car 0.25 of the distance between the latest updated frame and where we think it will travel to.
https://dewitters.com/dewitters-gameloop/

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

    uint32_t 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 );
    }

TODO: Include an option to cap the frame rate at a fixed rate, for example 60, with a strategy something like "if (time to even is > 2 ms) sleeping for 1 ms" so that we can try to hit our target frame rate without sleeping past it.