2001-04-26 16:45:43 +00:00
|
|
|
/*
|
|
|
|
SDL - Simple DirectMedia Layer
|
2010-01-24 21:10:53 +00:00
|
|
|
Copyright (C) 1997-2010 Sam Lantinga
|
2001-04-26 16:45:43 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2006-02-01 06:32:25 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
2001-04-26 16:45:43 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2006-02-01 06:32:25 +00:00
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2001-04-26 16:45:43 +00:00
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2006-02-01 06:32:25 +00:00
|
|
|
Lesser General Public License for more details.
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2006-02-01 06:32:25 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-04-26 16:45:43 +00:00
|
|
|
|
|
|
|
Sam Lantinga
|
2001-12-14 12:38:15 +00:00
|
|
|
slouken@libsdl.org
|
2001-04-26 16:45:43 +00:00
|
|
|
*/
|
2006-02-21 08:46:50 +00:00
|
|
|
#include "SDL_config.h"
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2011-01-20 18:04:05 -08:00
|
|
|
#ifdef SDL_TIMER_WINDOWS
|
2006-04-14 04:46:47 +00:00
|
|
|
|
2011-01-24 21:20:30 -08:00
|
|
|
#include "../../core/windows/SDL_windows.h"
|
2001-04-26 16:45:43 +00:00
|
|
|
#include <mmsystem.h>
|
|
|
|
|
|
|
|
#include "SDL_timer.h"
|
2006-02-16 10:11:48 +00:00
|
|
|
#include "../SDL_timer_c.h"
|
2001-04-26 16:45:43 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32_WCE
|
2006-07-10 21:04:37 +00:00
|
|
|
#error This is WinCE. Please use src/timer/wince/SDL_systimer.c instead.
|
2001-04-26 16:45:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define TIME_WRAP_VALUE (~(DWORD)0)
|
|
|
|
|
2001-07-07 08:03:34 +00:00
|
|
|
/* The first (low-resolution) ticks value of the application */
|
2001-04-26 16:45:43 +00:00
|
|
|
static DWORD start;
|
|
|
|
|
2001-07-07 08:03:34 +00:00
|
|
|
#ifndef USE_GETTICKCOUNT
|
|
|
|
/* Store if a high-resolution performance counter exists on the system */
|
|
|
|
static BOOL hires_timer_available;
|
|
|
|
/* The first high-resolution ticks value of the application */
|
|
|
|
static LARGE_INTEGER hires_start_ticks;
|
|
|
|
/* The number of ticks per second of the high-resolution performance counter */
|
|
|
|
static LARGE_INTEGER hires_ticks_per_second;
|
|
|
|
#endif
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
void
|
|
|
|
SDL_StartTicks(void)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
/* Set first ticks value */
|
2001-04-26 16:45:43 +00:00
|
|
|
#ifdef USE_GETTICKCOUNT
|
2006-07-10 21:04:37 +00:00
|
|
|
start = GetTickCount();
|
2001-04-26 16:45:43 +00:00
|
|
|
#else
|
2006-07-10 21:04:37 +00:00
|
|
|
#if 0 /* Apparently there are problems with QPC on Win2K */
|
|
|
|
if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE) {
|
|
|
|
hires_timer_available = TRUE;
|
|
|
|
QueryPerformanceCounter(&hires_start_ticks);
|
|
|
|
} else
|
2002-03-31 03:34:11 +00:00
|
|
|
#endif
|
2006-07-10 21:04:37 +00:00
|
|
|
{
|
|
|
|
hires_timer_available = FALSE;
|
|
|
|
timeBeginPeriod(1); /* use 1 ms timer precision */
|
|
|
|
start = timeGetTime();
|
|
|
|
}
|
2001-04-26 16:45:43 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
Uint32
|
|
|
|
SDL_GetTicks(void)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
DWORD now, ticks;
|
2001-07-07 08:03:34 +00:00
|
|
|
#ifndef USE_GETTICKCOUNT
|
2006-07-10 21:04:37 +00:00
|
|
|
LARGE_INTEGER hires_now;
|
2001-07-07 08:03:34 +00:00
|
|
|
#endif
|
2001-04-26 16:45:43 +00:00
|
|
|
|
|
|
|
#ifdef USE_GETTICKCOUNT
|
2006-07-10 21:04:37 +00:00
|
|
|
now = GetTickCount();
|
2001-04-26 16:45:43 +00:00
|
|
|
#else
|
2006-07-10 21:04:37 +00:00
|
|
|
if (hires_timer_available) {
|
|
|
|
QueryPerformanceCounter(&hires_now);
|
|
|
|
|
|
|
|
hires_now.QuadPart -= hires_start_ticks.QuadPart;
|
|
|
|
hires_now.QuadPart *= 1000;
|
|
|
|
hires_now.QuadPart /= hires_ticks_per_second.QuadPart;
|
|
|
|
|
|
|
|
return (DWORD) hires_now.QuadPart;
|
|
|
|
} else {
|
|
|
|
now = timeGetTime();
|
|
|
|
}
|
2001-04-26 16:45:43 +00:00
|
|
|
#endif
|
2001-07-07 08:03:34 +00:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (now < start) {
|
|
|
|
ticks = (TIME_WRAP_VALUE - start) + now;
|
|
|
|
} else {
|
|
|
|
ticks = (now - start);
|
|
|
|
}
|
|
|
|
return (ticks);
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
void
|
|
|
|
SDL_Delay(Uint32 ms)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
Sleep(ms);
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Data to handle a single periodic alarm */
|
|
|
|
static UINT timerID = 0;
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
static void CALLBACK
|
|
|
|
HandleAlarm(UINT uID, UINT uMsg, DWORD_PTR dwUser,
|
|
|
|
DWORD_PTR dw1, DWORD_PTR dw2)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
SDL_ThreadedTimerCheck();
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
SDL_SYS_TimerInit(void)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
MMRESULT result;
|
|
|
|
|
|
|
|
/* Set timer resolution */
|
|
|
|
result = timeBeginPeriod(TIMER_RESOLUTION);
|
|
|
|
if (result != TIMERR_NOERROR) {
|
|
|
|
SDL_SetError("Warning: Can't set %d ms timer resolution",
|
|
|
|
TIMER_RESOLUTION);
|
|
|
|
}
|
|
|
|
/* Allow 10 ms of drift so we don't chew on CPU */
|
|
|
|
timerID =
|
|
|
|
timeSetEvent(TIMER_RESOLUTION, 1, HandleAlarm, 0, TIME_PERIODIC);
|
|
|
|
if (!timerID) {
|
|
|
|
SDL_SetError("timeSetEvent() failed");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (SDL_SetTimerThreaded(1));
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
void
|
|
|
|
SDL_SYS_TimerQuit(void)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
if (timerID) {
|
|
|
|
timeKillEvent(timerID);
|
|
|
|
}
|
|
|
|
timeEndPeriod(TIMER_RESOLUTION);
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
SDL_SYS_StartTimer(void)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
SDL_SetError("Internal logic error: Win32 uses threaded timer");
|
|
|
|
return (-1);
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
void
|
|
|
|
SDL_SYS_StopTimer(void)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
return;
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2011-01-20 18:04:05 -08:00
|
|
|
#endif /* SDL_TIMER_WINDOWS */
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|