Fixed window/view bounds management with autorotation.
authorSam Lantinga
Tue, 18 Sep 2012 22:24:08 -0700
changeset 64373e52c17e2a07
parent 6436 29a35c72905a
child 6438 2c0fa683b18c
Fixed window/view bounds management with autorotation.
The trick is not to mess with the window frame and let iOS handle resizing the view automatically when the rotation occurs.
src/video/uikit/SDL_uikitopengles.m
src/video/uikit/SDL_uikitviewcontroller.m
src/video/uikit/SDL_uikitwindow.m
     1.1 --- a/src/video/uikit/SDL_uikitopengles.m	Tue Sep 18 01:45:15 2012 -0700
     1.2 +++ b/src/video/uikit/SDL_uikitopengles.m	Tue Sep 18 22:24:08 2012 -0700
     1.3 @@ -127,7 +127,7 @@
     1.4      }
     1.5  
     1.6      /* add the view to our window */
     1.7 -    [uiwindow addSubview: view ];
     1.8 +    [uiwindow addSubview: view];
     1.9  
    1.10      if ( UIKit_GL_MakeCurrent(_this, window, view) < 0 ) {
    1.11          UIKit_GL_DeleteContext(_this, view);
     2.1 --- a/src/video/uikit/SDL_uikitviewcontroller.m	Tue Sep 18 01:45:15 2012 -0700
     2.2 +++ b/src/video/uikit/SDL_uikitviewcontroller.m	Tue Sep 18 22:24:08 2012 -0700
     2.3 @@ -112,39 +112,14 @@
     2.4  {
     2.5      const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
     2.6      SDL_WindowData *data = self->window->driverdata;
     2.7 -    UIWindow *uiwindow = data->uiwindow;
     2.8      SDL_VideoDisplay *display = SDL_GetDisplayForWindow(self->window);
     2.9 -    SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
    2.10      SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
    2.11 -    UIScreen *uiscreen = displaydata->uiscreen;
    2.12 -    const int noborder = (self->window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS));
    2.13 -    CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
    2.14 -    const CGSize size = frame.size;
    2.15 +    const CGSize size = data->view.bounds.size;
    2.16      int w, h;
    2.17  
    2.18 -    switch (toInterfaceOrientation) {
    2.19 -        case UIInterfaceOrientationPortrait:
    2.20 -        case UIInterfaceOrientationPortraitUpsideDown:
    2.21 -            w = (size.width < size.height) ? size.width : size.height;
    2.22 -            h = (size.width > size.height) ? size.width : size.height;
    2.23 -            break;
    2.24 +    w = (int)(size.width * displaymodedata->scale);
    2.25 +    h = (int)(size.height * displaymodedata->scale);
    2.26  
    2.27 -        case UIInterfaceOrientationLandscapeLeft:
    2.28 -        case UIInterfaceOrientationLandscapeRight:
    2.29 -            w = (size.width > size.height) ? size.width : size.height;
    2.30 -            h = (size.width < size.height) ? size.width : size.height;
    2.31 -            break;
    2.32 -
    2.33 -        default:
    2.34 -            SDL_assert(0 && "Unexpected interface orientation!");
    2.35 -            return;
    2.36 -    }
    2.37 -
    2.38 -    w = (int)(w * displaymodedata->scale);
    2.39 -    h = (int)(h * displaymodedata->scale);
    2.40 -
    2.41 -    [uiwindow setFrame:frame];
    2.42 -    [data->view setFrame:frame];
    2.43      SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
    2.44  }
    2.45  
     3.1 --- a/src/video/uikit/SDL_uikitwindow.m	Tue Sep 18 01:45:15 2012 -0700
     3.2 +++ b/src/video/uikit/SDL_uikitwindow.m	Tue Sep 18 22:24:08 2012 -0700
     3.3 @@ -65,15 +65,22 @@
     3.4          window->x = 0;
     3.5          window->y = 0;
     3.6  
     3.7 +        CGRect bounds;
     3.8 +        if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
     3.9 +            bounds = [displaydata->uiscreen bounds];
    3.10 +        } else {
    3.11 +            bounds = [displaydata->uiscreen applicationFrame];
    3.12 +        }
    3.13 +
    3.14          /* Get frame dimensions in pixels */
    3.15 -        int width = (int)(uiwindow.frame.size.width * displaymodedata->scale);
    3.16 -        int height = (int)(uiwindow.frame.size.height * displaymodedata->scale);
    3.17 +        int width = (int)(bounds.size.width * displaymodedata->scale);
    3.18 +        int height = (int)(bounds.size.height * displaymodedata->scale);
    3.19  
    3.20          /* We can pick either width or height here and we'll rotate the
    3.21             screen to match, so we pick the closest to what we wanted.
    3.22           */
    3.23          if (window->w >= window->h) {
    3.24 -            if (uiwindow.frame.size.width > uiwindow.frame.size.height) {
    3.25 +            if (width > height) {
    3.26                  window->w = width;
    3.27                  window->h = height;
    3.28              } else {
    3.29 @@ -81,7 +88,7 @@
    3.30                  window->h = width;
    3.31              }
    3.32          } else {
    3.33 -            if (uiwindow.frame.size.width > uiwindow.frame.size.height) {
    3.34 +            if (width > height) {
    3.35                  window->w = height;
    3.36                  window->h = width;
    3.37              } else {
    3.38 @@ -112,12 +119,6 @@
    3.39              [UIApplication sharedApplication].statusBarHidden = NO;
    3.40          }
    3.41  
    3.42 -        //const UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
    3.43 -        //const BOOL landscape = (o == UIDeviceOrientationLandscapeLeft) ||
    3.44 -        //                           (o == UIDeviceOrientationLandscapeRight);
    3.45 -        //const BOOL rotate = ( ((window->w > window->h) && (!landscape)) ||
    3.46 -        //                      ((window->w < window->h) && (landscape)) );
    3.47 -
    3.48          // The View Controller will handle rotating the view when the
    3.49          //  device orientation changes. This will trigger resize events, if
    3.50          //  appropriate.
    3.51 @@ -125,7 +126,6 @@
    3.52          controller = [SDL_uikitviewcontroller alloc];
    3.53          data->viewcontroller = [controller initWithSDLWindow:window];
    3.54          [data->viewcontroller setTitle:@"SDL App"];  // !!! FIXME: hook up SDL_SetWindowTitle()
    3.55 -        // !!! FIXME: if (rotate), force a "resize" right at the start
    3.56      }
    3.57  
    3.58      return 0;
    3.59 @@ -187,10 +187,7 @@
    3.60      /* ignore the size user requested, and make a fullscreen window */
    3.61      // !!! FIXME: can we have a smaller view?
    3.62      UIWindow *uiwindow = [UIWindow alloc];
    3.63 -    if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS))
    3.64 -        uiwindow = [uiwindow initWithFrame:[data->uiscreen bounds]];
    3.65 -    else
    3.66 -        uiwindow = [uiwindow initWithFrame:[data->uiscreen applicationFrame]];
    3.67 +    uiwindow = [uiwindow initWithFrame:[data->uiscreen bounds]];
    3.68  
    3.69      // put the window on an external display if appropriate. This implicitly
    3.70      //  does [uiwindow setframe:[uiscreen bounds]], so don't do it on the
    3.71 @@ -244,21 +241,26 @@
    3.72  
    3.73      if (fullscreen) {
    3.74          [UIApplication sharedApplication].statusBarHidden = YES;
    3.75 -        uiwindow.frame = [displaydata->uiscreen bounds];
    3.76      } else {
    3.77          [UIApplication sharedApplication].statusBarHidden = NO;
    3.78 -        uiwindow.frame = [displaydata->uiscreen applicationFrame];
    3.79 +    }
    3.80 +
    3.81 +    CGRect bounds;
    3.82 +    if (fullscreen) {
    3.83 +        bounds = [displaydata->uiscreen bounds];
    3.84 +    } else {
    3.85 +        bounds = [displaydata->uiscreen applicationFrame];
    3.86      }
    3.87  
    3.88      /* Get frame dimensions in pixels */
    3.89 -    int width = (int)(uiwindow.frame.size.width * displaymodedata->scale);
    3.90 -    int height = (int)(uiwindow.frame.size.height * displaymodedata->scale);
    3.91 +    int width = (int)(bounds.size.width * displaymodedata->scale);
    3.92 +    int height = (int)(bounds.size.height * displaymodedata->scale);
    3.93  
    3.94      /* We can pick either width or height here and we'll rotate the
    3.95         screen to match, so we pick the closest to what we wanted.
    3.96       */
    3.97      if (window->w >= window->h) {
    3.98 -        if (uiwindow.frame.size.width > uiwindow.frame.size.height) {
    3.99 +        if (width > height) {
   3.100              window->w = width;
   3.101              window->h = height;
   3.102          } else {
   3.103 @@ -266,7 +268,7 @@
   3.104              window->h = width;
   3.105          }
   3.106      } else {
   3.107 -        if (uiwindow.frame.size.width > uiwindow.frame.size.height) {
   3.108 +        if (width > height) {
   3.109              window->w = height;
   3.110              window->h = width;
   3.111          } else {