Timer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * Copyright (c) 2006-2019 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. // LOVE
  21. #include "common/config.h"
  22. #include "common/int.h"
  23. #include "common/delay.h"
  24. #include "Timer.h"
  25. #if defined(LOVE_WINDOWS)
  26. #include <windows.h>
  27. #elif defined(LOVE_MACOS) || defined(LOVE_IOS)
  28. #include <mach/mach_time.h>
  29. #include <sys/time.h>
  30. #elif defined(LOVE_LINUX)
  31. #include <unistd.h>
  32. #include <time.h>
  33. #include <sys/time.h>
  34. #endif
  35. #if defined(LOVE_LINUX)
  36. static inline double getTimeOfDay()
  37. {
  38. timeval t;
  39. gettimeofday(&t, NULL);
  40. return (double) t.tv_sec + (double) t.tv_usec / 1000000.0;
  41. }
  42. #endif
  43. namespace love
  44. {
  45. namespace timer
  46. {
  47. Timer::Timer()
  48. : currTime(0)
  49. , prevFpsUpdate(0)
  50. , fps(0)
  51. , averageDelta(0)
  52. , fpsUpdateFrequency(1)
  53. , frames(0)
  54. , dt(0)
  55. {
  56. prevFpsUpdate = currTime = getTime();
  57. }
  58. double Timer::step()
  59. {
  60. // Frames rendered
  61. frames++;
  62. // "Current" time is previous time by now.
  63. prevTime = currTime;
  64. // Get time from system.
  65. currTime = getTime();
  66. // Convert to number of seconds.
  67. dt = currTime - prevTime;
  68. double timeSinceLast = currTime - prevFpsUpdate;
  69. // Update FPS?
  70. if (timeSinceLast > fpsUpdateFrequency)
  71. {
  72. fps = int((frames/timeSinceLast) + 0.5);
  73. averageDelta = timeSinceLast/frames;
  74. prevFpsUpdate = currTime;
  75. frames = 0;
  76. }
  77. return dt;
  78. }
  79. void Timer::sleep(double seconds) const
  80. {
  81. if (seconds >= 0)
  82. love::sleep((unsigned int)(seconds*1000));
  83. }
  84. double Timer::getDelta() const
  85. {
  86. return dt;
  87. }
  88. int Timer::getFPS() const
  89. {
  90. return fps;
  91. }
  92. double Timer::getAverageDelta() const
  93. {
  94. return averageDelta;
  95. }
  96. double Timer::getTimerPeriod()
  97. {
  98. #if defined(LOVE_MACOS) || defined(LOVE_IOS)
  99. mach_timebase_info_data_t info;
  100. mach_timebase_info(&info);
  101. return (double) info.numer / (double) info.denom / 1000000000.0;
  102. #elif defined(LOVE_WINDOWS)
  103. LARGE_INTEGER temp;
  104. if (QueryPerformanceFrequency(&temp) != 0 && temp.QuadPart != 0)
  105. return 1.0 / (double) temp.QuadPart;
  106. #endif
  107. return 0;
  108. }
  109. double Timer::getTime()
  110. {
  111. // The timer period (reciprocal of the frequency.)
  112. static const double timerPeriod = getTimerPeriod();
  113. #if defined(LOVE_LINUX)
  114. (void) timerPeriod; // Unused on linux
  115. double mt;
  116. // Check for POSIX timers and monotonic clocks. If not supported, use the gettimeofday fallback.
  117. #if _POSIX_TIMERS > 0 && defined(_POSIX_MONOTONIC_CLOCK) \
  118. && (defined(CLOCK_MONOTONIC_RAW) || defined(CLOCK_MONOTONIC))
  119. timespec t;
  120. #ifdef CLOCK_MONOTONIC_RAW
  121. clockid_t clk_id = CLOCK_MONOTONIC_RAW;
  122. #else
  123. clockid_t clk_id = CLOCK_MONOTONIC;
  124. #endif
  125. if (clock_gettime(clk_id, &t) == 0)
  126. mt = (double) t.tv_sec + (double) t.tv_nsec / 1000000000.0;
  127. else
  128. #endif
  129. mt = getTimeOfDay();
  130. return mt;
  131. #elif defined(LOVE_MACOS) || defined(LOVE_IOS)
  132. return (double) mach_absolute_time() * timerPeriod;
  133. #elif defined(LOVE_WINDOWS)
  134. LARGE_INTEGER microTime;
  135. QueryPerformanceCounter(&microTime);
  136. return (double) microTime.QuadPart * timerPeriod;
  137. #endif
  138. }
  139. } // timer
  140. } // love