From 43d4365de305b272b5be74787f16111665ce3b9d Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 18 Sep 2012 22:24:08 -0700 Subject: [PATCH] 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 | 2 +- src/video/uikit/SDL_uikitviewcontroller.m | 31 ++-------------- src/video/uikit/SDL_uikitwindow.m | 44 ++++++++++++----------- 3 files changed, 27 insertions(+), 50 deletions(-) diff --git a/src/video/uikit/SDL_uikitopengles.m b/src/video/uikit/SDL_uikitopengles.m index ca3c95e99..d819b1a24 100755 --- a/src/video/uikit/SDL_uikitopengles.m +++ b/src/video/uikit/SDL_uikitopengles.m @@ -127,7 +127,7 @@ SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window) } /* add the view to our window */ - [uiwindow addSubview: view ]; + [uiwindow addSubview: view]; if ( UIKit_GL_MakeCurrent(_this, window, view) < 0 ) { UIKit_GL_DeleteContext(_this, view); diff --git a/src/video/uikit/SDL_uikitviewcontroller.m b/src/video/uikit/SDL_uikitviewcontroller.m index c9a8e0b4d..8c21e6f32 100755 --- a/src/video/uikit/SDL_uikitviewcontroller.m +++ b/src/video/uikit/SDL_uikitviewcontroller.m @@ -112,39 +112,14 @@ - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO { const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation]; SDL_WindowData *data = self->window->driverdata; - UIWindow *uiwindow = data->uiwindow; SDL_VideoDisplay *display = SDL_GetDisplayForWindow(self->window); - SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata; SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata; - UIScreen *uiscreen = displaydata->uiscreen; - const int noborder = (self->window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)); - CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame]; - const CGSize size = frame.size; + const CGSize size = data->view.bounds.size; int w, h; - switch (toInterfaceOrientation) { - case UIInterfaceOrientationPortrait: - case UIInterfaceOrientationPortraitUpsideDown: - w = (size.width < size.height) ? size.width : size.height; - h = (size.width > size.height) ? size.width : size.height; - break; - - case UIInterfaceOrientationLandscapeLeft: - case UIInterfaceOrientationLandscapeRight: - w = (size.width > size.height) ? size.width : size.height; - h = (size.width < size.height) ? size.width : size.height; - break; - - default: - SDL_assert(0 && "Unexpected interface orientation!"); - return; - } - - w = (int)(w * displaymodedata->scale); - h = (int)(h * displaymodedata->scale); + w = (int)(size.width * displaymodedata->scale); + h = (int)(size.height * displaymodedata->scale); - [uiwindow setFrame:frame]; - [data->view setFrame:frame]; SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h); } diff --git a/src/video/uikit/SDL_uikitwindow.m b/src/video/uikit/SDL_uikitwindow.m index 2ff36ed38..b5b369d6e 100755 --- a/src/video/uikit/SDL_uikitwindow.m +++ b/src/video/uikit/SDL_uikitwindow.m @@ -65,15 +65,22 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo window->x = 0; window->y = 0; + CGRect bounds; + if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) { + bounds = [displaydata->uiscreen bounds]; + } else { + bounds = [displaydata->uiscreen applicationFrame]; + } + /* Get frame dimensions in pixels */ - int width = (int)(uiwindow.frame.size.width * displaymodedata->scale); - int height = (int)(uiwindow.frame.size.height * displaymodedata->scale); + int width = (int)(bounds.size.width * displaymodedata->scale); + int height = (int)(bounds.size.height * displaymodedata->scale); /* We can pick either width or height here and we'll rotate the screen to match, so we pick the closest to what we wanted. */ if (window->w >= window->h) { - if (uiwindow.frame.size.width > uiwindow.frame.size.height) { + if (width > height) { window->w = width; window->h = height; } else { @@ -81,7 +88,7 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo window->h = width; } } else { - if (uiwindow.frame.size.width > uiwindow.frame.size.height) { + if (width > height) { window->w = height; window->h = width; } else { @@ -112,12 +119,6 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo [UIApplication sharedApplication].statusBarHidden = NO; } - //const UIDeviceOrientation o = [[UIDevice currentDevice] orientation]; - //const BOOL landscape = (o == UIDeviceOrientationLandscapeLeft) || - // (o == UIDeviceOrientationLandscapeRight); - //const BOOL rotate = ( ((window->w > window->h) && (!landscape)) || - // ((window->w < window->h) && (landscape)) ); - // The View Controller will handle rotating the view when the // device orientation changes. This will trigger resize events, if // appropriate. @@ -125,7 +126,6 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo controller = [SDL_uikitviewcontroller alloc]; data->viewcontroller = [controller initWithSDLWindow:window]; [data->viewcontroller setTitle:@"SDL App"]; // !!! FIXME: hook up SDL_SetWindowTitle() - // !!! FIXME: if (rotate), force a "resize" right at the start } return 0; @@ -187,10 +187,7 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo /* ignore the size user requested, and make a fullscreen window */ // !!! FIXME: can we have a smaller view? UIWindow *uiwindow = [UIWindow alloc]; - if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) - uiwindow = [uiwindow initWithFrame:[data->uiscreen bounds]]; - else - uiwindow = [uiwindow initWithFrame:[data->uiscreen applicationFrame]]; + uiwindow = [uiwindow initWithFrame:[data->uiscreen bounds]]; // put the window on an external display if appropriate. This implicitly // does [uiwindow setframe:[uiscreen bounds]], so don't do it on the @@ -244,21 +241,26 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo if (fullscreen) { [UIApplication sharedApplication].statusBarHidden = YES; - uiwindow.frame = [displaydata->uiscreen bounds]; } else { [UIApplication sharedApplication].statusBarHidden = NO; - uiwindow.frame = [displaydata->uiscreen applicationFrame]; + } + + CGRect bounds; + if (fullscreen) { + bounds = [displaydata->uiscreen bounds]; + } else { + bounds = [displaydata->uiscreen applicationFrame]; } /* Get frame dimensions in pixels */ - int width = (int)(uiwindow.frame.size.width * displaymodedata->scale); - int height = (int)(uiwindow.frame.size.height * displaymodedata->scale); + int width = (int)(bounds.size.width * displaymodedata->scale); + int height = (int)(bounds.size.height * displaymodedata->scale); /* We can pick either width or height here and we'll rotate the screen to match, so we pick the closest to what we wanted. */ if (window->w >= window->h) { - if (uiwindow.frame.size.width > uiwindow.frame.size.height) { + if (width > height) { window->w = width; window->h = height; } else { @@ -266,7 +268,7 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo window->h = width; } } else { - if (uiwindow.frame.size.width > uiwindow.frame.size.height) { + if (width > height) { window->w = height; window->h = width; } else {