src/timer/windows/SDL_systimer.c
author David Ludwig <dludwig@pobox.com>
Sat, 15 Nov 2014 10:12:36 -0500
changeset 9222 f4ef6e5fb804
parent 8583 fb2933ca805f
permissions -rw-r--r--
WinRT: build fix for Visual C++ 2013 Update 4 Visual C++ 2013 Update 4 re-introduced the Sleep() function to WinRT apps (for code that targets Windows 8.1 and Windows Phone 8.1). This led to a build error, as SDL was defining it's own Sleep() function (to make up for the lack of a public Sleep() function). The fix makes sure that SDL's custom Sleep() function is only used when Windows' Sleep() is not available. Many thanks go out to Sergiu Marian Gaina for the fix!
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
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
}
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
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
    90
    /* Set first ticks value */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    91
#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
    92
    start = GetTickCount();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    93
#else
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7136
diff changeset
    94
    /* 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
    95
       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
    96
     */
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
    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
    98
        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
    99
        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
   100
    } 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
   101
        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
   102
#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
   103
        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
   104
#else
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   105
        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
   106
        start = timeGetTime();
8268
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   107
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   108
        SDL_AddHintCallback(SDL_HINT_TIMER_RESOLUTION,
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   109
                            SDL_TimerResolutionChanged, NULL);
8583
fb2933ca805f Integrated David Ludwig's support for Windows RT
Sam Lantinga <slouken@libsdl.org>
parents: 8268 8582
diff changeset
   110
#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
   111
    }
8583
fb2933ca805f Integrated David Ludwig's support for Windows RT
Sam Lantinga <slouken@libsdl.org>
parents: 8268 8582
diff changeset
   112
#endif /* USE_GETTICKCOUNT */
8268
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   113
}
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   114
8268
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   115
void
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   116
SDL_TicksQuit(void)
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   117
{
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   118
#ifndef USE_GETTICKCOUNT
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   119
    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
   120
#ifndef __WINRT__
8268
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   121
        SDL_DelHintCallback(SDL_HINT_TIMER_RESOLUTION,
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   122
                            SDL_TimerResolutionChanged, NULL);
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   123
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   124
        timeSetPeriod(0);
8583
fb2933ca805f Integrated David Ludwig's support for Windows RT
Sam Lantinga <slouken@libsdl.org>
parents: 8268 8582
diff changeset
   125
#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
   126
    }
8583
fb2933ca805f Integrated David Ludwig's support for Windows RT
Sam Lantinga <slouken@libsdl.org>
parents: 8268 8582
diff changeset
   127
#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
   128
8268
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   129
    ticks_started = SDL_FALSE;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   130
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   131
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
   132
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
   133
SDL_GetTicks(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   134
{
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   135
    DWORD now;
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   136
#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
   137
    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
   138
#endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   139
7652
cf8970659129 Patched to compile with Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 7650
diff changeset
   140
    if (!ticks_started) {
8268
3f8410f20405 Fixed bug 2423 - timeBeginPeriod & timeEndPeriod mismatch
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   141
        SDL_TicksInit();
7652
cf8970659129 Patched to compile with Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 7650
diff changeset
   142
    }
cf8970659129 Patched to compile with Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 7650
diff changeset
   143
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   144
#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
   145
    now = GetTickCount();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   146
#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
   147
    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
   148
        QueryPerformanceCounter(&hires_now);
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   149
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
        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
   151
        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
   152
        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
   153
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
   154
        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
   155
    } 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
   156
#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
   157
        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
   158
#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
   159
        now = timeGetTime();
8583
fb2933ca805f Integrated David Ludwig's support for Windows RT
Sam Lantinga <slouken@libsdl.org>
parents: 8268 8582
diff changeset
   160
#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
   161
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   162
#endif
89
69b8fac3e1c0 Added Holger Schemel's fix for SDL_GetTicks() on W2K
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   163
7432
58908b180ebd Added a hint to control the Windows timer resolution: SDL_HINT_TIMER_RESOLUTION
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   164
    return (now - start);
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
5514
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   167
Uint64
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   168
SDL_GetPerformanceCounter(void)
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   169
{
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   170
    LARGE_INTEGER counter;
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   171
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   172
    if (!QueryPerformanceCounter(&counter)) {
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   173
        return SDL_GetTicks();
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
    return counter.QuadPart;
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   176
}
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
Uint64
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   179
SDL_GetPerformanceFrequency(void)
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
    LARGE_INTEGER frequency;
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   182
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   183
    if (!QueryPerformanceFrequency(&frequency)) {
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   184
        return 1000;
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
    return frequency.QuadPart;
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   187
}
6bd701987ba9 Added high resolution timing API: SDL_GetPerformanceCounter(), SDL_GetPerformanceFrequency()
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   188
9222
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   189
/* 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
   190
 *
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   191
 * 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
   192
 * Windows Phone 8.1.
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
 * 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
   195
 *
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   196
 * 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
   197
 * 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
   198
 *    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
   199
 *    apps and libraries.
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   200
 */
f4ef6e5fb804 WinRT: build fix for Visual C++ 2013 Update 4
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   201
#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
   202
static void
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   203
Sleep(DWORD timeout)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   204
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   205
    static HANDLE mutex = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   206
    if ( ! mutex )
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
        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
   209
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   210
    WaitForSingleObjectEx(mutex, timeout, FALSE);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8535
diff changeset
   211
}
8478
337b5dc0797b WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0
David Ludwig <dludwig@pobox.com>
parents: 8477
diff changeset
   212
#endif
337b5dc0797b WinRT: build fixes and additional WinRT-related integrations with SDL 2.0.0
David Ludwig <dludwig@pobox.com>
parents: 8477
diff changeset
   213
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
   214
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
   215
SDL_Delay(Uint32 ms)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
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
    Sleep(ms);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   218
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   219
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   220
#endif /* SDL_TIMER_WINDOWS */
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
   221
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
   222
/* vi: set ts=4 sw=4 expandtab: */