Skip to content

Commit

Permalink
Fix controller issues
Browse files Browse the repository at this point in the history
  • Loading branch information
GeffDev committed Aug 10, 2024
1 parent 4469a66 commit c0652f7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
28 changes: 18 additions & 10 deletions RSDKv4/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ int lastMouseY = 0;

struct InputDevice {
#if RETRO_USING_SDL2
// we need the controller index reported from SDL2's controller added event
int index;
SDL_GameController *devicePtr;
SDL_Haptic *hapticPtr;
#endif
Expand Down Expand Up @@ -206,7 +208,7 @@ bool getControllerButton(byte buttonID)
}
#endif //! RETRO_USING_SDL2

void controllerInit(byte controllerID)
void controllerInit(int controllerID)
{
for (int i = 0; i < controllers.size(); ++i) {
if (controllers[i].id == controllerID) {
Expand All @@ -218,7 +220,8 @@ void controllerInit(byte controllerID)
SDL_GameController *controller = SDL_GameControllerOpen(controllerID);
if (controller) {
InputDevice device;
device.id = 0;
device.id = controllerID;
device.index = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller));
device.devicePtr = controller;
device.hapticPtr = SDL_HapticOpenFromJoystick(SDL_GameControllerGetJoystick(controller));
if (device.hapticPtr == NULL) {
Expand All @@ -239,22 +242,26 @@ void controllerInit(byte controllerID)
#endif
}

void controllerClose(byte controllerID)
void controllerClose(int controllerID)
{
#if RETRO_USING_SDL2
SDL_GameController *controller = SDL_GameControllerFromInstanceID(controllerID);
if (controller) {
SDL_GameControllerClose(controller);
#endif
for (int i = 0; i < controllers.size(); ++i) {
if (controllers[i].id == controllerID) {
controllers.erase(controllers.begin() + controllerID);
if (controllers.size() == 1) {
controllers.clear();
} else {
for (int i = 0; i < controllers.size(); ++i) {
if (controllers[i].index == controllerID) {
controllers.erase(controllers.begin() + i);
#if RETRO_USING_SDL2
if (controllers[i].hapticPtr) {
SDL_HapticClose(controllers[i].hapticPtr);
}
if (controllers[i].hapticPtr) {
SDL_HapticClose(controllers[i].hapticPtr);
}
#endif
break;
break;
}
}
}
#if RETRO_USING_SDL2
Expand Down Expand Up @@ -287,6 +294,7 @@ void InitInputDevices()
SDL_GameController *gamepad = SDL_GameControllerOpen(i);
InputDevice device;
device.id = 0;
device.index = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamepad));
device.devicePtr = gamepad;

if (SDL_GameControllerGetAttached(gamepad))
Expand Down
4 changes: 2 additions & 2 deletions RSDKv4/Input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ enum ExtraSDLButtons {
SDL_CONTROLLER_BUTTON_MAX_EXTRA,
};

void controllerInit(byte controllerID);
void controllerClose(byte controllerID);
void controllerInit(int controllerID);
void controllerClose(int controllerID);
#endif

#if RETRO_USING_SDL1
Expand Down

0 comments on commit c0652f7

Please sign in to comment.