Improve the SDL2 controller backend too

This commit is contained in:
Clownacy 2020-04-05 00:30:58 +01:00
parent c187660904
commit 26de0b6043
2 changed files with 42 additions and 13 deletions

View file

@ -102,11 +102,11 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status)
int total_hats; int total_hats;
const unsigned char *hats = glfwGetJoystickHats(connected_joystick_id, &total_hats); const unsigned char *hats = glfwGetJoystickHats(connected_joystick_id, &total_hats);
// Handle direction inputs // Handle directional inputs
status->bLeft = axes[0] < -DEADZONE; status->bLeft = axes[0] < axis_neutrals[0] - DEADZONE;
status->bRight = axes[0] > DEADZONE; status->bRight = axes[0] > axis_neutrals[0] + DEADZONE;
status->bUp = axes[1] < -DEADZONE; status->bUp = axes[1] < axis_neutrals[1] - DEADZONE;
status->bDown = axes[1] > DEADZONE; status->bDown = axes[1] > axis_neutrals[1] + DEADZONE;
// Handle button inputs // Handle button inputs
unsigned int buttons_done = 0; unsigned int buttons_done = 0;

View file

@ -12,6 +12,8 @@
static SDL_Joystick *joystick; static SDL_Joystick *joystick;
static Sint16 *axis_neutrals;
BOOL ControllerBackend_Init(void) BOOL ControllerBackend_Init(void)
{ {
SDL_InitSubSystem(SDL_INIT_JOYSTICK); SDL_InitSubSystem(SDL_INIT_JOYSTICK);
@ -37,20 +39,23 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status)
const size_t button_limit = sizeof(status->bButton) / sizeof(status->bButton[0]); 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_x = SDL_JoystickGetAxis(joystick, 0);
const Sint16 joystick_y = SDL_JoystickGetAxis(joystick, 1); const Sint16 joystick_y = SDL_JoystickGetAxis(joystick, 1);
status->bLeft = joystick_x < -DEADZONE; status->bLeft = joystick_x < axis_neutrals[0] - DEADZONE;
status->bRight = joystick_x > DEADZONE; status->bRight = joystick_x > axis_neutrals[0] + DEADZONE;
status->bUp = joystick_y < -DEADZONE; status->bUp = joystick_y < axis_neutrals[1] - DEADZONE;
status->bDown = joystick_y > DEADZONE; status->bDown = joystick_y > axis_neutrals[1] + DEADZONE;
// Handle button inputs
int total_buttons = SDL_JoystickNumButtons(joystick); int total_buttons = SDL_JoystickNumButtons(joystick);
int total_axes = SDL_JoystickNumAxes(joystick); int total_axes = SDL_JoystickNumAxes(joystick);
int total_hats = SDL_JoystickNumHats(joystick); int total_hats = SDL_JoystickNumHats(joystick);
unsigned int buttons_done = 0; unsigned int buttons_done = 0;
// Start with the joystick buttons
for (int i = 0; i < total_buttons; ++i) for (int i = 0; i < total_buttons; ++i)
{ {
status->bButton[buttons_done] = SDL_JoystickGetButton(joystick, i); status->bButton[buttons_done] = SDL_JoystickGetButton(joystick, i);
@ -59,21 +64,23 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status)
break; break;
} }
// Then the joystick axes
for (int i = 0; i < total_axes; ++i) for (int i = 0; i < total_axes; ++i)
{ {
Sint16 axis = SDL_JoystickGetAxis(joystick, 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) if (++buttons_done >= button_limit)
break; break;
status->bButton[buttons_done] = axis > DEADZONE; status->bButton[buttons_done] = axis > axis_neutrals[i] + DEADZONE;
if (++buttons_done >= button_limit) if (++buttons_done >= button_limit)
break; break;
} }
// Then the joystick hats
for (int i = 0; i < total_axes; ++i) for (int i = 0; i < total_axes; ++i)
{ {
Uint8 hat = SDL_JoystickGetHat(joystick, i); Uint8 hat = SDL_JoystickGetHat(joystick, i);
@ -99,7 +106,7 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status)
break; break;
} }
// Blank the buttons that do not // Blank any remaining buttons
for (size_t i = buttons_done; i < button_limit; ++i) for (size_t i = buttons_done; i < button_limit; ++i)
status->bButton[i] = FALSE; status->bButton[i] = FALSE;
@ -123,7 +130,26 @@ void ControllerBackend_JoystickConnect(Sint32 joystick_id)
joystick = SDL_JoystickOpen(joystick_id); joystick = SDL_JoystickOpen(joystick_id);
if (joystick != NULL) if (joystick != NULL)
{
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); 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 @@ void ControllerBackend_JoystickDisconnect(Sint32 joystick_id)
if (joystick_id == SDL_JoystickInstanceID(joystick)) if (joystick_id == SDL_JoystickInstanceID(joystick))
{ {
printf("Joystick #%d disconnected\n", joystick_id); printf("Joystick #%d disconnected\n", joystick_id);
SDL_JoystickClose(joystick);
joystick = NULL; joystick = NULL;
free(axis_neutrals);
} }
} }