src/video/uikit/SDL_uikitviewcontroller.m
author Alex Szpakowski <slime73@gmail.com>
Mon, 14 Jul 2014 22:35:48 -0300
branchiOS-improvements
changeset 9488 f15520430c94
parent 8149 681eb46b8ac4
child 9492 cad5a9995bde
permissions -rw-r--r--
Changed the way retina resolutions are handled in iOS.

Previously, SDL would always expose display modes and window dimensions in terms of pixels, and would add an extra 'fake' display mode on retina screens which would contain the non-retina resolution. Calling SDL_CreateWindow with the dimensions of that fake display mode would not work.

Now, SDL only exposes display modes and window dimensions in terms of points rather than pixels. If the SDL_WINDOW_ALLOW_HIGHDPI flag is passed into SDL_CreateWindow, then any OpenGL contexts created from that window will be sized in pixels rather than points (retrievable with SDL_GL_GetDrawableSize.) Window dimensions and mouse coordinates are still in terms of points rather than pixels even with that flag.

This matches the behavior of SDL in OS X more closely, and lets users choose whether to make use of retina displays and lets them handle it properly.
     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 #if SDL_VIDEO_DRIVER_UIKIT
    24 
    25 #include "SDL_video.h"
    26 #include "SDL_assert.h"
    27 #include "SDL_hints.h"
    28 #include "../SDL_sysvideo.h"
    29 #include "../../events/SDL_events_c.h"
    30 
    31 #include "SDL_uikitviewcontroller.h"
    32 #include "SDL_uikitvideo.h"
    33 #include "SDL_uikitmodes.h"
    34 #include "SDL_uikitwindow.h"
    35 
    36 
    37 @implementation SDL_uikitviewcontroller
    38 
    39 @synthesize window;
    40 
    41 - (id)initWithSDLWindow:(SDL_Window *)_window
    42 {
    43     self = [self init];
    44     if (self == nil) {
    45         return nil;
    46     }
    47     self.window = _window;
    48 
    49     return self;
    50 }
    51 
    52 - (void)loadView
    53 {
    54     /* do nothing. */
    55 }
    56 
    57 - (void)viewDidLayoutSubviews
    58 {
    59     if (window->flags & SDL_WINDOW_RESIZABLE) {
    60         SDL_WindowData *data = window->driverdata;
    61         const CGSize size = data->view.bounds.size;
    62         int w = (int) size.width;
    63         int h = (int) size.height;
    64 
    65         SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
    66     }
    67 }
    68 
    69 - (NSUInteger)supportedInterfaceOrientations
    70 {
    71     NSUInteger orientationMask = 0;
    72 
    73     const char *orientationsCString;
    74     if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
    75         BOOL rotate = NO;
    76         NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
    77                                                             encoding:NSUTF8StringEncoding];
    78         NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
    79                                  [NSCharacterSet characterSetWithCharactersInString:@" "]];
    80 
    81         if ([orientations containsObject:@"LandscapeLeft"]) {
    82             orientationMask |= UIInterfaceOrientationMaskLandscapeLeft;
    83         }
    84         if ([orientations containsObject:@"LandscapeRight"]) {
    85             orientationMask |= UIInterfaceOrientationMaskLandscapeRight;
    86         }
    87         if ([orientations containsObject:@"Portrait"]) {
    88             orientationMask |= UIInterfaceOrientationMaskPortrait;
    89         }
    90         if ([orientations containsObject:@"PortraitUpsideDown"]) {
    91             orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
    92         }
    93 
    94     } else if (self->window->flags & SDL_WINDOW_RESIZABLE) {
    95         orientationMask = UIInterfaceOrientationMaskAll;  /* any orientation is okay. */
    96     } else {
    97         if (self->window->w >= self->window->h) {
    98             orientationMask |= UIInterfaceOrientationMaskLandscape;
    99         }
   100         if (self->window->h >= self->window->w) {
   101             orientationMask |= (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
   102         }
   103     }
   104 
   105     /* Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation */
   106     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
   107         orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
   108     }
   109     return orientationMask;
   110 }
   111 
   112 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
   113 {
   114     NSUInteger orientationMask = [self supportedInterfaceOrientations];
   115     return (orientationMask & (1 << orient));
   116 }
   117 
   118 - (BOOL)prefersStatusBarHidden
   119 {
   120     if (self->window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
   121         return YES;
   122     } else {
   123         return NO;
   124     }
   125 }
   126 
   127 @end
   128 
   129 #endif /* SDL_VIDEO_DRIVER_UIKIT */
   130 
   131 /* vi: set ts=4 sw=4 expandtab: */