comparison src/timer/win32/SDL_systimer.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents 92947e3a18db
children 99210400e8b9
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
29 29
30 #include "SDL_timer.h" 30 #include "SDL_timer.h"
31 #include "../SDL_timer_c.h" 31 #include "../SDL_timer_c.h"
32 32
33 #ifdef _WIN32_WCE 33 #ifdef _WIN32_WCE
34 #error This is WinCE. Please use src/timer/wince/SDL_systimer.c instead. 34 #error This is WinCE. Please use src/timer/wince/SDL_systimer.c instead.
35 #endif 35 #endif
36 36
37 #define TIME_WRAP_VALUE (~(DWORD)0) 37 #define TIME_WRAP_VALUE (~(DWORD)0)
38 38
39 /* The first (low-resolution) ticks value of the application */ 39 /* The first (low-resolution) ticks value of the application */
46 static LARGE_INTEGER hires_start_ticks; 46 static LARGE_INTEGER hires_start_ticks;
47 /* The number of ticks per second of the high-resolution performance counter */ 47 /* The number of ticks per second of the high-resolution performance counter */
48 static LARGE_INTEGER hires_ticks_per_second; 48 static LARGE_INTEGER hires_ticks_per_second;
49 #endif 49 #endif
50 50
51 void SDL_StartTicks(void) 51 void
52 SDL_StartTicks(void)
52 { 53 {
53 /* Set first ticks value */ 54 /* Set first ticks value */
54 #ifdef USE_GETTICKCOUNT 55 #ifdef USE_GETTICKCOUNT
55 start = GetTickCount(); 56 start = GetTickCount();
56 #else 57 #else
57 #if 0 /* Apparently there are problems with QPC on Win2K */ 58 #if 0 /* Apparently there are problems with QPC on Win2K */
58 if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE) 59 if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE) {
59 { 60 hires_timer_available = TRUE;
60 hires_timer_available = TRUE; 61 QueryPerformanceCounter(&hires_start_ticks);
61 QueryPerformanceCounter(&hires_start_ticks); 62 } else
62 }
63 else
64 #endif 63 #endif
65 { 64 {
66 hires_timer_available = FALSE; 65 hires_timer_available = FALSE;
67 timeBeginPeriod(1); /* use 1 ms timer precision */ 66 timeBeginPeriod(1); /* use 1 ms timer precision */
68 start = timeGetTime(); 67 start = timeGetTime();
69 } 68 }
70 #endif 69 #endif
71 } 70 }
72 71
73 Uint32 SDL_GetTicks(void) 72 Uint32
73 SDL_GetTicks(void)
74 { 74 {
75 DWORD now, ticks; 75 DWORD now, ticks;
76 #ifndef USE_GETTICKCOUNT 76 #ifndef USE_GETTICKCOUNT
77 LARGE_INTEGER hires_now; 77 LARGE_INTEGER hires_now;
78 #endif 78 #endif
79 79
80 #ifdef USE_GETTICKCOUNT 80 #ifdef USE_GETTICKCOUNT
81 now = GetTickCount(); 81 now = GetTickCount();
82 #else 82 #else
83 if (hires_timer_available) 83 if (hires_timer_available) {
84 { 84 QueryPerformanceCounter(&hires_now);
85 QueryPerformanceCounter(&hires_now);
86 85
87 hires_now.QuadPart -= hires_start_ticks.QuadPart; 86 hires_now.QuadPart -= hires_start_ticks.QuadPart;
88 hires_now.QuadPart *= 1000; 87 hires_now.QuadPart *= 1000;
89 hires_now.QuadPart /= hires_ticks_per_second.QuadPart; 88 hires_now.QuadPart /= hires_ticks_per_second.QuadPart;
90 89
91 return (DWORD)hires_now.QuadPart; 90 return (DWORD) hires_now.QuadPart;
92 } 91 } else {
93 else 92 now = timeGetTime();
94 { 93 }
95 now = timeGetTime();
96 }
97 #endif 94 #endif
98 95
99 if ( now < start ) { 96 if (now < start) {
100 ticks = (TIME_WRAP_VALUE-start) + now; 97 ticks = (TIME_WRAP_VALUE - start) + now;
101 } else { 98 } else {
102 ticks = (now - start); 99 ticks = (now - start);
103 } 100 }
104 return(ticks); 101 return (ticks);
105 } 102 }
106 103
107 void SDL_Delay(Uint32 ms) 104 void
105 SDL_Delay(Uint32 ms)
108 { 106 {
109 Sleep(ms); 107 Sleep(ms);
110 } 108 }
111 109
112 /* Data to handle a single periodic alarm */ 110 /* Data to handle a single periodic alarm */
113 static UINT timerID = 0; 111 static UINT timerID = 0;
114 112
115 static void CALLBACK HandleAlarm(UINT uID, UINT uMsg, DWORD_PTR dwUser, 113 static void CALLBACK
116 DWORD_PTR dw1, DWORD_PTR dw2) 114 HandleAlarm(UINT uID, UINT uMsg, DWORD_PTR dwUser,
115 DWORD_PTR dw1, DWORD_PTR dw2)
117 { 116 {
118 SDL_ThreadedTimerCheck(); 117 SDL_ThreadedTimerCheck();
119 } 118 }
120 119
121 120
122 int SDL_SYS_TimerInit(void) 121 int
122 SDL_SYS_TimerInit(void)
123 { 123 {
124 MMRESULT result; 124 MMRESULT result;
125 125
126 /* Set timer resolution */ 126 /* Set timer resolution */
127 result = timeBeginPeriod(TIMER_RESOLUTION); 127 result = timeBeginPeriod(TIMER_RESOLUTION);
128 if ( result != TIMERR_NOERROR ) { 128 if (result != TIMERR_NOERROR) {
129 SDL_SetError("Warning: Can't set %d ms timer resolution", 129 SDL_SetError("Warning: Can't set %d ms timer resolution",
130 TIMER_RESOLUTION); 130 TIMER_RESOLUTION);
131 } 131 }
132 /* Allow 10 ms of drift so we don't chew on CPU */ 132 /* Allow 10 ms of drift so we don't chew on CPU */
133 timerID = timeSetEvent(TIMER_RESOLUTION,1,HandleAlarm,0,TIME_PERIODIC); 133 timerID =
134 if ( ! timerID ) { 134 timeSetEvent(TIMER_RESOLUTION, 1, HandleAlarm, 0, TIME_PERIODIC);
135 SDL_SetError("timeSetEvent() failed"); 135 if (!timerID) {
136 return(-1); 136 SDL_SetError("timeSetEvent() failed");
137 } 137 return (-1);
138 return(SDL_SetTimerThreaded(1)); 138 }
139 return (SDL_SetTimerThreaded(1));
139 } 140 }
140 141
141 void SDL_SYS_TimerQuit(void) 142 void
143 SDL_SYS_TimerQuit(void)
142 { 144 {
143 if ( timerID ) { 145 if (timerID) {
144 timeKillEvent(timerID); 146 timeKillEvent(timerID);
145 } 147 }
146 timeEndPeriod(TIMER_RESOLUTION); 148 timeEndPeriod(TIMER_RESOLUTION);
147 } 149 }
148 150
149 int SDL_SYS_StartTimer(void) 151 int
152 SDL_SYS_StartTimer(void)
150 { 153 {
151 SDL_SetError("Internal logic error: Win32 uses threaded timer"); 154 SDL_SetError("Internal logic error: Win32 uses threaded timer");
152 return(-1); 155 return (-1);
153 } 156 }
154 157
155 void SDL_SYS_StopTimer(void) 158 void
159 SDL_SYS_StopTimer(void)
156 { 160 {
157 return; 161 return;
158 } 162 }
159 163
160 #endif /* SDL_TIMER_WIN32 */ 164 #endif /* SDL_TIMER_WIN32 */
165 /* vi: set ts=4 sw=4 expandtab: */