/project/jni/sdl-1.3/src/timer/windows/SDL_systimer.c

https://github.com/aichunyu/FFPlayer · C · 131 lines · 85 code · 20 blank · 26 comment · 8 complexity · a7f5204384bcc121f7c3d7a3b8b78d64 MD5 · raw file

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2012 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_config.h"
  19. #ifdef SDL_TIMER_WINDOWS
  20. #include "../../core/windows/SDL_windows.h"
  21. #include <mmsystem.h>
  22. #include "SDL_timer.h"
  23. #ifdef _WIN32_WCE
  24. #error This is WinCE. Please use src/timer/wince/SDL_systimer.c instead.
  25. #endif
  26. #define TIME_WRAP_VALUE (~(DWORD)0)
  27. /* The first (low-resolution) ticks value of the application */
  28. static DWORD start;
  29. #ifndef USE_GETTICKCOUNT
  30. /* Store if a high-resolution performance counter exists on the system */
  31. static BOOL hires_timer_available;
  32. /* The first high-resolution ticks value of the application */
  33. static LARGE_INTEGER hires_start_ticks;
  34. /* The number of ticks per second of the high-resolution performance counter */
  35. static LARGE_INTEGER hires_ticks_per_second;
  36. #endif
  37. void
  38. SDL_StartTicks(void)
  39. {
  40. /* Set first ticks value */
  41. #ifdef USE_GETTICKCOUNT
  42. start = GetTickCount();
  43. #else
  44. #if 0 /* Apparently there are problems with QPC on Win2K */
  45. if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE) {
  46. hires_timer_available = TRUE;
  47. QueryPerformanceCounter(&hires_start_ticks);
  48. } else
  49. #endif
  50. {
  51. hires_timer_available = FALSE;
  52. timeBeginPeriod(1); /* use 1 ms timer precision */
  53. start = timeGetTime();
  54. }
  55. #endif
  56. }
  57. Uint32
  58. SDL_GetTicks(void)
  59. {
  60. DWORD now, ticks;
  61. #ifndef USE_GETTICKCOUNT
  62. LARGE_INTEGER hires_now;
  63. #endif
  64. #ifdef USE_GETTICKCOUNT
  65. now = GetTickCount();
  66. #else
  67. if (hires_timer_available) {
  68. QueryPerformanceCounter(&hires_now);
  69. hires_now.QuadPart -= hires_start_ticks.QuadPart;
  70. hires_now.QuadPart *= 1000;
  71. hires_now.QuadPart /= hires_ticks_per_second.QuadPart;
  72. return (DWORD) hires_now.QuadPart;
  73. } else {
  74. now = timeGetTime();
  75. }
  76. #endif
  77. if (now < start) {
  78. ticks = (TIME_WRAP_VALUE - start) + now;
  79. } else {
  80. ticks = (now - start);
  81. }
  82. return (ticks);
  83. }
  84. Uint64
  85. SDL_GetPerformanceCounter(void)
  86. {
  87. LARGE_INTEGER counter;
  88. if (!QueryPerformanceCounter(&counter)) {
  89. return SDL_GetTicks();
  90. }
  91. return counter.QuadPart;
  92. }
  93. Uint64
  94. SDL_GetPerformanceFrequency(void)
  95. {
  96. LARGE_INTEGER frequency;
  97. if (!QueryPerformanceFrequency(&frequency)) {
  98. return 1000;
  99. }
  100. return frequency.QuadPart;
  101. }
  102. void
  103. SDL_Delay(Uint32 ms)
  104. {
  105. Sleep(ms);
  106. }
  107. #endif /* SDL_TIMER_WINDOWS */
  108. /* vi: set ts=4 sw=4 expandtab: */