SDL_framerate.cpp 642 B

12345678910111213141516171819202122232425262728293031
  1. #include "StdInc.h"
  2. #include "SDL_framerate.h"
  3. #include <SDL.h>
  4. FPSManager::FPSManager(int rate)
  5. {
  6. this->rate = rate;
  7. this->rateticks = (1000.0 / (double) rate);
  8. this->fps = 0;
  9. }
  10. void FPSManager::init()
  11. {
  12. this->lastticks = SDL_GetTicks();
  13. }
  14. void FPSManager::framerateDelay()
  15. {
  16. Uint32 currentTicks = SDL_GetTicks();
  17. double diff = currentTicks - this->lastticks;
  18. if (diff < this->rateticks) // FPS is higher than it should be, then wait some time
  19. {
  20. SDL_Delay(ceil(this->rateticks) - diff);
  21. }
  22. this->fps = ceil(1000. / (SDL_GetTicks() - this->lastticks));
  23. this->lastticks = SDL_GetTicks();
  24. }