From 7f6574117d80090e6ceffaefef5e8ff2f0c0d5dc Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sat, 4 Apr 2020 19:14:40 +0100 Subject: [PATCH] Clean-up SDL2 controller backend --- src/Backends/SDL2/Controller.cpp | 50 ++++++++++---------------------- 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/src/Backends/SDL2/Controller.cpp b/src/Backends/SDL2/Controller.cpp index 2713753c..288165e0 100644 --- a/src/Backends/SDL2/Controller.cpp +++ b/src/Backends/SDL2/Controller.cpp @@ -7,27 +7,32 @@ #include "../../WindowsWrapper.h" -// The original names for these variables are unknown -static SDL_Joystick *joystick = NULL; -static int joystick_neutral_x = 0; -static int joystick_neutral_y = 0; - -// It looks like Pixel declared his functions early, so he could forward-reference -BOOL FindAndOpenDirectInputDevice(void); +static SDL_Joystick *joystick; +static int joystick_neutral_x; +static int joystick_neutral_y; BOOL ControllerBackend_Init(void) { SDL_InitSubSystem(SDL_INIT_JOYSTICK); - if (!FindAndOpenDirectInputDevice()) - return FALSE; + for (int i = 0; i < SDL_NumJoysticks(); ++i) + { + printf("Joystick #%d connected - %s\n", i, SDL_JoystickNameForIndex(i)); + + if (joystick == NULL) + { + joystick = SDL_JoystickOpen(i); + + if (joystick != NULL) + printf("Joystick #%d selected\n", i); + } + } return TRUE; } void ControllerBackend_Deinit(void) { - // Close opened joystick (if exists) if (joystick != NULL) { SDL_JoystickClose(joystick); @@ -37,31 +42,6 @@ void ControllerBackend_Deinit(void) SDL_QuitSubSystem(SDL_INIT_JOYSTICK); } -// The original name for this function and its variables are unknown. -// This function finds and hooks the first available DirectInput device (or SDL Joystick, in this case). -BOOL FindAndOpenDirectInputDevice(void) -{ - int i; - - for (i = 0; i < SDL_NumJoysticks(); ++i) - printf("Joystick #%d name: %s\n", i, SDL_JoystickNameForIndex(i)); - - // Open first available joystick - for (i = 0; i < SDL_NumJoysticks(); ++i) - { - joystick = SDL_JoystickOpen(i); - - // Break as soon as a joystick is properly opened - if (joystick != NULL) - { - printf("Joystick #%d selected\n", i); - return TRUE; - } - } - - return FALSE; -} - BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) { if (joystick == NULL)