Clean up that crashing mess I made
This commit is contained in:
parent
558f9b9bae
commit
b3166135eb
@ -159,40 +159,9 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height,
|
|||||||
return [[NSBundle mainBundle] pathForResource:name ofType:@"bin"];
|
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
|
- (void) initCommon
|
||||||
{
|
{
|
||||||
|
GB_init(&gb, cocoa_to_internal_model[current_model]);
|
||||||
GB_set_user_data(&gb, (__bridge void *)(self));
|
GB_set_user_data(&gb, (__bridge void *)(self));
|
||||||
GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
|
GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
|
||||||
GB_set_log_callback(&gb, (GB_log_callback_t) consoleLog);
|
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_camera_update_request_callback(&gb, cameraRequestUpdate);
|
||||||
GB_set_highpass_filter_mode(&gb, (GB_highpass_mode_t) [[NSUserDefaults standardUserDefaults] integerForKey:@"GBHighpassFilter"]);
|
GB_set_highpass_filter_mode(&gb, (GB_highpass_mode_t) [[NSUserDefaults standardUserDefaults] integerForKey:@"GBHighpassFilter"]);
|
||||||
GB_set_rewind_length(&gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBRewindLength"]);
|
GB_set_rewind_length(&gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBRewindLength"]);
|
||||||
[self loadROM];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void) vblank
|
- (void) vblank
|
||||||
@ -225,8 +193,6 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height,
|
|||||||
- (void) run
|
- (void) run
|
||||||
{
|
{
|
||||||
running = true;
|
running = true;
|
||||||
self.view.gb = &gb;
|
|
||||||
[self.view screenSizeChanged];
|
|
||||||
GB_set_pixels_output(&gb, self.view.pixels);
|
GB_set_pixels_output(&gb, self.view.pixels);
|
||||||
GB_set_sample_rate(&gb, 96000);
|
GB_set_sample_rate(&gb, 96000);
|
||||||
self.audioClient = [[GBAudioClient alloc] initWithRendererBlock:^(UInt32 sampleRate, UInt32 nFrames, GB_sample_t *buffer) {
|
self.audioClient = [[GBAudioClient alloc] initWithRendererBlock:^(UInt32 sampleRate, UInt32 nFrames, GB_sample_t *buffer) {
|
||||||
@ -277,17 +243,22 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height,
|
|||||||
GB_debugger_set_disabled(&gb, false);
|
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
|
- (IBAction)reset:(id)sender
|
||||||
{
|
{
|
||||||
[self stop];
|
[self stop];
|
||||||
|
size_t old_width = GB_get_screen_width(&gb);
|
||||||
|
[self loadBootROM];
|
||||||
|
|
||||||
if ([sender tag] != MODEL_NONE) {
|
if ([sender tag] != MODEL_NONE) {
|
||||||
current_model = (enum model)[sender tag];
|
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) {
|
if ([sender tag] == MODEL_NONE) {
|
||||||
GB_reset(&gb);
|
GB_reset(&gb);
|
||||||
}
|
}
|
||||||
@ -295,6 +266,10 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height,
|
|||||||
GB_switch_model_and_reset(&gb, cocoa_to_internal_model[current_model]);
|
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) {
|
if ([sender tag] != 0) {
|
||||||
/* User explictly selected a model, save the preference */
|
/* User explictly selected a model, save the preference */
|
||||||
[[NSUserDefaults standardUserDefaults] setBool:current_model == MODEL_DMG forKey:@"EmulateDMG"];
|
[[NSUserDefaults standardUserDefaults] setBool:current_model == MODEL_DMG forKey:@"EmulateDMG"];
|
||||||
@ -392,16 +367,20 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height,
|
|||||||
object:nil];
|
object:nil];
|
||||||
|
|
||||||
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateDMG"]) {
|
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateDMG"]) {
|
||||||
[self initDMG];
|
current_model = MODEL_DMG;
|
||||||
}
|
}
|
||||||
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateSGB"]) {
|
else if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateSGB"]) {
|
||||||
[self initSGB];
|
current_model = MODEL_SGB;
|
||||||
}
|
}
|
||||||
else {
|
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];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,8 +44,6 @@
|
|||||||
|
|
||||||
- (void) _init
|
- (void) _init
|
||||||
{
|
{
|
||||||
[self screenSizeChanged];
|
|
||||||
|
|
||||||
_shouldBlendFrameWithPrevious = 1;
|
_shouldBlendFrameWithPrevious = 1;
|
||||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ratioKeepingChanged) name:@"GBAspectChanged" object:nil];
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ratioKeepingChanged) name:@"GBAspectChanged" object:nil];
|
||||||
tracking_area = [ [NSTrackingArea alloc] initWithRect:(NSRect){}
|
tracking_area = [ [NSTrackingArea alloc] initWithRect:(NSRect){}
|
||||||
@ -61,7 +59,6 @@
|
|||||||
|
|
||||||
- (void)screenSizeChanged
|
- (void)screenSizeChanged
|
||||||
{
|
{
|
||||||
if (!_gb) return;
|
|
||||||
if (image_buffers[0]) free(image_buffers[0]);
|
if (image_buffers[0]) free(image_buffers[0]);
|
||||||
if (image_buffers[1]) free(image_buffers[1]);
|
if (image_buffers[1]) free(image_buffers[1]);
|
||||||
if (image_buffers[2]) free(image_buffers[2]);
|
if (image_buffers[2]) free(image_buffers[2]);
|
||||||
@ -127,7 +124,7 @@
|
|||||||
- (void)setFrame:(NSRect)frame
|
- (void)setFrame:(NSRect)frame
|
||||||
{
|
{
|
||||||
frame = self.superview.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 ratio = frame.size.width / frame.size.height;
|
||||||
double width = GB_get_screen_width(_gb);
|
double width = GB_get_screen_width(_gb);
|
||||||
double height = GB_get_screen_height(_gb);
|
double height = GB_get_screen_height(_gb);
|
||||||
|
@ -31,7 +31,6 @@ static const vector_float2 rect[] =
|
|||||||
- (void) allocateTextures
|
- (void) allocateTextures
|
||||||
{
|
{
|
||||||
if (!device) return;
|
if (!device) return;
|
||||||
if (!self.gb) return;
|
|
||||||
|
|
||||||
MTLTextureDescriptor *texture_descriptor = [[MTLTextureDescriptor alloc] init];
|
MTLTextureDescriptor *texture_descriptor = [[MTLTextureDescriptor alloc] init];
|
||||||
|
|
||||||
@ -45,12 +44,6 @@ static const vector_float2 rect[] =
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)screenSizeChanged
|
|
||||||
{
|
|
||||||
[super screenSizeChanged];
|
|
||||||
[self allocateTextures];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)createInternalView
|
- (void)createInternalView
|
||||||
{
|
{
|
||||||
MTKView *view = [[MTKView alloc] initWithFrame:self.frame device:(device = MTLCreateSystemDefaultDevice())];
|
MTKView *view = [[MTKView alloc] initWithFrame:self.frame device:(device = MTLCreateSystemDefaultDevice())];
|
||||||
@ -58,8 +51,6 @@ static const vector_float2 rect[] =
|
|||||||
self.internalView = view;
|
self.internalView = view;
|
||||||
view.paused = YES;
|
view.paused = YES;
|
||||||
|
|
||||||
[self allocateTextures];
|
|
||||||
|
|
||||||
vertices = [device newBufferWithBytes:rect
|
vertices = [device newBufferWithBytes:rect
|
||||||
length:sizeof(rect)
|
length:sizeof(rect)
|
||||||
options:MTLResourceStorageModeShared];
|
options:MTLResourceStorageModeShared];
|
||||||
@ -140,6 +131,10 @@ static const vector_float2 rect[] =
|
|||||||
- (void)drawInMTKView:(nonnull MTKView *)view
|
- (void)drawInMTKView:(nonnull MTKView *)view
|
||||||
{
|
{
|
||||||
if (!(view.window.occlusionState & NSWindowOcclusionStateVisible)) return;
|
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 = {
|
MTLRegion region = {
|
||||||
{0, 0, 0}, // MTLOrigin
|
{0, 0, 0}, // MTLOrigin
|
||||||
|
@ -765,10 +765,10 @@ uint32_t GB_get_clock_rate(GB_gameboy_t *gb)
|
|||||||
|
|
||||||
size_t GB_get_screen_width(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)
|
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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user