diff --git a/iOS/GBLoadROMTableViewController.m b/iOS/GBLoadROMTableViewController.m index 73e6b12..977c94c 100644 --- a/iOS/GBLoadROMTableViewController.m +++ b/iOS/GBLoadROMTableViewController.m @@ -1,4 +1,5 @@ #import "GBLoadROMTableViewController.h" +#import "GBTableViewCell.h" #import "GBROMManager.h" @interface GBLoadROMTableViewController () @@ -25,14 +26,35 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { - UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; + GBTableViewCell *cell = [[GBTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; NSString *rom = [GBROMManager sharedManager].allROMs[[indexPath indexAtPosition:1]]; cell.textLabel.text = rom; cell.accessoryType = [rom isEqualToString:[GBROMManager sharedManager].currentROM]? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; + NSString *pngPath = [[[GBROMManager sharedManager] autosaveStateFileForROM:rom] stringByAppendingPathExtension:@"png"]; + UIImage *image = [UIImage imageWithContentsOfFile:pngPath]; + if (!image) { + static dispatch_once_t onceToken; + static UIImage *emptyImage = nil; + dispatch_once(&onceToken, ^{ + UIGraphicsBeginImageContextWithOptions((CGSize){160, 144}, false, 1); + emptyImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + }); + image = emptyImage; + } + cell.padding = 4; + cell.imageView.image = image; + cell.imageView.contentMode = UIViewContentModeScaleAspectFit; + return cell; } +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath +{ + return 64; +} + - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"ROM Library"; @@ -46,9 +68,9 @@ }]; } -- (BOOL)isModalInPresentation +- (UIModalPresentationStyle)modalPresentationStyle { - return true; + return UIModalPresentationFormSheet; } @end diff --git a/iOS/GBROMManager.m b/iOS/GBROMManager.m index 1c8efd7..b9af0a7 100644 --- a/iOS/GBROMManager.m +++ b/iOS/GBROMManager.m @@ -60,7 +60,7 @@ return self.romFile; } NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject; - NSString *romDirectory = [root stringByAppendingPathComponent:_currentROM]; + NSString *romDirectory = [root stringByAppendingPathComponent:rom]; return [self romFileForDirectory:romDirectory]; } diff --git a/iOS/GBTableViewCell.h b/iOS/GBTableViewCell.h new file mode 100644 index 0000000..1db9d65 --- /dev/null +++ b/iOS/GBTableViewCell.h @@ -0,0 +1,5 @@ +#import + +@interface GBTableViewCell : UITableViewCell +@property double padding; +@end diff --git a/iOS/GBTableViewCell.m b/iOS/GBTableViewCell.m new file mode 100644 index 0000000..1884e45 --- /dev/null +++ b/iOS/GBTableViewCell.m @@ -0,0 +1,11 @@ +#import "GBTableViewCell.h" + +@implementation GBTableViewCell + +-(void )layoutSubviews +{ + [super layoutSubviews]; + self.imageView.frame = CGRectInset(self.imageView.frame, 0, self.padding); +} + +@end diff --git a/iOS/GBViewController.m b/iOS/GBViewController.m index bb3e7be..bcdf16d 100644 --- a/iOS/GBViewController.m +++ b/iOS/GBViewController.m @@ -305,6 +305,44 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) _stopping = false; } +- (UIImage *)imageFromData:(NSData *)data width:(unsigned)width height:(unsigned)height +{ + /* Convert the screenshot to a CGImageRef */ + CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data.bytes, data.length, NULL); + CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); + CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; + CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; + + CGImageRef iref = CGImageCreate(width, + height, + 8, + 32, + 4 * width, + colorSpaceRef, + bitmapInfo, + provider, + NULL, + true, + renderingIntent); + + UIImage *ret = [[UIImage alloc] initWithCGImage:iref]; + CGColorSpaceRelease(colorSpaceRef); + CGDataProviderRelease(provider); + CGImageRelease(iref); + return ret; +} + +- (void)saveStateToFile:(NSString *)file +{ + GB_save_state(&_gb, file.fileSystemRepresentation); + NSData *data = [NSData dataWithBytes:_gbView.previousBuffer + length:GB_get_screen_width(&_gb) * + GB_get_screen_height(&_gb) * + sizeof(*_gbView.previousBuffer)]; + UIImage *screenshot = [self imageFromData:data width:GB_get_screen_width(&_gb) height:GB_get_screen_height(&_gb)]; + [UIImagePNGRepresentation(screenshot) writeToFile:[file stringByAppendingPathExtension:@"png"] atomically:false]; +} + - (void)postRun { [_audioLock lock]; @@ -316,7 +354,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp) _audioClient = nil; GB_save_battery(&_gb, [GBROMManager sharedManager].batterySaveFile.fileSystemRepresentation); - GB_save_state(&_gb, [GBROMManager sharedManager].autosaveStateFile.fileSystemRepresentation); + [self saveStateToFile:[GBROMManager sharedManager].autosaveStateFile]; } - (void)start