src/timer/windows/SDL_systimer.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 20 Apr 2015 12:22:44 -0400
changeset 9566 7454bfce9202
parent 9222 f4ef6e5fb804
child 9567 b69cac1cbc8f
permissions -rw-r--r--
Windows: Always set the system timer resolution to 1ms by default. An existing hint lets apps that don't need the timer resolution changed avoid this, to save battery, etc, but this fixes several problems in timing, audio callbacks not firing fast enough, etc. Fixes Bugzilla #2944.
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
8149
681eb46b8ac4 Fixed bug 2374 - Update copyright for 2014...
Sam Lantinga <slouken@libsdl.org>
parents: 8093
diff changeset
     3
  Copyright (C) 1997-2014 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
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7652
diff changeset
    21
#include "../../SDL_internal.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
8478
337b5dc0797b WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0
David Ludwig <dludwig@pobox.com>
parents: 8477
diff changeset
    44
#ifndef __WINRT__
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
9566
7454bfce9202 Windows: Always set the system timer resolution to 1ms by default.
Ryan C. Gordon <icculus@icculus.org>
parents: 9222
diff changeset
    46
timeSetPeriod(const UINT uPeriod)
7432
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
}
8478
337b5dc0797b WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0
David Ludwig <dludwig@pobox.com>
parents: 8477
diff changeset
    78
#endif /* ifndef __WINRT__ */
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
    79
8268
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
    80
#endif /* !USE_GETTICKCOUNT */
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
    81
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
    82
void
8268
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
    83
SDL_TicksInit(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    84
{
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
    85
    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
    86
        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
    87
    }
8268
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
    88
    ticks_started = SDL_TRUE;
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
    89
9566
7454bfce9202 Windows: Always set the system timer resolution to 1ms by default.
Ryan C. Gordon <icculus@icculus.org>
parents: 9222
diff changeset
    90
    /* if we didn't set a precision, set it high. This affects lots of things
7454bfce9202 Windows: Always set the system timer resolution to 1ms by default.
Ryan C. Gordon <icculus@icculus.org>
parents: 9222
diff changeset
    91
       on Windows besides the SDL timers, like audio callbacks, etc. */
7454bfce9202 Windows: Always set the system timer resolution to 1ms by default.
Ryan C. Gordon <icculus@icculus.org>
parents: 9222
diff changeset
    92
    SDL_AddHintCallback(SDL_HINT_TIMER_RESOLUTION,
7454bfce9202 Windows: Always set the system timer resolution to 1ms by default.
Ryan C. Gordon <icculus@icculus.org>
parents: 9222
diff changeset
    93
                        SDL_TimerResolutionChanged, NULL);
7454bfce9202 Windows: Always set the system timer resolution to 1ms by default.
Ryan C. Gordon <icculus@icculus.org>
parents: 9222
diff changeset
    94
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
    95
    /* Set first ticks value */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    96
#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
    97
    start = GetTickCount();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    98
#else
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7136
diff changeset
    99
    /* 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
   100
       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
   101
     */
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
   102
    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
   103
        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
   104
        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
   105
    } 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
   106
        hires_timer_available = FALSE;
8478
337b5dc0797b WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0
David Ludwig <dludwig@pobox.com>
parents: 8477
diff changeset
   107
#ifdef __WINRT__
337b5dc0797b WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0
David Ludwig <dludwig@pobox.com>
parents: 8477
diff changeset
   108
        start = 0;            /* the timer failed to start! */
337b5dc0797b WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0
David Ludwig <dludwig@pobox.com>
parents: 8477
diff changeset
   109
#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
   110
        start = timeGetTime();
8583
fb2933ca805f Integrated David Ludwig's support for Windows RT
Sam Lantinga <slouken@libsdl.org>
parents: 8268 8582
diff changeset
   111
#endif /* __WINRT__ */
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
   112
    }
8583
fb2933ca805f Integrated David Ludwig's support for Windows RT
Sam Lantinga <slouken@libsdl.org>
parents: 8268 8582
diff changeset
   113
#endif /* USE_GETTICKCOUNT */
8268
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   114
}
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   115
8268
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   116
void
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   117
SDL_TicksQuit(void)
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   118
{
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   119
#ifndef USE_GETTICKCOUNT
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   120
    if (!hires_timer_available) {
8478
337b5dc0797b WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0
David Ludwig <dludwig@pobox.com>
parents: 8477
diff changeset
   121
#ifndef __WINRT__
8268
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   122
        SDL_DelHintCallback(SDL_HINT_TIMER_RESOLUTION,
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   123
                            SDL_TimerResolutionChanged, NULL);
8583
fb2933ca805f Integrated David Ludwig's support for Windows RT
Sam Lantinga <slouken@libsdl.org>
parents: 8268 8582
diff changeset
   124
#endif /* __WINRT__ */
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
   125
    }
8583
fb2933ca805f Integrated David Ludwig's support for Windows RT
Sam Lantinga <slouken@libsdl.org>
parents: 8268 8582
diff changeset
   126
#endif /* USE_GETTICKCOUNT */
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   127
9566
7454bfce9202 Windows: Always set the system timer resolution to 1ms by default.
Ryan C. Gordon <icculus@icculus.org>
parents: 9222
diff changeset
   128
#ifndef __WINRT__
7454bfce9202 Windows: Always set the system timer resolution to 1ms by default.
Ryan C. Gordon <icculus@icculus.org>
parents: 9222
diff changeset
   129
    timeSetPeriod(0);  /* always release our timer resolution request. */
7454bfce9202 Windows: Always set the system timer resolution to 1ms by default.
Ryan C. Gordon <icculus@icculus.org>
parents: 9222
diff changeset
   130
#endif
7454bfce9202 Windows: Always set the system timer resolution to 1ms by default.
Ryan C. Gordon <icculus@icculus.org>
parents: 9222
diff changeset
   131
8268
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   132
    ticks_started = SDL_FALSE;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   133
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   134
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
   135
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
   136
SDL_GetTicks(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   137
{
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   138
    DWORD now;
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   139
#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
   140
    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
   141
#endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   142
7652
cf8970659129 Patched to compile with Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 7650
diff changeset
   143
    if (!ticks_started) {
8268
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   144
        SDL_TicksInit();
7652
cf8970659129 Patched to compile with Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 7650
diff changeset
   145
    }
cf8970659129 Patched to compile with Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 7650
diff changeset
   146
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   147
#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
   148
    now = GetTickCount();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   149
#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
   150
    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
   151
        QueryPerformanceCounter(&hires_now);
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   152
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
   153
        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
   154
        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
   155
        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
   156
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
   157
        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
   158
    } else {
8478
337b5dc0797b WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0
David Ludwig <dludwig@pobox.com>
parents: 8477
diff changeset
   159
#ifdef __WINRT__
337b5dc0797b WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0
David Ludwig <dludwig@pobox.com>
parents: 8477
diff changeset
   160
        now = 0;
337b5dc0797b WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0
David Ludwig <dludwig@pobox.com>
parents: 8477
diff changeset
   161
#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
   162
        now = timeGetTime();
8583
fb2933ca805f Integrated David Ludwig's support for Windows RT
Sam Lantinga <slouken@libsdl.org>
parents: 8268 8582
diff changeset
   163
#endif /* __WINRT__ */
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
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   165
#endif
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   166
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   167
    return (now - start);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   168
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   169
5514
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   170
Uint64
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   171
SDL_GetPerformanceCounter(void)
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   172
{
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   173
    LARGE_INTEGER counter;
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   174
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   175
    if (!QueryPerformanceCounter(&counter)) {
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   176
        return SDL_GetTicks();
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   177
    }
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   178
    return counter.QuadPart;
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   179
}
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   180
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   181
Uint64
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   182
SDL_GetPerformanceFrequency(void)
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   183
{
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   184
    LARGE_INTEGER frequency;
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   185
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   186
    if (!QueryPerformanceFrequency(&frequency)) {
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   187
        return 1000;
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   188
    }
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   189
    return frequency.QuadPart;
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   190
}
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   191
9222
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   192
/* Sleep() is not publicly available to apps in early versions of WinRT.
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   193
 *
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   194
 * Visual C++ 2013 Update 4 re-introduced Sleep() for Windows 8.1 and
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   195
 * Windows Phone 8.1.
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   196
 *
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   197
 * Use the compiler version to determine availability.
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   198
 *
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   199
 * NOTE #1: _MSC_FULL_VER == 180030723 for Visual C++ 2013 Update 3.
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   200
 * NOTE #2: Visual C++ 2013, when compiling for Windows 8.0 and
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   201
 *    Windows Phone 8.0, uses the Visual C++ 2012 compiler to build
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   202
 *    apps and libraries.
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   203
 */
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   204
#if defined(__WINRT__) && defined(_MSC_FULL_VER) && (_MSC_FULL_VER <= 180030723)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   205
static void
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   206
Sleep(DWORD timeout)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   207
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   208
    static HANDLE mutex = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   209
    if ( ! mutex )
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   210
    {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   211
        mutex = CreateEventEx(0, 0, 0, EVENT_ALL_ACCESS);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   212
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   213
    WaitForSingleObjectEx(mutex, timeout, FALSE);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   214
}
8478
337b5dc0797b WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0
David Ludwig <dludwig@pobox.com>
parents: 8477
diff changeset
   215
#endif
337b5dc0797b WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0
David Ludwig <dludwig@pobox.com>
parents: 8477
diff changeset
   216
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
   217
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
   218
SDL_Delay(Uint32 ms)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   219
{
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
   220
    Sleep(ms);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   221
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   222
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   223
#endif /* SDL_TIMER_WINDOWS */
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   224
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
   225
/* vi: set ts=4 sw=4 expandtab: */