diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 24e99a32..c65b69e5 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -159,40 +159,9 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, return [[NSBundle mainBundle] pathForResource:name ofType:@"bin"]; } -/* Todo: Unify the 4 init functions */ -- (void) initDMG -{ - current_model = MODEL_DMG; - GB_init(&gb, cocoa_to_internal_model[current_model]); - GB_load_boot_rom(&gb, [[self bootROMPathForName:@"dmg_boot"] UTF8String]); - [self initCommon]; -} - -- (void) initSGB -{ - current_model = MODEL_SGB; - GB_init(&gb, cocoa_to_internal_model[current_model]); - GB_load_boot_rom(&gb, [[self bootROMPathForName:@"sgb_boot"] UTF8String]); - [self initCommon]; -} - -- (void) initCGB -{ - if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateAGB"]) { - current_model = MODEL_AGB; - GB_init(&gb, cocoa_to_internal_model[current_model]); - GB_load_boot_rom(&gb, [[self bootROMPathForName:@"agb_boot"] UTF8String]); - } - else { - current_model = MODEL_CGB; - GB_init(&gb, cocoa_to_internal_model[current_model]); - GB_load_boot_rom(&gb, [[self bootROMPathForName:@"cgb_boot"] UTF8String]); - } - [self initCommon]; -} - - (void) initCommon { + GB_init(&gb, cocoa_to_internal_model[current_model]); GB_set_user_data(&gb, (__bridge void *)(self)); GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank); GB_set_log_callback(&gb, (GB_log_callback_t) consoleLog); @@ -204,7 +173,6 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, GB_set_camera_update_request_callback(&gb, cameraRequestUpdate); GB_set_highpass_filter_mode(&gb, (GB_highpass_mode_t) [[NSUserDefaults standardUserDefaults] integerForKey:@"GBHighpassFilter"]); GB_set_rewind_length(&gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBRewindLength"]); - [self loadROM]; } - (void) vblank @@ -225,8 +193,6 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, - (void) run { running = true; - self.view.gb = &gb; - [self.view screenSizeChanged]; GB_set_pixels_output(&gb, self.view.pixels); GB_set_sample_rate(&gb, 96000); self.audioClient = [[GBAudioClient alloc] initWithRendererBlock:^(UInt32 sampleRate, UInt32 nFrames, GB_sample_t *buffer) { @@ -277,23 +243,32 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, GB_debugger_set_disabled(&gb, false); } +- (void) loadBootROM +{ + static NSString * const boot_names[] = {@"dmg_boot", @"cgb_boot", @"agb_boot", @"sgb_boot"}; + GB_load_boot_rom(&gb, [[self bootROMPathForName:boot_names[current_model - 1]] UTF8String]); +} + - (IBAction)reset:(id)sender { [self stop]; - + size_t old_width = GB_get_screen_width(&gb); + [self loadBootROM]; + if ([sender tag] != MODEL_NONE) { current_model = (enum model)[sender tag]; } - static NSString * const boot_names[] = {@"dmg_boot", @"cgb_boot", @"agb_boot", @"sgb_boot"}; - GB_load_boot_rom(&gb, [[self bootROMPathForName:boot_names[current_model - 1]] UTF8String]); - if ([sender tag] == MODEL_NONE) { GB_reset(&gb); } else { GB_switch_model_and_reset(&gb, cocoa_to_internal_model[current_model]); } + + if (old_width != GB_get_screen_width(&gb)) { + [self.view screenSizeChanged]; + } if ([sender tag] != 0) { /* User explictly selected a model, save the preference */ @@ -392,16 +367,20 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, object:nil]; if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateDMG"]) { - [self initDMG]; + current_model = MODEL_DMG; } else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateSGB"]) { - [self initSGB]; + current_model = MODEL_SGB; } else { - [self initCGB]; + current_model = [[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateAGB"]? MODEL_AGB : MODEL_CGB; } - [self start]; + [self initCommon]; + self.view.gb = &gb; + [self.view screenSizeChanged]; + [self loadROM]; + [self reset:nil]; } diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index debac87f..194ab5a7 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -43,9 +43,7 @@ } - (void) _init -{ - [self screenSizeChanged]; - +{ _shouldBlendFrameWithPrevious = 1; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ratioKeepingChanged) name:@"GBAspectChanged" object:nil]; tracking_area = [ [NSTrackingArea alloc] initWithRect:(NSRect){} @@ -61,7 +59,6 @@ - (void)screenSizeChanged { - if (!_gb) return; if (image_buffers[0]) free(image_buffers[0]); if (image_buffers[1]) free(image_buffers[1]); if (image_buffers[2]) free(image_buffers[2]); @@ -127,7 +124,7 @@ - (void)setFrame:(NSRect)frame { frame = self.superview.frame; - if (![[NSUserDefaults standardUserDefaults] boolForKey:@"GBAspectRatioUnkept"]) { + if (_gb && ![[NSUserDefaults standardUserDefaults] boolForKey:@"GBAspectRatioUnkept"]) { double ratio = frame.size.width / frame.size.height; double width = GB_get_screen_width(_gb); double height = GB_get_screen_height(_gb); diff --git a/Cocoa/GBViewMetal.m b/Cocoa/GBViewMetal.m index 124db778..9acb11e9 100644 --- a/Cocoa/GBViewMetal.m +++ b/Cocoa/GBViewMetal.m @@ -31,7 +31,6 @@ static const vector_float2 rect[] = - (void) allocateTextures { if (!device) return; - if (!self.gb) return; MTLTextureDescriptor *texture_descriptor = [[MTLTextureDescriptor alloc] init]; @@ -45,12 +44,6 @@ static const vector_float2 rect[] = } -- (void)screenSizeChanged -{ - [super screenSizeChanged]; - [self allocateTextures]; -} - - (void)createInternalView { MTKView *view = [[MTKView alloc] initWithFrame:self.frame device:(device = MTLCreateSystemDefaultDevice())]; @@ -58,8 +51,6 @@ static const vector_float2 rect[] = self.internalView = view; view.paused = YES; - [self allocateTextures]; - vertices = [device newBufferWithBytes:rect length:sizeof(rect) options:MTLResourceStorageModeShared]; @@ -140,6 +131,10 @@ static const vector_float2 rect[] = - (void)drawInMTKView:(nonnull MTKView *)view { if (!(view.window.occlusionState & NSWindowOcclusionStateVisible)) return; + if (texture.width != GB_get_screen_width(self.gb) || + texture.height != GB_get_screen_height(self.gb)) { + [self allocateTextures]; + } MTLRegion region = { {0, 0, 0}, // MTLOrigin diff --git a/Core/gb.c b/Core/gb.c index 0d19fc83..17864209 100644 --- a/Core/gb.c +++ b/Core/gb.c @@ -765,10 +765,10 @@ uint32_t GB_get_clock_rate(GB_gameboy_t *gb) size_t GB_get_screen_width(GB_gameboy_t *gb) { - return (gb && GB_is_sgb(gb))? 256 : 160; + return GB_is_sgb(gb)? 256 : 160; } size_t GB_get_screen_height(GB_gameboy_t *gb) { - return (gb && GB_is_sgb(gb))? 224 : 144; + return GB_is_sgb(gb)? 224 : 144; }