Mercurial > sdl-ios-xcode
annotate src/timer/win32/SDL_systimer.c @ 1635:92947e3a18db
Make sure code is only compiled if the appropriate subsystem is enabled
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Fri, 14 Apr 2006 04:46:47 +0000 |
parents | 420b3f47806d |
children | 782fd950bd46 c121d94672cb a1b03ba2fcd0 |
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 */ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
22 #include "SDL_config.h" |
0 | 23 |
1635
92947e3a18db
Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents:
1497
diff
changeset
|
24 #ifdef SDL_TIMER_WIN32 |
92947e3a18db
Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents:
1497
diff
changeset
|
25 |
1433
bb6839704ed6
SDL_windows.h is no longer necessary
Sam Lantinga <slouken@libsdl.org>
parents:
1402
diff
changeset
|
26 #define WIN32_LEAN_AND_MEAN |
bb6839704ed6
SDL_windows.h is no longer necessary
Sam Lantinga <slouken@libsdl.org>
parents:
1402
diff
changeset
|
27 #include <windows.h> |
0 | 28 #include <mmsystem.h> |
29 | |
30 #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
|
31 #include "../SDL_timer_c.h" |
0 | 32 |
33 #ifdef _WIN32_WCE | |
1180
bdcb8bb4c831
From: Tyler Montbriand <tsm@accesscomm.ca>
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
34 #error This is WinCE. Please use src/timer/wince/SDL_systimer.c instead. |
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 /* Data to handle a single periodic alarm */ | |
113 static UINT timerID = 0; | |
114 | |
1480 | 115 static void CALLBACK HandleAlarm(UINT uID, UINT uMsg, DWORD_PTR dwUser, |
116 DWORD_PTR dw1, DWORD_PTR dw2) | |
0 | 117 { |
118 SDL_ThreadedTimerCheck(); | |
119 } | |
120 | |
121 | |
122 int SDL_SYS_TimerInit(void) | |
123 { | |
124 MMRESULT result; | |
125 | |
126 /* Set timer resolution */ | |
127 result = timeBeginPeriod(TIMER_RESOLUTION); | |
128 if ( result != TIMERR_NOERROR ) { | |
129 SDL_SetError("Warning: Can't set %d ms timer resolution", | |
130 TIMER_RESOLUTION); | |
131 } | |
132 /* Allow 10 ms of drift so we don't chew on CPU */ | |
133 timerID = timeSetEvent(TIMER_RESOLUTION,1,HandleAlarm,0,TIME_PERIODIC); | |
134 if ( ! timerID ) { | |
135 SDL_SetError("timeSetEvent() failed"); | |
136 return(-1); | |
137 } | |
138 return(SDL_SetTimerThreaded(1)); | |
139 } | |
140 | |
141 void SDL_SYS_TimerQuit(void) | |
142 { | |
143 if ( timerID ) { | |
144 timeKillEvent(timerID); | |
145 } | |
146 timeEndPeriod(TIMER_RESOLUTION); | |
147 } | |
148 | |
149 int SDL_SYS_StartTimer(void) | |
150 { | |
151 SDL_SetError("Internal logic error: Win32 uses threaded timer"); | |
152 return(-1); | |
153 } | |
154 | |
155 void SDL_SYS_StopTimer(void) | |
156 { | |
157 return; | |
158 } | |
159 | |
1635
92947e3a18db
Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents:
1497
diff
changeset
|
160 #endif /* SDL_TIMER_WIN32 */ |