Mercurial > sdl-ios-xcode
comparison src/timer/wince/SDL_systimer.c @ 1668:4da1ee79c9af SDL-1.3
more tweaking indent options
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 29 May 2006 04:04:35 +0000 |
parents | 782fd950bd46 |
children |
comparison
equal
deleted
inserted
replaced
1667:1fddae038bc8 | 1668:4da1ee79c9af |
---|---|
33 | 33 |
34 static Uint64 start_date; | 34 static Uint64 start_date; |
35 static Uint64 start_ticks; | 35 static Uint64 start_ticks; |
36 | 36 |
37 static Uint64 | 37 static Uint64 |
38 wce_ticks (void) | 38 wce_ticks(void) |
39 { | 39 { |
40 return ((Uint64) GetTickCount ()); | 40 return ((Uint64) GetTickCount()); |
41 } | 41 } |
42 | 42 |
43 static Uint64 | 43 static Uint64 |
44 wce_date (void) | 44 wce_date(void) |
45 { | 45 { |
46 union | 46 union |
47 { | 47 { |
48 FILETIME ftime; | 48 FILETIME ftime; |
49 Uint64 itime; | 49 Uint64 itime; |
50 } ftime; | 50 } ftime; |
51 SYSTEMTIME stime; | 51 SYSTEMTIME stime; |
52 | 52 |
53 GetSystemTime (&stime); | 53 GetSystemTime(&stime); |
54 SystemTimeToFileTime (&stime, &ftime.ftime); | 54 SystemTimeToFileTime(&stime, &ftime.ftime); |
55 ftime.itime /= 10000; // Convert 100ns intervals to 1ms intervals | 55 ftime.itime /= 10000; // Convert 100ns intervals to 1ms intervals |
56 // Remove ms portion, which can't be relied on | 56 // Remove ms portion, which can't be relied on |
57 ftime.itime -= (ftime.itime % 1000); | 57 ftime.itime -= (ftime.itime % 1000); |
58 return (ftime.itime); | 58 return (ftime.itime); |
59 } | 59 } |
60 | 60 |
61 static Sint32 | 61 static Sint32 |
62 wce_rel_ticks (void) | 62 wce_rel_ticks(void) |
63 { | 63 { |
64 return ((Sint32) (wce_ticks () - start_ticks)); | 64 return ((Sint32) (wce_ticks() - start_ticks)); |
65 } | 65 } |
66 | 66 |
67 static Sint32 | 67 static Sint32 |
68 wce_rel_date (void) | 68 wce_rel_date(void) |
69 { | 69 { |
70 return ((Sint32) (wce_date () - start_date)); | 70 return ((Sint32) (wce_date() - start_date)); |
71 } | 71 } |
72 | 72 |
73 /* Return time in ms relative to when SDL was started */ | 73 /* Return time in ms relative to when SDL was started */ |
74 Uint32 | 74 Uint32 |
75 SDL_GetTicks () | 75 SDL_GetTicks() |
76 { | 76 { |
77 Sint32 offset = wce_rel_date () - wce_rel_ticks (); | 77 Sint32 offset = wce_rel_date() - wce_rel_ticks(); |
78 if ((offset < -1000) || (offset > 1000)) { | 78 if ((offset < -1000) || (offset > 1000)) { |
79 // fprintf(stderr,"Time desync(%+d), resyncing\n",offset/1000); | 79 // fprintf(stderr,"Time desync(%+d), resyncing\n",offset/1000); |
80 start_ticks -= offset; | 80 start_ticks -= offset; |
81 } | 81 } |
82 | 82 |
83 return ((Uint32) wce_rel_ticks ()); | 83 return ((Uint32) wce_rel_ticks()); |
84 } | 84 } |
85 | 85 |
86 /* Give up approx. givem milliseconds to the OS. */ | 86 /* Give up approx. givem milliseconds to the OS. */ |
87 void | 87 void |
88 SDL_Delay (Uint32 ms) | 88 SDL_Delay(Uint32 ms) |
89 { | 89 { |
90 Sleep (ms); | 90 Sleep(ms); |
91 } | 91 } |
92 | 92 |
93 /* Recard start-time of application for reference */ | 93 /* Recard start-time of application for reference */ |
94 void | 94 void |
95 SDL_StartTicks (void) | 95 SDL_StartTicks(void) |
96 { | 96 { |
97 start_date = wce_date (); | 97 start_date = wce_date(); |
98 start_ticks = wce_ticks (); | 98 start_ticks = wce_ticks(); |
99 } | 99 } |
100 | 100 |
101 static UINT WIN_timer; | 101 static UINT WIN_timer; |
102 | 102 |
103 #if ( _WIN32_WCE <= 420 ) | 103 #if ( _WIN32_WCE <= 420 ) |
104 | 104 |
105 static HANDLE timersThread = 0; | 105 static HANDLE timersThread = 0; |
106 static HANDLE timersQuitEvent = 0; | 106 static HANDLE timersQuitEvent = 0; |
107 | 107 |
108 DWORD | 108 DWORD |
109 TimersThreadProc (void *data) | 109 TimersThreadProc(void *data) |
110 { | 110 { |
111 while (WaitForSingleObject (timersQuitEvent, 10) == WAIT_TIMEOUT) { | 111 while (WaitForSingleObject(timersQuitEvent, 10) == WAIT_TIMEOUT) { |
112 SDL_ThreadedTimerCheck (); | 112 SDL_ThreadedTimerCheck(); |
113 } | 113 } |
114 return 0; | 114 return 0; |
115 } | 115 } |
116 | 116 |
117 int | 117 int |
118 SDL_SYS_TimerInit (void) | 118 SDL_SYS_TimerInit(void) |
119 { | 119 { |
120 // create a thread to process a threaded timers | 120 // create a thread to process a threaded timers |
121 // SetTimer does not suit the needs because | 121 // SetTimer does not suit the needs because |
122 // TimerCallbackProc will be called only when WM_TIMER occured | 122 // TimerCallbackProc will be called only when WM_TIMER occured |
123 | 123 |
124 timersQuitEvent = CreateEvent (0, TRUE, FALSE, 0); | 124 timersQuitEvent = CreateEvent(0, TRUE, FALSE, 0); |
125 if (!timersQuitEvent) { | 125 if (!timersQuitEvent) { |
126 SDL_SetError ("Cannot create event for timers thread"); | 126 SDL_SetError("Cannot create event for timers thread"); |
127 return -1; | 127 return -1; |
128 } | 128 } |
129 timersThread = CreateThread (NULL, 0, TimersThreadProc, 0, 0, 0); | 129 timersThread = CreateThread(NULL, 0, TimersThreadProc, 0, 0, 0); |
130 if (!timersThread) { | 130 if (!timersThread) { |
131 SDL_SetError | 131 SDL_SetError |
132 ("Cannot create timers thread, check amount of RAM available"); | 132 ("Cannot create timers thread, check amount of RAM available"); |
133 return -1; | 133 return -1; |
134 } | 134 } |
135 SetThreadPriority (timersThread, THREAD_PRIORITY_HIGHEST); | 135 SetThreadPriority(timersThread, THREAD_PRIORITY_HIGHEST); |
136 | 136 |
137 return (SDL_SetTimerThreaded (1)); | 137 return (SDL_SetTimerThreaded(1)); |
138 } | 138 } |
139 | 139 |
140 void | 140 void |
141 SDL_SYS_TimerQuit (void) | 141 SDL_SYS_TimerQuit(void) |
142 { | 142 { |
143 SetEvent (timersQuitEvent); | 143 SetEvent(timersQuitEvent); |
144 if (WaitForSingleObject (timersThread, 2000) == WAIT_TIMEOUT) | 144 if (WaitForSingleObject(timersThread, 2000) == WAIT_TIMEOUT) |
145 TerminateThread (timersThread, 0); | 145 TerminateThread(timersThread, 0); |
146 CloseHandle (timersThread); | 146 CloseHandle(timersThread); |
147 CloseHandle (timersQuitEvent); | 147 CloseHandle(timersQuitEvent); |
148 return; | 148 return; |
149 } | 149 } |
150 | 150 |
151 #else | 151 #else |
152 | 152 |
154 | 154 |
155 /* Data to handle a single periodic alarm */ | 155 /* Data to handle a single periodic alarm */ |
156 static UINT timerID = 0; | 156 static UINT timerID = 0; |
157 | 157 |
158 static void CALLBACK | 158 static void CALLBACK |
159 HandleAlarm (UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2) | 159 HandleAlarm(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2) |
160 { | 160 { |
161 SDL_ThreadedTimerCheck (); | 161 SDL_ThreadedTimerCheck(); |
162 } | 162 } |
163 | 163 |
164 | 164 |
165 int | 165 int |
166 SDL_SYS_TimerInit (void) | 166 SDL_SYS_TimerInit(void) |
167 { | 167 { |
168 MMRESULT result; | 168 MMRESULT result; |
169 | 169 |
170 /* Set timer resolution */ | 170 /* Set timer resolution */ |
171 result = timeBeginPeriod (TIMER_RESOLUTION); | 171 result = timeBeginPeriod(TIMER_RESOLUTION); |
172 if (result != TIMERR_NOERROR) { | 172 if (result != TIMERR_NOERROR) { |
173 SDL_SetError ("Warning: Can't set %d ms timer resolution", | 173 SDL_SetError("Warning: Can't set %d ms timer resolution", |
174 TIMER_RESOLUTION); | 174 TIMER_RESOLUTION); |
175 } | 175 } |
176 /* Allow 10 ms of drift so we don't chew on CPU */ | 176 /* Allow 10 ms of drift so we don't chew on CPU */ |
177 timerID = | 177 timerID = |
178 timeSetEvent (TIMER_RESOLUTION, 1, HandleAlarm, 0, TIME_PERIODIC); | 178 timeSetEvent(TIMER_RESOLUTION, 1, HandleAlarm, 0, TIME_PERIODIC); |
179 if (!timerID) { | 179 if (!timerID) { |
180 SDL_SetError ("timeSetEvent() failed"); | 180 SDL_SetError("timeSetEvent() failed"); |
181 return (-1); | 181 return (-1); |
182 } | 182 } |
183 return (SDL_SetTimerThreaded (1)); | 183 return (SDL_SetTimerThreaded(1)); |
184 } | 184 } |
185 | 185 |
186 void | 186 void |
187 SDL_SYS_TimerQuit (void) | 187 SDL_SYS_TimerQuit(void) |
188 { | 188 { |
189 if (timerID) { | 189 if (timerID) { |
190 timeKillEvent (timerID); | 190 timeKillEvent(timerID); |
191 } | 191 } |
192 timeEndPeriod (TIMER_RESOLUTION); | 192 timeEndPeriod(TIMER_RESOLUTION); |
193 } | 193 } |
194 | 194 |
195 #endif | 195 #endif |
196 | 196 |
197 int | 197 int |
198 SDL_SYS_StartTimer (void) | 198 SDL_SYS_StartTimer(void) |
199 { | 199 { |
200 SDL_SetError ("Internal logic error: WinCE uses threaded timer"); | 200 SDL_SetError("Internal logic error: WinCE uses threaded timer"); |
201 return (-1); | 201 return (-1); |
202 } | 202 } |
203 | 203 |
204 void | 204 void |
205 SDL_SYS_StopTimer (void) | 205 SDL_SYS_StopTimer(void) |
206 { | 206 { |
207 return; | 207 return; |
208 } | 208 } |
209 | 209 |
210 #endif /* SDL_TIMER_WINCE */ | 210 #endif /* SDL_TIMER_WINCE */ |