SDL_framerate.cpp 852 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * SDL_framerate.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "../stdafx.h"
  11. #include "SDL_framerate.h"
  12. #include <SDL.h>
  13. FPSManager::FPSManager(int rate)
  14. {
  15. this->rate = rate;
  16. this->rateticks = (1000.0 / (double) rate);
  17. this->fps = 0;
  18. }
  19. void FPSManager::init()
  20. {
  21. this->lastticks = SDL_GetTicks();
  22. }
  23. void FPSManager::framerateDelay()
  24. {
  25. Uint32 currentTicks = SDL_GetTicks();
  26. double diff = currentTicks - this->lastticks;
  27. if (diff < this->rateticks) // FPS is higher than it should be, then wait some time
  28. {
  29. SDL_Delay(ceil(this->rateticks) - diff);
  30. }
  31. this->fps = ceil(1000. / (SDL_GetTicks() - this->lastticks));
  32. this->lastticks = SDL_GetTicks();
  33. }