Navigation Menu

Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed window/view bounds management with autorotation.
Browse files Browse the repository at this point in the history
The trick is not to mess with the window frame and let iOS handle resizing the view automatically when the rotation occurs.
  • Loading branch information
slouken committed Sep 19, 2012
1 parent 838db7e commit 43d4365
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/video/uikit/SDL_uikitopengles.m
Expand Up @@ -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);
Expand Down
31 changes: 3 additions & 28 deletions src/video/uikit/SDL_uikitviewcontroller.m
Expand Up @@ -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);
}

Expand Down
44 changes: 23 additions & 21 deletions src/video/uikit/SDL_uikitwindow.m
Expand Up @@ -65,23 +65,30 @@ 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 {
window->w = height;
window->h = width;
}
} else {
if (uiwindow.frame.size.width > uiwindow.frame.size.height) {
if (width > height) {
window->w = height;
window->h = width;
} else {
Expand Down Expand Up @@ -112,20 +119,13 @@ 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.
SDL_uikitviewcontroller *controller;
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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -244,29 +241,34 @@ 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 {
window->w = height;
window->h = width;
}
} else {
if (uiwindow.frame.size.width > uiwindow.frame.size.height) {
if (width > height) {
window->w = height;
window->h = width;
} else {
Expand Down

0 comments on commit 43d4365

Please sign in to comment.