Added haptic implementation

This commit is contained in:
Zerocker 2020-05-30 20:37:37 +09:00
parent cf122ee6e8
commit 2bc577bc3c
3 changed files with 31 additions and 7 deletions

View file

@ -30,7 +30,8 @@ s32 osContInit(OSMesgQueue *mq, u8 *controllerBits, OSContStatus *status) {
} }
s32 osMotorStart(void *pfs) { s32 osMotorStart(void *pfs) {
return controller_rumble_play(0.5, 1000); // Since rumble stops by osMotorStop, its duration is not nessecary.
return controller_rumble_play(configRumbleStrength / 100.0, 50);
} }
s32 osMotorStop(void *pfs) { s32 osMotorStop(void *pfs) {

View file

@ -34,7 +34,7 @@ extern u8 newcam_mouse;
static bool init_ok; static bool init_ok;
static SDL_GameController *sdl_cntrl; static SDL_GameController *sdl_cntrl;
static SDL_Haptic *sdl_haptic;
static u32 num_joy_binds = 0; static u32 num_joy_binds = 0;
static u32 num_mouse_binds = 0; static u32 num_mouse_binds = 0;
@ -81,7 +81,7 @@ static void controller_sdl_bind(void) {
} }
static void controller_sdl_init(void) { static void controller_sdl_init(void) {
if (SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_EVENTS) != 0) { if (SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_EVENTS | SDL_INIT_HAPTIC) != 0) {
fprintf(stderr, "SDL init error: %s\n", SDL_GetError()); fprintf(stderr, "SDL init error: %s\n", SDL_GetError());
return; return;
} }
@ -130,6 +130,7 @@ static void controller_sdl_read(OSContPad *pad) {
for (int i = 0; i < SDL_NumJoysticks(); i++) { for (int i = 0; i < SDL_NumJoysticks(); i++) {
if (SDL_IsGameController(i)) { if (SDL_IsGameController(i)) {
sdl_cntrl = SDL_GameControllerOpen(i); sdl_cntrl = SDL_GameControllerOpen(i);
sdl_haptic = SDL_HapticOpen(i);
if (sdl_cntrl != NULL) { if (sdl_cntrl != NULL) {
break; break;
} }
@ -213,21 +214,43 @@ static void controller_sdl_shutdown(void) {
SDL_GameControllerClose(sdl_cntrl); SDL_GameControllerClose(sdl_cntrl);
sdl_cntrl = NULL; sdl_cntrl = NULL;
} }
if (sdl_haptic) {
SDL_HapticClose(sdl_haptic);
sdl_haptic = NULL;
}
SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER); SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
} }
init_ok = false; init_ok = false;
} }
s32 controller_rumble_init(void) { u32 controller_rumble_init(void) {
if (SDL_HapticRumbleSupported(sdl_haptic) != SDL_TRUE) {
printf("Controller does not support haptics! %s\n", SDL_GetError());
return 1;
}
if (SDL_HapticRumbleInit(sdl_haptic) != 0) {
printf("Unable to initialize rumble! %s\n", SDL_GetError());
return 1;
}
return 0; return 0;
} }
s32 controller_rumble_play(f32 strength, u32 length) { s32 controller_rumble_play(f32 strength, u32 length) {
if (SDL_HapticRumblePlay(sdl_haptic, strength, length) != 0) {
printf("Unable to start rumble! %s\n", SDL_GetError());
return -1;
} else {
return 0; return 0;
}
} }
s32 controller_rumble_stop(void) { s32 controller_rumble_stop(void) {
if (SDL_HapticRumbleStop(sdl_haptic) != 0) {
printf("Unable to stop rumble! %s\n", SDL_GetError());
return -1;
} else {
return 0; return 0;
}
} }
struct ControllerAPI controller_sdl = { struct ControllerAPI controller_sdl = {

View file

@ -7,7 +7,7 @@
extern struct ControllerAPI controller_sdl; extern struct ControllerAPI controller_sdl;
s32 controller_rumble_init(void); u32 controller_rumble_init(void);
s32 controller_rumble_play(f32 strength, u32 length); s32 controller_rumble_play(f32 strength, u32 length);
s32 controller_rumble_stop(void); s32 controller_rumble_stop(void);