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