Timer.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * Copyright (c) 2006-2013 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "common/config.h"
  21. #include "common/delay.h"
  22. #ifdef LOVE_WINDOWS
  23. # include <windows.h>
  24. # include <time.h>
  25. #else
  26. # include <sys/time.h>
  27. #endif
  28. #include "Timer.h"
  29. namespace love
  30. {
  31. namespace timer
  32. {
  33. namespace sdl
  34. {
  35. Timer::Timer()
  36. : currTime(0)
  37. , prevFpsUpdate(0)
  38. , fps(0)
  39. , averageDelta(0)
  40. , fpsUpdateFrequency(1)
  41. , frames(0)
  42. , dt(0)
  43. {
  44. // Init the SDL timer system.
  45. if (SDL_InitSubSystem(SDL_INIT_TIMER) < 0)
  46. throw Exception(SDL_GetError());
  47. }
  48. Timer::~Timer()
  49. {
  50. // Quit SDL timer.
  51. SDL_QuitSubSystem(SDL_INIT_TIMER);
  52. }
  53. const char *Timer::getName() const
  54. {
  55. return "love.timer.sdl";
  56. }
  57. void Timer::step()
  58. {
  59. // Frames rendered
  60. frames++;
  61. // "Current" time is previous time by now.
  62. prevTime = currTime;
  63. // Get ticks from SDL
  64. currTime = getMicroTime();
  65. // Convert to number of seconds
  66. dt = currTime - prevTime;
  67. double timeSinceLast = currTime - prevFpsUpdate;
  68. // Update FPS?
  69. if (timeSinceLast > fpsUpdateFrequency)
  70. {
  71. fps = int((frames/timeSinceLast) + 0.5);
  72. averageDelta = timeSinceLast/frames;
  73. prevFpsUpdate = currTime;
  74. frames = 0;
  75. }
  76. }
  77. void Timer::sleep(double seconds) const
  78. {
  79. if (seconds > 0)
  80. delay((int)(seconds*1000));
  81. }
  82. double Timer::getDelta() const
  83. {
  84. return dt;
  85. }
  86. int Timer::getFPS() const
  87. {
  88. return fps;
  89. }
  90. double Timer::getAverageDelta() const
  91. {
  92. return averageDelta;
  93. }
  94. double Timer::getTime() const
  95. {
  96. return SDL_GetTicks()/1000.0;
  97. }
  98. double Timer::getMicroTime() const
  99. {
  100. #ifdef LOVE_WINDOWS
  101. static __int64 freq = 0;
  102. if (!freq)
  103. {
  104. LARGE_INTEGER temp;
  105. QueryPerformanceFrequency(&temp);
  106. freq = (__int64) temp.QuadPart;
  107. }
  108. LARGE_INTEGER microTime;
  109. QueryPerformanceCounter(&microTime);
  110. // The 64 to 32 bit integer conversion, assuming the fraction part down
  111. // to microseconds takes 20 bits, should not be a problem unless the
  112. // system has an uptime of a few decades.
  113. return (double) microTime.QuadPart / (double) freq;
  114. #else
  115. timeval t;
  116. gettimeofday(&t, NULL);
  117. return t.tv_sec + t.tv_usec/1000000.0;
  118. #endif
  119. }
  120. } // sdl
  121. } // timer
  122. } // love