git.y1.nz

SameBoy

Accurate GB/GBC emulator
download: https://git.y1.nz/archives/sameboy.tar.gz
README | Files | Log | Refs | LICENSE

commit 7624688e3b99f7c80ea3c33179bcde5c87acbff5
parent be765a3e7ed700ea0cf8e4d7f5d1b4e262efa4cf
Author: Lior Halphon <LIJI32@gmail.com>
Date:   Sat, 14 Jan 2023 01:25:36 +0200

Bare bones ROM loading functionality (requires modification of the app container to add ROMs)

Diffstat:
AiOS/GBLoadROMTableViewController.h5+++++
AiOS/GBLoadROMTableViewController.m42++++++++++++++++++++++++++++++++++++++++++
AiOS/GBROMManager.h9+++++++++
AiOS/GBROMManager.m69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MiOS/GBViewController.m27+++++++++++++++++++++++++++
5 files changed, 152 insertions(+), 0 deletions(-)

diff --git a/iOS/GBLoadROMTableViewController.h b/iOS/GBLoadROMTableViewController.h @@ -0,0 +1,5 @@ +#import <UIKit/UIKit.h> + +@interface GBLoadROMTableViewController : UITableViewController + +@end diff --git a/iOS/GBLoadROMTableViewController.m b/iOS/GBLoadROMTableViewController.m @@ -0,0 +1,42 @@ +#import "GBLoadROMTableViewController.h" +#import "GBROMManager.h" + +@interface GBLoadROMTableViewController () + +@end + +@implementation GBLoadROMTableViewController + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView +{ + return 1; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section +{ + 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]]; + + return cell; +} + +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath +{ + [GBROMManager sharedManager].currentROM = [GBROMManager sharedManager].allROMs[[indexPath indexAtPosition:1]]; + [self.presentingViewController dismissViewControllerAnimated:true completion:^{ + [[NSNotificationCenter defaultCenter] postNotificationName:@"GBROMChanged" object:nil]; + }]; +} + +- (BOOL)isModalInPresentation +{ + return true; +} + +@end diff --git a/iOS/GBROMManager.h b/iOS/GBROMManager.h @@ -0,0 +1,9 @@ +#import <Foundation/Foundation.h> + +@interface GBROMManager : NSObject ++ (instancetype) sharedManager; + +@property (readonly) NSArray<NSString *> *allROMs; +@property (nonatomic) NSString *currentROM; +@property (readonly) NSString *romFile; +@end diff --git a/iOS/GBROMManager.m b/iOS/GBROMManager.m @@ -0,0 +1,69 @@ +#import "GBROMManager.h" + +@implementation GBROMManager +{ + NSString *_romFile; +} + ++ (instancetype)sharedManager +{ + static dispatch_once_t onceToken; + static GBROMManager *manager; + dispatch_once(&onceToken, ^{ + manager = [[self alloc] init]; + }); + return manager; +} + +- (instancetype)init +{ + self = [super init]; + if (!self) return nil; + self.currentROM = [[NSUserDefaults standardUserDefaults] stringForKey:@"GBLastROM"]; + return self; +} + +- (void)setCurrentROM:(NSString *)currentROM +{ + _romFile = nil; + _currentROM = currentROM; + if (currentROM && !self.romFile) { + _currentROM = nil; + } + + [[NSUserDefaults standardUserDefaults] setObject:_currentROM forKey:@"GBLastROM"]; +} + +- (NSString *)romFileForDirectory:(NSString *)romDirectory +{ + for (NSString *filename in [NSFileManager.defaultManager enumeratorAtPath:romDirectory]) { + if ([@[@"gb", @"gbc", @"isx"] containsObject:filename.pathExtension.lowercaseString]) { + return [romDirectory stringByAppendingPathComponent:filename]; + } + } + + return nil; +} + +- (NSString *)romFile +{ + if (_romFile) return _romFile; + if (!_currentROM) return nil; + NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject; + NSString *romDirectory = [root stringByAppendingPathComponent:_currentROM]; + return _romFile = [self romFileForDirectory:romDirectory]; +} + +- (NSArray<NSString *> *)allROMs +{ + NSMutableArray<NSString *> *ret = [NSMutableArray array]; + NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject; + for (NSString *romDirectory in [NSFileManager.defaultManager enumeratorAtPath:root]) { + if ([self romFileForDirectory:[root stringByAppendingPathComponent:romDirectory]]) { + [ret addObject:romDirectory]; + } + } + return ret; +} + +@end diff --git a/iOS/GBViewController.m b/iOS/GBViewController.m @@ -3,6 +3,8 @@ #import "GBVerticalLayout.h" #import "GBViewMetal.h" #import "GBAudioClient.h" +#import "GBROMManager.h" +#import "GBLoadROMTableViewController.h" #include <Core/gb.h> @implementation GBViewController @@ -11,6 +13,7 @@ GBView *_gbView; volatile bool _running; volatile bool _stopping; + bool _romLoaded; GBLayout *_currentLayout; GBHorizontalLayout *_horizontalLayout; GBVerticalLayout *_verticalLayout; @@ -138,11 +141,34 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) _audioLock = [[NSCondition alloc] init]; + [self loadROM]; + [[NSNotificationCenter defaultCenter] addObserverForName:@"GBROMChanged" + object:nil + queue:nil + usingBlock:^(NSNotification * _Nonnull note) { + [self loadROM]; + [self start]; + }]; return true; } +- (void)loadROM +{ + GBROMManager *romManager = [GBROMManager sharedManager]; + if (romManager.romFile) { + // Todo: display errors and warnings + _romLoaded = GB_load_rom(&_gb, romManager.romFile.fileSystemRepresentation) == 0; + } +} + - (void)applicationDidBecomeActive:(UIApplication *)application { + if (self.presentedViewController) return; + if (!_romLoaded) { + [self presentViewController:[[GBLoadROMTableViewController alloc] init] + animated:true + completion:nil]; + } [self start]; } @@ -253,6 +279,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) - (void)start { + if (!_romLoaded) return; if (_running) return; _running = true; [[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] start];

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.