SDL Library Documentation
Prev | Chapter 4. Examples | Next

# Time Examples

## Time based game loop

    #define TICK_INTERVAL    30

    static Uint32 next_time;

    Uint32 time_left(void)
    {
        Uint32 now;

        now = SDL_GetTicks();
        if(next_time <= now)
            return 0;
        else
            return next_time - now;
    }

    /* main game loop */

        next_time = SDL_GetTicks() + TICK_INTERVAL;
        while ( game_running ) {
            update_game_state();
            SDL_Delay(time_left());
            next_time += TICK_INTERVAL;
        }

Prev | Home | Next
CDROM Examples | Up | SDL Reference