SDL_systimer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
  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. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #ifdef SDL_TIMER_WINDOWS
  20. #include "../../core/windows/SDL_windows.h"
  21. #include <mmsystem.h>
  22. #include "SDL_timer.h"
  23. #include "SDL_hints.h"
  24. /* The first (low-resolution) ticks value of the application */
  25. static DWORD start = 0;
  26. static BOOL ticks_started = FALSE;
  27. /* Store if a high-resolution performance counter exists on the system */
  28. static BOOL hires_timer_available;
  29. /* The first high-resolution ticks value of the application */
  30. static LARGE_INTEGER hires_start_ticks;
  31. /* The number of ticks per second of the high-resolution performance counter */
  32. static LARGE_INTEGER hires_ticks_per_second;
  33. static void
  34. SDL_SetSystemTimerResolution(const UINT uPeriod)
  35. {
  36. #ifndef __WINRT__
  37. static UINT timer_period = 0;
  38. if (uPeriod != timer_period) {
  39. if (timer_period) {
  40. timeEndPeriod(timer_period);
  41. }
  42. timer_period = uPeriod;
  43. if (timer_period) {
  44. timeBeginPeriod(timer_period);
  45. }
  46. }
  47. #endif
  48. }
  49. static void
  50. SDL_TimerResolutionChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  51. {
  52. UINT uPeriod;
  53. /* Unless the hint says otherwise, let's have good sleep precision */
  54. if (hint && *hint) {
  55. uPeriod = SDL_atoi(hint);
  56. } else {
  57. uPeriod = 1;
  58. }
  59. if (uPeriod || oldValue != hint) {
  60. SDL_SetSystemTimerResolution(uPeriod);
  61. }
  62. }
  63. void
  64. SDL_TicksInit(void)
  65. {
  66. if (ticks_started) {
  67. return;
  68. }
  69. ticks_started = SDL_TRUE;
  70. /* if we didn't set a precision, set it high. This affects lots of things
  71. on Windows besides the SDL timers, like audio callbacks, etc. */
  72. SDL_AddHintCallback(SDL_HINT_TIMER_RESOLUTION,
  73. SDL_TimerResolutionChanged, NULL);
  74. /* Set first ticks value */
  75. /* QueryPerformanceCounter has had problems in the past, but lots of games
  76. use it, so we'll rely on it here.
  77. */
  78. if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE) {
  79. hires_timer_available = TRUE;
  80. QueryPerformanceCounter(&hires_start_ticks);
  81. } else {
  82. hires_timer_available = FALSE;
  83. #ifndef __WINRT__
  84. start = timeGetTime();
  85. #endif /* __WINRT__ */
  86. }
  87. }
  88. void
  89. SDL_TicksQuit(void)
  90. {
  91. if (!hires_timer_available) {
  92. SDL_DelHintCallback(SDL_HINT_TIMER_RESOLUTION,
  93. SDL_TimerResolutionChanged, NULL);
  94. }
  95. SDL_SetSystemTimerResolution(0); /* always release our timer resolution request. */
  96. start = 0;
  97. ticks_started = SDL_FALSE;
  98. }
  99. Uint32
  100. SDL_GetTicks(void)
  101. {
  102. DWORD now = 0;
  103. LARGE_INTEGER hires_now;
  104. if (!ticks_started) {
  105. SDL_TicksInit();
  106. }
  107. if (hires_timer_available) {
  108. QueryPerformanceCounter(&hires_now);
  109. hires_now.QuadPart -= hires_start_ticks.QuadPart;
  110. hires_now.QuadPart *= 1000;
  111. hires_now.QuadPart /= hires_ticks_per_second.QuadPart;
  112. return (DWORD) hires_now.QuadPart;
  113. } else {
  114. #ifndef __WINRT__
  115. now = timeGetTime();
  116. #endif /* __WINRT__ */
  117. }
  118. return (now - start);
  119. }
  120. Uint64
  121. SDL_GetPerformanceCounter(void)
  122. {
  123. LARGE_INTEGER counter;
  124. if (!QueryPerformanceCounter(&counter)) {
  125. return SDL_GetTicks();
  126. }
  127. return counter.QuadPart;
  128. }
  129. Uint64
  130. SDL_GetPerformanceFrequency(void)
  131. {
  132. LARGE_INTEGER frequency;
  133. if (!QueryPerformanceFrequency(&frequency)) {
  134. return 1000;
  135. }
  136. return frequency.QuadPart;
  137. }
  138. void
  139. SDL_Delay(Uint32 ms)
  140. {
  141. /* Sleep() is not publicly available to apps in early versions of WinRT.
  142. *
  143. * Visual C++ 2013 Update 4 re-introduced Sleep() for Windows 8.1 and
  144. * Windows Phone 8.1.
  145. *
  146. * Use the compiler version to determine availability.
  147. *
  148. * NOTE #1: _MSC_FULL_VER == 180030723 for Visual C++ 2013 Update 3.
  149. * NOTE #2: Visual C++ 2013, when compiling for Windows 8.0 and
  150. * Windows Phone 8.0, uses the Visual C++ 2012 compiler to build
  151. * apps and libraries.
  152. */
  153. #if defined(__WINRT__) && defined(_MSC_FULL_VER) && (_MSC_FULL_VER <= 180030723)
  154. static HANDLE mutex = 0;
  155. if (!mutex) {
  156. mutex = CreateEventEx(0, 0, 0, EVENT_ALL_ACCESS);
  157. }
  158. WaitForSingleObjectEx(mutex, ms, FALSE);
  159. #else
  160. if (!ticks_started) {
  161. SDL_TicksInit();
  162. }
  163. Sleep(ms);
  164. #endif
  165. }
  166. #endif /* SDL_TIMER_WINDOWS */
  167. /* vi: set ts=4 sw=4 expandtab: */