From 3672409c518d0b0db6cd99479e35d771361223fd Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Mon, 14 Jul 2014 22:35:48 -0300 Subject: [PATCH] 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. --- src/video/uikit/SDL_uikitmodes.h | 2 - src/video/uikit/SDL_uikitmodes.m | 50 ++++++++--------------- src/video/uikit/SDL_uikitopengles.h | 2 + src/video/uikit/SDL_uikitopengles.m | 42 ++++++++++++++----- src/video/uikit/SDL_uikitopenglview.h | 4 ++ src/video/uikit/SDL_uikitopenglview.m | 7 +++- src/video/uikit/SDL_uikitvideo.m | 7 ++-- src/video/uikit/SDL_uikitview.m | 14 +------ src/video/uikit/SDL_uikitviewcontroller.h | 2 +- src/video/uikit/SDL_uikitviewcontroller.m | 12 ++---- src/video/uikit/SDL_uikitwindow.m | 17 ++++---- 11 files changed, 77 insertions(+), 82 deletions(-) diff --git a/src/video/uikit/SDL_uikitmodes.h b/src/video/uikit/SDL_uikitmodes.h index 9f2e9dd34..276685039 100644 --- a/src/video/uikit/SDL_uikitmodes.h +++ b/src/video/uikit/SDL_uikitmodes.h @@ -28,13 +28,11 @@ typedef struct { UIScreen *uiscreen; - CGFloat scale; } SDL_DisplayData; typedef struct { UIScreenMode *uiscreenmode; - CGFloat scale; } SDL_DisplayModeData; extern SDL_bool UIKit_IsDisplayLandscape(UIScreen *uiscreen); diff --git a/src/video/uikit/SDL_uikitmodes.m b/src/video/uikit/SDL_uikitmodes.m index 05e33d9c9..20a433d55 100644 --- a/src/video/uikit/SDL_uikitmodes.m +++ b/src/video/uikit/SDL_uikitmodes.m @@ -28,7 +28,7 @@ static int UIKit_AllocateDisplayModeData(SDL_DisplayMode * mode, - UIScreenMode * uiscreenmode, CGFloat scale) + UIScreenMode * uiscreenmode) { SDL_DisplayModeData *data = NULL; @@ -41,8 +41,6 @@ UIKit_AllocateDisplayModeData(SDL_DisplayMode * mode, data->uiscreenmode = uiscreenmode; [data->uiscreenmode retain]; - - data->scale = scale; } mode->driverdata = data; @@ -63,14 +61,14 @@ UIKit_FreeDisplayModeData(SDL_DisplayMode * mode) static int UIKit_AddSingleDisplayMode(SDL_VideoDisplay * display, int w, int h, - UIScreenMode * uiscreenmode, CGFloat scale) + UIScreenMode * uiscreenmode) { SDL_DisplayMode mode; SDL_zero(mode); mode.format = SDL_PIXELFORMAT_ABGR8888; mode.refresh_rate = 0; - if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode, scale) < 0) { + if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode) < 0) { return -1; } @@ -85,16 +83,16 @@ UIKit_AddSingleDisplayMode(SDL_VideoDisplay * display, int w, int h, } static int -UIKit_AddDisplayMode(SDL_VideoDisplay * display, int w, int h, CGFloat scale, +UIKit_AddDisplayMode(SDL_VideoDisplay * display, int w, int h, UIScreenMode * uiscreenmode, SDL_bool addRotation) { - if (UIKit_AddSingleDisplayMode(display, w, h, uiscreenmode, scale) < 0) { + if (UIKit_AddSingleDisplayMode(display, w, h, uiscreenmode) < 0) { return -1; } if (addRotation) { /* Add the rotated version */ - if (UIKit_AddSingleDisplayMode(display, h, w, uiscreenmode, scale) < 0) { + if (UIKit_AddSingleDisplayMode(display, h, w, uiscreenmode) < 0) { return -1; } } @@ -114,24 +112,16 @@ UIKit_AddDisplay(UIScreen *uiscreen) size.height = height; } - /* When dealing with UIKit all coordinates are specified in terms of - * what Apple refers to as points. [UIScreen scale] indicates the - * relationship between points and pixels. Since SDL has no notion - * of points, we must compensate in all cases where dealing with such - * units. - */ - CGFloat scale = [uiscreen scale]; - SDL_VideoDisplay display; SDL_DisplayMode mode; SDL_zero(mode); mode.format = SDL_PIXELFORMAT_ABGR8888; - mode.w = (int)(size.width * scale); - mode.h = (int)(size.height * scale); + mode.w = (int) size.width; + mode.h = (int) size.height; - UIScreenMode * uiscreenmode = [uiscreen currentMode]; + UIScreenMode *uiscreenmode = [uiscreen currentMode]; - if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode, scale) < 0) { + if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode) < 0) { return -1; } @@ -148,7 +138,6 @@ UIKit_AddDisplay(UIScreen *uiscreen) [uiscreen retain]; data->uiscreen = uiscreen; - data->scale = scale; display.driverdata = data; SDL_AddVideoDisplay(&display); @@ -186,11 +175,14 @@ UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display) SDL_bool isLandscape = UIKit_IsDisplayLandscape(data->uiscreen); SDL_bool addRotation = (data->uiscreen == [UIScreen mainScreen]); + CGFloat scale = data->uiscreen.scale; for (UIScreenMode *uimode in [data->uiscreen availableModes]) { + /* The size of a UIScreenMode is in pixels, but we deal exclusively in + * points (except in SDL_GL_GetDrawableSize.) */ CGSize size = [uimode size]; - int w = (int)size.width; - int h = (int)size.height; + int w = (int)(size.width / scale); + int h = (int)(size.height / scale); /* Make sure the width/height are oriented correctly */ if (isLandscape != (w > h)) { @@ -200,17 +192,7 @@ UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display) } /* Add the native screen resolution. */ - UIKit_AddDisplayMode(display, w, h, data->scale, uimode, addRotation); - - if (data->scale != 1.0f) { - /* Add the native screen resolution divided by its scale. - * This is so devices capable of e.g. 640x960 also advertise 320x480. - */ - UIKit_AddDisplayMode(display, - (int)(size.width / data->scale), - (int)(size.height / data->scale), - 1.0f, uimode, addRotation); - } + UIKit_AddDisplayMode(display, w, h, uimode, addRotation); } } diff --git a/src/video/uikit/SDL_uikitopengles.h b/src/video/uikit/SDL_uikitopengles.h index 947678cae..f16918935 100644 --- a/src/video/uikit/SDL_uikitopengles.h +++ b/src/video/uikit/SDL_uikitopengles.h @@ -25,6 +25,8 @@ extern int UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context); +extern void UIKit_GL_GetDrawableSize(_THIS, SDL_Window * window, + int * w, int * h); extern void UIKit_GL_SwapWindow(_THIS, SDL_Window * window); extern SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window); extern void UIKit_GL_DeleteContext(_THIS, SDL_GLContext context); diff --git a/src/video/uikit/SDL_uikitopengles.m b/src/video/uikit/SDL_uikitopengles.m index 42303e3cb..7313df6aa 100644 --- a/src/video/uikit/SDL_uikitopengles.m +++ b/src/video/uikit/SDL_uikitopengles.m @@ -49,7 +49,8 @@ UIKit_GL_GetProcAddress(_THIS, const char *proc) /* note that SDL_GL_Delete context makes it current without passing the window */ -int UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context) +int +UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context) { if (context) { SDL_WindowData *data = (SDL_WindowData *)window->driverdata; @@ -62,6 +63,19 @@ int UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context) return 0; } +void UIKit_GL_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h) +{ + SDL_WindowData *data = (SDL_WindowData *)window->driverdata; + + if (w) { + *w = data->view.backingWidth; + } + if (h) { + *h = data->view.backingHeight; + } +} + + int UIKit_GL_LoadLibrary(_THIS, const char *path) { @@ -96,15 +110,22 @@ void UIKit_GL_SwapWindow(_THIS, SDL_Window * window) */ } -SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window) +SDL_GLContext +UIKit_GL_CreateContext(_THIS, SDL_Window * window) { SDL_uikitopenglview *view; SDL_WindowData *data = (SDL_WindowData *) window->driverdata; - SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window); - SDL_DisplayData *displaydata = display->driverdata; - SDL_DisplayModeData *displaymodedata = display->current_mode.driverdata; UIWindow *uiwindow = data->uiwindow; EAGLSharegroup *share_group = nil; + CGFloat scale = 1.0; + + if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) { + /* Set the scale to the natural scale factor of the screen - the backing + dimensions of the OpenGL view will match the pixel dimensions of the + screen rather than the dimensions in points. + */ + scale = [uiwindow screen].scale; + } if (_this->gl_config.share_with_current_context) { SDL_uikitopenglview *view = (SDL_uikitopenglview *) SDL_GL_GetCurrentContext(); @@ -114,12 +135,12 @@ SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window) /* construct our view, passing in SDL's OpenGL configuration data */ CGRect frame; if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) { - frame = [displaydata->uiscreen bounds]; + frame = [[uiwindow screen] bounds]; } else { - frame = [displaydata->uiscreen applicationFrame]; + frame = [[uiwindow screen] applicationFrame]; } view = [[SDL_uikitopenglview alloc] initWithFrame: frame - scale: displaymodedata->scale + scale: scale retainBacking: _this->gl_config.retained_backing rBits: _this->gl_config.red_size gBits: _this->gl_config.green_size @@ -152,7 +173,7 @@ SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window) } /* Make this window the current mouse focus for touch input */ - if (displaydata->uiscreen == [UIScreen mainScreen]) { + if ([uiwindow screen] == [UIScreen mainScreen]) { SDL_SetMouseFocus(window); SDL_SetKeyboardFocus(window); } @@ -160,7 +181,8 @@ SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window) return view; } -void UIKit_GL_DeleteContext(_THIS, SDL_GLContext context) +void +UIKit_GL_DeleteContext(_THIS, SDL_GLContext context) { /* the delegate has retained the view, this will release him */ SDL_uikitopenglview *view = (SDL_uikitopenglview *)context; diff --git a/src/video/uikit/SDL_uikitopenglview.h b/src/video/uikit/SDL_uikitopenglview.h index eed2e5b70..3eb9b4f44 100644 --- a/src/video/uikit/SDL_uikitopenglview.h +++ b/src/video/uikit/SDL_uikitopenglview.h @@ -55,6 +55,10 @@ @property (nonatomic, retain, readonly) EAGLContext *context; +/* The width and height of the drawable in pixels (as opposed to points.) */ +@property (nonatomic, readonly) int backingWidth; +@property (nonatomic, readonly) int backingHeight; + - (void)swapBuffers; - (void)setCurrentContext; diff --git a/src/video/uikit/SDL_uikitopenglview.m b/src/video/uikit/SDL_uikitopenglview.m index 144100bf7..91f690563 100644 --- a/src/video/uikit/SDL_uikitopenglview.m +++ b/src/video/uikit/SDL_uikitopenglview.m @@ -32,6 +32,9 @@ @synthesize context; +@synthesize backingWidth = backingWidth; +@synthesize backingHeight = backingHeight; + + (Class)layerClass { return [CAEAGLLayer class]; @@ -74,7 +77,9 @@ eaglLayer.opaque = YES; eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: - [NSNumber numberWithBool: retained], kEAGLDrawablePropertyRetainedBacking, colorFormat, kEAGLDrawablePropertyColorFormat, nil]; + [NSNumber numberWithBool: retained], kEAGLDrawablePropertyRetainedBacking, + colorFormat, kEAGLDrawablePropertyColorFormat, + nil]; context = [[EAGLContext alloc] initWithAPI:api sharegroup:shareGroup]; if (!context || ![EAGLContext setCurrentContext:context]) { diff --git a/src/video/uikit/SDL_uikitvideo.m b/src/video/uikit/SDL_uikitvideo.m index 4893bc163..e4266ea2e 100644 --- a/src/video/uikit/SDL_uikitvideo.m +++ b/src/video/uikit/SDL_uikitvideo.m @@ -93,12 +93,13 @@ UIKit_CreateDevice(int devindex) #endif /* OpenGL (ES) functions */ - device->GL_MakeCurrent = UIKit_GL_MakeCurrent; - device->GL_SwapWindow = UIKit_GL_SwapWindow; + device->GL_MakeCurrent = UIKit_GL_MakeCurrent; + device->GL_GetDrawableSize = UIKit_GL_GetDrawableSize; + device->GL_SwapWindow = UIKit_GL_SwapWindow; device->GL_CreateContext = UIKit_GL_CreateContext; device->GL_DeleteContext = UIKit_GL_DeleteContext; device->GL_GetProcAddress = UIKit_GL_GetProcAddress; - device->GL_LoadLibrary = UIKit_GL_LoadLibrary; + device->GL_LoadLibrary = UIKit_GL_LoadLibrary; device->free = UIKit_DeleteDevice; device->gl_config.accelerated = 1; diff --git a/src/video/uikit/SDL_uikitview.m b/src/video/uikit/SDL_uikitview.m index a7fa3d511..7312307f5 100644 --- a/src/video/uikit/SDL_uikitview.m +++ b/src/video/uikit/SDL_uikitview.m @@ -65,19 +65,12 @@ void _uikit_keyboard_init(); { CGPoint point = [touch locationInView: self]; - /* Get the display scale and apply that to the input coordinates */ - SDL_Window *window = viewcontroller.window; - SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window); - SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata; - if (normalize) { CGRect bounds = [self bounds]; point.x /= bounds.size.width; point.y /= bounds.size.height; - } else { - point.x *= displaymodedata->scale; - point.y *= displaymodedata->scale; } + return point; } @@ -360,7 +353,6 @@ void _uikit_keyboard_update() { int height = view.keyboardHeight; int offsetx = 0; int offsety = 0; - float scale = [UIScreen mainScreen].scale; if (height) { int sw,sh; SDL_GetWindowSize(window,&sw,&sh); @@ -382,9 +374,6 @@ void _uikit_keyboard_update() { offsety = -offsety; } - offsetx /= scale; - offsety /= scale; - view.frame = CGRectMake(offsetx,offsety,view.frame.size.width,view.frame.size.height); } @@ -412,7 +401,6 @@ void _uikit_keyboard_init() { if (ui_orient == UIInterfaceOrientationLandscapeRight || ui_orient == UIInterfaceOrientationLandscapeLeft) { height = keyboardSize.width; } - height *= [UIScreen mainScreen].scale; _uikit_keyboard_set_height(height); } ]; diff --git a/src/video/uikit/SDL_uikitviewcontroller.h b/src/video/uikit/SDL_uikitviewcontroller.h index e8d595d9b..cc14d4c40 100644 --- a/src/video/uikit/SDL_uikitviewcontroller.h +++ b/src/video/uikit/SDL_uikitviewcontroller.h @@ -28,7 +28,7 @@ SDL_Window *window; } -@property (readwrite) SDL_Window *window; +@property (nonatomic, readwrite) SDL_Window *window; - (id)initWithSDLWindow:(SDL_Window *)_window; - (void)loadView; diff --git a/src/video/uikit/SDL_uikitviewcontroller.m b/src/video/uikit/SDL_uikitviewcontroller.m index 447b80b80..3bedcc4ec 100644 --- a/src/video/uikit/SDL_uikitviewcontroller.m +++ b/src/video/uikit/SDL_uikitviewcontroller.m @@ -56,15 +56,11 @@ - (void)viewDidLayoutSubviews { - if (self->window->flags & SDL_WINDOW_RESIZABLE) { - SDL_WindowData *data = self->window->driverdata; - SDL_VideoDisplay *display = SDL_GetDisplayForWindow(self->window); - SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata; + if (window->flags & SDL_WINDOW_RESIZABLE) { + SDL_WindowData *data = window->driverdata; const CGSize size = data->view.bounds.size; - int w, h; - - w = (int)(size.width * displaymodedata->scale); - h = (int)(size.height * displaymodedata->scale); + int w = (int) size.width; + int h = (int) size.height; 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 2b1567778..8c348353c 100644 --- a/src/video/uikit/SDL_uikitwindow.m +++ b/src/video/uikit/SDL_uikitwindow.m @@ -47,7 +47,6 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created) { SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window); - SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata; SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata; SDL_WindowData *data; @@ -72,9 +71,9 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo bounds = [displaydata->uiscreen applicationFrame]; } - /* Get frame dimensions in pixels */ - int width = (int)(bounds.size.width * displaymodedata->scale); - int height = (int)(bounds.size.height * displaymodedata->scale); + /* Get frame dimensions */ + int width = (int) bounds.size.width; + int height = (int) bounds.size.height; /* Make sure the width/height are oriented correctly */ if (UIKit_IsDisplayLandscape(displaydata->uiscreen) != (width > height)) { @@ -191,8 +190,7 @@ UIKit_CreateWindow(_THIS, SDL_Window *window) /* ignore the size user requested, and make a fullscreen window */ /* !!! FIXME: can we have a smaller view? */ - UIWindow *uiwindow = [UIWindow alloc]; - uiwindow = [uiwindow initWithFrame:[data->uiscreen bounds]]; + UIWindow *uiwindow = [[UIWindow alloc] 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 @@ -243,7 +241,6 @@ void UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen) { SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata; - SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata; UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow; if (fullscreen) { @@ -259,9 +256,9 @@ UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display bounds = [displaydata->uiscreen applicationFrame]; } - /* Get frame dimensions in pixels */ - int width = (int)(bounds.size.width * displaymodedata->scale); - int height = (int)(bounds.size.height * displaymodedata->scale); + /* Get frame dimensions */ + int width = (int) bounds.size.width; + int height = (int) bounds.size.height; /* 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.