src/timer/wince/SDL_systimer.c
author Sam Lantinga
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 1180 bdcb8bb4c831
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
icculus@1180
     1
/*
icculus@1180
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
icculus@1180
     4
icculus@1180
     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
icculus@1180
     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.
icculus@1180
     9
icculus@1180
    10
    This library is distributed in the hope that it will be useful,
icculus@1180
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1180
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
icculus@1180
    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
icculus@1180
    18
icculus@1180
    19
    Sam Lantinga
icculus@1180
    20
    slouken@libsdl.org
icculus@1180
    21
*/
icculus@1180
    22
icculus@1180
    23
#include <windows.h>
icculus@1180
    24
#include <mmsystem.h>
icculus@1180
    25
icculus@1180
    26
#include "SDL_timer.h"
icculus@1180
    27
#include "SDL_timer_c.h"
icculus@1180
    28
#include "SDL_error.h"
icculus@1180
    29
icculus@1180
    30
static Uint64 start_date;
icculus@1180
    31
static Uint64 start_ticks;
icculus@1180
    32
icculus@1180
    33
static Uint64 wce_ticks(void)
icculus@1180
    34
{
icculus@1180
    35
  return((Uint64)GetTickCount());
icculus@1180
    36
}
icculus@1180
    37
icculus@1180
    38
static Uint64 wce_date(void)
icculus@1180
    39
{
icculus@1180
    40
  union
icculus@1180
    41
  {
icculus@1180
    42
	FILETIME ftime;
icculus@1180
    43
	Uint64 itime;
icculus@1180
    44
  } ftime;
icculus@1180
    45
  SYSTEMTIME stime;
icculus@1180
    46
icculus@1180
    47
  GetSystemTime(&stime);
icculus@1180
    48
  SystemTimeToFileTime(&stime,&ftime.ftime);
icculus@1180
    49
  ftime.itime/=10000; // Convert 100ns intervals to 1ms intervals
icculus@1180
    50
  // Remove ms portion, which can't be relied on
icculus@1180
    51
  ftime.itime -= (ftime.itime % 1000);
icculus@1180
    52
  return(ftime.itime);
icculus@1180
    53
}
icculus@1180
    54
icculus@1180
    55
static Sint32 wce_rel_ticks(void)
icculus@1180
    56
{
icculus@1180
    57
  return((Sint32)(wce_ticks()-start_ticks));
icculus@1180
    58
}
icculus@1180
    59
icculus@1180
    60
static Sint32 wce_rel_date(void)
icculus@1180
    61
{
icculus@1180
    62
  return((Sint32)(wce_date()-start_date));
icculus@1180
    63
}
icculus@1180
    64
icculus@1180
    65
/* Return time in ms relative to when SDL was started */
icculus@1180
    66
Uint32 SDL_GetTicks()
icculus@1180
    67
{
icculus@1180
    68
  Sint32 offset=wce_rel_date()-wce_rel_ticks();
icculus@1180
    69
  if((offset < -1000) || (offset > 1000))
icculus@1180
    70
  {
icculus@1180
    71
//    fprintf(stderr,"Time desync(%+d), resyncing\n",offset/1000);
icculus@1180
    72
	start_ticks-=offset;
icculus@1180
    73
  }
icculus@1180
    74
icculus@1180
    75
  return((Uint32)wce_rel_ticks());
icculus@1180
    76
}
icculus@1180
    77
icculus@1180
    78
/* Give up approx. givem milliseconds to the OS. */
icculus@1180
    79
void SDL_Delay(Uint32 ms)
icculus@1180
    80
{
icculus@1180
    81
  Sleep(ms);
icculus@1180
    82
}
icculus@1180
    83
icculus@1180
    84
/* Recard start-time of application for reference */
icculus@1180
    85
void SDL_StartTicks(void)
icculus@1180
    86
{
icculus@1180
    87
  start_date=wce_date();
icculus@1180
    88
  start_ticks=wce_ticks();
icculus@1180
    89
}
icculus@1180
    90
icculus@1180
    91
static UINT WIN_timer;
icculus@1180
    92
icculus@1180
    93
int SDL_SYS_TimerInit(void)
icculus@1180
    94
{
icculus@1180
    95
	return(0);
icculus@1180
    96
}
icculus@1180
    97
icculus@1180
    98
void SDL_SYS_TimerQuit(void)
icculus@1180
    99
{
icculus@1180
   100
	return;
icculus@1180
   101
}
icculus@1180
   102
icculus@1180
   103
/* Forward declaration because this is called by the timer callback */
icculus@1180
   104
int SDL_SYS_StartTimer(void);
icculus@1180
   105
icculus@1180
   106
static VOID CALLBACK TimerCallbackProc(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
icculus@1180
   107
{
icculus@1180
   108
	Uint32 ms;
icculus@1180
   109
icculus@1180
   110
	ms = SDL_alarm_callback(SDL_alarm_interval);
icculus@1180
   111
	if ( ms != SDL_alarm_interval ) {
icculus@1180
   112
		KillTimer(NULL, idEvent);
icculus@1180
   113
		if ( ms ) {
icculus@1180
   114
			SDL_alarm_interval = ROUND_RESOLUTION(ms);
icculus@1180
   115
			SDL_SYS_StartTimer();
icculus@1180
   116
		} else {
icculus@1180
   117
			SDL_alarm_interval = 0;
icculus@1180
   118
		}
icculus@1180
   119
	}
icculus@1180
   120
}
icculus@1180
   121
icculus@1180
   122
int SDL_SYS_StartTimer(void)
icculus@1180
   123
{
icculus@1180
   124
	int retval;
icculus@1180
   125
icculus@1180
   126
	WIN_timer = SetTimer(NULL, 0, SDL_alarm_interval, TimerCallbackProc);
icculus@1180
   127
	if ( WIN_timer ) {
icculus@1180
   128
		retval = 0;
icculus@1180
   129
	} else {
icculus@1180
   130
		retval = -1;
icculus@1180
   131
	}
icculus@1180
   132
	return retval;
icculus@1180
   133
}
icculus@1180
   134
icculus@1180
   135
void SDL_SYS_StopTimer(void)
icculus@1180
   136
{
icculus@1180
   137
	if ( WIN_timer ) {
icculus@1180
   138
		KillTimer(NULL, WIN_timer);
icculus@1180
   139
		WIN_timer = 0;
icculus@1180
   140
	}
icculus@1180
   141
}
icculus@1180
   142