8000 iOS: Implement accelerometer support by hrydgard · Pull Request #19243 · hrydgard/ppsspp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

iOS: Implement accelerometer support #19243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ elseif(IOS AND NOT LIBRETRO)
Common/Battery/AppleBatteryClient.m
)

set(nativeExtraLibs ${nativeExtraLibs} "-framework Foundation -framework MediaPlayer -framework AudioToolbox -framework CoreGraphics -framework QuartzCore -framework UIKit -framework GLKit -framework OpenAL -framework AVFoundation -framework CoreLocation -framework CoreText -framework CoreVideo -framework CoreMedia -framework CoreServices -framework Metal -framework IOSurface" )
set(nativeExtraLibs ${nativeExtraLibs} "-framework Foundation -framework MediaPlayer -framework AudioToolbox -framework CoreGraphics -framework CoreMotion -framework QuartzCore -framework UIKit -framework GLKit -framework OpenAL -framework AVFoundation -framework CoreLocation -framework CoreText -framework CoreVideo -framework CoreMedia -framework CoreServices -framework Metal -framework IOSurface" )
if(EXISTS "${CMAKE_IOS_SDK_ROOT}/System/Library/Frameworks/GameController.framework")
set(nativeExtraLibs ${nativeExtraLibs} "-weak_framework GameController")
endif()
Expand Down
4 changes: 4 additions & 0 deletions ios/Controls.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <map>

#import <GameController/GameController.h>
#import <CoreMotion/CoreMotion.h>

#include "iCade/iCadeState.h"
#include "Common/Input/InputState.h"

Expand Down Expand Up @@ -38,3 +40,5 @@ struct ICadeTracker {
double lastSelectPress = 0.0f;
double lastStartPress = 0.0f;
};

void ProcessAccelerometerData(CMAccelerometerData *accData);
8 changes: 8 additions & 0 deletions ios/Controls.mm
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,11 @@ bool SetupController(GCController *controller) {
NativeKey(key);
}
}

void ProcessAccelerometerData(CMAccelerometerData *accData) {
CMAcceleration acc = accData.acceleration;
// INFO_LOG(SYSTEM, "%f %f %f", acc.x, acc.y, acc.z);

// Might need to change these for portrait or inverse landscape
NativeAccelerometer(-acc.x, -acc.y, -acc.z);
}
30 changes: 29 additions & 1 deletion ios/ViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ @interface PPSSPPViewControllerGL () {

//@property (nonatomic) iCadeReaderView* iCadeView;
@property (nonatomic) GCController *gameController __attribute__((weak_import));
@property (strong, nonatomic) CMMotionManager *motionManager;
@property (strong, nonatomic) NSOperationQueue *accelerometerQueue;

@end

Expand All @@ -124,6 +126,9 @@ -(id) init {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controllerDidConnect:) name:GCControllerDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controllerDidDisconnect:) name:GCControllerDidDisconnectNotification object:nil];
}
self.accelerometerQueue = [[NSOperationQueue alloc] init];
self.accelerometerQueue.name = @"AccelerometerQueue";
self.accelerometerQueue.maxConcurrentOperationCount = 1;
return self;
}

Expand Down Expand Up @@ -269,7 +274,9 @@ - (void)viewDidLoad {
[mBackGestureRecognizer setEdges:UIRectEdgeLeft];
[[self view] addGestureRecognizer:mBackGestureRecognizer];

INFO_LOG(G3D, "Done with viewDidLoad. Next up, OpenGL");
// Initialize the motion manager for accelerometer control.
self.motionManager = [[CMMotionManager alloc] init];
INFO_LOG(G3D, "Done with viewDidLoad.");
}

- (void)handleSwipeFrom:(UIScreenEdgePanGestureRecognizer *)recognizer
Expand All @@ -291,13 +298,34 @@ - (void)appWillTerminate:(NSNotification *)notification

- (void)didBecomeActive {
INFO_LOG(SYSTEM, "didBecomeActive begin");
if (self.motionManager.accelerometerAvailable) {
self.motionManager.accelerometerUpdateInterval = 1.0 / 60.0;
INFO_LOG(G3D, "Starting accelerometer updates.");

[self.motionManager startAccelerometerUpdatesToQueue:self.accelerometerQueue
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
if (error) {
NSLog(@"Accelerometer error: %@", error);
return;
}
ProcessAccelerometerData(accelerometerData);
}];
} else {
INFO_LOG(G3D, "No accelerometer available, not starting updates.");
}
[self runGLRenderLoop];
INFO_LOG(SYSTEM, "didBecomeActive end");
}

- (void)willResignActive {
INFO_LOG(SYSTEM, "willResignActive begin");
[self requestExitGLRenderLoop];

// Stop accelerometer updates
if (self.motionManager.accelerometerActive) {
INFO_LOG(G3D, "Stopping accelerometer updates");
[self.motionManager stopAccelerometerUpdates];
}
INFO_LOG(SYSTEM, "willResignActive end");
}

Expand Down
31 changes: 30 additions & 1 deletion ios/ViewControllerMetal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ @interface PPSSPPViewControllerMetal () {
}

@property (nonatomic) GCController *gameController __attribute__((weak_import));
@property (strong, nonatomic) CMMotionManager *motionManager;
@property (strong, nonatomic) NSOperationQueue *accelerometerQueue;

@end // @interface

Expand All @@ -228,6 +230,9 @@ - (id)init {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controllerDidConnect:) name:GCControllerDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(controllerDidDisconnect:) name:GCControllerDidDisconnectNotification object:nil];
}
self.accelerometerQueue = [[NSOperationQueue alloc] init];
self.accelerometerQueue.name = @"AccelerometerQueue";
self.accelerometerQueue.maxConcurrentOperationCount = 1;
return self;
}

Expand Down Expand Up @@ -338,7 +343,21 @@ - (void)requestExitVulkanRenderLoop {
// These two are forwarded from the appDelegate
- (void)didBecomeActive {
INFO_LOG(G3D, "didBecomeActive GL");

if (self.motionManager.accelerometerAvailable) {
self.motionManager.accelerometerUpdateInterval = 1.0 / 60.0;
INFO_LOG(G3D, "Starting accelerometer updates.");

[self.motionManager startAccelerometerUpdatesToQueue:self.accelerometerQueue
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
if (error) {
NSLog(@"Accelerometer error: %@", error);
return;
}
ProcessAccelerometerData(accelerometerData);
}];
} else {
INFO_LOG(G3D, "No accelerometer available, not starting updates.");
}
// Spin up the emu thread. It will in turn spin up the Vulkan render thread
// on its own.
[self runVulkanRenderLoop];
Expand All @@ -347,6 +366,12 @@ - (void)didBecomeActive {
- (void)willResignActive {
INFO_LOG(G3D, "willResignActive GL");
[self requestExitVulkanRenderLoop];

// Stop accelerometer updates
if (self.motionManager.accelerometerActive) {
INFO_LOG(G3D, "Stopping accelerometer updates");
[self.motionManager stopAccelerometerUpdates];
}
}

- (void)shutdown
Expand Down Expand Up @@ -419,6 +444,9 @@ - (void)viewDidLoad {
UIScreenEdgePanGestureRecognizer *mBackGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:) ];
[mBackGestureRecognizer setEdges:UIRectEdgeLeft];
[[self view] addGestureRecognizer:mBackGestureRecognizer];

// Initialize the motion manager for accelerometer control.
self.motionManager = [[CMMotionManager alloc] init];
}

// Allow device rotation to resize the swapchain
Expand All @@ -444,6 +472,7 @@ - (void)viewWillDisappear:(BOOL)animated {

- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear: animated];
INFO_LOG(G3D, "viewWillDisappear");
}

- (void)viewDidAppear:(BOOL)animated {
Expand Down
Loading
0