Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - SDL: Limiting framerate issue

Although the following code does some power saving, the FPS is not capped properly. When it is supposed to lock the framerate at 60 FPS, I get 82. Same for 30, I get 49 FPS.

Calculating FPS:

previousTime = currentTime;
currentTime = SDL_GetTicks();
fps_++;

if (currentTime - lastOutput >= 1000)
{
    lastOutput = currentTime;
    fps = fps_; // the variable 'fps' is displayed 
    fps_ = 0;
}

Limiting FPS:

if (currentTime - previousTime < 1000 / maxFPS)
{
    SDL_Delay(1000 / maxFPS - currentTime + previousTime);
}

What did I mess up?

like image 821
Aaron Avatar asked May 25 '26 00:05

Aaron


1 Answers

When you create a Renderer like this:

SDL_Renderer *renderPtr = SDL_CreateRenderer(windowPtr, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

The SDL_RENDERER_PRESENTVSYNC FLAG Will match the Present Function to your monitor Refresh rate.

I hope this can help!! Has caused me much headaches!!!

Here is a reference: https://wiki.libsdl.org/SDL_CreateRenderer

like image 160
HgMerk Avatar answered May 26 '26 12:05

HgMerk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!