You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

152 line
3.2 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 _WIN32
  19. # define WIN32_LEAN_AND_MEAN
  20. # include <windows.h>
  21. #elif defined __CELLOS_LV2__
  22. # include <sys/sys_time.h>
  23. # include <sys/timer.h>
  24. # include <sys/time_util.h>
  25. #else
  26. # include <SDL.h>
  27. #endif
  28. #include "core.h"
  29. namespace lol
  30. {
  31. /*
  32. * Timer implementation class
  33. */
  34. class TimerData
  35. {
  36. friend class Timer;
  37. private:
  38. TimerData()
  39. {
  40. #if defined __linux__ || defined __APPLE__
  41. gettimeofday(&tv0, NULL);
  42. #elif defined _WIN32
  43. LARGE_INTEGER tmp;
  44. QueryPerformanceFrequency(&tmp);
  45. ms_per_cycle = 1e3f / tmp.QuadPart;
  46. QueryPerformanceCounter(&cycles0);
  47. #elif defined __CELLOS_LV2__
  48. ms_per_cycle = 1e3f / sys_time_get_timebase_frequency();
  49. SYS_TIMEBASE_GET(cycles0);
  50. #else
  51. SDL_Init(SDL_INIT_TIMER);
  52. ticks0 = SDL_GetTicks();
  53. #endif
  54. }
  55. float GetOrWait(float deltams, bool update)
  56. {
  57. float ret, towait;
  58. #if defined __linux__ || defined __APPLE__
  59. struct timeval tv;
  60. gettimeofday(&tv, NULL);
  61. ret = 1e-3f * (tv.tv_usec - tv0.tv_usec)
  62. + 1e3f * (tv.tv_sec - tv0.tv_sec);
  63. if (update)
  64. tv0 = tv;
  65. towait = deltams - ret;
  66. if (towait > 0.0f)
  67. usleep((int)(towait * 1e3f));
  68. #elif defined _WIN32
  69. LARGE_INTEGER cycles;
  70. QueryPerformanceCounter(&cycles);
  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. ret = ms_per_cycle * cycles;
  81. if (update)
  82. cycles0 = cycles;
  83. towait = deltams - ret;
  84. if (towait > 0.0f)
  85. sys_timer_usleep((int)(towait * 1e3f));
  86. #else
  87. /* The crappy SDL fallback */
  88. Uint32 ticks = SDL_GetTicks();
  89. ret = ticks - ticks0;
  90. if (update)
  91. ticks0 = ticks;
  92. towait = deltams - ret;
  93. if (towait > 0.5f)
  94. SDL_Delay((int)(towait + 0.5f));
  95. #endif
  96. return ret;
  97. }
  98. #if defined __linux__ || defined __APPLE__
  99. struct timeval tv0;
  100. #elif defined _WIN32
  101. float ms_per_cycle;
  102. LARGE_INTEGER cycles0;
  103. #elif defined __CELLOS_LV2__
  104. float ms_per_cycle;
  105. uint64_t cycles0;
  106. #else
  107. Uint32 ticks0;
  108. #endif
  109. };
  110. /*
  111. * Timer public class
  112. */
  113. Timer::Timer()
  114. : data(new TimerData())
  115. {
  116. }
  117. Timer::~Timer()
  118. {
  119. delete data;
  120. }
  121. float Timer::GetMs()
  122. {
  123. return data->GetOrWait(0.0f, true);
  124. }
  125. float Timer::PollMs()
  126. {
  127. return data->GetOrWait(0.0f, false);
  128. }
  129. void Timer::WaitMs(float deltams)
  130. {
  131. (void)data->GetOrWait(deltams, false);
  132. }
  133. } /* namespace lol */