src/timer/windows/SDL_systimer.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 09 Apr 2012 23:55:43 -0400
changeset 6305 601b0e251822
parent 6138 4c64952a58fb
child 6430 48d519500f7e
permissions -rwxr-xr-x
SDL_ExitProcess() was ignoring exit code parameter.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@5062
    23
#ifdef SDL_TIMER_WINDOWS
slouken@1635
    24
slouken@5090
    25
#include "../../core/windows/SDL_windows.h"
slouken@5114
    26
#include <mmsystem.h>
slouken@0
    27
slouken@0
    28
#include "SDL_timer.h"
slouken@0
    29
slouken@0
    30
#ifdef _WIN32_WCE
slouken@1895
    31
#error This is WinCE. Please use src/timer/wince/SDL_systimer.c instead.
slouken@0
    32
#endif
slouken@0
    33
slouken@0
    34
#define TIME_WRAP_VALUE	(~(DWORD)0)
slouken@0
    35
slouken@89
    36
/* The first (low-resolution) ticks value of the application */
slouken@0
    37
static DWORD start;
slouken@0
    38
slouken@89
    39
#ifndef USE_GETTICKCOUNT
slouken@89
    40
/* Store if a high-resolution performance counter exists on the system */
slouken@89
    41
static BOOL hires_timer_available;
slouken@89
    42
/* The first high-resolution ticks value of the application */
slouken@89
    43
static LARGE_INTEGER hires_start_ticks;
slouken@89
    44
/* The number of ticks per second of the high-resolution performance counter */
slouken@89
    45
static LARGE_INTEGER hires_ticks_per_second;
slouken@89
    46
#endif
slouken@89
    47
slouken@1895
    48
void
slouken@1895
    49
SDL_StartTicks(void)
slouken@0
    50
{
slouken@1895
    51
    /* Set first ticks value */
slouken@0
    52
#ifdef USE_GETTICKCOUNT
slouken@1895
    53
    start = GetTickCount();
slouken@0
    54
#else
slouken@1895
    55
#if 0                           /* Apparently there are problems with QPC on Win2K */
slouken@1895
    56
    if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE) {
slouken@1895
    57
        hires_timer_available = TRUE;
slouken@1895
    58
        QueryPerformanceCounter(&hires_start_ticks);
slouken@1895
    59
    } else
slouken@326
    60
#endif
slouken@1895
    61
    {
slouken@1895
    62
        hires_timer_available = FALSE;
slouken@1895
    63
        timeBeginPeriod(1);     /* use 1 ms timer precision */
slouken@1895
    64
        start = timeGetTime();
slouken@1895
    65
    }
slouken@0
    66
#endif
slouken@0
    67
}
slouken@0
    68
slouken@1895
    69
Uint32
slouken@1895
    70
SDL_GetTicks(void)
slouken@0
    71
{
slouken@1895
    72
    DWORD now, ticks;
slouken@89
    73
#ifndef USE_GETTICKCOUNT
slouken@1895
    74
    LARGE_INTEGER hires_now;
slouken@89
    75
#endif
slouken@0
    76
slouken@0
    77
#ifdef USE_GETTICKCOUNT
slouken@1895
    78
    now = GetTickCount();
slouken@0
    79
#else
slouken@1895
    80
    if (hires_timer_available) {
slouken@1895
    81
        QueryPerformanceCounter(&hires_now);
slouken@89
    82
slouken@1895
    83
        hires_now.QuadPart -= hires_start_ticks.QuadPart;
slouken@1895
    84
        hires_now.QuadPart *= 1000;
slouken@1895
    85
        hires_now.QuadPart /= hires_ticks_per_second.QuadPart;
slouken@89
    86
slouken@1895
    87
        return (DWORD) hires_now.QuadPart;
slouken@1895
    88
    } else {
slouken@1895
    89
        now = timeGetTime();
slouken@1895
    90
    }
slouken@0
    91
#endif
slouken@89
    92
slouken@1895
    93
    if (now < start) {
slouken@1895
    94
        ticks = (TIME_WRAP_VALUE - start) + now;
slouken@1895
    95
    } else {
slouken@1895
    96
        ticks = (now - start);
slouken@1895
    97
    }
slouken@1895
    98
    return (ticks);
slouken@0
    99
}
slouken@0
   100
slouken@5514
   101
Uint64
slouken@5514
   102
SDL_GetPerformanceCounter(void)
slouken@5514
   103
{
slouken@5514
   104
    LARGE_INTEGER counter;
slouken@5514
   105
slouken@5514
   106
    if (!QueryPerformanceCounter(&counter)) {
slouken@5514
   107
        return SDL_GetTicks();
slouken@5514
   108
    }
slouken@5514
   109
    return counter.QuadPart;
slouken@5514
   110
}
slouken@5514
   111
slouken@5514
   112
Uint64
slouken@5514
   113
SDL_GetPerformanceFrequency(void)
slouken@5514
   114
{
slouken@5514
   115
    LARGE_INTEGER frequency;
slouken@5514
   116
slouken@5514
   117
    if (!QueryPerformanceFrequency(&frequency)) {
slouken@5514
   118
        return 1000;
slouken@5514
   119
    }
slouken@5514
   120
    return frequency.QuadPart;
slouken@5514
   121
}
slouken@5514
   122
slouken@1895
   123
void
slouken@1895
   124
SDL_Delay(Uint32 ms)
slouken@0
   125
{
slouken@1895
   126
    Sleep(ms);
slouken@0
   127
}
slouken@0
   128
slouken@5062
   129
#endif /* SDL_TIMER_WINDOWS */
slouken@5062
   130
slouken@1895
   131
/* vi: set ts=4 sw=4 expandtab: */