Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

166 wiersze
3.5 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstdlib>
  14. #include <stdint.h>
  15. #if defined __linux__ || defined __APPLE__
  16. # include <sys/time.h>
  17. # include <unistd.h>
  18. #elif defined _XBOX
  19. # include <xtl.h>
  20. # include <xbox.h>
  21. #elif defined _WIN32
  22. # define WIN32_LEAN_AND_MEAN
  23. # include <windows.h>
  24. #elif defined __CELLOS_LV2__
  25. # include <sys/sys_time.h>
  26. # include <sys/timer.h>
  27. # include <sys/time_util.h>
  28. #else
  29. # include <SDL.h>
  30. #endif
  31. #include "core.h"
  32. namespace lol
  33. {
  34. /*
  35. * Timer implementation class
  36. */
  37. class TimerData
  38. {
  39. friend class Timer;
  40. private:
  41. TimerData()
  42. {
  43. #if defined __linux__ || defined __APPLE__
  44. gettimeofday(&tv0, NULL);
  45. #elif defined _WIN32
  46. QueryPerformanceCounter(&cycles0);
  47. #elif defined __CELLOS_LV2__
  48. SYS_TIMEBASE_GET(cycles0);
  49. #else
  50. SDL_Init(SDL_INIT_TIMER);
  51. ticks0 = SDL_GetTicks();
  52. #endif
  53. }
  54. float GetOrWait(float deltams, bool update)
  55. {
  56. float ret, towait;
  57. #if defined __linux__ || defined __APPLE__
  58. struct timeval tv;
  59. gettimeofday(&tv, NULL);
  60. ret = 1e-3f * (tv.tv_usec - tv0.tv_usec)
  61. + 1e3f * (tv.tv_sec - tv0.tv_sec);
  62. if (update)
  63. tv0 = tv;
  64. towait = deltams - ret;
  65. if (towait > 0.0f)
  66. usleep((int)(towait * 1e3f));
  67. #elif defined _WIN32
  68. LARGE_INTEGER cycles;
  69. QueryPerformanceCounter(&cycles);
  70. static float ms_per_cycle = GetMsPerCycle();
  71. ret = ms_per_cycle * (cycles.QuadPart - cycles0.QuadPart);
  72. if (update)
  73. cycles0 = cycles;
  74. towait = deltams - ret;
  75. if (towait > 5e-4f)
  76. Sleep((int)(towait + 0.5f));
  77. #elif defined __CELLOS_LV2__
  78. uint64_t cycles;
  79. SYS_TIMEBASE_GET(cycles);
  80. static float ms_per_cycle = GetMsPerCycle();
  81. ret = ms_per_cycle * (cycles - cycles0);
  82. if (update)
  83. cycles0 = cycles;
  84. towait = deltams - ret;
  85. if (towait > 0.0f)
  86. sys_timer_usleep((int)(towait * 1e3f));
  87. #else
  88. /* The crappy SDL fallback */
  89. Uint32 ticks = SDL_GetTicks();
  90. ret = ticks - ticks0;
  91. if (update)
  92. ticks0 = ticks;
  93. towait = deltams - ret;
  94. if (towait > 0.5f)
  95. SDL_Delay((int)(towait + 0.5f));
  96. #endif
  97. return ret;
  98. }
  99. static float GetMsPerCycle()
  100. {
  101. #if defined __linux__ || defined __APPLE__
  102. return 1.0f;
  103. #elif defined _WIN32
  104. LARGE_INTEGER tmp;
  105. QueryPerformanceFrequency(&tmp);
  106. return 1e3f / tmp.QuadPart;
  107. #elif defined __CELLOS_LV2__
  108. return 1e3f / sys_time_get_timebase_frequency();
  109. #else
  110. return 1.0f;
  111. #endif
  112. }
  113. #if defined __linux__ || defined __APPLE__
  114. struct timeval tv0;
  115. #elif defined _WIN32
  116. LARGE_INTEGER cycles0;
  117. #elif defined __CELLOS_LV2__
  118. uint64_t cycles0;
  119. #else
  120. Uint32 ticks0;
  121. #endif
  122. };
  123. /*
  124. * Timer public class
  125. */
  126. Timer::Timer()
  127. : data(new TimerData())
  128. {
  129. }
  130. Timer::~Timer()
  131. {
  132. delete data;
  133. }
  134. float Timer::GetMs()
  135. {
  136. return data->GetOrWait(0.0f, true);
  137. }
  138. float Timer::PollMs()
  139. {
  140. return data->GetOrWait(0.0f, false);
  141. }
  142. void Timer::WaitMs(float deltams)
  143. {
  144. (void)data->GetOrWait(deltams, false);
  145. }
  146. } /* namespace lol */