src/timer/win32/SDL_systimer.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 07 Mar 2006 05:21:32 +0000
changeset 1480 0a2bd6507477
parent 1433 bb6839704ed6
child 1497 420b3f47806d
permissions -rw-r--r--
More Win64 updates
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@1433
    24
#define WIN32_LEAN_AND_MEAN
slouken@1433
    25
#include <windows.h>
slouken@0
    26
#include <mmsystem.h>
slouken@0
    27
slouken@0
    28
#include "SDL_timer.h"
slouken@1361
    29
#include "../SDL_timer_c.h"
slouken@0
    30
slouken@0
    31
#ifdef _WIN32_WCE
icculus@1180
    32
  #error This is WinCE. Please use src/timer/wince/SDL_systimer.c instead.
icculus@1180
    33
icculus@1180
    34
  /* but if you really want to use this file, use these #defines... */
icculus@1180
    35
  #define USE_GETTICKCOUNT
icculus@1180
    36
  #define USE_SETTIMER
slouken@0
    37
#endif
slouken@0
    38
slouken@0
    39
#define TIME_WRAP_VALUE	(~(DWORD)0)
slouken@0
    40
slouken@89
    41
/* The first (low-resolution) ticks value of the application */
slouken@0
    42
static DWORD start;
slouken@0
    43
slouken@89
    44
#ifndef USE_GETTICKCOUNT
slouken@89
    45
/* Store if a high-resolution performance counter exists on the system */
slouken@89
    46
static BOOL hires_timer_available;
slouken@89
    47
/* The first high-resolution ticks value of the application */
slouken@89
    48
static LARGE_INTEGER hires_start_ticks;
slouken@89
    49
/* The number of ticks per second of the high-resolution performance counter */
slouken@89
    50
static LARGE_INTEGER hires_ticks_per_second;
slouken@89
    51
#endif
slouken@89
    52
slouken@0
    53
void SDL_StartTicks(void)
slouken@0
    54
{
slouken@0
    55
	/* Set first ticks value */
slouken@0
    56
#ifdef USE_GETTICKCOUNT
slouken@0
    57
	start = GetTickCount();
slouken@0
    58
#else
slouken@326
    59
#if 0 /* Apparently there are problems with QPC on Win2K */
slouken@89
    60
	if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE)
slouken@89
    61
	{
slouken@89
    62
		hires_timer_available = TRUE;
slouken@89
    63
		QueryPerformanceCounter(&hires_start_ticks);
slouken@89
    64
	}
slouken@89
    65
	else
slouken@326
    66
#endif
slouken@89
    67
	{
slouken@89
    68
		hires_timer_available = FALSE;
slouken@89
    69
		timeBeginPeriod(1);		/* use 1 ms timer precision */
slouken@89
    70
		start = timeGetTime();
slouken@89
    71
	}
slouken@0
    72
#endif
slouken@0
    73
}
slouken@0
    74
slouken@0
    75
Uint32 SDL_GetTicks(void)
slouken@0
    76
{
slouken@0
    77
	DWORD now, ticks;
slouken@89
    78
#ifndef USE_GETTICKCOUNT
slouken@89
    79
	LARGE_INTEGER hires_now;
slouken@89
    80
#endif
slouken@0
    81
slouken@0
    82
#ifdef USE_GETTICKCOUNT
slouken@0
    83
	now = GetTickCount();
slouken@0
    84
#else
slouken@89
    85
	if (hires_timer_available)
slouken@89
    86
	{
slouken@89
    87
		QueryPerformanceCounter(&hires_now);
slouken@89
    88
slouken@89
    89
		hires_now.QuadPart -= hires_start_ticks.QuadPart;
slouken@89
    90
		hires_now.QuadPart *= 1000;
slouken@89
    91
		hires_now.QuadPart /= hires_ticks_per_second.QuadPart;
slouken@89
    92
slouken@89
    93
		return (DWORD)hires_now.QuadPart;
slouken@89
    94
	}
slouken@89
    95
	else
slouken@89
    96
	{
slouken@89
    97
		now = timeGetTime();
slouken@89
    98
	}
slouken@0
    99
#endif
slouken@89
   100
slouken@0
   101
	if ( now < start ) {
slouken@0
   102
		ticks = (TIME_WRAP_VALUE-start) + now;
slouken@0
   103
	} else {
slouken@0
   104
		ticks = (now - start);
slouken@0
   105
	}
slouken@0
   106
	return(ticks);
slouken@0
   107
}
slouken@0
   108
slouken@0
   109
void SDL_Delay(Uint32 ms)
slouken@0
   110
{
slouken@0
   111
	Sleep(ms);
slouken@0
   112
}
slouken@0
   113
slouken@0
   114
#ifdef USE_SETTIMER
slouken@0
   115
slouken@0
   116
static UINT WIN_timer;
slouken@0
   117
slouken@0
   118
int SDL_SYS_TimerInit(void)
slouken@0
   119
{
slouken@0
   120
	return(0);
slouken@0
   121
}
slouken@0
   122
slouken@0
   123
void SDL_SYS_TimerQuit(void)
slouken@0
   124
{
slouken@0
   125
	return;
slouken@0
   126
}
slouken@0
   127
slouken@0
   128
/* Forward declaration because this is called by the timer callback */
slouken@0
   129
int SDL_SYS_StartTimer(void);
slouken@0
   130
slouken@1480
   131
static VOID CALLBACK TimerCallbackProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
slouken@0
   132
{
slouken@0
   133
	Uint32 ms;
slouken@0
   134
slouken@0
   135
	ms = SDL_alarm_callback(SDL_alarm_interval);
slouken@0
   136
	if ( ms != SDL_alarm_interval ) {
slouken@0
   137
		KillTimer(NULL, idEvent);
slouken@0
   138
		if ( ms ) {
slouken@0
   139
			SDL_alarm_interval = ROUND_RESOLUTION(ms);
slouken@0
   140
			SDL_SYS_StartTimer();
slouken@0
   141
		} else {
slouken@0
   142
			SDL_alarm_interval = 0;
slouken@0
   143
		}
slouken@0
   144
	}
slouken@0
   145
}
slouken@0
   146
slouken@0
   147
int SDL_SYS_StartTimer(void)
slouken@0
   148
{
slouken@0
   149
	int retval;
slouken@0
   150
slouken@0
   151
	WIN_timer = SetTimer(NULL, 0, SDL_alarm_interval, TimerCallbackProc);
slouken@0
   152
	if ( WIN_timer ) {
slouken@0
   153
		retval = 0;
slouken@0
   154
	} else {
slouken@0
   155
		retval = -1;
slouken@0
   156
	}
slouken@0
   157
	return retval;
slouken@0
   158
}
slouken@0
   159
slouken@0
   160
void SDL_SYS_StopTimer(void)
slouken@0
   161
{
slouken@0
   162
	if ( WIN_timer ) {
slouken@0
   163
		KillTimer(NULL, WIN_timer);
slouken@0
   164
		WIN_timer = 0;
slouken@0
   165
	}
slouken@0
   166
}
slouken@0
   167
slouken@0
   168
#else /* !USE_SETTIMER */
slouken@0
   169
slouken@0
   170
/* Data to handle a single periodic alarm */
slouken@0
   171
static UINT timerID = 0;
slouken@0
   172
slouken@1480
   173
static void CALLBACK HandleAlarm(UINT uID,  UINT uMsg, DWORD_PTR dwUser,
slouken@1480
   174
						DWORD_PTR dw1, DWORD_PTR dw2)
slouken@0
   175
{
slouken@0
   176
	SDL_ThreadedTimerCheck();
slouken@0
   177
}
slouken@0
   178
slouken@0
   179
slouken@0
   180
int SDL_SYS_TimerInit(void)
slouken@0
   181
{
slouken@0
   182
	MMRESULT result;
slouken@0
   183
slouken@0
   184
	/* Set timer resolution */
slouken@0
   185
	result = timeBeginPeriod(TIMER_RESOLUTION);
slouken@0
   186
	if ( result != TIMERR_NOERROR ) {
slouken@0
   187
		SDL_SetError("Warning: Can't set %d ms timer resolution",
slouken@0
   188
							TIMER_RESOLUTION);
slouken@0
   189
	}
slouken@0
   190
	/* Allow 10 ms of drift so we don't chew on CPU */
slouken@0
   191
	timerID = timeSetEvent(TIMER_RESOLUTION,1,HandleAlarm,0,TIME_PERIODIC);
slouken@0
   192
	if ( ! timerID ) {
slouken@0
   193
		SDL_SetError("timeSetEvent() failed");
slouken@0
   194
		return(-1);
slouken@0
   195
	}
slouken@0
   196
	return(SDL_SetTimerThreaded(1));
slouken@0
   197
}
slouken@0
   198
slouken@0
   199
void SDL_SYS_TimerQuit(void)
slouken@0
   200
{
slouken@0
   201
	if ( timerID ) {
slouken@0
   202
		timeKillEvent(timerID);
slouken@0
   203
	}
slouken@0
   204
	timeEndPeriod(TIMER_RESOLUTION);
slouken@0
   205
}
slouken@0
   206
slouken@0
   207
int SDL_SYS_StartTimer(void)
slouken@0
   208
{
slouken@0
   209
	SDL_SetError("Internal logic error: Win32 uses threaded timer");
slouken@0
   210
	return(-1);
slouken@0
   211
}
slouken@0
   212
slouken@0
   213
void SDL_SYS_StopTimer(void)
slouken@0
   214
{
slouken@0
   215
	return;
slouken@0
   216
}
slouken@0
   217
slouken@0
   218
#endif /* USE_SETTIMER */