mirror of
https://github.com/Relintai/sdl2_frt.git
synced 2024-12-16 11:06:49 +01:00
Update the existing haptic player when we rumble on iOS
This commit is contained in:
parent
645a3280ec
commit
d9aea0c3a0
@ -827,6 +827,7 @@ IOS_MFIJoystickUpdate(SDL_Joystick * joystick)
|
|||||||
@implementation SDL_RumbleMotor {
|
@implementation SDL_RumbleMotor {
|
||||||
CHHapticEngine *engine API_AVAILABLE(ios(13.0), tvos(14.0));
|
CHHapticEngine *engine API_AVAILABLE(ios(13.0), tvos(14.0));
|
||||||
id<CHHapticPatternPlayer> player API_AVAILABLE(ios(13.0), tvos(14.0));
|
id<CHHapticPatternPlayer> player API_AVAILABLE(ios(13.0), tvos(14.0));
|
||||||
|
bool active;
|
||||||
}
|
}
|
||||||
|
|
||||||
-(void)cleanup
|
-(void)cleanup
|
||||||
@ -843,78 +844,92 @@ IOS_MFIJoystickUpdate(SDL_Joystick * joystick)
|
|||||||
|
|
||||||
-(int)setIntensity:(float)intensity
|
-(int)setIntensity:(float)intensity
|
||||||
{
|
{
|
||||||
if (@available(iOS 14.0, tvOS 14.0, *)) {
|
@autoreleasepool {
|
||||||
NSError *error;
|
if (@available(iOS 14.0, tvOS 14.0, *)) {
|
||||||
|
NSError *error;
|
||||||
if (self->player != nil) {
|
|
||||||
[self->player stopAtTime:0 error:&error];
|
if (self->engine == nil) {
|
||||||
self->player = nil;
|
return SDL_SetError("Haptics engine was stopped");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self->engine == nil) {
|
if (intensity == 0.0f) {
|
||||||
return SDL_SetError("Haptics engine not available");
|
if (self->player && self->active) {
|
||||||
}
|
[self->player stopAtTime:0 error:&error];
|
||||||
|
}
|
||||||
if (intensity <= 0.01f) {
|
self->active = false;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CHHapticEventParameter *param = [[CHHapticEventParameter alloc] initWithParameterID:CHHapticEventParameterIDHapticIntensity value:intensity];
|
if (self->player == nil) {
|
||||||
CHHapticEvent *event = [[CHHapticEvent alloc] initWithEventType:CHHapticEventTypeHapticContinuous parameters:[NSArray arrayWithObjects:param, nil] relativeTime:0 duration:GCHapticDurationInfinite];
|
CHHapticEventParameter *param = [[CHHapticEventParameter alloc] initWithParameterID:CHHapticEventParameterIDHapticIntensity value:1.0f];
|
||||||
CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithEvents:[NSArray arrayWithObject:event] parameters:[[NSArray alloc] init] error:&error];
|
CHHapticEvent *event = [[CHHapticEvent alloc] initWithEventType:CHHapticEventTypeHapticContinuous parameters:[NSArray arrayWithObjects:param, nil] relativeTime:0 duration:GCHapticDurationInfinite];
|
||||||
if (error != nil) {
|
CHHapticPattern *pattern = [[CHHapticPattern alloc] initWithEvents:[NSArray arrayWithObject:event] parameters:[[NSArray alloc] init] error:&error];
|
||||||
return SDL_SetError("Couldn't create haptic pattern: %s", [error.localizedDescription UTF8String]);
|
if (error != nil) {
|
||||||
}
|
return SDL_SetError("Couldn't create haptic pattern: %s", [error.localizedDescription UTF8String]);
|
||||||
|
}
|
||||||
|
|
||||||
self->player = [self->engine createPlayerWithPattern:pattern error:&error];
|
self->player = [self->engine createPlayerWithPattern:pattern error:&error];
|
||||||
if (error != nil) {
|
if (error != nil) {
|
||||||
return SDL_SetError("Couldn't create haptic player: %s", [error.localizedDescription UTF8String]);
|
return SDL_SetError("Couldn't create haptic player: %s", [error.localizedDescription UTF8String]);
|
||||||
}
|
}
|
||||||
|
self->active = false;
|
||||||
|
}
|
||||||
|
|
||||||
[self->player startAtTime:0 error:&error];
|
CHHapticDynamicParameter *param = [[CHHapticDynamicParameter alloc] initWithParameterID:CHHapticDynamicParameterIDHapticIntensityControl value:intensity relativeTime:0];
|
||||||
if (error != nil) {
|
[self->player sendParameters:[NSArray arrayWithObject:param] atTime:0 error:&error];
|
||||||
self->player = nil;
|
if (error != nil) {
|
||||||
return SDL_SetError("Couldn't start playback: %s", [error.localizedDescription UTF8String]);
|
return SDL_SetError("Couldn't update haptic player: %s", [error.localizedDescription UTF8String]);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return 0;
|
if (!self->active) {
|
||||||
|
[self->player startAtTime:0 error:&error];
|
||||||
|
self->active = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
-(id) initWithController:(GCController*)controller locality:(GCHapticsLocality)locality API_AVAILABLE(ios(14.0), tvos(14.0))
|
-(id) initWithController:(GCController*)controller locality:(GCHapticsLocality)locality API_AVAILABLE(ios(14.0), tvos(14.0))
|
||||||
{
|
{
|
||||||
NSError *error;
|
@autoreleasepool {
|
||||||
|
NSError *error;
|
||||||
|
|
||||||
self->engine = [controller.haptics createEngineWithLocality:locality];
|
self->engine = [controller.haptics createEngineWithLocality:locality];
|
||||||
if (self->engine == nil) {
|
if (self->engine == nil) {
|
||||||
return nil;
|
SDL_SetError("Couldn't create haptics engine");
|
||||||
}
|
return nil;
|
||||||
|
}
|
||||||
[self->engine startAndReturnError:&error];
|
|
||||||
if (error != nil) {
|
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
__weak typeof(self) weakSelf = self;
|
[self->engine startAndReturnError:&error];
|
||||||
self->engine.stoppedHandler = ^(CHHapticEngineStoppedReason stoppedReason) {
|
if (error != nil) {
|
||||||
SDL_RumbleMotor *_this = weakSelf;
|
SDL_SetError("Couldn't start haptics engine");
|
||||||
if (_this == nil) {
|
return nil;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
_this->player = nil;
|
__weak typeof(self) weakSelf = self;
|
||||||
_this->engine = nil;
|
self->engine.stoppedHandler = ^(CHHapticEngineStoppedReason stoppedReason) {
|
||||||
};
|
SDL_RumbleMotor *_this = weakSelf;
|
||||||
self->engine.resetHandler = ^{
|
if (_this == nil) {
|
||||||
SDL_RumbleMotor *_this = weakSelf;
|
return;
|
||||||
if (_this == nil) {
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_this->player = nil;
|
_this->player = nil;
|
||||||
[_this->engine startAndReturnError:nil];
|
_this->engine = nil;
|
||||||
};
|
};
|
||||||
|
self->engine.resetHandler = ^{
|
||||||
return self;
|
SDL_RumbleMotor *_this = weakSelf;
|
||||||
|
if (_this == nil) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_this->player = nil;
|
||||||
|
[_this->engine startAndReturnError:nil];
|
||||||
|
};
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
@ -943,6 +958,12 @@ IOS_MFIJoystickUpdate(SDL_Joystick * joystick)
|
|||||||
return ((result < 0) ? -1 : 0);
|
return ((result < 0) ? -1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-(void)cleanup
|
||||||
|
{
|
||||||
|
[self->low_frequency_motor cleanup];
|
||||||
|
[self->high_frequency_motor cleanup];
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
static SDL_RumbleContext *IOS_JoystickInitRumble(GCController *controller)
|
static SDL_RumbleContext *IOS_JoystickInitRumble(GCController *controller)
|
||||||
@ -1015,10 +1036,15 @@ IOS_JoystickClose(SDL_Joystick * joystick)
|
|||||||
device->joystick = NULL;
|
device->joystick = NULL;
|
||||||
|
|
||||||
@autoreleasepool {
|
@autoreleasepool {
|
||||||
|
#ifdef ENABLE_MFI_RUMBLE
|
||||||
if (device->rumble) {
|
if (device->rumble) {
|
||||||
|
SDL_RumbleContext *rumble = (__bridge SDL_RumbleContext *)device->rumble;
|
||||||
|
|
||||||
|
[rumble cleanup];
|
||||||
CFRelease(device->rumble);
|
CFRelease(device->rumble);
|
||||||
device->rumble = NULL;
|
device->rumble = NULL;
|
||||||
}
|
}
|
||||||
|
#endif /* ENABLE_MFI_RUMBLE */
|
||||||
|
|
||||||
if (device->accelerometer) {
|
if (device->accelerometer) {
|
||||||
#if !TARGET_OS_TV
|
#if !TARGET_OS_TV
|
||||||
|
Loading…
Reference in New Issue
Block a user