Mercurial > sdl-ios-xcode
annotate src/timer/win32/SDL_systimer.c @ 297:f6ffac90895c
Updated copyright information for 2002
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 06 Mar 2002 11:23:08 +0000 |
parents | e8157fcb3114 |
children | 72d55d02cb47 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
297
f6ffac90895c
Updated copyright information for 2002
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
89
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 #include <windows.h> | |
29 #include <mmsystem.h> | |
30 | |
31 #include "SDL_timer.h" | |
32 #include "SDL_timer_c.h" | |
33 #include "SDL_error.h" | |
34 | |
35 #ifdef _WIN32_WCE | |
36 #define USE_GETTICKCOUNT | |
37 #define USE_SETTIMER | |
38 #endif | |
39 | |
40 #define TIME_WRAP_VALUE (~(DWORD)0) | |
41 | |
89
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
42 /* The first (low-resolution) ticks value of the application */ |
0 | 43 static DWORD start; |
44 | |
89
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
45 #ifndef USE_GETTICKCOUNT |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
46 /* Store if a high-resolution performance counter exists on the system */ |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
47 static BOOL hires_timer_available; |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
48 /* The first high-resolution ticks value of the application */ |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
49 static LARGE_INTEGER hires_start_ticks; |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
50 /* The number of ticks per second of the high-resolution performance counter */ |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
51 static LARGE_INTEGER hires_ticks_per_second; |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
52 #endif |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
53 |
0 | 54 void SDL_StartTicks(void) |
55 { | |
56 /* Set first ticks value */ | |
57 #ifdef USE_GETTICKCOUNT | |
58 start = GetTickCount(); | |
59 #else | |
89
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
60 if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE) |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
61 { |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
62 hires_timer_available = TRUE; |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
63 QueryPerformanceCounter(&hires_start_ticks); |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
64 } |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
65 else |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
66 { |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
67 hires_timer_available = FALSE; |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
68 timeBeginPeriod(1); /* use 1 ms timer precision */ |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
69 start = timeGetTime(); |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
70 } |
0 | 71 #endif |
72 } | |
73 | |
74 Uint32 SDL_GetTicks(void) | |
75 { | |
76 DWORD now, ticks; | |
89
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
77 #ifndef USE_GETTICKCOUNT |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
78 LARGE_INTEGER hires_now; |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
79 #endif |
0 | 80 |
81 #ifdef USE_GETTICKCOUNT | |
82 now = GetTickCount(); | |
83 #else | |
89
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
84 if (hires_timer_available) |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
85 { |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
86 QueryPerformanceCounter(&hires_now); |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
87 |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
88 hires_now.QuadPart -= hires_start_ticks.QuadPart; |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
89 hires_now.QuadPart *= 1000; |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
90 hires_now.QuadPart /= hires_ticks_per_second.QuadPart; |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
91 |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
92 return (DWORD)hires_now.QuadPart; |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
93 } |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
94 else |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
95 { |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
96 now = timeGetTime(); |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
97 } |
0 | 98 #endif |
89
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
99 |
0 | 100 if ( now < start ) { |
101 ticks = (TIME_WRAP_VALUE-start) + now; | |
102 } else { | |
103 ticks = (now - start); | |
104 } | |
105 return(ticks); | |
106 } | |
107 | |
108 void SDL_Delay(Uint32 ms) | |
109 { | |
110 Sleep(ms); | |
111 } | |
112 | |
113 #ifdef USE_SETTIMER | |
114 | |
115 static UINT WIN_timer; | |
116 | |
117 int SDL_SYS_TimerInit(void) | |
118 { | |
119 return(0); | |
120 } | |
121 | |
122 void SDL_SYS_TimerQuit(void) | |
123 { | |
124 return; | |
125 } | |
126 | |
127 /* Forward declaration because this is called by the timer callback */ | |
128 int SDL_SYS_StartTimer(void); | |
129 | |
130 static VOID CALLBACK TimerCallbackProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime) | |
131 { | |
132 Uint32 ms; | |
133 | |
134 ms = SDL_alarm_callback(SDL_alarm_interval); | |
135 if ( ms != SDL_alarm_interval ) { | |
136 KillTimer(NULL, idEvent); | |
137 if ( ms ) { | |
138 SDL_alarm_interval = ROUND_RESOLUTION(ms); | |
139 SDL_SYS_StartTimer(); | |
140 } else { | |
141 SDL_alarm_interval = 0; | |
142 } | |
143 } | |
144 } | |
145 | |
146 int SDL_SYS_StartTimer(void) | |
147 { | |
148 int retval; | |
149 | |
150 WIN_timer = SetTimer(NULL, 0, SDL_alarm_interval, TimerCallbackProc); | |
151 if ( WIN_timer ) { | |
152 retval = 0; | |
153 } else { | |
154 retval = -1; | |
155 } | |
156 return retval; | |
157 } | |
158 | |
159 void SDL_SYS_StopTimer(void) | |
160 { | |
161 if ( WIN_timer ) { | |
162 KillTimer(NULL, WIN_timer); | |
163 WIN_timer = 0; | |
164 } | |
165 } | |
166 | |
167 #else /* !USE_SETTIMER */ | |
168 | |
169 /* Data to handle a single periodic alarm */ | |
170 static UINT timerID = 0; | |
171 | |
172 static void CALLBACK HandleAlarm(UINT uID, UINT uMsg, DWORD dwUser, | |
173 DWORD dw1, DWORD dw2) | |
174 { | |
175 SDL_ThreadedTimerCheck(); | |
176 } | |
177 | |
178 | |
179 int SDL_SYS_TimerInit(void) | |
180 { | |
181 MMRESULT result; | |
182 | |
183 /* Set timer resolution */ | |
184 result = timeBeginPeriod(TIMER_RESOLUTION); | |
185 if ( result != TIMERR_NOERROR ) { | |
186 SDL_SetError("Warning: Can't set %d ms timer resolution", | |
187 TIMER_RESOLUTION); | |
188 } | |
189 /* Allow 10 ms of drift so we don't chew on CPU */ | |
190 timerID = timeSetEvent(TIMER_RESOLUTION,1,HandleAlarm,0,TIME_PERIODIC); | |
191 if ( ! timerID ) { | |
192 SDL_SetError("timeSetEvent() failed"); | |
193 return(-1); | |
194 } | |
195 return(SDL_SetTimerThreaded(1)); | |
196 } | |
197 | |
198 void SDL_SYS_TimerQuit(void) | |
199 { | |
200 if ( timerID ) { | |
201 timeKillEvent(timerID); | |
202 } | |
203 timeEndPeriod(TIMER_RESOLUTION); | |
204 } | |
205 | |
206 int SDL_SYS_StartTimer(void) | |
207 { | |
208 SDL_SetError("Internal logic error: Win32 uses threaded timer"); | |
209 return(-1); | |
210 } | |
211 | |
212 void SDL_SYS_StopTimer(void) | |
213 { | |
214 return; | |
215 } | |
216 | |
217 #endif /* USE_SETTIMER */ |