Comment GLFW3 code, restore some vanilla behaviour

This commit is contained in:
Clownacy 2020-04-04 18:55:44 +01:00
parent 0cf1a781b7
commit e874b75357

View file

@ -11,6 +11,8 @@
static BOOL joystick_connected; static BOOL joystick_connected;
static int connected_joystick_id; static int connected_joystick_id;
static int joystick_neutral_x;
static int joystick_neutral_y;
static void JoystickCallback(int joystick_id, int event) static void JoystickCallback(int joystick_id, int event)
{ {
@ -21,13 +23,23 @@ static void JoystickCallback(int joystick_id, int event)
if (!joystick_connected) if (!joystick_connected)
{ {
#if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 3) int total_axis;
if (glfwJoystickIsGamepad(joystick_id) == GLFW_TRUE) // Avoid selecting things like laptop touchpads const float *axis = glfwGetJoystickAxes(connected_joystick_id, &total_axis);
#endif
if (total_axis >= 2)
{ {
printf("Joystick #%d selected\n", joystick_id); #if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 3)
joystick_connected = TRUE; if (glfwJoystickIsGamepad(joystick_id) == GLFW_TRUE) // Avoid selecting things like laptop touchpads
connected_joystick_id = joystick_id; #endif
{
printf("Joystick #%d selected\n", joystick_id);
joystick_connected = TRUE;
connected_joystick_id = joystick_id;
// Reset default stick positions (this is performed in ResetJoystickStatus in vanilla Cave Story
joystick_neutral_x = axis[0];
joystick_neutral_y = axis[1];
}
} }
} }
@ -51,10 +63,12 @@ void ControllerBackend_Deinit(void)
BOOL ControllerBackend_Init(void) BOOL ControllerBackend_Init(void)
{ {
// Connect joysticks that are already plugged-in
for (int i = GLFW_JOYSTICK_1; i < GLFW_JOYSTICK_LAST; ++i) for (int i = GLFW_JOYSTICK_1; i < GLFW_JOYSTICK_LAST; ++i)
if (glfwJoystickPresent(i) == GLFW_TRUE) if (glfwJoystickPresent(i) == GLFW_TRUE)
JoystickCallback(i, GLFW_CONNECTED); JoystickCallback(i, GLFW_CONNECTED);
// Set-up the callback for future (dis)connections
glfwSetJoystickCallback(JoystickCallback); glfwSetJoystickCallback(JoystickCallback);
return TRUE; return TRUE;
@ -65,14 +79,16 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status)
if (!joystick_connected) if (!joystick_connected)
return FALSE; return FALSE;
// Read axis
int total_axis; int total_axis;
const float *axis = glfwGetJoystickAxes(connected_joystick_id, &total_axis); const float *axis = glfwGetJoystickAxes(connected_joystick_id, &total_axis);
status->bLeft = axis[0] < -DEADZONE; status->bLeft = axis[0] < joystick_neutral_x - DEADZONE;
status->bRight = axis[0] > DEADZONE; status->bRight = axis[0] > joystick_neutral_x + DEADZONE;
status->bUp = axis[1] < -DEADZONE; status->bUp = axis[1] < joystick_neutral_y - DEADZONE;
status->bDown = axis[1] > DEADZONE; status->bDown = axis[1] > joystick_neutral_y + DEADZONE;
// Read buttons
int total_buttons; int total_buttons;
const unsigned char *buttons = glfwGetJoystickButtons(connected_joystick_id, &total_buttons); const unsigned char *buttons = glfwGetJoystickButtons(connected_joystick_id, &total_buttons);
@ -93,5 +109,7 @@ BOOL ControllerBackend_ResetJoystickStatus(void)
if (!joystick_connected) if (!joystick_connected)
return FALSE; return FALSE;
// The code that would normally run here has been moved to JoystickCallback, to better-support hotplugging
return TRUE; return TRUE;
} }