Add screenshots to the ROM library (A similar functionality will be given to the state manager)

This commit is contained in:
Lior Halphon 2023-01-15 23:00:06 +02:00
parent 181a236fce
commit 00367a79b4
5 changed files with 81 additions and 5 deletions

View File

@ -1,4 +1,5 @@
#import "GBLoadROMTableViewController.h" #import "GBLoadROMTableViewController.h"
#import "GBTableViewCell.h"
#import "GBROMManager.h" #import "GBROMManager.h"
@interface GBLoadROMTableViewController () @interface GBLoadROMTableViewController ()
@ -25,14 +26,35 @@
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath - (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]]; NSString *rom = [GBROMManager sharedManager].allROMs[[indexPath indexAtPosition:1]];
cell.textLabel.text = rom; cell.textLabel.text = rom;
cell.accessoryType = [rom isEqualToString:[GBROMManager sharedManager].currentROM]? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 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; return cell;
} }
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 64;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{ {
return @"ROM Library"; return @"ROM Library";
@ -46,9 +68,9 @@
}]; }];
} }
- (BOOL)isModalInPresentation - (UIModalPresentationStyle)modalPresentationStyle
{ {
return true; return UIModalPresentationFormSheet;
} }
@end @end

View File

@ -60,7 +60,7 @@
return self.romFile; return self.romFile;
} }
NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject; NSString *root = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject;
NSString *romDirectory = [root stringByAppendingPathComponent:_currentROM]; NSString *romDirectory = [root stringByAppendingPathComponent:rom];
return [self romFileForDirectory:romDirectory]; return [self romFileForDirectory:romDirectory];
} }

5
iOS/GBTableViewCell.h Normal file
View File

@ -0,0 +1,5 @@
#import <UIKit/UIKit.h>
@interface GBTableViewCell : UITableViewCell
@property double padding;
@end

11
iOS/GBTableViewCell.m Normal file
View File

@ -0,0 +1,11 @@
#import "GBTableViewCell.h"
@implementation GBTableViewCell
-(void )layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = CGRectInset(self.imageView.frame, 0, self.padding);
}
@end

View File

@ -305,6 +305,44 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp)
_stopping = false; _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 - (void)postRun
{ {
[_audioLock lock]; [_audioLock lock];
@ -316,7 +354,7 @@ static void rumbleCallback(GB_gameboy_t *gb, double amp)
_audioClient = nil; _audioClient = nil;
GB_save_battery(&_gb, [GBROMManager sharedManager].batterySaveFile.fileSystemRepresentation); GB_save_battery(&_gb, [GBROMManager sharedManager].batterySaveFile.fileSystemRepresentation);
GB_save_state(&_gb, [GBROMManager sharedManager].autosaveStateFile.fileSystemRepresentation); [self saveStateToFile:[GBROMManager sharedManager].autosaveStateFile];
} }
- (void)start - (void)start