Change: [OSX] Render screen in full native resolution on HiDPI displays.

This commit is contained in:
Michael Lutz 2021-01-07 21:18:25 +01:00
parent 8906e9e0fd
commit 0fc763bc55
4 changed files with 45 additions and 6 deletions

View File

@ -29,5 +29,7 @@
<string>Copyright 2004-${CURRENT_YEAR} The OpenTTD team</string> <string>Copyright 2004-${CURRENT_YEAR} The OpenTTD team</string>
<key>NSPrincipalClass</key> <key>NSPrincipalClass</key>
<string>NSApplication</string> <string>NSApplication</string>
<key>NSHighResolutionCapable</key>
<string>True</string>
</dict> </dict>
</plist> </plist>

View File

@ -322,6 +322,11 @@ void VideoDriver_Cocoa::GameSizeChanged()
BlitterFactory::GetCurrentBlitter()->PostResize(); BlitterFactory::GetCurrentBlitter()->PostResize();
::GameSizeChanged(); ::GameSizeChanged();
/* We need to store the window size as non-Retina size in
* the config file to get same windows size on next start. */
_cur_resolution.width = [ this->cocoaview frame ].size.width;
_cur_resolution.height = [ this->cocoaview frame ].size.height;
} }
/** /**
@ -491,7 +496,7 @@ void VideoDriver_Cocoa::Draw(bool force_update)
/* Normally drawRect will be automatically called by Mac OS X during next update cycle, /* Normally drawRect will be automatically called by Mac OS X during next update cycle,
* and then blitting will occur. If force_update is true, it will be done right now. */ * and then blitting will occur. If force_update is true, it will be done right now. */
[ this->cocoaview setNeedsDisplayInRect:dirtyrect ]; [ this->cocoaview setNeedsDisplayInRect:[ this->cocoaview getVirtualRect:dirtyrect ] ];
if (force_update) [ this->cocoaview displayIfNeeded ]; if (force_update) [ this->cocoaview displayIfNeeded ];
} }
@ -530,7 +535,7 @@ void VideoDriver_Cocoa::AllocateBackingStore()
{ {
if (this->window == nil || this->cocoaview == nil || this->setup) return; if (this->window == nil || this->cocoaview == nil || this->setup) return;
NSRect newframe = [ this->cocoaview frame ]; NSRect newframe = [ this->cocoaview getRealRect:[ this->cocoaview frame ] ];
this->window_width = (int)newframe.size.width; this->window_width = (int)newframe.size.width;
this->window_height = (int)newframe.size.height; this->window_height = (int)newframe.size.height;
@ -743,6 +748,13 @@ void VideoDriver_Cocoa::GameLoop()
CGImageRelease(fullImage); CGImageRelease(fullImage);
} }
- (void)viewDidChangeBackingProperties
{
[ super viewDidChangeBackingProperties ];
self.layer.contentsScale = [ driver->cocoaview getContentsScale ];
}
@end @end
#endif /* WITH_COCOA */ #endif /* WITH_COCOA */

View File

@ -38,6 +38,9 @@ extern NSString *OTTDMainLaunchGameEngine;
/** Subclass of NSView to support mouse awareness and text input. */ /** Subclass of NSView to support mouse awareness and text input. */
@interface OTTD_CocoaView : NSView <NSTextInputClient> @interface OTTD_CocoaView : NSView <NSTextInputClient>
- (NSRect)getRealRect:(NSRect)rect;
- (NSRect)getVirtualRect:(NSRect)rect;
- (CGFloat)getContentsScale;
- (NSPoint)mousePositionFromEvent:(NSEvent *)e; - (NSPoint)mousePositionFromEvent:(NSEvent *)e;
@end @end

View File

@ -417,6 +417,30 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
float _current_magnification; float _current_magnification;
NSUInteger _current_mods; NSUInteger _current_mods;
bool _emulated_down; bool _emulated_down;
bool _use_hidpi;
}
- (instancetype)initWithFrame:(NSRect)frameRect
{
if (self = [ super initWithFrame:frameRect ]) {
self->_use_hidpi = [ self respondsToSelector:@selector(convertRectToBacking:) ] && [ self respondsToSelector:@selector(convertRectFromBacking:) ];
}
return self;
}
- (NSRect)getRealRect:(NSRect)rect
{
return _use_hidpi ? [ self convertRectToBacking:rect ] : rect;
}
- (NSRect)getVirtualRect:(NSRect)rect
{
return _use_hidpi ? [ self convertRectFromBacking:rect ] : rect;
}
- (CGFloat)getContentsScale
{
return _use_hidpi && self.window != nil ? [ self.window backingScaleFactor ] : 1.0f;
} }
/** /**
@ -483,15 +507,13 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
if ([ e window ] == nil) pt = [ self.window convertRectFromScreen:NSMakeRect(pt.x, pt.y, 0, 0) ].origin; if ([ e window ] == nil) pt = [ self.window convertRectFromScreen:NSMakeRect(pt.x, pt.y, 0, 0) ].origin;
pt = [ self convertPoint:pt fromView:nil ]; pt = [ self convertPoint:pt fromView:nil ];
pt.y = self.bounds.size.height - pt.y; return [ self getRealRect:NSMakeRect(pt.x, self.bounds.size.height - pt.y, 0, 0) ].origin;
return pt;
} }
- (void)internalMouseMoveEvent:(NSEvent *)event - (void)internalMouseMoveEvent:(NSEvent *)event
{ {
if (_cursor.fix_at) { if (_cursor.fix_at) {
_cursor.UpdateCursorPositionRelative(event.deltaX, event.deltaY); _cursor.UpdateCursorPositionRelative(event.deltaX * self.getContentsScale, event.deltaY * self.getContentsScale);
} else { } else {
NSPoint pt = [ self mousePositionFromEvent:event ]; NSPoint pt = [ self mousePositionFromEvent:event ];
_cursor.UpdateCursorPosition(pt.x, pt.y, false); _cursor.UpdateCursorPosition(pt.x, pt.y, false);