Video Events Audio CD-ROM Threads Time

Time Examples


Introduction Function List Function Reference Examples

  • Get the current time, in milliseconds

SDL_GetTicks() tells how many milliseconds have past since an arbitrary point in the past.

Example:
#define TICK_INTERVAL    30

Uint32 TimeLeft(void)
{
    static Uint32 next_time = 0;
    Uint32 now;

    now = SDL_GetTicks();
    if ( next_time <= now ) {
        next_time = now+TICK_INTERVAL;
        return(0);
    }
    return(next_time-now);
}
  • Wait a specified number of milliseconds

SDL_Delay() allows you to wait for some number of milliseconds.

Since the operating systems supported by SDL are multi-tasking, there is no way to guarantee that your application will delay exactly the requested time. This should be used more as a way of idling for a while rather than to wake up at a particular time.

Example:
{
    while ( game_running ) {
        UpdateGameState();
        SDL_Delay(TimeLeft());
    }
}