comparison src/timer/windows/SDL_systimer.c @ 5062:e8916fe9cfc8

Fixed bug #925 Changed "win32" to "windows"
author Sam Lantinga <slouken@libsdl.org>
date Thu, 20 Jan 2011 18:04:05 -0800
parents src/timer/win32/SDL_systimer.c@f7b03b6838cb
children 327f181542f1
comparison
equal deleted inserted replaced
5061:9e9940eae455 5062:e8916fe9cfc8
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2010 Sam Lantinga
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Sam Lantinga
20 slouken@libsdl.org
21 */
22 #include "SDL_config.h"
23
24 #ifdef SDL_TIMER_WINDOWS
25
26 #define WIN32_LEAN_AND_MEAN
27 #include <windows.h>
28 #include <mmsystem.h>
29
30 #include "SDL_timer.h"
31 #include "../SDL_timer_c.h"
32
33 #ifdef _WIN32_WCE
34 #error This is WinCE. Please use src/timer/wince/SDL_systimer.c instead.
35 #endif
36
37 #define TIME_WRAP_VALUE (~(DWORD)0)
38
39 /* The first (low-resolution) ticks value of the application */
40 static DWORD start;
41
42 #ifndef USE_GETTICKCOUNT
43 /* Store if a high-resolution performance counter exists on the system */
44 static BOOL hires_timer_available;
45 /* The first high-resolution ticks value of the application */
46 static LARGE_INTEGER hires_start_ticks;
47 /* The number of ticks per second of the high-resolution performance counter */
48 static LARGE_INTEGER hires_ticks_per_second;
49 #endif
50
51 void
52 SDL_StartTicks(void)
53 {
54 /* Set first ticks value */
55 #ifdef USE_GETTICKCOUNT
56 start = GetTickCount();
57 #else
58 #if 0 /* Apparently there are problems with QPC on Win2K */
59 if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE) {
60 hires_timer_available = TRUE;
61 QueryPerformanceCounter(&hires_start_ticks);
62 } else
63 #endif
64 {
65 hires_timer_available = FALSE;
66 timeBeginPeriod(1); /* use 1 ms timer precision */
67 start = timeGetTime();
68 }
69 #endif
70 }
71
72 Uint32
73 SDL_GetTicks(void)
74 {
75 DWORD now, ticks;
76 #ifndef USE_GETTICKCOUNT
77 LARGE_INTEGER hires_now;
78 #endif
79
80 #ifdef USE_GETTICKCOUNT
81 now = GetTickCount();
82 #else
83 if (hires_timer_available) {
84 QueryPerformanceCounter(&hires_now);
85
86 hires_now.QuadPart -= hires_start_ticks.QuadPart;
87 hires_now.QuadPart *= 1000;
88 hires_now.QuadPart /= hires_ticks_per_second.QuadPart;
89
90 return (DWORD) hires_now.QuadPart;
91 } else {
92 now = timeGetTime();
93 }
94 #endif
95
96 if (now < start) {
97 ticks = (TIME_WRAP_VALUE - start) + now;
98 } else {
99 ticks = (now - start);
100 }
101 return (ticks);
102 }
103
104 void
105 SDL_Delay(Uint32 ms)
106 {
107 Sleep(ms);
108 }
109
110 /* Data to handle a single periodic alarm */
111 static UINT timerID = 0;
112
113 static void CALLBACK
114 HandleAlarm(UINT uID, UINT uMsg, DWORD_PTR dwUser,
115 DWORD_PTR dw1, DWORD_PTR dw2)
116 {
117 SDL_ThreadedTimerCheck();
118 }
119
120
121 int
122 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 =
134 timeSetEvent(TIMER_RESOLUTION, 1, HandleAlarm, 0, TIME_PERIODIC);
135 if (!timerID) {
136 SDL_SetError("timeSetEvent() failed");
137 return (-1);
138 }
139 return (SDL_SetTimerThreaded(1));
140 }
141
142 void
143 SDL_SYS_TimerQuit(void)
144 {
145 if (timerID) {
146 timeKillEvent(timerID);
147 }
148 timeEndPeriod(TIMER_RESOLUTION);
149 }
150
151 int
152 SDL_SYS_StartTimer(void)
153 {
154 SDL_SetError("Internal logic error: Win32 uses threaded timer");
155 return (-1);
156 }
157
158 void
159 SDL_SYS_StopTimer(void)
160 {
161 return;
162 }
163
164 #endif /* SDL_TIMER_WINDOWS */
165
166 /* vi: set ts=4 sw=4 expandtab: */