Reputation: 61
I've already made a few programs using SDL2. Frame rate wasn't much of an issue since I never planned to release those programs and just used the SDL_RENDER_PRESENTVSYNC to cap the frame rate to 60 (the refresh rate of my monitor). Now I want to make an application I can send to friends and I wanted to know if my frame-rate implementation is any good.
unsigned int a = SDL_GetTicks();
unsigned int b = SDL_GetTicks();
double delta = 0;
while (running)
{
a = SDL_GetTicks();
delta += a - b;
if (delta > 1000/60.0)
{
std::cout << "fps: " << 1000 / delta << std::endl;
Update();
Render();
delta = 0;
}
b = SDL_GetTicks();
}
I have a good feeling that I've messed up my calculations horribly but many of the other implementations I have found online usually seem pretty long winded. If possible I would like to keep the frame cap implementation within the game loop and as simple as possible.
Upvotes: 5
Views: 14590
Reputation: 511
You should put the line b = SDL_GetTicks();
inside the if
statement and before the Update();
. And delta = a - b
instead of delta += a - b
.
And you could also use b=a
instead of calling the SDL_GetTicks()
function.
while (running)
{
a = SDL_GetTicks();
delta = a - b;
if (delta > 1000/60.0)
{
std::cout << "fps: " << 1000 / delta << std::endl;
b = a;
Update();
Render();
}
}
Upvotes: 0