Mercurial > sdl-ios-xcode
annotate src/timer/win32/SDL_systimer.c @ 1361:19418e4422cb
New configure-based build system. Still work in progress, but much improved
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 16 Feb 2006 10:11:48 +0000 |
parents | c71e05b4dc2e |
children | d910939febfa |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1180
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1180
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1180
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1180
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1180
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1180
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1180
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 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 | |
1330
450721ad5436
It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
23 #include "SDL_windows.h" |
0 | 24 #include <mmsystem.h> |
25 | |
26 #include "SDL_timer.h" | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
27 #include "../SDL_timer_c.h" |
0 | 28 |
29 #ifdef _WIN32_WCE | |
1180
bdcb8bb4c831
From: Tyler Montbriand <tsm@accesscomm.ca>
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
30 #error This is WinCE. Please use src/timer/wince/SDL_systimer.c instead. |
bdcb8bb4c831
From: Tyler Montbriand <tsm@accesscomm.ca>
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
31 |
bdcb8bb4c831
From: Tyler Montbriand <tsm@accesscomm.ca>
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
32 /* but if you really want to use this file, use these #defines... */ |
bdcb8bb4c831
From: Tyler Montbriand <tsm@accesscomm.ca>
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
33 #define USE_GETTICKCOUNT |
bdcb8bb4c831
From: Tyler Montbriand <tsm@accesscomm.ca>
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
34 #define USE_SETTIMER |
0 | 35 #endif |
36 | |
37 #define TIME_WRAP_VALUE (~(DWORD)0) | |
38 | |
89
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
39 /* The first (low-resolution) ticks value of the application */ |
0 | 40 static DWORD start; |
41 | |
89
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
42 #ifndef USE_GETTICKCOUNT |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
43 /* 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
|
44 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
|
45 /* 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
|
46 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
|
47 /* 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
|
48 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
|
49 #endif |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
50 |
0 | 51 void SDL_StartTicks(void) |
52 { | |
53 /* Set first ticks value */ | |
54 #ifdef USE_GETTICKCOUNT | |
55 start = GetTickCount(); | |
56 #else | |
326
72d55d02cb47
Disabled QueryPerformanceCounter(), due to problems on Win2K
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
57 #if 0 /* Apparently there are problems with QPC on Win2K */ |
89
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
58 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
|
59 { |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
60 hires_timer_available = TRUE; |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
61 QueryPerformanceCounter(&hires_start_ticks); |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
62 } |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
63 else |
326
72d55d02cb47
Disabled QueryPerformanceCounter(), due to problems on Win2K
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
64 #endif |
89
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
65 { |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
66 hires_timer_available = FALSE; |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
67 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
|
68 start = timeGetTime(); |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
69 } |
0 | 70 #endif |
71 } | |
72 | |
73 Uint32 SDL_GetTicks(void) | |
74 { | |
75 DWORD now, ticks; | |
89
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
76 #ifndef USE_GETTICKCOUNT |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
77 LARGE_INTEGER hires_now; |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
78 #endif |
0 | 79 |
80 #ifdef USE_GETTICKCOUNT | |
81 now = GetTickCount(); | |
82 #else | |
89
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
83 if (hires_timer_available) |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
84 { |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
85 QueryPerformanceCounter(&hires_now); |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
86 |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
87 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
|
88 hires_now.QuadPart *= 1000; |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
89 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
|
90 |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
91 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
|
92 } |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
93 else |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
94 { |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
95 now = timeGetTime(); |
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
96 } |
0 | 97 #endif |
89
69b8fac3e1c0
Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
98 |
0 | 99 if ( now < start ) { |
100 ticks = (TIME_WRAP_VALUE-start) + now; | |
101 } else { | |
102 ticks = (now - start); | |
103 } | |
104 return(ticks); | |
105 } | |
106 | |
107 void SDL_Delay(Uint32 ms) | |
108 { | |
109 Sleep(ms); | |
110 } | |
111 | |
112 #ifdef USE_SETTIMER | |
113 | |
114 static UINT WIN_timer; | |
115 | |
116 int SDL_SYS_TimerInit(void) | |
117 { | |
118 return(0); | |
119 } | |
120 | |
121 void SDL_SYS_TimerQuit(void) | |
122 { | |
123 return; | |
124 } | |
125 | |
126 /* Forward declaration because this is called by the timer callback */ | |
127 int SDL_SYS_StartTimer(void); | |
128 | |
129 static VOID CALLBACK TimerCallbackProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime) | |
130 { | |
131 Uint32 ms; | |
132 | |
133 ms = SDL_alarm_callback(SDL_alarm_interval); | |
134 if ( ms != SDL_alarm_interval ) { | |
135 KillTimer(NULL, idEvent); | |
136 if ( ms ) { | |
137 SDL_alarm_interval = ROUND_RESOLUTION(ms); | |
138 SDL_SYS_StartTimer(); | |
139 } else { | |
140 SDL_alarm_interval = 0; | |
141 } | |
142 } | |
143 } | |
144 | |
145 int SDL_SYS_StartTimer(void) | |
146 { | |
147 int retval; | |
148 | |
149 WIN_timer = SetTimer(NULL, 0, SDL_alarm_interval, TimerCallbackProc); | |
150 if ( WIN_timer ) { | |
151 retval = 0; | |
152 } else { | |
153 retval = -1; | |
154 } | |
155 return retval; | |
156 } | |
157 | |
158 void SDL_SYS_StopTimer(void) | |
159 { | |
160 if ( WIN_timer ) { | |
161 KillTimer(NULL, WIN_timer); | |
162 WIN_timer = 0; | |
163 } | |
164 } | |
165 | |
166 #else /* !USE_SETTIMER */ | |
167 | |
168 /* Data to handle a single periodic alarm */ | |
169 static UINT timerID = 0; | |
170 | |
171 static void CALLBACK HandleAlarm(UINT uID, UINT uMsg, DWORD dwUser, | |
172 DWORD dw1, DWORD dw2) | |
173 { | |
174 SDL_ThreadedTimerCheck(); | |
175 } | |
176 | |
177 | |
178 int SDL_SYS_TimerInit(void) | |
179 { | |
180 MMRESULT result; | |
181 | |
182 /* Set timer resolution */ | |
183 result = timeBeginPeriod(TIMER_RESOLUTION); | |
184 if ( result != TIMERR_NOERROR ) { | |
185 SDL_SetError("Warning: Can't set %d ms timer resolution", | |
186 TIMER_RESOLUTION); | |
187 } | |
188 /* Allow 10 ms of drift so we don't chew on CPU */ | |
189 timerID = timeSetEvent(TIMER_RESOLUTION,1,HandleAlarm,0,TIME_PERIODIC); | |
190 if ( ! timerID ) { | |
191 SDL_SetError("timeSetEvent() failed"); | |
192 return(-1); | |
193 } | |
194 return(SDL_SetTimerThreaded(1)); | |
195 } | |
196 | |
197 void SDL_SYS_TimerQuit(void) | |
198 { | |
199 if ( timerID ) { | |
200 timeKillEvent(timerID); | |
201 } | |
202 timeEndPeriod(TIMER_RESOLUTION); | |
203 } | |
204 | |
205 int SDL_SYS_StartTimer(void) | |
206 { | |
207 SDL_SetError("Internal logic error: Win32 uses threaded timer"); | |
208 return(-1); | |
209 } | |
210 | |
211 void SDL_SYS_StopTimer(void) | |
212 { | |
213 return; | |
214 } | |
215 | |
216 #endif /* USE_SETTIMER */ |