src/timer/windows/SDL_systimer.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 09 Apr 2015 22:28:37 -0400
changeset 9541 cf8fab52e33b
parent 9222 f4ef6e5fb804
child 9566 7454bfce9202
permissions -rw-r--r--
Merged Alex Szpakowski's iOS-improvement branch to default.

Fixes Bugzilla #2798.
Fixes Bugzilla #2212.
Fixes Bugzilla #2826.
Fixes Bugzilla #2661.
Fixes Bugzilla #1885.
Fixes Bugzilla #1578.
Fixes Bugzilla #2751.

(whew!)

Notable changes, from Alex's notes:

- The SDL_WINDOW_ALLOW_HIGHDPI flag is now needed (along with SDL_GL_GetDrawableSize or SDL_GetRendererOutputSize) to use Retina / high DPI resolutions, bringing SDL’s Retina-related behavior on iOS in line with Mac OS X. Window dimensions and display modes are now in the “points” (non-high DPI) coordinate system rather than pixels, whereas SDL_GL_GetDrawableSize is in pixels.

- Reworked the custom extended launch screen code:
- It now hides after the first SDL_PumpEvents call rather than SDL_CreateWindow, and it fades out in a similar manner to the system launch screen behavior.
- It now mirrors the system launch screen behavior when deciding which image to display: it falls back to using the Launch Images dictionary in Info.plist if the iOS 8+ launch screen nib isn’t available, and if the Launch Images dictionary doesn’t exist it uses the old standard launch image names.
- The extended launch screen can now be disabled via the SDL_IPHONE_LAUNCHSCREEN define in SDL_config_iphoneos.h.

- Added support for SDL_HINT_ACCELEROMETER_AS_JOYSTICK.

- Added access to a window view's renderbuffer and framebuffer to syswm.

- Added OpenGL ES debug labels for the Renderbuffer and Framebuffer Objects created with SDL_GL_CreateContext.

- Added support for sRGB OpenGL ES contexts on iOS 7+.

- Updated OpenGL ES contexts to support native-resolution rendering (when SDL_WINDOW_ALLOW_HIGHDPI is enabled) on the iPhone 6 Plus, i.e. 1080x1920 rather than 1242x2208.

- Updated SDL_GL_CreateContext, SDL_GL_SwapWindow, SDL_GL_MakeCurrent, and SDL_GL_DeleteContext to be more robust.

- Updated SDL windows to display a UIView at all times, even when an OpenGL context is not active. This allows rotation, touch events, and other windowing-related events to work properly without an active OpenGL context. It also makes it easier to use SDL_GetWindowWMInfo after creating a SDL window.

- Updated the iOS-specific Objective-C code to use cleaner and more modern language features and APIs, including ARC instead of manual reference counting.

- Updated SDL_HINT_ORIENTATIONS to allow disabling custom orientations if the hint is set with no valid orientation names.

- Fixed several rotation and orientation bugs with windows and display modes, especially in iOS 8+.

- Fixed SDL_SetWindowFullscreen failing to update the status bar visibility on iOS 7+.

- Fixed the orientation of the offset applied to the window’s view when the onscreen keyboard is shown in iOS 8+.

- Fixed SDL_IsScreenKeyboardShown (patch by Phil Hassey.)

- Fixed several major memory leaks caused by missing autorelease pool blocks in the iOS-specific Objective-C code.

- Removed several dead code paths.

- The iOS 7 SDK (Xcode 5) or newer is now required to build SDL for iOS.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 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_internal.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 #include "SDL_hints.h"
    30 
    31 
    32 /* The first (low-resolution) ticks value of the application */
    33 static DWORD start;
    34 static BOOL ticks_started = FALSE; 
    35 
    36 #ifndef USE_GETTICKCOUNT
    37 /* Store if a high-resolution performance counter exists on the system */
    38 static BOOL hires_timer_available;
    39 /* The first high-resolution ticks value of the application */
    40 static LARGE_INTEGER hires_start_ticks;
    41 /* The number of ticks per second of the high-resolution performance counter */
    42 static LARGE_INTEGER hires_ticks_per_second;
    43 
    44 #ifndef __WINRT__
    45 static void
    46 timeSetPeriod(UINT uPeriod)
    47 {
    48     static UINT timer_period = 0;
    49 
    50     if (uPeriod != timer_period) {
    51         if (timer_period) {
    52             timeEndPeriod(timer_period);
    53         }
    54 
    55         timer_period = uPeriod;
    56 
    57         if (timer_period) {
    58             timeBeginPeriod(timer_period);
    59         }
    60     }
    61 }
    62 
    63 static void
    64 SDL_TimerResolutionChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
    65 {
    66     UINT uPeriod;
    67 
    68     /* Unless the hint says otherwise, let's have good sleep precision */
    69     if (hint && *hint) {
    70         uPeriod = SDL_atoi(hint);
    71     } else {
    72         uPeriod = 1;
    73     }
    74     if (uPeriod || oldValue != hint) {
    75         timeSetPeriod(uPeriod);
    76     }
    77 }
    78 #endif /* ifndef __WINRT__ */
    79 
    80 #endif /* !USE_GETTICKCOUNT */
    81 
    82 void
    83 SDL_TicksInit(void)
    84 {
    85     if (ticks_started) {
    86         return;
    87     }
    88     ticks_started = SDL_TRUE;
    89 
    90     /* Set first ticks value */
    91 #ifdef USE_GETTICKCOUNT
    92     start = GetTickCount();
    93 #else
    94     /* QueryPerformanceCounter has had problems in the past, but lots of games
    95        use it, so we'll rely on it here.
    96      */
    97     if (QueryPerformanceFrequency(&hires_ticks_per_second) == TRUE) {
    98         hires_timer_available = TRUE;
    99         QueryPerformanceCounter(&hires_start_ticks);
   100     } else {
   101         hires_timer_available = FALSE;
   102 #ifdef __WINRT__
   103         start = 0;            /* the timer failed to start! */
   104 #else
   105         timeSetPeriod(1);     /* use 1 ms timer precision */
   106         start = timeGetTime();
   107 
   108         SDL_AddHintCallback(SDL_HINT_TIMER_RESOLUTION,
   109                             SDL_TimerResolutionChanged, NULL);
   110 #endif /* __WINRT__ */
   111     }
   112 #endif /* USE_GETTICKCOUNT */
   113 }
   114 
   115 void
   116 SDL_TicksQuit(void)
   117 {
   118 #ifndef USE_GETTICKCOUNT
   119     if (!hires_timer_available) {
   120 #ifndef __WINRT__
   121         SDL_DelHintCallback(SDL_HINT_TIMER_RESOLUTION,
   122                             SDL_TimerResolutionChanged, NULL);
   123 
   124         timeSetPeriod(0);
   125 #endif /* __WINRT__ */
   126     }
   127 #endif /* USE_GETTICKCOUNT */
   128 
   129     ticks_started = SDL_FALSE;
   130 }
   131 
   132 Uint32
   133 SDL_GetTicks(void)
   134 {
   135     DWORD now;
   136 #ifndef USE_GETTICKCOUNT
   137     LARGE_INTEGER hires_now;
   138 #endif
   139 
   140     if (!ticks_started) {
   141         SDL_TicksInit();
   142     }
   143 
   144 #ifdef USE_GETTICKCOUNT
   145     now = GetTickCount();
   146 #else
   147     if (hires_timer_available) {
   148         QueryPerformanceCounter(&hires_now);
   149 
   150         hires_now.QuadPart -= hires_start_ticks.QuadPart;
   151         hires_now.QuadPart *= 1000;
   152         hires_now.QuadPart /= hires_ticks_per_second.QuadPart;
   153 
   154         return (DWORD) hires_now.QuadPart;
   155     } else {
   156 #ifdef __WINRT__
   157         now = 0;
   158 #else
   159         now = timeGetTime();
   160 #endif /* __WINRT__ */
   161     }
   162 #endif
   163 
   164     return (now - start);
   165 }
   166 
   167 Uint64
   168 SDL_GetPerformanceCounter(void)
   169 {
   170     LARGE_INTEGER counter;
   171 
   172     if (!QueryPerformanceCounter(&counter)) {
   173         return SDL_GetTicks();
   174     }
   175     return counter.QuadPart;
   176 }
   177 
   178 Uint64
   179 SDL_GetPerformanceFrequency(void)
   180 {
   181     LARGE_INTEGER frequency;
   182 
   183     if (!QueryPerformanceFrequency(&frequency)) {
   184         return 1000;
   185     }
   186     return frequency.QuadPart;
   187 }
   188 
   189 /* Sleep() is not publicly available to apps in early versions of WinRT.
   190  *
   191  * Visual C++ 2013 Update 4 re-introduced Sleep() for Windows 8.1 and
   192  * Windows Phone 8.1.
   193  *
   194  * Use the compiler version to determine availability.
   195  *
   196  * NOTE #1: _MSC_FULL_VER == 180030723 for Visual C++ 2013 Update 3.
   197  * NOTE #2: Visual C++ 2013, when compiling for Windows 8.0 and
   198  *    Windows Phone 8.0, uses the Visual C++ 2012 compiler to build
   199  *    apps and libraries.
   200  */
   201 #if defined(__WINRT__) && defined(_MSC_FULL_VER) && (_MSC_FULL_VER <= 180030723)
   202 static void
   203 Sleep(DWORD timeout)
   204 {
   205     static HANDLE mutex = 0;
   206     if ( ! mutex )
   207     {
   208         mutex = CreateEventEx(0, 0, 0, EVENT_ALL_ACCESS);
   209     }
   210     WaitForSingleObjectEx(mutex, timeout, FALSE);
   211 }
   212 #endif
   213 
   214 void
   215 SDL_Delay(Uint32 ms)
   216 {
   217     Sleep(ms);
   218 }
   219 
   220 #endif /* SDL_TIMER_WINDOWS */
   221 
   222 /* vi: set ts=4 sw=4 expandtab: */