From b66f7ea9090bb19a683c2a989c503f1e665e2ff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 22 May 2024 21:36:55 +0200 Subject: [PATCH] Vulkan on iOS: Hook up remaining functionality --- ios/ViewController.mm | 34 +++------ ios/ViewControllerCommon.h | 3 + ios/ViewControllerMetal.mm | 149 ++++++++++++++++++++++++++++++------- 3 files changed, 136 insertions(+), 50 deletions(-) diff --git a/ios/ViewController.mm b/ios/ViewController.mm index 8758ed92b7..cec6e3d434 100644 --- a/ios/ViewController.mm +++ b/ios/ViewController.mm @@ -39,13 +39,6 @@ #include "Core/HLE/sceUsbCam.h" #include "Core/HLE/sceUsbGps.h" -#include -#include -#include - -#define IS_IPAD() ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) -#define IS_IPHONE() ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) - class IOSGLESContext : public GraphicsContext { public: IOSGLESContext() { @@ -118,12 +111,8 @@ id sharedViewController; g_iCadeTracker.InitKeyMap(); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate:) name:UIApplicationWillTerminateNotification object:nil]; - - if ([GCController class]) // Checking the availability of a GameController framework - { - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controllerDidConnect:) name:GCControllerDidConnectNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controllerDidDisconnect:) name:GCControllerDidDisconnectNotification object:nil]; - } + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controllerDidConnect:) name:GCControllerDidConnectNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controllerDidDisconnect:) name:GCControllerDidDisconnectNotification object:nil]; } return self; } @@ -174,10 +163,6 @@ extern float g_safeInsetBottom; self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; } - UIScreenEdgePanGestureRecognizer *mBackGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:) ]; - [mBackGestureRecognizer setEdges:UIRectEdgeLeft]; - [[self view] addGestureRecognizer:mBackGestureRecognizer]; - GLKView* view = (GLKView *)self.view; view.context = self.context; view.drawableDepthFormat = GLKViewDrawableDepthFormat24; @@ -200,10 +185,8 @@ extern float g_safeInsetBottom; self.iCadeView.delegate = self; self.iCadeView.active = YES;*/ - if ([GCController class]) { - if ([[GCController controllers] count] > 0) { - [self setupController:[[GCController controllers] firstObject]]; - } + if ([[GCController controllers] count] > 0) { + [self setupController:[[GCController controllers] firstObject]]; } cameraHelper = [[CameraHelper alloc] init]; @@ -214,6 +197,10 @@ extern float g_safeInsetBottom; [self hideKeyboard]; + UIScreenEdgePanGestureRecognizer *mBackGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:) ]; + [mBackGestureRecognizer setEdges:UIRectEdgeLeft]; + [[self view] addGestureRecognizer:mBackGestureRecognizer]; + // Was previously DISPATCH_QUEUE_PRIORITY_HIGH. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NativeInitGraphics(graphicsContext); @@ -279,9 +266,7 @@ extern float g_safeInsetBottom; [[NSNotificationCenter defaultCenter] removeObserver:self]; - if ([GCController class]) { - self.gameController = nil; - } + self.gameController = nil; if (graphicsContext) { graphicsContext->Shutdown(); @@ -294,6 +279,7 @@ extern float g_safeInsetBottom; - (void)dealloc { + INFO_LOG(SYSTEM, "dealloc"); [self shutdown]; } diff --git a/ios/ViewControllerCommon.h b/ios/ViewControllerCommon.h index 2758cb10cb..f4e9159e22 100644 --- a/ios/ViewControllerCommon.h +++ b/ios/ViewControllerCommon.h @@ -19,3 +19,6 @@ @end extern id sharedViewController; + +#define IS_IPAD() ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) +#define IS_IPHONE() ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) diff --git a/ios/ViewControllerMetal.mm b/ios/ViewControllerMetal.mm index bc7113ae78..32061b89eb 100644 --- a/ios/ViewControllerMetal.mm +++ b/ios/ViewControllerMetal.mm @@ -23,6 +23,8 @@ #include "Core/Config.h" #include "Core/ConfigValues.h" #include "Core/System.h" +#include "Core/HLE/sceUsbCam.h" +#include "Core/HLE/sceUsbGps.h" // TODO: Share this between backends. static uint32_t FlagsFromConfig() { @@ -249,12 +251,8 @@ static std::mutex renderLock; g_iCadeTracker.InitKeyMap(); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate:) name:UIApplicationWillTerminateNotification object:nil]; - - if ([GCController class]) // Checking the availability of a GameController framework - { - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controllerDidConnect:) name:GCControllerDidConnectNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controllerDidDisconnect:) name:GCControllerDidDisconnectNotification object:nil]; - } + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controllerDidConnect:) name:GCControllerDidConnectNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controllerDidDisconnect:) name:GCControllerDidDisconnectNotification object:nil]; } return self; } @@ -361,11 +359,25 @@ void VulkanRenderLoop(IOSVulkanContext *graphicsContext, CAMetalLayer *metalLaye int desiredBackbufferSizeX = g_display.pixel_xres; int desiredBackbufferSizeY = g_display.pixel_yres; + if ([[GCController controllers] count] > 0) { + [self setupController:[[GCController controllers] firstObject]]; + } + INFO_LOG(G3D, "Detected size: %dx%d", desiredBackbufferSizeX, desiredBackbufferSizeY); CAMetalLayer *layer = (CAMetalLayer *)self.view.layer; + cameraHelper = [[CameraHelper alloc] init]; + [cameraHelper setDelegate:self]; + + locationHelper = [[LocationHelper alloc] init]; + [locationHelper setDelegate:self]; + [self hideKeyboard]; + UIScreenEdgePanGestureRecognizer *mBackGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:) ]; + [mBackGestureRecognizer setEdges:UIRectEdgeLeft]; + [[self view] addGestureRecognizer:mBackGestureRecognizer]; + // Spin up the emu thread. It will in turn spin up the Vulkan render thread // on its own. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ @@ -384,33 +396,19 @@ void VulkanRenderLoop(IOSVulkanContext *graphicsContext, CAMetalLayer *metalLaye return [self view]; } -/** Since this is a single-view app, initialize Vulkan as view is appearing. */ -- (void)viewWillAppear:(BOOL) animated { +- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear: animated]; - self.view.contentScaleFactor = UIScreen.mainScreen.nativeScale; - - uint32_t fps = 60; - /* - _displayLink = [CADisplayLink displayLinkWithTarget: self selector: @selector(renderLoop)]; - [_displayLink setFrameInterval: 60 / fps]; - [_displayLink addToRunLoop: NSRunLoop.currentRunLoop forMode: NSDefaultRunLoopMode]; - */ } -/* --(void) renderLoop { - demo_draw(&demo); -} -*/ - -- (void)viewDidDisappear: (BOOL) animated { - // [_displayLink invalidate]; - // [_displayLink release]; - // demo_cleanup(&demo); +- (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear: animated]; } +- (void)viewDidAppear:(BOOL)animated { + [super viewDidAppear:animated]; + [self hideKeyboard]; +} - (BOOL)prefersHomeIndicatorAutoHidden { return YES; @@ -440,11 +438,42 @@ extern float g_safeInsetBottom; } } +// Enables tapping for edge area. +-(UIRectEdge)preferredScreenEdgesDeferringSystemGestures +{ + return UIRectEdgeAll; +} + - (void)bindDefaultFBO { // Do nothing } +- (NSUInteger)supportedInterfaceOrientations +{ + return UIInterfaceOrientationMaskLandscape; +} + +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event +{ + g_touchTracker.Began(touches, self.view); +} + +- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event +{ + g_touchTracker.Moved(touches, self.view); +} + +- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event +{ + g_touchTracker.Ended(touches, self.view); +} + +- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event +{ + g_touchTracker.Cancelled(touches, self.view); +} + - (void)buttonDown:(iCadeState)button { g_iCadeTracker.ButtonDown(button); @@ -455,6 +484,74 @@ extern float g_safeInsetBottom; g_iCadeTracker.ButtonUp(button); } +- (void)controllerDidConnect:(NSNotification *)note +{ + if (![[GCController controllers] containsObject:self.gameController]) self.gameController = nil; + + if (self.gameController != nil) return; // already have a connected controller + + [self setupController:(GCController *)note.object]; +} + +- (void)controllerDidDisconnect:(NSNotification *)note +{ + if (self.gameController == note.object) { + self.gameController = nil; + + if ([[GCController controllers] count] > 0) { + [self setupController:[[GCController controllers] firstObject]]; + } + } +} + +- (void)setupController:(GCController *)controller +{ + self.gameController = controller; + if (!SetupController(controller)) { + self.gameController = nil; + } +} + +- (void)startVideo:(int)width height:(int)height { + [cameraHelper startVideo:width h:height]; +} + +- (void)stopVideo { + [cameraHelper stopVideo]; +} + +- (void)PushCameraImageIOS:(long long)len buffer:(unsigned char*)data { + Camera::pushCameraImage(len, data); +} + +- (void)startLocation { + [locationHelper startLocationUpdates]; +} + +- (void)stopLocation { + [locationHelper stopLocationUpdates]; +} + +- (void)SetGpsDataIOS:(CLLocation *)newLocation { + GPS::setGpsData((long long)newLocation.timestamp.timeIntervalSince1970, + newLocation.horizontalAccuracy/5.0, + newLocation.coordinate.latitude, newLocation.coordinate.longitude, + newLocation.altitude, + MAX(newLocation.speed * 3.6, 0.0), /* m/s to km/h */ + 0 /* bearing */); +} + +- (void)handleSwipeFrom:(UIScreenEdgePanGestureRecognizer *)recognizer +{ + if (recognizer.state == UIGestureRecognizerStateEnded) { + KeyInput key; + key.flags = KEY_DOWN | KEY_UP; + key.keyCode = NKCODE_BACK; + key.deviceId = DEVICE_ID_TOUCH; + NativeKey(key); + INFO_LOG(SYSTEM, "Detected back swipe"); + } +} // The below is inspired by https://stackoverflow.com/questions/7253477/how-to-display-the-iphone-ipad-keyboard-over-a-full-screen-opengl-es-app // It's a bit limited but good enough.