From 1d420f51e77b6a127d5d5bb597f171873422f3c3 Mon Sep 17 00:00:00 2001 From: Yoshi Sugawara Date: Fri, 1 Apr 2016 04:25:12 -0400 Subject: [PATCH] added save states controls to options menu; support for saving and restoring states on a game-by-game basis --- Common.iphone/KBDController.mm | 91 ++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 4 deletions(-) diff --git a/Common.iphone/KBDController.mm b/Common.iphone/KBDController.mm index 00d46cd..4f297b6 100644 --- a/Common.iphone/KBDController.mm +++ b/Common.iphone/KBDController.mm @@ -19,6 +19,8 @@ #import #include "../kegs/src/paddles.h" #import "../kegs/iOS/emulatorView.h" +#include "../kegs/src/SaveState.h" +#include "../common/activedownload.h" #ifdef ACTIVEGS_CUSTOMKEYS #include "UICustomKey.h" @@ -273,6 +275,11 @@ int isHardwareKeyboard() } +@interface KBDController() { + UISegmentedControl *saveStateSegmentedControl; +} +@end + @implementation KBDController @synthesize textField = _textField; @@ -878,6 +885,40 @@ extern int x_frame_rate ; printf("control not found\n"); } + +-(void) saveStateButtonPressed:(id)sender { + MyString dir = CDownload::getPersistentPath(); + dir = dir + ACTIVEGS_DIRECTORY_SEPARATOR + "savestates" + ACTIVEGS_DIRECTORY_SEPARATOR + config.name; + NSFileManager *fileManager = [NSFileManager defaultManager]; + NSError *error; + [fileManager createDirectoryAtPath:[NSString stringWithCString:dir.c_str() encoding:NSUTF8StringEncoding] withIntermediateDirectories:YES attributes:nil error:&error]; + dir += ACTIVEGS_DIRECTORY_SEPARATOR; + NSString *segIndex = [saveStateSegmentedControl titleForSegmentAtIndex:[saveStateSegmentedControl selectedSegmentIndex]]; + dir += "save"; + dir += [segIndex UTF8String]; + g_savestate.saveState(dir.c_str()); + [pManager setNotificationText:[NSString stringWithFormat:@"Saved State #%@",segIndex]]; +} + +-(void) loadStateButtonPressed:(id)sender { + NSString *segIndex = [saveStateSegmentedControl titleForSegmentAtIndex:[saveStateSegmentedControl selectedSegmentIndex]]; + MyString dir = CDownload::getPersistentPath(); + dir += ACTIVEGS_DIRECTORY_SEPARATOR; + dir += "savestates"; + dir += ACTIVEGS_DIRECTORY_SEPARATOR; + dir += config.name; + dir += ACTIVEGS_DIRECTORY_SEPARATOR; + dir += "save"; + dir += [segIndex UTF8String]; + FILE *fHandle = fopen(dir.c_str(),"r"); + if ( fHandle == NULL ) { + [pManager setNotificationText:[NSString stringWithFormat:@"State #%@ Does Not Exist",segIndex]]; + return; + } + g_savestate.restoreState(dir.c_str()); + [pManager setNotificationText:[NSString stringWithFormat:@"Loaded State #%@",segIndex]]; +} + // -(void)addRuntimeControls { @@ -904,7 +945,7 @@ extern int x_frame_rate ; UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(OPTIONMARGIN,l,OPTIONWIDTH,LINEHEIGHT)]; label.text = [NSString stringWithUTF8String:runtimeControlDefs[i].name]; - label.textAlignment = UITextAlignmentCenter; + label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont systemFontOfSize:12*res]; label.backgroundColor = [UIColor clearColor]; [self.runtimeControlsOptions addSubview:label]; @@ -933,6 +974,51 @@ extern int x_frame_rate ; nbs++; } + + // hack in some save state controls + l += 2.0; + UILabel* saveStatelabel = [[UILabel alloc] initWithFrame:CGRectMake(OPTIONMARGIN,l,OPTIONWIDTH,LINEHEIGHT)]; + saveStatelabel.text = @"Save States"; + saveStatelabel.textAlignment = NSTextAlignmentCenter; + saveStatelabel.font = [UIFont systemFontOfSize:12*res]; + saveStatelabel.backgroundColor = [UIColor clearColor]; + [self.runtimeControlsOptions addSubview:saveStatelabel]; + l += LINEHEIGHT; + + saveStateSegmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(OPTIONMARGIN, l, OPTIONWIDTH, LINEHEIGHT)]; + for (int segIndex = 0; segIndex < 6; segIndex++) { + [saveStateSegmentedControl insertSegmentWithTitle:[NSString stringWithFormat:@"%d",segIndex] atIndex:segIndex animated:NO]; + } + [saveStateSegmentedControl setSelectedSegmentIndex:0]; + [self.runtimeControlsOptions addSubview:saveStateSegmentedControl]; + + l += LINEHEIGHT; + l += 2.0; + + UIButton *saveStateButton = [UIButton buttonWithType:UIButtonTypeCustom]; + saveStateButton.frame = CGRectMake(OPTIONMARGIN,l,OPTIONWIDTH * 0.4,LINEHEIGHT); + [saveStateButton setTitle:@"Save" forState:UIControlStateNormal]; + saveStateButton.titleLabel.font = [UIFont systemFontOfSize:12*res]; + [saveStateButton setTitleColor:self.view.tintColor forState:UIControlStateNormal]; + saveStateButton.backgroundColor = [UIColor clearColor]; + saveStateButton.layer.borderWidth = 1.0f; + saveStateButton.layer.borderColor = [self.view.tintColor CGColor]; + [saveStateButton addTarget:self action:@selector(saveStateButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; + [self.runtimeControlsOptions addSubview:saveStateButton]; + + UIButton *loadStateButton = [UIButton buttonWithType:UIButtonTypeCustom]; + loadStateButton.frame = CGRectMake((OPTIONWIDTH + OPTIONMARGIN) - (OPTIONWIDTH * 0.4),l,OPTIONWIDTH * 0.4,LINEHEIGHT); + [loadStateButton setTitle:@"Load" forState:UIControlStateNormal]; + loadStateButton.titleLabel.font = [UIFont systemFontOfSize:12*res]; + loadStateButton.backgroundColor = [UIColor clearColor]; + [loadStateButton setTitleColor:self.view.tintColor forState:UIControlStateNormal]; + loadStateButton.layer.borderWidth = 1.0f; + loadStateButton.layer.borderColor = [self.view.tintColor CGColor]; + [loadStateButton addTarget:self action:@selector(loadStateButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; + [self.runtimeControlsOptions addSubview:loadStateButton]; + + l+=LINEHEIGHT; + nbs++; float w = OPTIONWIDTH+OPTIONMARGIN*2; @@ -1034,9 +1120,6 @@ extern int x_frame_rate ; -(void)addDiskSelection { - self.diskSelectionOptions; - self.diskSelection; - self.diskSelectionOptions = nil; self.diskSelection = nil;