From bdd7db354d86b5319a8bb3654d96f3858d3f4acc Mon Sep 17 00:00:00 2001 From: Michael Buckley Date: Wed, 18 Mar 2020 20:14:57 -0700 Subject: [PATCH] Mac: Fix redraw issues on dual monitor setups when closing and reopening the window --- macosx/Snes9x/AppDelegate.m | 19 +++++++++++-------- macosx/mac-os.h | 2 ++ macosx/mac-os.mm | 25 ++++++++++++++++--------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/macosx/Snes9x/AppDelegate.m b/macosx/Snes9x/AppDelegate.m index 46575a5a..ccc206a6 100644 --- a/macosx/Snes9x/AppDelegate.m +++ b/macosx/Snes9x/AppDelegate.m @@ -52,13 +52,6 @@ static NSWindowFrameAutosaveName const kMainWindowIdentifier = @"s9xMainWindow"; window.frameAutosaveName = kMainWindowIdentifier; window.releasedWhenClosed = NO; - [window.contentView addSubview:s9xView]; - [s9xView.topAnchor constraintEqualToAnchor:window.contentView.topAnchor].active = YES; - [s9xView.bottomAnchor constraintEqualToAnchor:window.contentView.bottomAnchor].active = YES; - [s9xView.centerXAnchor constraintEqualToAnchor:window.contentView.centerXAnchor].active = YES; - [s9xView.leftAnchor constraintGreaterThanOrEqualToAnchor:window.contentView.leftAnchor].active = YES; - [s9xView.rightAnchor constraintLessThanOrEqualToAnchor:window.contentView.rightAnchor].active = YES; - if ( ![window setFrameUsingName:kMainWindowIdentifier] ) { [window center]; @@ -456,8 +449,18 @@ static NSWindowFrameAutosaveName const kMainWindowIdentifier = @"s9xMainWindow"; { if ([self.s9xEngine loadROM:url]) { + [self.s9xEngine recreateS9xView]; + + NSWindow *window = self.window; + [window.contentView addSubview:s9xView]; + [s9xView.topAnchor constraintEqualToAnchor:window.contentView.topAnchor].active = YES; + [s9xView.bottomAnchor constraintEqualToAnchor:window.contentView.bottomAnchor].active = YES; + [s9xView.centerXAnchor constraintEqualToAnchor:window.contentView.centerXAnchor].active = YES; + [s9xView.leftAnchor constraintGreaterThanOrEqualToAnchor:window.contentView.leftAnchor].active = YES; + [s9xView.rightAnchor constraintLessThanOrEqualToAnchor:window.contentView.rightAnchor].active = YES; - [self.window makeKeyAndOrderFront:self]; + + [window makeKeyAndOrderFront:self]; [NSDocumentController.sharedDocumentController noteNewRecentDocumentURL:url]; return YES; } diff --git a/macosx/mac-os.h b/macosx/mac-os.h index 2e094fbd..3b6e9e9f 100644 --- a/macosx/mac-os.h +++ b/macosx/mac-os.h @@ -164,6 +164,8 @@ extern id inputDelegate; @property (nonatomic, weak) id inputDelegate; +- (void)recreateS9xView; + - (void)start; - (void)stop; diff --git a/macosx/mac-os.mm b/macosx/mac-os.mm index b74454a7..9828acd7 100644 --- a/macosx/mac-os.mm +++ b/macosx/mac-os.mm @@ -2976,15 +2976,7 @@ void QuitWithFatalError ( NSString *message) if (self = [super init]) { Initialize(); - - CGRect frame = NSMakeRect(0, 0, SNES_WIDTH * 2, SNES_HEIGHT * 2); - s9xView = [[S9xView alloc] initWithFrame:frame]; - s9xView.translatesAutoresizingMaskIntoConstraints = NO; - s9xView.autoresizingMask = NSViewWidthSizable|NSViewHeightSizable; - [s9xView addConstraint:[NSLayoutConstraint constraintWithItem:s9xView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:s9xView attribute:NSLayoutAttributeWidth multiplier:(CGFloat)SNES_HEIGHT/(CGFloat)SNES_WIDTH constant:0.0]]; - [s9xView addConstraint:[NSLayoutConstraint constraintWithItem:s9xView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SNES_WIDTH * 2.0]]; - [s9xView addConstraint:[NSLayoutConstraint constraintWithItem:s9xView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SNES_HEIGHT * 2.0]]; - s9xView.device = MTLCreateSystemDefaultDevice(); + [self recreateS9xView]; } return self; @@ -2995,6 +2987,21 @@ void QuitWithFatalError ( NSString *message) Deinitialize(); } +- (void)recreateS9xView +{ + [s9xView removeFromSuperview]; + S9xDeinitDisplay(); + CGRect frame = NSMakeRect(0, 0, SNES_WIDTH * 2, SNES_HEIGHT * 2); + s9xView = [[S9xView alloc] initWithFrame:frame]; + s9xView.translatesAutoresizingMaskIntoConstraints = NO; + s9xView.autoresizingMask = NSViewWidthSizable|NSViewHeightSizable; + [s9xView addConstraint:[NSLayoutConstraint constraintWithItem:s9xView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:s9xView attribute:NSLayoutAttributeWidth multiplier:(CGFloat)SNES_HEIGHT/(CGFloat)SNES_WIDTH constant:0.0]]; + [s9xView addConstraint:[NSLayoutConstraint constraintWithItem:s9xView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SNES_WIDTH * 2.0]]; + [s9xView addConstraint:[NSLayoutConstraint constraintWithItem:s9xView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SNES_HEIGHT * 2.0]]; + s9xView.device = MTLCreateSystemDefaultDevice(); + S9xInitDisplay(NULL, NULL); +} + - (void)start { #ifdef DEBUGGER