Navigation Menu

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

Commit

Permalink
Make sure mode width/height and status bar orientation match
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Sep 30, 2012
1 parent 4f6929d commit 5d0bae4
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 47 deletions.
2 changes: 2 additions & 0 deletions src/video/uikit/SDL_uikitmodes.h
Expand Up @@ -39,6 +39,8 @@ typedef struct

extern BOOL SDL_UIKit_supports_multiple_displays;

extern SDL_bool UIKit_IsDisplayLandscape(UIScreen *uiscreen);

extern int UIKit_InitModes(_THIS);
extern void UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display);
extern int UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
Expand Down
74 changes: 56 additions & 18 deletions src/video/uikit/SDL_uikitmodes.m
Expand Up @@ -93,13 +93,13 @@

static int
UIKit_AddDisplayMode(SDL_VideoDisplay * display, int w, int h, CGFloat scale,
UIScreenMode * uiscreenmode, BOOL addRotated)
UIScreenMode * uiscreenmode, SDL_bool addRotation)
{
if (UIKit_AddSingleDisplayMode(display, w, h, uiscreenmode, scale) < 0) {
return -1;
}

if (addRotated) {
if (addRotation) {
// Add the rotated version
if (UIKit_AddSingleDisplayMode(display, h, w, uiscreenmode, scale) < 0) {
return -1;
Expand All @@ -114,6 +114,13 @@
{
CGSize size = [uiscreen bounds].size;

// Make sure the width/height are oriented correctly
if (UIKit_IsDisplayLandscape(uiscreen) != (size.width > size.height)) {
CGFloat height = size.width;
size.width = size.height;
size.height = height;
}

// When dealing with UIKit all coordinates are specified in terms of
// what Apple refers to as points. On earlier devices without the
// so called "Retina" display, there is a one to one mapping between
Expand All @@ -127,7 +134,7 @@
} else {
scale = 1.0f; // iOS < 4.0
}

SDL_VideoDisplay display;
SDL_DisplayMode mode;
SDL_zero(mode);
Expand Down Expand Up @@ -168,6 +175,16 @@
return 0;
}

SDL_bool
UIKit_IsDisplayLandscape(UIScreen *uiscreen)
{
if (uiscreen == [UIScreen mainScreen]) {
return UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
} else {
CGSize size = [uiscreen bounds].size;
return (size.width > size.height);
}
}

int
UIKit_InitModes(_THIS)
Expand Down Expand Up @@ -203,17 +220,26 @@
{
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;

SDL_bool isLandscape = UIKit_IsDisplayLandscape(data->uiscreen);
SDL_bool addRotation = (data->uiscreen == [UIScreen mainScreen]);

if (SDL_UIKit_supports_multiple_displays) {
// availableModes showed up in 3.2 (the iPad and later). We should only
// land here for at least that version of the OS.
for (UIScreenMode *uimode in [data->uiscreen availableModes]) {
CGSize size = [uimode size];
int w = (int)size.width;
int h = (int)size.height;
BOOL addRotated = (data->uiscreen == [UIScreen mainScreen]);


// Make sure the width/height are oriented correctly
if (isLandscape != (w > h)) {
int tmp = w;
w = h;
h = tmp;
}

// Add the native screen resolution.
UIKit_AddDisplayMode(display, w, h, data->scale, uimode, addRotated);
UIKit_AddDisplayMode(display, w, h, data->scale, uimode, addRotation);

if (data->scale != 1.0f) {
// Add the native screen resolution divided by its scale.
Expand All @@ -222,37 +248,49 @@
UIKit_AddDisplayMode(display,
(int)(size.width / data->scale),
(int)(size.height / data->scale),
1.0f, uimode, addRotated);
1.0f, uimode, addRotation);
}
}
} else {
const CGRect rect = [data->uiscreen bounds];
UIKit_AddDisplayMode(display,
(int)rect.size.width, (int)rect.size.height,
1.0f, nil, YES);
const CGSize size = [data->uiscreen bounds].size;
int w = (int)size.width;
int h = (int)size.height;

// Make sure the width/height are oriented correctly
if (isLandscape != (w > h)) {
int tmp = w;
w = h;
h = tmp;
}

UIKit_AddDisplayMode(display, w, h, 1.0f, nil, addRotation);
}
}

int
UIKit_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
{
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;

if (!SDL_UIKit_supports_multiple_displays) {
// Not on at least iPhoneOS 3.2 (versions prior to iPad).
SDL_assert(mode->driverdata == NULL);
} else {
SDL_DisplayModeData *modedata = (SDL_DisplayModeData *)mode->driverdata;
[data->uiscreen setCurrentMode:modedata->uiscreenmode];

if (mode->w > mode->h) {
if (!UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
} else if (mode->w < mode->h) {
if (!UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
if (data->uiscreen == [UIScreen mainScreen]) {
if (mode->w > mode->h) {
if (!UIKit_IsDisplayLandscape(data->uiscreen)) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
}
} else if (mode->w < mode->h) {
if (UIKit_IsDisplayLandscape(data->uiscreen)) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
}
}
}
}

return 0;
}

Expand Down
62 changes: 33 additions & 29 deletions src/video/uikit/SDL_uikitwindow.m
Expand Up @@ -76,32 +76,16 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo
/* Get frame dimensions in pixels */
int width = (int)(bounds.size.width * displaymodedata->scale);
int height = (int)(bounds.size.height * displaymodedata->scale);

if ([UIScreen mainScreen] == displaydata->uiscreen) {
/* 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 (width > height) {
window->w = width;
window->h = height;
} else {
window->w = height;
window->h = width;
}
} else {
if (width > height) {
window->w = height;
window->h = width;
} else {
window->w = width;
window->h = height;
}
}
} else {
window->w = width;
window->h = height;

// Make sure the width/height are oriented correctly
if (UIKit_IsDisplayLandscape(displaydata->uiscreen) != (width > height)) {
int temp = width;
width = height;
height = temp;
}

window->w = width;
window->h = height;
}

window->driverdata = data;
Expand All @@ -112,13 +96,13 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo
// SDL_WINDOW_BORDERLESS controls whether status bar is hidden.
// This is only set if the window is on the main screen. Other screens
// just force the window to have the borderless flag.
if ([UIScreen mainScreen] == displaydata->uiscreen) {
if (displaydata->uiscreen == [UIScreen mainScreen]) {
window->flags |= SDL_WINDOW_INPUT_FOCUS; // always has input focus

if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
[UIApplication sharedApplication].statusBarHidden = YES;
if ([UIApplication sharedApplication].statusBarHidden) {
window->flags |= SDL_WINDOW_BORDERLESS;
} else {
[UIApplication sharedApplication].statusBarHidden = NO;
window->flags &= ~SDL_WINDOW_BORDERLESS;
}
} else {
window->flags &= ~SDL_WINDOW_RESIZABLE; // window is NEVER resizeable
Expand Down Expand Up @@ -182,6 +166,26 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo
}
}
}

if (data->uiscreen == [UIScreen mainScreen]) {
if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
[UIApplication sharedApplication].statusBarHidden = YES;
} else {
[UIApplication sharedApplication].statusBarHidden = NO;
}
}

if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
if (window->w > window->h) {
if (!UIKit_IsDisplayLandscape(data->uiscreen)) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
}
} else if (window->w < window->h) {
if (UIKit_IsDisplayLandscape(data->uiscreen)) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
}
}
}

/* ignore the size user requested, and make a fullscreen window */
// !!! FIXME: can we have a smaller view?
Expand Down

0 comments on commit 5d0bae4

Please sign in to comment.