From 2888d9c56eba4dedb93a84a3c57957e4a907d428 Mon Sep 17 00:00:00 2001 From: jqdg Date: Sun, 15 Aug 2021 09:27:32 -0700 Subject: [PATCH] Improve high DPI handling on macOS --- src/SFML/Window/OSX/SFOpenGLView.h | 3 ++- src/SFML/Window/OSX/SFOpenGLView.mm | 18 ++++++++++++++---- src/SFML/Window/OSX/SFWindowController.h | 1 + src/SFML/Window/OSX/SFWindowController.mm | 11 ++++++++--- src/SFML/Window/OSX/WindowImplCocoa.mm | 4 ---- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/SFML/Window/OSX/SFOpenGLView.h b/src/SFML/Window/OSX/SFOpenGLView.h index 978586a0c..38145f5a0 100644 --- a/src/SFML/Window/OSX/SFOpenGLView.h +++ b/src/SFML/Window/OSX/SFOpenGLView.h @@ -78,6 +78,7 @@ namespace sf { BOOL m_cursorGrabbed; ///< Is the mouse cursor trapped? CGFloat m_deltaXBuffer; ///< See note about cursor grabbing above CGFloat m_deltaYBuffer; ///< See note about cursor grabbing above + BOOL m_highDpi; ///< Is high-DPI enabled? // Hidden text view used to convert key event to actual chars. // We use a silent responder to prevent sound alerts. @@ -99,7 +100,7 @@ namespace sf { /// \return an initialized view /// //////////////////////////////////////////////////////////// --(id)initWithFrame:(NSRect)frameRect fullscreen:(BOOL)isFullscreen; +-(id)initWithFrame:(NSRect)frameRect fullscreen:(BOOL)isFullscreen highDpi:(BOOL)isHighDpi; //////////////////////////////////////////////////////////// /// \brief Finish the creation of the SFML OpenGL view diff --git a/src/SFML/Window/OSX/SFOpenGLView.mm b/src/SFML/Window/OSX/SFOpenGLView.mm index 612a1cc93..e0245a8e4 100644 --- a/src/SFML/Window/OSX/SFOpenGLView.mm +++ b/src/SFML/Window/OSX/SFOpenGLView.mm @@ -86,11 +86,17 @@ //////////////////////////////////////////////////////// -(id)initWithFrame:(NSRect)frameRect { - return [self initWithFrame:frameRect fullscreen:NO]; + return [self initWithFrame:frameRect fullscreen:NO highDpi:NO]; } //////////////////////////////////////////////////////// -(id)initWithFrame:(NSRect)frameRect fullscreen:(BOOL)isFullscreen +{ + return [self initWithFrame:frameRect fullscreen:isFullscreen highDpi:NO]; +} + +//////////////////////////////////////////////////////// +-(id)initWithFrame:(NSRect)frameRect fullscreen:(BOOL)isFullscreen highDpi:(BOOL)isHighDpi { if ((self = [super initWithFrame:frameRect])) { @@ -118,8 +124,12 @@ m_hiddenTextView = [[NSTextView alloc] initWithFrame:NSZeroRect]; [m_hiddenTextView setNextResponder:m_silentResponder]; - // Request high resolution on high DPI displays - [self setWantsBestResolutionOpenGLSurface:YES]; + // If high DPI is requested, then use high resolution for the OpenGL view. + // Currently, isHighDpi is always expected to be NO, and so the OpenGL view will render scaled. + // Note: setWantsBestResolutionOpenGLSurface requires YES to work properly, rather than simply non-zero value. + // isHighDpi is an Objective-C BOOL, which can have non-zero values other than YES. + m_highDpi = isHighDpi ? YES : NO; + [self setWantsBestResolutionOpenGLSurface:m_highDpi]; // At that point, the view isn't attached to a window. We defer the rest of // the initialization process to later. @@ -224,7 +234,7 @@ NSWindow* window = [self window]; NSScreen* screen = window ? [window screen] : [NSScreen mainScreen]; CGFloat oldScaleFactor = m_scaleFactor; - m_scaleFactor = [screen backingScaleFactor]; + m_scaleFactor = m_highDpi ? [screen backingScaleFactor] : 1.0; // Send a resize event if the scaling factor changed if ((m_scaleFactor != oldScaleFactor) && (m_requester != 0)) { diff --git a/src/SFML/Window/OSX/SFWindowController.h b/src/SFML/Window/OSX/SFWindowController.h index 5dac50173..531a93f61 100644 --- a/src/SFML/Window/OSX/SFWindowController.h +++ b/src/SFML/Window/OSX/SFWindowController.h @@ -62,6 +62,7 @@ namespace sf { sf::priv::WindowImplCocoa* m_requester; ///< Requester BOOL m_fullscreen; ///< Indicate whether the window is fullscreen or not BOOL m_restoreResize; ///< See note above + BOOL m_highDpi; ///< Support high-DPI rendering or not } //////////////////////////////////////////////////////////// diff --git a/src/SFML/Window/OSX/SFWindowController.mm b/src/SFML/Window/OSX/SFWindowController.mm index 4ba0e3d2d..ea05f15e8 100644 --- a/src/SFML/Window/OSX/SFWindowController.mm +++ b/src/SFML/Window/OSX/SFWindowController.mm @@ -99,6 +99,7 @@ m_requester = 0; m_fullscreen = NO; // assuming this is the case... too hard to handle anyway. m_restoreResize = NO; + m_highDpi = NO; // Retain the window for our own use. m_window = [window retain]; @@ -111,7 +112,8 @@ // Create the view. m_oglView = [[SFOpenGLView alloc] initWithFrame:[[m_window contentView] frame] - fullscreen:NO]; + fullscreen:NO + highDpi:NO]; if (m_oglView == nil) { @@ -153,6 +155,7 @@ m_requester = 0; m_fullscreen = (style & sf::Style::Fullscreen); m_restoreResize = NO; + m_highDpi = NO; if (m_fullscreen) [self setupFullscreenViewWithMode:mode]; @@ -218,7 +221,8 @@ NSRect oglRect = NSMakeRect(x, y, width, height); m_oglView = [[SFOpenGLView alloc] initWithFrame:oglRect - fullscreen:YES]; + fullscreen:YES + highDpi:m_highDpi]; if (m_oglView == nil) { @@ -277,7 +281,8 @@ // Create the view. m_oglView = [[SFOpenGLView alloc] initWithFrame:[[m_window contentView] frame] - fullscreen:NO]; + fullscreen:NO + highDpi:m_highDpi]; if (m_oglView == nil) { diff --git a/src/SFML/Window/OSX/WindowImplCocoa.mm b/src/SFML/Window/OSX/WindowImplCocoa.mm index 94e38dce3..ce524ae67 100644 --- a/src/SFML/Window/OSX/WindowImplCocoa.mm +++ b/src/SFML/Window/OSX/WindowImplCocoa.mm @@ -130,9 +130,6 @@ m_showCursor(true) // Transform the app process. setUpProcess(); - // Use backing size - scaleInWidthHeight(mode, nil); - m_delegate = [[SFWindowController alloc] initWithMode:mode andStyle:style]; [m_delegate changeTitle:sfStringToNSString(title)]; [m_delegate setRequesterTo:this]; @@ -544,4 +541,3 @@ bool WindowImplCocoa::hasFocus() const } // namespace priv } // namespace sf -