Mercurial > sdl-ios-xcode
comparison src/timer/wince/SDL_systimer.c @ 5131:2c500f37abcf
merged: might need to check main.c in the iOS template to make sure no changes were abandoned.
author | Eric Wing <ewing . public |-at-| gmail . com> |
---|---|
date | Tue, 01 Feb 2011 00:37:02 -0800 |
parents | 481dabb098ef |
children | b530ef003506 |
comparison
equal
deleted
inserted
replaced
5078:067973aec4d8 | 5131:2c500f37abcf |
---|---|
21 */ | 21 */ |
22 #include "SDL_config.h" | 22 #include "SDL_config.h" |
23 | 23 |
24 #ifdef SDL_TIMER_WINCE | 24 #ifdef SDL_TIMER_WINCE |
25 | 25 |
26 #define WIN32_LEAN_AND_MEAN | 26 #include "../../core/windows/SDL_windows.h" |
27 #include <windows.h> | |
28 #include <mmsystem.h> | |
29 | 27 |
30 #include "SDL_thread.h" | |
31 #include "SDL_timer.h" | 28 #include "SDL_timer.h" |
32 #include "../SDL_timer_c.h" | |
33 | 29 |
34 static Uint64 start_date; | 30 static Uint64 start_date; |
35 static Uint64 start_ticks; | 31 static Uint64 start_ticks; |
36 | 32 |
37 static Uint64 | 33 static Uint64 |
68 wce_rel_date(void) | 64 wce_rel_date(void) |
69 { | 65 { |
70 return ((Sint32) (wce_date() - start_date)); | 66 return ((Sint32) (wce_date() - start_date)); |
71 } | 67 } |
72 | 68 |
69 /* Recard start-time of application for reference */ | |
70 void | |
71 SDL_StartTicks(void) | |
72 { | |
73 start_date = wce_date(); | |
74 start_ticks = wce_ticks(); | |
75 } | |
76 | |
73 /* Return time in ms relative to when SDL was started */ | 77 /* Return time in ms relative to when SDL was started */ |
74 Uint32 | 78 Uint32 |
75 SDL_GetTicks() | 79 SDL_GetTicks() |
76 { | 80 { |
77 Sint32 offset = wce_rel_date() - wce_rel_ticks(); | 81 Sint32 offset = wce_rel_date() - wce_rel_ticks(); |
88 SDL_Delay(Uint32 ms) | 92 SDL_Delay(Uint32 ms) |
89 { | 93 { |
90 Sleep(ms); | 94 Sleep(ms); |
91 } | 95 } |
92 | 96 |
93 /* Recard start-time of application for reference */ | 97 #endif /* SDL_TIMER_WINCE */ |
94 void | |
95 SDL_StartTicks(void) | |
96 { | |
97 start_date = wce_date(); | |
98 start_ticks = wce_ticks(); | |
99 } | |
100 | 98 |
101 static UINT WIN_timer; | |
102 | |
103 #if ( _WIN32_WCE <= 420 ) | |
104 | |
105 static HANDLE timersThread = 0; | |
106 static HANDLE timersQuitEvent = 0; | |
107 | |
108 DWORD | |
109 TimersThreadProc(void *data) | |
110 { | |
111 while (WaitForSingleObject(timersQuitEvent, 10) == WAIT_TIMEOUT) { | |
112 SDL_ThreadedTimerCheck(); | |
113 } | |
114 return 0; | |
115 } | |
116 | |
117 int | |
118 SDL_SYS_TimerInit(void) | |
119 { | |
120 // create a thread to process a threaded timers | |
121 // SetTimer does not suit the needs because | |
122 // TimerCallbackProc will be called only when WM_TIMER occured | |
123 | |
124 timersQuitEvent = CreateEvent(0, TRUE, FALSE, 0); | |
125 if (!timersQuitEvent) { | |
126 SDL_SetError("Cannot create event for timers thread"); | |
127 return -1; | |
128 } | |
129 timersThread = CreateThread(NULL, 0, TimersThreadProc, 0, 0, 0); | |
130 if (!timersThread) { | |
131 SDL_SetError | |
132 ("Cannot create timers thread, check amount of RAM available"); | |
133 return -1; | |
134 } | |
135 SetThreadPriority(timersThread, THREAD_PRIORITY_HIGHEST); | |
136 | |
137 return (SDL_SetTimerThreaded(1)); | |
138 } | |
139 | |
140 void | |
141 SDL_SYS_TimerQuit(void) | |
142 { | |
143 SetEvent(timersQuitEvent); | |
144 if (WaitForSingleObject(timersThread, 2000) == WAIT_TIMEOUT) | |
145 TerminateThread(timersThread, 0); | |
146 CloseHandle(timersThread); | |
147 CloseHandle(timersQuitEvent); | |
148 return; | |
149 } | |
150 | |
151 #else | |
152 | |
153 #pragma comment(lib, "mmtimer.lib") | |
154 | |
155 /* Data to handle a single periodic alarm */ | |
156 static UINT timerID = 0; | |
157 | |
158 static void CALLBACK | |
159 HandleAlarm(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2) | |
160 { | |
161 SDL_ThreadedTimerCheck(); | |
162 } | |
163 | |
164 | |
165 int | |
166 SDL_SYS_TimerInit(void) | |
167 { | |
168 MMRESULT result; | |
169 | |
170 /* Set timer resolution */ | |
171 result = timeBeginPeriod(TIMER_RESOLUTION); | |
172 if (result != TIMERR_NOERROR) { | |
173 SDL_SetError("Warning: Can't set %d ms timer resolution", | |
174 TIMER_RESOLUTION); | |
175 } | |
176 /* Allow 10 ms of drift so we don't chew on CPU */ | |
177 timerID = | |
178 timeSetEvent(TIMER_RESOLUTION, 1, HandleAlarm, 0, TIME_PERIODIC); | |
179 if (!timerID) { | |
180 SDL_SetError("timeSetEvent() failed"); | |
181 return (-1); | |
182 } | |
183 return (SDL_SetTimerThreaded(1)); | |
184 } | |
185 | |
186 void | |
187 SDL_SYS_TimerQuit(void) | |
188 { | |
189 if (timerID) { | |
190 timeKillEvent(timerID); | |
191 } | |
192 timeEndPeriod(TIMER_RESOLUTION); | |
193 } | |
194 | |
195 #endif | |
196 | |
197 int | |
198 SDL_SYS_StartTimer(void) | |
199 { | |
200 SDL_SetError("Internal logic error: WinCE uses threaded timer"); | |
201 return (-1); | |
202 } | |
203 | |
204 void | |
205 SDL_SYS_StopTimer(void) | |
206 { | |
207 return; | |
208 } | |
209 | |
210 #endif /* SDL_TIMER_WINCE */ | |
211 /* vi: set ts=4 sw=4 expandtab: */ | 99 /* vi: set ts=4 sw=4 expandtab: */ |