Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

178 rindas
3.3 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 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://www.wtfpl.net/ for more details.
  9. //
  10. #include <lol/engine-internal.h>
  11. #include <cstdlib>
  12. #include <stdint.h>
  13. #if __linux__ || __native_client__ || __APPLE__ \
  14. || (HAVE_GETTIMEOFDAY && HAVE_USLEEP && HAVE_SYS_TIME_H && HAVE_UNISTD_H)
  15. # include <sys/time.h>
  16. # include <unistd.h>
  17. #elif _XBOX
  18. # include <xtl.h>
  19. # undef near /* Fuck Microsoft */
  20. # undef far /* Fuck Microsoft again */
  21. # include <xbox.h>
  22. #elif _WIN32
  23. # define WIN32_LEAN_AND_MEAN
  24. # include <windows.h>
  25. #elif HAVE_SDL_SDL_H
  26. # include <SDL/SDL.h>
  27. #else
  28. # include <SDL.h>
  29. #endif
  30. namespace lol
  31. {
  32. /*
  33. * Timer implementation class
  34. */
  35. class TimerData
  36. {
  37. friend class Timer;
  38. private:
  39. TimerData()
  40. {
  41. Init();
  42. (void)GetSeconds(true);
  43. }
  44. #if __linux__ || __native_client__ || __APPLE__ \
  45. || (HAVE_GETTIMEOFDAY && HAVE_USLEEP)
  46. inline void Init()
  47. {
  48. m_tv.tv_usec = 0;
  49. m_tv.tv_sec = 0;
  50. }
  51. float GetSeconds(bool reset)
  52. {
  53. struct timeval tv, tv0 = m_tv;
  54. gettimeofday(&tv, nullptr);
  55. if (reset)
  56. m_tv = tv;
  57. return 1e-6f * (tv.tv_usec - tv0.tv_usec) + (tv.tv_sec - tv0.tv_sec);
  58. }
  59. static void WaitSeconds(float seconds)
  60. {
  61. if (seconds > 0.0f)
  62. usleep((int)(seconds * 1e6f));
  63. }
  64. struct timeval m_tv;
  65. #elif _WIN32
  66. inline void Init()
  67. {
  68. m_cycles.QuadPart = 0;
  69. }
  70. float GetSeconds(bool reset)
  71. {
  72. static float secs_per_cycle = GetSecondsPerCycle();
  73. LARGE_INTEGER cycles, cycles0 = m_cycles;
  74. QueryPerformanceCounter(&cycles);
  75. if (reset)
  76. m_cycles = cycles;
  77. return secs_per_cycle * (cycles.QuadPart - cycles0.QuadPart);
  78. }
  79. static void WaitSeconds(float seconds)
  80. {
  81. if (seconds > 5e-4f)
  82. Sleep((int)(seconds * 1e3f + 0.5f));
  83. }
  84. static float GetSecondsPerCycle()
  85. {
  86. LARGE_INTEGER tmp;
  87. QueryPerformanceFrequency(&tmp);
  88. return 1.f / tmp.QuadPart;
  89. }
  90. LARGE_INTEGER m_cycles;
  91. #else
  92. inline void Init()
  93. {
  94. m_ticks = 0;
  95. }
  96. float GetSeconds(bool reset)
  97. {
  98. static bool initialised = Init();
  99. /* The crappy SDL fallback */
  100. Uint32 ticks = SDL_GetTicks(), ticks0 = m_ticks;
  101. if (reset)
  102. m_ticks = ticks;
  103. return 1e-3f * (ticks - ticks0);
  104. }
  105. static void WaitSeconds(float seconds)
  106. {
  107. if (seconds > 5e-4f)
  108. SDL_Delay((int)(seconds * 1e3f + 0.5f));
  109. }
  110. static bool Init()
  111. {
  112. SDL_Init(SDL_INIT_TIMER);
  113. return true;
  114. }
  115. Uint32 m_ticks;
  116. #endif
  117. };
  118. /*
  119. * Timer public class
  120. */
  121. Timer::Timer()
  122. : data(new TimerData())
  123. {
  124. }
  125. Timer::~Timer()
  126. {
  127. delete data;
  128. }
  129. void Timer::Reset()
  130. {
  131. (void)data->GetSeconds(true);
  132. }
  133. float Timer::Get()
  134. {
  135. return data->GetSeconds(true);
  136. }
  137. float Timer::Poll()
  138. {
  139. return data->GetSeconds(false);
  140. }
  141. void Timer::Wait(float seconds)
  142. {
  143. float secs_elapsed = data->GetSeconds(false);
  144. data->WaitSeconds(seconds - secs_elapsed);
  145. }
  146. } /* namespace lol */