ref: 26de0b6043726b7c10b22020f1ee81e0bd167192
parent: c1876609045eb1cf8c4cd779edbc31b625b8f148
author: Clownacy <Clownacy@users.noreply.github.com>
date: Sat Apr 4 20:30:58 EDT 2020
Improve the SDL2 controller backend too
--- a/src/Backends/GLFW3/Controller.cpp
+++ b/src/Backends/GLFW3/Controller.cpp
@@ -102,11 +102,11 @@
int total_hats;
const unsigned char *hats = glfwGetJoystickHats(connected_joystick_id, &total_hats);
- // Handle direction inputs
- status->bLeft = axes[0] < -DEADZONE;
- status->bRight = axes[0] > DEADZONE;
- status->bUp = axes[1] < -DEADZONE;
- status->bDown = axes[1] > DEADZONE;
+ // Handle directional inputs
+ status->bLeft = axes[0] < axis_neutrals[0] - DEADZONE;
+ status->bRight = axes[0] > axis_neutrals[0] + DEADZONE;
+ status->bUp = axes[1] < axis_neutrals[1] - DEADZONE;
+ status->bDown = axes[1] > axis_neutrals[1] + DEADZONE;
// Handle button inputs
unsigned int buttons_done = 0;
--- a/src/Backends/SDL2/Controller.cpp
+++ b/src/Backends/SDL2/Controller.cpp
@@ -12,6 +12,8 @@
static SDL_Joystick *joystick;
+static Sint16 *axis_neutrals;
+
BOOL ControllerBackend_Init(void)
{
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
@@ -37,14 +39,16 @@
const size_t button_limit = sizeof(status->bButton) / sizeof(status->bButton[0]);
+ // Handle directional inputs
const Sint16 joystick_x = SDL_JoystickGetAxis(joystick, 0);
const Sint16 joystick_y = SDL_JoystickGetAxis(joystick, 1);
- status->bLeft = joystick_x < -DEADZONE;
- status->bRight = joystick_x > DEADZONE;
- status->bUp = joystick_y < -DEADZONE;
- status->bDown = joystick_y > DEADZONE;
+ status->bLeft = joystick_x < axis_neutrals[0] - DEADZONE;
+ status->bRight = joystick_x > axis_neutrals[0] + DEADZONE;
+ status->bUp = joystick_y < axis_neutrals[1] - DEADZONE;
+ status->bDown = joystick_y > axis_neutrals[1] + DEADZONE;
+ // Handle button inputs
int total_buttons = SDL_JoystickNumButtons(joystick);
int total_axes = SDL_JoystickNumAxes(joystick);
int total_hats = SDL_JoystickNumHats(joystick);
@@ -51,6 +55,7 @@
unsigned int buttons_done = 0;
+ // Start with the joystick buttons
for (int i = 0; i < total_buttons; ++i)
{
status->bButton[buttons_done] = SDL_JoystickGetButton(joystick, i);
@@ -59,21 +64,23 @@
break;
}
+ // Then the joystick axes
for (int i = 0; i < total_axes; ++i)
{
Sint16 axis = SDL_JoystickGetAxis(joystick, i);
- status->bButton[buttons_done] = axis < -DEADZONE;
+ status->bButton[buttons_done] = axis < axis_neutrals[i] - DEADZONE;
if (++buttons_done >= button_limit)
break;
- status->bButton[buttons_done] = axis > DEADZONE;
+ status->bButton[buttons_done] = axis > axis_neutrals[i] + DEADZONE;
if (++buttons_done >= button_limit)
break;
}
+ // Then the joystick hats
for (int i = 0; i < total_axes; ++i)
{
Uint8 hat = SDL_JoystickGetHat(joystick, i);
@@ -99,7 +106,7 @@
break;
}
- // Blank the buttons that do not
+ // Blank any remaining buttons
for (size_t i = buttons_done; i < button_limit; ++i)
status->bButton[i] = FALSE;
@@ -123,7 +130,26 @@
joystick = SDL_JoystickOpen(joystick_id);
if (joystick != NULL)
- printf("Joystick #%d selected\n", joystick_id);
+ {
+ int total_axes = SDL_JoystickNumAxes(joystick);
+ int total_buttons = SDL_JoystickNumButtons(joystick);
+
+ if (total_axes >= 2 && total_buttons >= 6)
+ {
+ printf("Joystick #%d selected\n", joystick_id);
+
+ // Set up neutral axes
+ axis_neutrals = (Sint16*)malloc(sizeof(Sint16) * total_axes);
+
+ for (int i = 0; i < total_axes; ++i)
+ axis_neutrals[i] = SDL_JoystickGetAxis(joystick, i);
+ }
+ else
+ {
+ SDL_JoystickClose(joystick);
+ joystick = NULL;
+ }
+ }
}
}
@@ -132,6 +158,9 @@
if (joystick_id == SDL_JoystickInstanceID(joystick))
{
printf("Joystick #%d disconnected\n", joystick_id);
+ SDL_JoystickClose(joystick);
joystick = NULL;
+
+ free(axis_neutrals);
}
}