19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL - Simple DirectMedia Layer 39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Copyright (C) 1997-2012 Sam Lantinga 49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is free software; you can redistribute it and/or 69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall modify it under the terms of the GNU Lesser General Public 79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License as published by the Free Software Foundation; either 89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall version 2.1 of the License, or (at your option) any later version. 99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is distributed in the hope that it will be useful, 119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall but WITHOUT ANY WARRANTY; without even the implied warranty of 129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Lesser General Public License for more details. 149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall You should have received a copy of the GNU Lesser General Public 169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License along with this library; if not, write to the Free Software 179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sam Lantinga 209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall slouken@libsdl.org 219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h" 239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_TIMER_WIN32 259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define WIN32_LEAN_AND_MEAN 279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <windows.h> 289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <mmsystem.h> 299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h" 319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_timer_c.h" 329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef _WIN32_WCE 349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall #error This is WinCE. Please use src/timer/wince/SDL_systimer.c instead. 359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define TIME_WRAP_VALUE (~(DWORD)0) 389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The first (low-resolution) ticks value of the application */ 409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic DWORD start; 419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef USE_GETTICKCOUNT 439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Store if a high-resolution performance counter exists on the system */ 449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic BOOL hires_timer_available; 459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The first high-resolution ticks value of the application */ 469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic LARGE_INTEGER hires_start_ticks; 479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The number of ticks per second of the high-resolution performance counter */ 489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic LARGE_INTEGER hires_ticks_per_second; 499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_StartTicks(void) 529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set first ticks value */ 549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_GETTICKCOUNT 559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall start = GetTickCount(); 569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0 /* Apparently there are problems with QPC on Win2K */ 589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE) 599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hires_timer_available = TRUE; 619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall QueryPerformanceCounter(&hires_start_ticks); 629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else 649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hires_timer_available = FALSE; 679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall timeBeginPeriod(1); /* use 1 ms timer precision */ 689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall start = timeGetTime(); 699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint32 SDL_GetTicks(void) 749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DWORD now, ticks; 769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef USE_GETTICKCOUNT 779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall LARGE_INTEGER hires_now; 789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_GETTICKCOUNT 819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall now = GetTickCount(); 829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (hires_timer_available) 849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall QueryPerformanceCounter(&hires_now); 869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hires_now.QuadPart -= hires_start_ticks.QuadPart; 889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hires_now.QuadPart *= 1000; 899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hires_now.QuadPart /= hires_ticks_per_second.QuadPart; 909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (DWORD)hires_now.QuadPart; 929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else 949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall now = timeGetTime(); 969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( now < start ) { 1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ticks = (TIME_WRAP_VALUE-start) + now; 1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ticks = (now - start); 1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(ticks); 1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_Delay(Uint32 ms) 1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sleep(ms); 1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Data to handle a single periodic alarm */ 1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic UINT timerID = 0; 1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void CALLBACK HandleAlarm(UINT uID, UINT uMsg, DWORD_PTR dwUser, 1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall DWORD_PTR dw1, DWORD_PTR dw2) 1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_ThreadedTimerCheck(); 1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_TimerInit(void) 1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MMRESULT result; 1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set timer resolution */ 1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = timeBeginPeriod(TIMER_RESOLUTION); 1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( result != TIMERR_NOERROR ) { 1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Warning: Can't set %d ms timer resolution", 1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall TIMER_RESOLUTION); 1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Allow 10 ms of drift so we don't chew on CPU */ 1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall timerID = timeSetEvent(TIMER_RESOLUTION,1,HandleAlarm,0,TIME_PERIODIC); 1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ! timerID ) { 1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("timeSetEvent() failed"); 1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(SDL_SetTimerThreaded(1)); 1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_TimerQuit(void) 1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( timerID ) { 1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall timeKillEvent(timerID); 1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall timeEndPeriod(TIMER_RESOLUTION); 1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_StartTimer(void) 1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Internal logic error: Win32 uses threaded timer"); 1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_StopTimer(void) 1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_TIMER_WIN32 */ 161