Play-/Source/ui_ios/EmulatorViewController.mm

304 lines
10 KiB
Text
Raw Normal View History

2015-07-22 02:48:26 -04:00
#include "PathUtils.h"
#import "EmulatorViewController.h"
#import "GlEsView.h"
#include "../PS2VM.h"
#include "../PS2VM_Preferences.h"
#include "../AppConfig.h"
#include "PreferenceDefs.h"
2015-07-22 02:48:26 -04:00
#include "GSH_OpenGLiOS.h"
#include "IosUtils.h"
#include "PH_Generic.h"
2016-04-04 11:51:15 -04:00
#include "../../tools/PsfPlayer/Source/SH_OpenAL.h"
2015-07-22 02:48:26 -04:00
CPS2VM* g_virtualMachine = nullptr;
@interface EmulatorViewController ()
@end
@implementation EmulatorViewController
+(void)registerPreferences
{
CAppConfig::GetInstance().RegisterPreferenceBoolean(PREFERENCE_UI_SHOWFPS, false);
CAppConfig::GetInstance().RegisterPreferenceBoolean(PREFERENCE_UI_SHOWVIRTUALPAD, true);
2016-04-04 11:51:15 -04:00
CAppConfig::GetInstance().RegisterPreferenceBoolean(PREFERENCE_AUDIO_ENABLEOUTPUT, true);
}
2015-07-22 02:48:26 -04:00
-(void)viewDidLoad
{
self.connectObserver = [[NSNotificationCenter defaultCenter] addObserverForName:GCControllerDidConnectNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
if ([[GCController controllers] count] == 1) {
[self toggleHardwareController:YES];
}
}];
self.disconnectObserver = [[NSNotificationCenter defaultCenter] addObserverForName:GCControllerDidDisconnectNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
if (![[GCController controllers] count]) {
[self toggleHardwareController:NO];
}
}];
if ([[GCController controllers] count]) {
[self toggleHardwareController:YES];
}
self.iCadeReader = [[iCadeReaderView alloc] init];
[self.view addSubview:self.iCadeReader];
self.iCadeReader.delegate = self;
self.iCadeReader.active = YES;
}
-(void)viewDidAppear: (BOOL)animated
{
assert(g_virtualMachine == nullptr);
2015-07-22 02:48:26 -04:00
g_virtualMachine = new CPS2VM();
g_virtualMachine->Initialize();
g_virtualMachine->CreateGSHandler(CGSH_OpenGLiOS::GetFactoryFunction((CAEAGLLayer*)self.view.layer));
2015-07-22 02:48:26 -04:00
g_virtualMachine->CreatePadHandler(CPH_Generic::GetFactoryFunction());
2015-10-26 22:26:34 -04:00
2016-04-04 11:51:15 -04:00
if(CAppConfig::GetInstance().GetPreferenceBoolean(PREFERENCE_AUDIO_ENABLEOUTPUT))
{
g_virtualMachine->CreateSoundHandler(&CSH_OpenAL::HandlerFactory);
}
const CGRect screenBounds = [[UIScreen mainScreen] bounds];
2015-10-26 22:26:34 -04:00
if(CAppConfig::GetInstance().GetPreferenceBoolean(PREFERENCE_UI_SHOWVIRTUALPAD))
{
auto padHandler = static_cast<CPH_Generic*>(g_virtualMachine->GetPadHandler());
self.virtualPadView = [[VirtualPadView alloc] initWithFrame: screenBounds padHandler: padHandler];
[self.view addSubview: self.virtualPadView];
2018-12-23 10:19:21 -05:00
[self.view sendSubviewToBack: self.virtualPadView];
}
if(CAppConfig::GetInstance().GetPreferenceBoolean(PREFERENCE_UI_SHOWFPS))
{
[self setupFpsCounter];
}
2015-11-24 10:31:44 -05:00
2015-07-22 02:48:26 -04:00
g_virtualMachine->Pause();
g_virtualMachine->Reset();
2017-12-05 18:28:48 -05:00
auto imagePath = boost::filesystem::path([self.imagePath fileSystemRepresentation]);
if(IosUtils::IsLoadableExecutableFileName(self.imagePath))
{
2017-12-05 18:28:48 -05:00
g_virtualMachine->m_ee->m_os->BootFromFile(imagePath);
}
else
{
2018-01-15 14:14:59 -05:00
CAppConfig::GetInstance().SetPreferencePath(PREF_PS2_CDROM0_PATH, imagePath);
g_virtualMachine->Reset();
2015-12-06 14:53:35 -05:00
g_virtualMachine->m_ee->m_os->BootFromCDROM();
}
2015-07-22 02:48:26 -04:00
g_virtualMachine->Resume();
}
-(void)viewDidDisappear: (BOOL)animated
{
g_virtualMachine->Pause();
g_virtualMachine->Destroy();
delete g_virtualMachine;
g_virtualMachine = nullptr;
}
2016-01-30 21:06:39 -05:00
-(void)toggleHardwareController: (BOOL)useHardware
{
if(useHardware)
{
self.gController = [GCController controllers][0];
if(self.gController.extendedGamepad)
2016-01-30 21:06:39 -05:00
{
[self.gController.extendedGamepad setValueChangedHandler:
^(GCExtendedGamepad* gamepad, GCControllerElement* element)
2016-01-30 21:06:39 -05:00
{
auto padHandler = static_cast<CPH_Generic*>(g_virtualMachine->GetPadHandler());
if(!padHandler) return;
if (element == gamepad.buttonA) padHandler->SetButtonState(PS2::CControllerInfo::CROSS, gamepad.buttonA.pressed);
else if(element == gamepad.buttonB) padHandler->SetButtonState(PS2::CControllerInfo::CIRCLE, gamepad.buttonB.pressed);
else if(element == gamepad.buttonX) padHandler->SetButtonState(PS2::CControllerInfo::SQUARE, gamepad.buttonX.pressed);
else if(element == gamepad.buttonY) padHandler->SetButtonState(PS2::CControllerInfo::TRIANGLE, gamepad.buttonY.pressed);
else if(element == gamepad.leftShoulder) padHandler->SetButtonState(PS2::CControllerInfo::L1, gamepad.leftShoulder.pressed);
else if(element == gamepad.rightShoulder) padHandler->SetButtonState(PS2::CControllerInfo::R1, gamepad.rightShoulder.pressed);
else if(element == gamepad.leftTrigger) padHandler->SetButtonState(PS2::CControllerInfo::L2, gamepad.leftTrigger.pressed);
else if(element == gamepad.rightTrigger) padHandler->SetButtonState(PS2::CControllerInfo::R2, gamepad.rightTrigger.pressed);
else if(element == gamepad.dpad)
2016-01-30 21:06:39 -05:00
{
padHandler->SetButtonState(PS2::CControllerInfo::DPAD_UP, gamepad.dpad.up.pressed);
padHandler->SetButtonState(PS2::CControllerInfo::DPAD_DOWN, gamepad.dpad.down.pressed);
padHandler->SetButtonState(PS2::CControllerInfo::DPAD_LEFT, gamepad.dpad.left.pressed);
padHandler->SetButtonState(PS2::CControllerInfo::DPAD_RIGHT, gamepad.dpad.right.pressed);
2016-01-30 21:06:39 -05:00
}
else if(element == gamepad.leftThumbstick)
2016-01-30 21:06:39 -05:00
{
padHandler->SetAxisState(PS2::CControllerInfo::ANALOG_LEFT_X, gamepad.leftThumbstick.xAxis.value);
padHandler->SetAxisState(PS2::CControllerInfo::ANALOG_LEFT_Y, -gamepad.leftThumbstick.yAxis.value);
2016-01-30 21:06:39 -05:00
}
else if(element == gamepad.rightThumbstick)
2016-01-30 21:06:39 -05:00
{
padHandler->SetAxisState(PS2::CControllerInfo::ANALOG_RIGHT_X, gamepad.rightThumbstick.xAxis.value);
padHandler->SetAxisState(PS2::CControllerInfo::ANALOG_RIGHT_Y, -gamepad.rightThumbstick.yAxis.value);
2016-01-30 21:06:39 -05:00
}
}
];
}
else if(self.gController.gamepad)
2016-01-30 21:06:39 -05:00
{
[self.gController.gamepad setValueChangedHandler:
^(GCGamepad* gamepad, GCControllerElement* element)
2016-01-30 21:06:39 -05:00
{
auto padHandler = static_cast<CPH_Generic*>(g_virtualMachine->GetPadHandler());
if(!padHandler) return;
if (element == gamepad.buttonA) padHandler->SetButtonState(PS2::CControllerInfo::CROSS, gamepad.buttonA.pressed);
else if(element == gamepad.buttonB) padHandler->SetButtonState(PS2::CControllerInfo::CIRCLE, gamepad.buttonB.pressed);
else if(element == gamepad.buttonX) padHandler->SetButtonState(PS2::CControllerInfo::SQUARE, gamepad.buttonX.pressed);
else if(element == gamepad.buttonY) padHandler->SetButtonState(PS2::CControllerInfo::TRIANGLE, gamepad.buttonY.pressed);
else if(element == gamepad.leftShoulder) padHandler->SetButtonState(PS2::CControllerInfo::L1, gamepad.leftShoulder.pressed);
else if(element == gamepad.rightShoulder) padHandler->SetButtonState(PS2::CControllerInfo::R1, gamepad.rightShoulder.pressed);
else if(element == gamepad.dpad)
2016-01-30 21:06:39 -05:00
{
padHandler->SetButtonState(PS2::CControllerInfo::DPAD_UP, gamepad.dpad.up.pressed);
padHandler->SetButtonState(PS2::CControllerInfo::DPAD_DOWN, gamepad.dpad.down.pressed);
padHandler->SetButtonState(PS2::CControllerInfo::DPAD_LEFT, gamepad.dpad.left.pressed);
padHandler->SetButtonState(PS2::CControllerInfo::DPAD_RIGHT, gamepad.dpad.right.pressed);
padHandler->SetAxisState(PS2::CControllerInfo::ANALOG_LEFT_X, gamepad.dpad.xAxis.value);
padHandler->SetAxisState(PS2::CControllerInfo::ANALOG_LEFT_Y, -gamepad.dpad.yAxis.value);
2016-01-30 21:06:39 -05:00
}
}
];
//Add controller pause handler here
2016-01-30 21:06:39 -05:00
}
}
else
{
self.gController = nil;
}
}
2015-11-24 10:31:44 -05:00
-(void)setupFpsCounter
{
auto screenBounds = [[UIScreen mainScreen] bounds];
self.fpsCounterLabel = [[UILabel alloc] initWithFrame: screenBounds];
self.fpsCounterLabel.textColor = [UIColor whiteColor];
[self.view addSubview: self.fpsCounterLabel];
g_virtualMachine->GetGSHandler()->OnNewFrame.connect(
[self] (uint32 drawCallCount)
{
@synchronized(self)
{
self.frames++;
self.drawCallCount += drawCallCount;
}
}
);
[NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @selector(updateFpsCounter) userInfo: nil repeats: YES];
}
-(void)updateFpsCounter
{
@synchronized(self)
{
uint32 dcpf = (self.frames != 0) ? (self.drawCallCount / self.frames) : 0;
self.fpsCounterLabel.text = [NSString stringWithFormat: @"%d f/s, %d dc/f",
self.frames, dcpf];
self.frames = 0;
self.drawCallCount = 0;
}
[self.fpsCounterLabel sizeToFit];
}
2018-12-23 22:07:34 -05:00
-(void)onLoadStateButtonClick
{
g_virtualMachine->Resume();
}
-(void)onSaveStateButtonClick
{
auto dataPath = Framework::PathUtils::GetPersonalDataPath();
auto statePath = dataPath / "state.sta";
g_virtualMachine->SaveState(statePath.c_str());
NSLog(@"Saved state to '%s'.", statePath.string().c_str());
2018-12-23 22:07:34 -05:00
g_virtualMachine->Resume();
}
2018-12-23 22:07:34 -05:00
-(void)onExitButtonClick
2016-01-08 18:39:27 -05:00
{
[self dismissViewControllerAnimated: YES completion: nil];
}
2018-12-23 22:07:34 -05:00
-(void)onResumeButtonClick
{
g_virtualMachine->Resume();
}
-(IBAction)onPauseButtonClick: (id)sender
{
g_virtualMachine->Pause();
UIAlertController* alert = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle: UIAlertControllerStyleActionSheet];
//Load State
{
UIAlertAction* action = [UIAlertAction
actionWithTitle: @"Load State"
style: UIAlertActionStyleDefault
handler: ^(UIAlertAction*) { [self onLoadStateButtonClick]; }
];
[alert addAction: action];
}
//Save State
{
UIAlertAction* action = [UIAlertAction
actionWithTitle: @"Save State"
style: UIAlertActionStyleDefault
handler: ^(UIAlertAction*) { [self onSaveStateButtonClick]; }
];
[alert addAction: action];
}
//Resume
{
UIAlertAction* action = [UIAlertAction
actionWithTitle: @"Resume"
style: UIAlertActionStyleCancel
handler: ^(UIAlertAction*) { [self onResumeButtonClick]; }
];
[alert addAction: action];
}
//Exit
{
UIAlertAction* action = [UIAlertAction
actionWithTitle: @"Exit"
style: UIAlertActionStyleDestructive
handler: ^(UIAlertAction*) { [self onExitButtonClick]; }
];
[alert addAction: action];
}
[self presentViewController: alert animated: YES completion: nil];
}
2015-07-22 02:48:26 -04:00
-(BOOL)prefersStatusBarHidden
{
return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self.connectObserver];
[[NSNotificationCenter defaultCenter] removeObserver:self.disconnectObserver];
}
2015-07-22 02:48:26 -04:00
@end