2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
24 #ifdef SDL_TIMER_WIN32
26 #define WIN32_LEAN_AND_MEAN
30 #include "SDL_timer.h"
31 #include "../SDL_timer_c.h"
34 #error This is WinCE. Please use src/timer/wince/SDL_systimer.c instead.
37 #define TIME_WRAP_VALUE (~(DWORD)0)
39 /* The first (low-resolution) ticks value of the application */
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;
51 void SDL_StartTicks(void)
53 /* Set first ticks value */
54 #ifdef USE_GETTICKCOUNT
55 start = GetTickCount();
57 #if 0 /* Apparently there are problems with QPC on Win2K */
58 if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE)
60 hires_timer_available = TRUE;
61 QueryPerformanceCounter(&hires_start_ticks);
66 hires_timer_available = FALSE;
67 timeBeginPeriod(1); /* use 1 ms timer precision */
68 start = timeGetTime();
73 Uint32 SDL_GetTicks(void)
76 #ifndef USE_GETTICKCOUNT
77 LARGE_INTEGER hires_now;
80 #ifdef USE_GETTICKCOUNT
83 if (hires_timer_available)
85 QueryPerformanceCounter(&hires_now);
87 hires_now.QuadPart -= hires_start_ticks.QuadPart;
88 hires_now.QuadPart *= 1000;
89 hires_now.QuadPart /= hires_ticks_per_second.QuadPart;
91 return (DWORD)hires_now.QuadPart;
100 ticks = (TIME_WRAP_VALUE-start) + now;
102 ticks = (now - start);
107 void SDL_Delay(Uint32 ms)
112 /* Data to handle a single periodic alarm */
113 static UINT timerID = 0;
115 static void CALLBACK HandleAlarm(UINT uID, UINT uMsg, DWORD_PTR dwUser,
116 DWORD_PTR dw1, DWORD_PTR dw2)
118 SDL_ThreadedTimerCheck();
122 int SDL_SYS_TimerInit(void)
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",
132 /* Allow 10 ms of drift so we don't chew on CPU */
133 timerID = timeSetEvent(TIMER_RESOLUTION,1,HandleAlarm,0,TIME_PERIODIC);
135 SDL_SetError("timeSetEvent() failed");
138 return(SDL_SetTimerThreaded(1));
141 void SDL_SYS_TimerQuit(void)
144 timeKillEvent(timerID);
146 timeEndPeriod(TIMER_RESOLUTION);
149 int SDL_SYS_StartTimer(void)
151 SDL_SetError("Internal logic error: Win32 uses threaded timer");
155 void SDL_SYS_StopTimer(void)
160 #endif /* SDL_TIMER_WIN32 */