ref: e0674d8f54ef1fe2a2bfd39a45ca845bf3d908dd
parent: ac533cb35e1da17c7bfa5edcee736231f7c6f3db
author: Clownacy <[email protected]>
date: Sat Apr 4 20:08:29 EDT 2020
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.
--- a/src/Backends/GLFW3/Controller.cpp
+++ b/src/Backends/GLFW3/Controller.cpp
@@ -2,6 +2,7 @@
#include <stddef.h>
#include <stdio.h>
+#include <stdlib.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
@@ -13,6 +14,8 @@
static BOOL joystick_connected;
static int connected_joystick_id;
+static float *axis_neutrals;
+
static void JoystickCallback(int joystick_id, int event)
{
switch (event)
@@ -34,6 +37,15 @@
printf("Joystick #%d selected\n", joystick_id);
joystick_connected = TRUE;
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 @@
{
printf("Joystick #%d disconnected\n", connected_joystick_id);
joystick_connected = FALSE;
+
+ free(axis_neutrals);
}
break;
@@ -89,14 +103,23 @@
int total_hats;
const unsigned char *hats = glfwGetJoystickHats(connected_joystick_id, &total_hats);
- status->bLeft = axes[0] < -DEADZONE;
- status->bRight = axes[0] > DEADZONE;
- status->bUp = axes[1] < -DEADZONE;
- status->bDown = axes[1] > DEADZONE;
+ // Handle direction inputs
+ if (axes >= 1)
+ {
+ status->bLeft = axes[0] < -DEADZONE;
+ status->bRight = axes[0] > DEADZONE;
+ }
+ if (axes >= 2)
+ {
+ status->bUp = axes[1] < -DEADZONE;
+ status->bDown = axes[1] > DEADZONE;
+ }
+ // Handle button inputs
unsigned int buttons_done = 0;
+ // Start with the joystick buttons
for (int i = 0; i < total_buttons; ++i)
{
status->bButton[buttons_done] = buttons[i] == GLFW_PRESS;
@@ -105,20 +128,21 @@
break;
}
+ // Then the joystick axes
for (int i = 0; i < total_axes; ++i)
{
- status->bButton[buttons_done] = axes[i] < -DEADZONE;
- printf("\n%d %d\n", buttons_done, button_limit);
+ status->bButton[buttons_done] = axes[i] < axis_neutrals[i] - DEADZONE;
+
if (++buttons_done >= button_limit)
break;
- status->bButton[buttons_done] = axes[i] > DEADZONE;
- printf("%d\n", buttons_done);
+ status->bButton[buttons_done] = axes[i] > axis_neutrals[i] + DEADZONE;
if (++buttons_done >= button_limit)
break;
}
+ // Then the joystick hats
for (int i = 0; i < total_axes; ++i)
{
status->bButton[buttons_done] = hats[i] == GLFW_HAT_UP;
@@ -142,7 +166,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;