Small OpenGL based c++ rendering library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fpsmanager.cpp 627B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "fpsmanager.h"
  2. namespace segl {
  3. FPSManager::FPSManager(int _fps) {
  4. setFPS(_fps);
  5. frames = 0;
  6. framerate = 0;
  7. lastticks = SDL_GetTicks();
  8. }
  9. void FPSManager::setFPS(int _fps) {
  10. if(_fps<1 || _fps > 500)
  11. return;
  12. tickrate = 1000.0f/(float)_fps;
  13. }
  14. void FPSManager::delay() {
  15. frames++;
  16. Uint32 now = SDL_GetTicks();
  17. Uint32 target = lastticks + (Uint32)(tickrate * (float)frames);
  18. if(now <= target) {
  19. SDL_Delay(target-now);
  20. } else {
  21. framerate = frames/((now-lastticks)/1000.0f);
  22. frames = 0;
  23. lastticks = SDL_GetTicks();
  24. }
  25. }
  26. float FPSManager::getFPS() {
  27. return framerate;
  28. }
  29. } // namespace segl