libsegl/fpsmanager.cpp

34 lines
589 B
C++

#include "fpsmanager.h"
FPSManager::FPSManager(int _fps) {
setFPS(_fps);
frames = 0;
framerate = 0;
lastticks = SDL_GetTicks();
}
void FPSManager::setFPS(int _fps) {
if(_fps<1 || _fps > 500)
return;
tickrate = 1000.0f/(float)_fps;
}
void FPSManager::delay() {
frames++;
Uint32 now = SDL_GetTicks();
Uint32 target = lastticks + (Uint32)(tickrate * (float)frames);
if(now <= target) {
SDL_Delay(target-now);
} else {
framerate = frames/((now-lastticks)/1000.0f);
frames = 0;
lastticks = SDL_GetTicks();
}
}
float FPSManager::getFPS() {
return framerate;
}