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