SameBoy | Accurate GB/GBC emulator |
| download: https://git.y1.nz/archives/sameboy.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit 6673427c54a15de42d621131b73ac44381cd6065 parent 0549dc702c914fe2f3625eea75fac4aefb934d12 Author: Lior Halphon <LIJI32@gmail.com> Date: Sun, 15 Jan 2023 21:51:27 +0200 Add model selection Diffstat:
| M | iOS/GBLoadROMTableViewController.m | 16 | ++++++++++++++-- |
| M | iOS/GBMenuViewController.m | 6 | ++---- |
| A | iOS/GBOptionViewController.h | 11 | +++++++++++ |
| A | iOS/GBOptionViewController.m | 74 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | iOS/GBViewController.h | 1 | + |
| M | iOS/GBViewController.m | 57 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- |
| M | iOS/main.m | 1 | + |
7 files changed, 158 insertions(+), 8 deletions(-)
diff --git a/iOS/GBLoadROMTableViewController.m b/iOS/GBLoadROMTableViewController.m @@ -7,6 +7,12 @@ @implementation GBLoadROMTableViewController +- (instancetype)init +{ + self = [super initWithStyle:UITableViewStyleGrouped]; + return self; +} + - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; @@ -17,15 +23,21 @@ return [GBROMManager sharedManager].allROMs.count; } - - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; - cell.textLabel.text = [GBROMManager sharedManager].allROMs[[indexPath indexAtPosition:1]]; + NSString *rom = [GBROMManager sharedManager].allROMs[[indexPath indexAtPosition:1]]; + cell.textLabel.text = rom; + cell.accessoryType = [rom isEqualToString:[GBROMManager sharedManager].currentROM]? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; return cell; } +- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section +{ + return @"ROM Library"; +} + - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [GBROMManager sharedManager].currentROM = [GBROMManager sharedManager].allROMs[[indexPath indexAtPosition:1]]; diff --git a/iOS/GBMenuViewController.m b/iOS/GBMenuViewController.m @@ -19,9 +19,7 @@ preferredStyle:style]; [ret addAction:[UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleCancel - handler:^(UIAlertAction * _Nonnull action) { - [(GBViewController *)[UIApplication sharedApplication].delegate start]; - }]]; + handler:nil]]; return ret; } @@ -38,7 +36,7 @@ } buttons[] = { {@"Reset", @"arrow.2.circlepath", SelectorString(reset)}, {@"Library", @"bookmark", SelectorString(openLibrary)}, - {@"Model", @"ModelTemplate", nil}, + {@"Model", @"ModelTemplate", SelectorString(changeModel)}, {@"States", @"square.stack", nil}, {@"Settings", @"gear", nil}, {@"About", @"info.circle", nil}, diff --git a/iOS/GBOptionViewController.h b/iOS/GBOptionViewController.h @@ -0,0 +1,11 @@ +#import <UIKit/UIKit.h> + +@interface GBOptionViewController : UITableViewController +- (instancetype)initWithHeader:(NSString *)header; +@property NSString *header; +@property NSString *footer; +@property (getter=isModal) bool modal; +- (void)addOption:(NSString *)title + withCheckmark:(bool)checked + action:(void (^)(void))block; +@end diff --git a/iOS/GBOptionViewController.m b/iOS/GBOptionViewController.m @@ -0,0 +1,74 @@ +#import "GBOptionViewController.h" + +@interface GBOptionViewController () + +@end + +@implementation GBOptionViewController +{ + NSMutableArray<NSString *> *_options; + NSMutableArray<void (^)(void)> *_actions; + NSMutableArray<NSNumber *> *_checked; +} + +- (instancetype)initWithHeader:(NSString *)header +{ + self = [super initWithStyle:UITableViewStyleGrouped]; + self.header = header; + _options = [NSMutableArray array]; + _actions = [NSMutableArray array]; + _checked = [NSMutableArray array]; + return self; +} + +- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section +{ + return self.header; +} + +- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section +{ + return self.footer; +} + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView +{ + return 1; +} + +- (NSInteger)tableView:(UITableView *)tableView + numberOfRowsInSection:(NSInteger)section +{ + return _options.count; +} + +- (void)addOption:(NSString *)title + withCheckmark:(bool)checked + action:(void (^)(void))block +{ + [_options addObject:title]; + [_actions addObject:block]; + [_checked addObject:@(checked)]; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath +{ + UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; + cell.textLabel.text = _options[[indexPath indexAtPosition:1]]; + cell.accessoryType = _checked[[indexPath indexAtPosition:1]].boolValue? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; + + return cell; +} + +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath +{ + [self.presentingViewController dismissViewControllerAnimated:true completion:^{ + _actions[[indexPath indexAtPosition:1]](); + }]; +} + +- (BOOL)isModalInPresentation +{ + return self.isModal; +} +@end diff --git a/iOS/GBViewController.h b/iOS/GBViewController.h @@ -6,4 +6,5 @@ - (void)openLibrary; - (void)start; - (void)stop; +- (void)changeModel; @end diff --git a/iOS/GBViewController.m b/iOS/GBViewController.m @@ -8,6 +8,7 @@ #import "GBBackgroundView.h" #import "GBHapticManager.h" #import "GBMenuViewController.h" +#import "GBOptionViewController.h" #include <Core/gb.h> @implementation GBViewController @@ -69,7 +70,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) - (void)initGameBoy { - GB_init(&_gb, GB_MODEL_CGB_E); + GB_init(&_gb, [[NSUserDefaults standardUserDefaults] integerForKey:@"GBCGBModel"]); GB_set_user_data(&_gb, (__bridge void *)(self)); GB_set_boot_rom_load_callback(&_gb, (GB_boot_rom_load_callback_t)loadBootROM); GB_set_vblank_callback(&_gb, (GB_vblank_callback_t) vblank); @@ -125,6 +126,15 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) return true; } +- (void)loadState:(NSString *)state +{ + GB_model_t model; + if (!GB_get_state_model(state.fileSystemRepresentation, &model)) { + GB_switch_model_and_reset(&_gb, model); + GB_load_state(&_gb, state.fileSystemRepresentation); + } +} + - (void)loadROM { [self stop]; @@ -135,7 +145,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) if (_romLoaded) { GB_reset(&_gb); GB_load_battery(&_gb, [GBROMManager sharedManager].batterySaveFile.fileSystemRepresentation); - GB_load_state(&_gb, [GBROMManager sharedManager].autosaveStateFile.fileSystemRepresentation); + [self loadState:[GBROMManager sharedManager].autosaveStateFile]; } } } @@ -168,6 +178,49 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) completion:nil]; } +- (void)changeModel +{ + GBOptionViewController *controller = [[GBOptionViewController alloc] initWithHeader:@"Select a model to emulate"]; + controller.footer = @"Changing the emulated model will reset the emulator"; + + GB_model_t currentModel = GB_get_model(&_gb); + struct { + NSString *title; + NSString *settingKey; + bool checked; + } items[] = { + {@"Game Boy", @"GBDMGModel", currentModel < GB_MODEL_SGB}, + {@"Game Boy Pocket/Light", nil, currentModel == GB_MODEL_MGB}, + {@"Super Game Boy", @"GBSGBModel", GB_is_sgb(&_gb)}, + {@"Game Boy Color", @"GBCGBModel", GB_is_cgb(&_gb) && currentModel <= GB_MODEL_CGB_E}, + {@"Game Boy Advance", @"GBAGBModel", currentModel > GB_MODEL_CGB_E}, + }; + + for (unsigned i = 0; i < sizeof(items) / sizeof(items[0]); i++) { + GB_model_t model = GB_MODEL_MGB; + if (items[i].settingKey) { + model = [[NSUserDefaults standardUserDefaults] integerForKey:items[i].settingKey]; + } + [controller addOption:items[i].title withCheckmark:items[i].checked action:^{ + GB_switch_model_and_reset(&_gb, model); + [self start]; + }]; + } + [self presentViewController:controller animated:true completion:nil]; +} + +- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion +{ + [super dismissViewControllerAnimated:flag completion:^() { + if (completion) { + completion(); + } + if (!self.presentedViewController) { + [self start]; + } + }]; +} + - (void)applicationWillResignActive:(UIApplication *)application { [self stop]; diff --git a/iOS/main.m b/iOS/main.m @@ -17,6 +17,7 @@ int main(int argc, char * argv[]) @"GBAGBModel": @(GB_MODEL_AGB_A), @"GBSGBModel": @(GB_MODEL_SGB2), @"GBRumbleMode": @(GB_RUMBLE_CARTRIDGE_ONLY), + @"GBColorPalette": @(1), @"GBVolume": @(1.0),
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.