0
votes

Alright, so I'm trying to cap my framerate at 60 frames per second, but the method I'm using is slowing it down to like 40.

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

Then I call it like this: SDL_Delay(TimeLeft());

How can I cap my framerate without going over it, or having it cap it too soon?

1
Why do you need to cap the framerate? This question has popped up a few times lately, and the solution is (almost always) don't cap, adjust.ssube

1 Answers

3
votes

You need to record the time before drawing the current frame, and then delay the appropriate amount from then.

For example, some pseudo code to do it would be

markedTime = currentTime();
drawFrame(); 
delayFrom(markedTime, 1/60);

markedTime is the time recorded before drawFrame() was called. delayFrom() is a function that delays from a given time instead of "now". 1/60 is the amount of time to delay from the first argument, in seconds.