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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With