vanhempi
f6881092c8
commit
2016d1cb50
@ -0,0 +1,33 @@
|
||||
#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;
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
#ifndef __FPSMANAGER_H
|
||||
#define __FPSMANAGER_H
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
class FPSManager {
|
||||
private:
|
||||
Uint32 lastticks;
|
||||
int frames;
|
||||
float tickrate;
|
||||
float framerate;
|
||||
public:
|
||||
FPSManager(int _fps);
|
||||
|
||||
void setFPS(int _fps);
|
||||
void delay();
|
||||
float getFPS();
|
||||
};
|
||||
|
||||
#endif
|
Ladataan…
Viittaa uudesa ongelmassa