src/timer/windows/SDL_systimer.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 17 Aug 2013 20:46:34 -0400
changeset 7652 cf8970659129
parent 7650 cd3465de2f42
child 8093 b43765095a6f
child 8535 e8ee0708ef5c
permissions -rw-r--r--
Patched to compile with Visual Studio.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5514
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6430
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5514
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5514
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5514
diff changeset
     7
  arising from the use of this software.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5514
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5514
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5514
diff changeset
    11
  freely, subject to the following restrictions:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5514
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5514
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5514
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5514
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5514
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5514
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5514
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
    21
#include "SDL_config.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
    23
#ifdef SDL_TIMER_WINDOWS
1635
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1497
diff changeset
    24
5090
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
    25
#include "../../core/windows/SDL_windows.h"
5114
e337f792c6a7 Fixed compiling under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5111
diff changeset
    26
#include <mmsystem.h>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    28
#include "SDL_timer.h"
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    29
#include "SDL_hints.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    31
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
    32
/* The first (low-resolution) ticks value of the application */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
static DWORD start;
7650
cd3465de2f42 Fixed for consistency with the other platforms
Sam Lantinga <slouken@libsdl.org>
parents: 7649
diff changeset
    34
static BOOL ticks_started = FALSE; 
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    35
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
    36
#ifndef USE_GETTICKCOUNT
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
    37
/* Store if a high-resolution performance counter exists on the system */
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
    38
static BOOL hires_timer_available;
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
    39
/* The first high-resolution ticks value of the application */
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
    40
static LARGE_INTEGER hires_start_ticks;
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
    41
/* The number of ticks per second of the high-resolution performance counter */
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
    42
static LARGE_INTEGER hires_ticks_per_second;
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
    43
#endif
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
    44
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    45
static void
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    46
timeSetPeriod(UINT uPeriod)
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    47
{
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    48
    static UINT timer_period = 0;
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    49
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    50
    if (uPeriod != timer_period) {
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    51
        if (timer_period) {
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    52
            timeEndPeriod(timer_period);
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    53
        }
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    54
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    55
        timer_period = uPeriod;
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    56
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    57
        if (timer_period) {
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    58
            timeBeginPeriod(timer_period);
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    59
        }
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    60
    }
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    61
}
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    62
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    63
static void
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    64
SDL_TimerResolutionChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    65
{
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    66
    UINT uPeriod;
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    67
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    68
    /* Unless the hint says otherwise, let's have good sleep precision */
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    69
    if (hint && *hint) {
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    70
        uPeriod = SDL_atoi(hint);
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    71
    } else {
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    72
        uPeriod = 1;
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    73
    }
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    74
    if (uPeriod || oldValue != hint) {
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    75
        timeSetPeriod(uPeriod);
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    76
    }
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    77
}
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    78
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    79
void
7649
4f801cd08f3f auto init the ticks if the GetTicks and the like methods are called before SDL_Init().. This prevents annoying game bugs such as caching SDL_GetPerformanceFrequency in a static initializer
Edward Rudd <urkle@outoforder.cc>
parents: 7432
diff changeset
    80
SDL_InitTicks(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    81
{
7649
4f801cd08f3f auto init the ticks if the GetTicks and the like methods are called before SDL_Init().. This prevents annoying game bugs such as caching SDL_GetPerformanceFrequency in a static initializer
Edward Rudd <urkle@outoforder.cc>
parents: 7432
diff changeset
    82
    if (ticks_started) {
4f801cd08f3f auto init the ticks if the GetTicks and the like methods are called before SDL_Init().. This prevents annoying game bugs such as caching SDL_GetPerformanceFrequency in a static initializer
Edward Rudd <urkle@outoforder.cc>
parents: 7432
diff changeset
    83
        return;
4f801cd08f3f auto init the ticks if the GetTicks and the like methods are called before SDL_Init().. This prevents annoying game bugs such as caching SDL_GetPerformanceFrequency in a static initializer
Edward Rudd <urkle@outoforder.cc>
parents: 7432
diff changeset
    84
    }
4f801cd08f3f auto init the ticks if the GetTicks and the like methods are called before SDL_Init().. This prevents annoying game bugs such as caching SDL_GetPerformanceFrequency in a static initializer
Edward Rudd <urkle@outoforder.cc>
parents: 7432
diff changeset
    85
    ticks_started = TRUE;
4f801cd08f3f auto init the ticks if the GetTicks and the like methods are called before SDL_Init().. This prevents annoying game bugs such as caching SDL_GetPerformanceFrequency in a static initializer
Edward Rudd <urkle@outoforder.cc>
parents: 7432
diff changeset
    86
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    87
    /* Set first ticks value */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    88
#ifdef USE_GETTICKCOUNT
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    89
    start = GetTickCount();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    90
#else
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7136
diff changeset
    91
    /* QueryPerformanceCounter has had problems in the past, but lots of games
7136
65c709a5da8a QueryPerformanceCounter() is reliable these days, and setting the timer frequency higher can have adverse power consequences.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    92
       use it, so we'll rely on it here.
65c709a5da8a QueryPerformanceCounter() is reliable these days, and setting the timer frequency higher can have adverse power consequences.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    93
     */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    94
    if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    95
        hires_timer_available = TRUE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    96
        QueryPerformanceCounter(&hires_start_ticks);
7136
65c709a5da8a QueryPerformanceCounter() is reliable these days, and setting the timer frequency higher can have adverse power consequences.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    97
    } else {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    98
        hires_timer_available = FALSE;
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    99
        timeSetPeriod(1);     /* use 1 ms timer precision */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   100
        start = timeGetTime();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   101
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   102
#endif
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   103
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   104
    SDL_AddHintCallback(SDL_HINT_TIMER_RESOLUTION,
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   105
                        SDL_TimerResolutionChanged, NULL);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   106
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   107
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   108
Uint32
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   109
SDL_GetTicks(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   110
{
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   111
    DWORD now;
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   112
#ifndef USE_GETTICKCOUNT
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   113
    LARGE_INTEGER hires_now;
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   114
#endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   115
7652
cf8970659129 Patched to compile with Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 7650
diff changeset
   116
    if (!ticks_started) {
cf8970659129 Patched to compile with Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 7650
diff changeset
   117
        SDL_InitTicks();
cf8970659129 Patched to compile with Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 7650
diff changeset
   118
    }
cf8970659129 Patched to compile with Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 7650
diff changeset
   119
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   120
#ifdef USE_GETTICKCOUNT
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   121
    now = GetTickCount();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   122
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   123
    if (hires_timer_available) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   124
        QueryPerformanceCounter(&hires_now);
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   125
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   126
        hires_now.QuadPart -= hires_start_ticks.QuadPart;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   127
        hires_now.QuadPart *= 1000;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   128
        hires_now.QuadPart /= hires_ticks_per_second.QuadPart;
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   129
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   130
        return (DWORD) hires_now.QuadPart;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   131
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   132
        now = timeGetTime();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   133
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   134
#endif
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   135
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   136
    return (now - start);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   137
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   138
5514
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   139
Uint64
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   140
SDL_GetPerformanceCounter(void)
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   141
{
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   142
    LARGE_INTEGER counter;
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   143
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   144
    if (!QueryPerformanceCounter(&counter)) {
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   145
        return SDL_GetTicks();
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   146
    }
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   147
    return counter.QuadPart;
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   148
}
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   149
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   150
Uint64
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   151
SDL_GetPerformanceFrequency(void)
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   152
{
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   153
    LARGE_INTEGER frequency;
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   154
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   155
    if (!QueryPerformanceFrequency(&frequency)) {
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   156
        return 1000;
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   157
    }
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   158
    return frequency.QuadPart;
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   159
}
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   160
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   161
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   162
SDL_Delay(Uint32 ms)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   163
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   164
    Sleep(ms);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   165
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   166
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   167
#endif /* SDL_TIMER_WINDOWS */
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   168
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   169
/* vi: set ts=4 sw=4 expandtab: */