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.
This commit is contained in:
Alex Szpakowski 2014-07-14 22:35:48 -03:00
parent 734b523302
commit 3672409c51
11 changed files with 77 additions and 82 deletions

View File

@ -28,13 +28,11 @@
typedef struct typedef struct
{ {
UIScreen *uiscreen; UIScreen *uiscreen;
CGFloat scale;
} SDL_DisplayData; } SDL_DisplayData;
typedef struct typedef struct
{ {
UIScreenMode *uiscreenmode; UIScreenMode *uiscreenmode;
CGFloat scale;
} SDL_DisplayModeData; } SDL_DisplayModeData;
extern SDL_bool UIKit_IsDisplayLandscape(UIScreen *uiscreen); extern SDL_bool UIKit_IsDisplayLandscape(UIScreen *uiscreen);

View File

@ -28,7 +28,7 @@
static int static int
UIKit_AllocateDisplayModeData(SDL_DisplayMode * mode, UIKit_AllocateDisplayModeData(SDL_DisplayMode * mode,
UIScreenMode * uiscreenmode, CGFloat scale) UIScreenMode * uiscreenmode)
{ {
SDL_DisplayModeData *data = NULL; SDL_DisplayModeData *data = NULL;
@ -41,8 +41,6 @@ UIKit_AllocateDisplayModeData(SDL_DisplayMode * mode,
data->uiscreenmode = uiscreenmode; data->uiscreenmode = uiscreenmode;
[data->uiscreenmode retain]; [data->uiscreenmode retain];
data->scale = scale;
} }
mode->driverdata = data; mode->driverdata = data;
@ -63,14 +61,14 @@ UIKit_FreeDisplayModeData(SDL_DisplayMode * mode)
static int static int
UIKit_AddSingleDisplayMode(SDL_VideoDisplay * display, int w, int h, UIKit_AddSingleDisplayMode(SDL_VideoDisplay * display, int w, int h,
UIScreenMode * uiscreenmode, CGFloat scale) UIScreenMode * uiscreenmode)
{ {
SDL_DisplayMode mode; SDL_DisplayMode mode;
SDL_zero(mode); SDL_zero(mode);
mode.format = SDL_PIXELFORMAT_ABGR8888; mode.format = SDL_PIXELFORMAT_ABGR8888;
mode.refresh_rate = 0; mode.refresh_rate = 0;
if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode, scale) < 0) { if (UIKit_AllocateDisplayModeData(&mode, uiscreenmode) < 0) {
return -1; return -1;
} }
@ -85,16 +83,16 @@ UIKit_AddSingleDisplayMode(SDL_VideoDisplay * display, int w, int h,
} }
static int 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) UIScreenMode * uiscreenmode, SDL_bool addRotation)
{ {
if (UIKit_AddSingleDisplayMode(display, w, h, uiscreenmode, scale) < 0) { if (UIKit_AddSingleDisplayMode(display, w, h, uiscreenmode) < 0) {
return -1; return -1;
} }
if (addRotation) { if (addRotation) {
/* Add the rotated version */ /* Add the rotated version */
if (UIKit_AddSingleDisplayMode(display, h, w, uiscreenmode, scale) < 0) { if (UIKit_AddSingleDisplayMode(display, h, w, uiscreenmode) < 0) {
return -1; return -1;
} }
} }
@ -114,24 +112,16 @@ UIKit_AddDisplay(UIScreen *uiscreen)
size.height = height; 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_VideoDisplay display;
SDL_DisplayMode mode; SDL_DisplayMode mode;
SDL_zero(mode); SDL_zero(mode);
mode.format = SDL_PIXELFORMAT_ABGR8888; mode.format = SDL_PIXELFORMAT_ABGR8888;
mode.w = (int)(size.width * scale); mode.w = (int) size.width;
mode.h = (int)(size.height * scale); 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; return -1;
} }
@ -148,7 +138,6 @@ UIKit_AddDisplay(UIScreen *uiscreen)
[uiscreen retain]; [uiscreen retain];
data->uiscreen = uiscreen; data->uiscreen = uiscreen;
data->scale = scale;
display.driverdata = data; display.driverdata = data;
SDL_AddVideoDisplay(&display); SDL_AddVideoDisplay(&display);
@ -186,11 +175,14 @@ UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
SDL_bool isLandscape = UIKit_IsDisplayLandscape(data->uiscreen); SDL_bool isLandscape = UIKit_IsDisplayLandscape(data->uiscreen);
SDL_bool addRotation = (data->uiscreen == [UIScreen mainScreen]); SDL_bool addRotation = (data->uiscreen == [UIScreen mainScreen]);
CGFloat scale = data->uiscreen.scale;
for (UIScreenMode *uimode in [data->uiscreen availableModes]) { 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]; CGSize size = [uimode size];
int w = (int)size.width; int w = (int)(size.width / scale);
int h = (int)size.height; int h = (int)(size.height / scale);
/* Make sure the width/height are oriented correctly */ /* Make sure the width/height are oriented correctly */
if (isLandscape != (w > h)) { if (isLandscape != (w > h)) {
@ -200,17 +192,7 @@ UIKit_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
} }
/* Add the native screen resolution. */ /* Add the native screen resolution. */
UIKit_AddDisplayMode(display, w, h, data->scale, uimode, addRotation); UIKit_AddDisplayMode(display, w, h, 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);
}
} }
} }

View File

@ -25,6 +25,8 @@
extern int UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, extern int UIKit_GL_MakeCurrent(_THIS, SDL_Window * window,
SDL_GLContext context); 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 void UIKit_GL_SwapWindow(_THIS, SDL_Window * window);
extern SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window); extern SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window);
extern void UIKit_GL_DeleteContext(_THIS, SDL_GLContext context); extern void UIKit_GL_DeleteContext(_THIS, SDL_GLContext context);

View File

@ -49,7 +49,8 @@ UIKit_GL_GetProcAddress(_THIS, const char *proc)
/* /*
note that SDL_GL_Delete context makes it current without passing the window 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) { if (context) {
SDL_WindowData *data = (SDL_WindowData *)window->driverdata; SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
@ -62,6 +63,19 @@ int UIKit_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
return 0; 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 int
UIKit_GL_LoadLibrary(_THIS, const char *path) 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_uikitopenglview *view;
SDL_WindowData *data = (SDL_WindowData *) window->driverdata; 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; UIWindow *uiwindow = data->uiwindow;
EAGLSharegroup *share_group = nil; 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) { if (_this->gl_config.share_with_current_context) {
SDL_uikitopenglview *view = (SDL_uikitopenglview *) SDL_GL_GetCurrentContext(); 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 */ /* construct our view, passing in SDL's OpenGL configuration data */
CGRect frame; CGRect frame;
if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) { if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
frame = [displaydata->uiscreen bounds]; frame = [[uiwindow screen] bounds];
} else { } else {
frame = [displaydata->uiscreen applicationFrame]; frame = [[uiwindow screen] applicationFrame];
} }
view = [[SDL_uikitopenglview alloc] initWithFrame: frame view = [[SDL_uikitopenglview alloc] initWithFrame: frame
scale: displaymodedata->scale scale: scale
retainBacking: _this->gl_config.retained_backing retainBacking: _this->gl_config.retained_backing
rBits: _this->gl_config.red_size rBits: _this->gl_config.red_size
gBits: _this->gl_config.green_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 */ /* Make this window the current mouse focus for touch input */
if (displaydata->uiscreen == [UIScreen mainScreen]) { if ([uiwindow screen] == [UIScreen mainScreen]) {
SDL_SetMouseFocus(window); SDL_SetMouseFocus(window);
SDL_SetKeyboardFocus(window); SDL_SetKeyboardFocus(window);
} }
@ -160,7 +181,8 @@ SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window)
return view; 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 */ /* the delegate has retained the view, this will release him */
SDL_uikitopenglview *view = (SDL_uikitopenglview *)context; SDL_uikitopenglview *view = (SDL_uikitopenglview *)context;

View File

@ -55,6 +55,10 @@
@property (nonatomic, retain, readonly) EAGLContext *context; @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)swapBuffers;
- (void)setCurrentContext; - (void)setCurrentContext;

View File

@ -32,6 +32,9 @@
@synthesize context; @synthesize context;
@synthesize backingWidth = backingWidth;
@synthesize backingHeight = backingHeight;
+ (Class)layerClass + (Class)layerClass
{ {
return [CAEAGLLayer class]; return [CAEAGLLayer class];
@ -74,7 +77,9 @@
eaglLayer.opaque = YES; eaglLayer.opaque = YES;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: 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]; context = [[EAGLContext alloc] initWithAPI:api sharegroup:shareGroup];
if (!context || ![EAGLContext setCurrentContext:context]) { if (!context || ![EAGLContext setCurrentContext:context]) {

View File

@ -94,6 +94,7 @@ UIKit_CreateDevice(int devindex)
/* OpenGL (ES) functions */ /* OpenGL (ES) functions */
device->GL_MakeCurrent = UIKit_GL_MakeCurrent; device->GL_MakeCurrent = UIKit_GL_MakeCurrent;
device->GL_GetDrawableSize = UIKit_GL_GetDrawableSize;
device->GL_SwapWindow = UIKit_GL_SwapWindow; device->GL_SwapWindow = UIKit_GL_SwapWindow;
device->GL_CreateContext = UIKit_GL_CreateContext; device->GL_CreateContext = UIKit_GL_CreateContext;
device->GL_DeleteContext = UIKit_GL_DeleteContext; device->GL_DeleteContext = UIKit_GL_DeleteContext;

View File

@ -65,19 +65,12 @@ void _uikit_keyboard_init();
{ {
CGPoint point = [touch locationInView: self]; 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) { if (normalize) {
CGRect bounds = [self bounds]; CGRect bounds = [self bounds];
point.x /= bounds.size.width; point.x /= bounds.size.width;
point.y /= bounds.size.height; point.y /= bounds.size.height;
} else {
point.x *= displaymodedata->scale;
point.y *= displaymodedata->scale;
} }
return point; return point;
} }
@ -360,7 +353,6 @@ void _uikit_keyboard_update() {
int height = view.keyboardHeight; int height = view.keyboardHeight;
int offsetx = 0; int offsetx = 0;
int offsety = 0; int offsety = 0;
float scale = [UIScreen mainScreen].scale;
if (height) { if (height) {
int sw,sh; int sw,sh;
SDL_GetWindowSize(window,&sw,&sh); SDL_GetWindowSize(window,&sw,&sh);
@ -382,9 +374,6 @@ void _uikit_keyboard_update() {
offsety = -offsety; offsety = -offsety;
} }
offsetx /= scale;
offsety /= scale;
view.frame = CGRectMake(offsetx,offsety,view.frame.size.width,view.frame.size.height); 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) { if (ui_orient == UIInterfaceOrientationLandscapeRight || ui_orient == UIInterfaceOrientationLandscapeLeft) {
height = keyboardSize.width; height = keyboardSize.width;
} }
height *= [UIScreen mainScreen].scale;
_uikit_keyboard_set_height(height); _uikit_keyboard_set_height(height);
} }
]; ];

View File

@ -28,7 +28,7 @@
SDL_Window *window; SDL_Window *window;
} }
@property (readwrite) SDL_Window *window; @property (nonatomic, readwrite) SDL_Window *window;
- (id)initWithSDLWindow:(SDL_Window *)_window; - (id)initWithSDLWindow:(SDL_Window *)_window;
- (void)loadView; - (void)loadView;

View File

@ -56,15 +56,11 @@
- (void)viewDidLayoutSubviews - (void)viewDidLayoutSubviews
{ {
if (self->window->flags & SDL_WINDOW_RESIZABLE) { if (window->flags & SDL_WINDOW_RESIZABLE) {
SDL_WindowData *data = self->window->driverdata; SDL_WindowData *data = window->driverdata;
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(self->window);
SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
const CGSize size = data->view.bounds.size; const CGSize size = data->view.bounds.size;
int w, h; int w = (int) size.width;
int h = (int) size.height;
w = (int)(size.width * displaymodedata->scale);
h = (int)(size.height * displaymodedata->scale);
SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h); SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
} }

View File

@ -47,7 +47,6 @@
static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created) static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bool created)
{ {
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window); SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata; SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
SDL_WindowData *data; SDL_WindowData *data;
@ -72,9 +71,9 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo
bounds = [displaydata->uiscreen applicationFrame]; bounds = [displaydata->uiscreen applicationFrame];
} }
/* Get frame dimensions in pixels */ /* Get frame dimensions */
int width = (int)(bounds.size.width * displaymodedata->scale); int width = (int) bounds.size.width;
int height = (int)(bounds.size.height * displaymodedata->scale); int height = (int) bounds.size.height;
/* Make sure the width/height are oriented correctly */ /* Make sure the width/height are oriented correctly */
if (UIKit_IsDisplayLandscape(displaydata->uiscreen) != (width > height)) { 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 */ /* ignore the size user requested, and make a fullscreen window */
/* !!! FIXME: can we have a smaller view? */ /* !!! FIXME: can we have a smaller view? */
UIWindow *uiwindow = [UIWindow alloc]; UIWindow *uiwindow = [[UIWindow alloc] initWithFrame:[data->uiscreen bounds]];
uiwindow = [uiwindow initWithFrame:[data->uiscreen bounds]];
/* put the window on an external display if appropriate. This implicitly /* put the window on an external display if appropriate. This implicitly
* does [uiwindow setframe:[uiscreen bounds]], so don't do it on the * 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) UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
{ {
SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata; SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow; UIWindow *uiwindow = ((SDL_WindowData *) window->driverdata)->uiwindow;
if (fullscreen) { if (fullscreen) {
@ -259,9 +256,9 @@ UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display
bounds = [displaydata->uiscreen applicationFrame]; bounds = [displaydata->uiscreen applicationFrame];
} }
/* Get frame dimensions in pixels */ /* Get frame dimensions */
int width = (int)(bounds.size.width * displaymodedata->scale); int width = (int) bounds.size.width;
int height = (int)(bounds.size.height * displaymodedata->scale); int height = (int) bounds.size.height;
/* We can pick either width or height here and we'll rotate the /* 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. screen to match, so we pick the closest to what we wanted.