Restore support for joystick axes neutrals
These are useful for PS3 analogue triggers, which are -1.0f by default, and go up to 1.0f when pressed.
This commit is contained in:
parent
ac533cb35e
commit
e0674d8f54
1 changed files with 33 additions and 9 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define GLFW_INCLUDE_NONE
|
#define GLFW_INCLUDE_NONE
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
@ -13,6 +14,8 @@
|
||||||
static BOOL joystick_connected;
|
static BOOL joystick_connected;
|
||||||
static int connected_joystick_id;
|
static int connected_joystick_id;
|
||||||
|
|
||||||
|
static float *axis_neutrals;
|
||||||
|
|
||||||
static void JoystickCallback(int joystick_id, int event)
|
static void JoystickCallback(int joystick_id, int event)
|
||||||
{
|
{
|
||||||
switch (event)
|
switch (event)
|
||||||
|
@ -34,6 +37,15 @@ static void JoystickCallback(int joystick_id, int event)
|
||||||
printf("Joystick #%d selected\n", joystick_id);
|
printf("Joystick #%d selected\n", joystick_id);
|
||||||
joystick_connected = TRUE;
|
joystick_connected = TRUE;
|
||||||
connected_joystick_id = joystick_id;
|
connected_joystick_id = joystick_id;
|
||||||
|
|
||||||
|
// Set up neutral axes
|
||||||
|
int total_axes;
|
||||||
|
const float *axes = glfwGetJoystickAxes(connected_joystick_id, &total_axes);
|
||||||
|
|
||||||
|
axis_neutrals = (float*)malloc(sizeof(float) * total_axes);
|
||||||
|
|
||||||
|
for (int i = 0; i < total_axes; ++i)
|
||||||
|
axis_neutrals[i] = axes[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,6 +57,8 @@ static void JoystickCallback(int joystick_id, int event)
|
||||||
{
|
{
|
||||||
printf("Joystick #%d disconnected\n", connected_joystick_id);
|
printf("Joystick #%d disconnected\n", connected_joystick_id);
|
||||||
joystick_connected = FALSE;
|
joystick_connected = FALSE;
|
||||||
|
|
||||||
|
free(axis_neutrals);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
@ -89,14 +103,23 @@ 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
|
||||||
|
if (axes >= 1)
|
||||||
|
{
|
||||||
status->bLeft = axes[0] < -DEADZONE;
|
status->bLeft = axes[0] < -DEADZONE;
|
||||||
status->bRight = axes[0] > DEADZONE;
|
status->bRight = axes[0] > DEADZONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (axes >= 2)
|
||||||
|
{
|
||||||
status->bUp = axes[1] < -DEADZONE;
|
status->bUp = axes[1] < -DEADZONE;
|
||||||
status->bDown = axes[1] > DEADZONE;
|
status->bDown = axes[1] > DEADZONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle button inputs
|
||||||
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] = buttons[i] == GLFW_PRESS;
|
status->bButton[buttons_done] = buttons[i] == GLFW_PRESS;
|
||||||
|
@ -105,20 +128,21 @@ 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)
|
||||||
{
|
{
|
||||||
status->bButton[buttons_done] = axes[i] < -DEADZONE;
|
status->bButton[buttons_done] = axes[i] < axis_neutrals[i] - DEADZONE;
|
||||||
printf("\n%d %d\n", buttons_done, button_limit);
|
|
||||||
if (++buttons_done >= button_limit)
|
if (++buttons_done >= button_limit)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
status->bButton[buttons_done] = axes[i] > DEADZONE;
|
status->bButton[buttons_done] = axes[i] > axis_neutrals[i] + DEADZONE;
|
||||||
printf("%d\n", buttons_done);
|
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
status->bButton[buttons_done] = hats[i] == GLFW_HAT_UP;
|
status->bButton[buttons_done] = hats[i] == GLFW_HAT_UP;
|
||||||
|
@ -142,7 +166,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;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue