From 2aeda93aebf5e15c489ccbb78554295e20272e0a Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sat, 4 Apr 2020 22:54:58 +0100 Subject: [PATCH 01/19] SDL2 controller backend - support axes and hats These are now mapped to buttons - will have more of a use in the enhanced branch. --- src/Backends/SDL2/Controller.cpp | 83 +++++++++++++++++++++++++++++--- src/Input.h | 2 +- 2 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/Backends/SDL2/Controller.cpp b/src/Backends/SDL2/Controller.cpp index bedc56d5..b50b0f06 100644 --- a/src/Backends/SDL2/Controller.cpp +++ b/src/Backends/SDL2/Controller.cpp @@ -37,6 +37,8 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) if (joystick == NULL) return FALSE; + const size_t button_limit = sizeof(status->bButton) / sizeof(status->bButton); + // Read axis const Sint16 joystick_x = SDL_JoystickGetAxis(joystick, 0); const Sint16 joystick_y = SDL_JoystickGetAxis(joystick, 1); @@ -46,17 +48,82 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) status->bUp = joystick_y < joystick_neutral_y - DEADZONE; status->bDown = joystick_y > joystick_neutral_y + DEADZONE; - // The original `Input.cpp` assumed there were 32 buttons (because of DirectInput's `DIJOYSTATE` struct) - int numButtons = SDL_JoystickNumButtons(joystick); - if (numButtons > 32) - numButtons = 32; + int total_buttons = SDL_JoystickNumButtons(joystick); + int total_axes = SDL_JoystickNumAxes(joystick); + int total_hats = SDL_JoystickNumHats(joystick); - // Read whatever buttons actually exist - for (int i = 0; i < numButtons; ++i) - status->bButton[i] = SDL_JoystickGetButton(joystick, i); + unsigned int buttons_done = 0; + + for (int i = 0; i < total_buttons; ++i) + { + status->bButton[buttons_done] = SDL_JoystickGetButton(joystick, i); + + if (++buttons_done < button_limit) + break; + } + + for (int i = 0; i < total_axes && buttons_done < button_limit; ++i) + { + Sint16 axis = SDL_JoystickGetAxis(joystick, i); + + status->bButton[buttons_done] = axis < -DEADZONE; + + if (++buttons_done < button_limit) + break; + + status->bButton[buttons_done] = axis > DEADZONE; + + if (++buttons_done < button_limit) + break; + } + + for (int i = 0; i < total_axes && buttons_done < button_limit; ++i) + { + Uint8 hat = SDL_JoystickGetHat(joystick, i); + + status->bButton[buttons_done] = hat == SDL_HAT_UP; + + if (++buttons_done < button_limit) + break; + + status->bButton[buttons_done] = hat == SDL_HAT_RIGHT; + + if (++buttons_done < button_limit) + break; + + status->bButton[buttons_done] = hat == SDL_HAT_DOWN; + + if (++buttons_done < button_limit) + break; + + status->bButton[buttons_done] = hat == SDL_HAT_LEFT; + + if (++buttons_done < button_limit) + break; + + status->bButton[buttons_done] = hat == SDL_HAT_RIGHTUP; + + if (++buttons_done < button_limit) + break; + + status->bButton[buttons_done] = hat == SDL_HAT_RIGHTDOWN; + + if (++buttons_done < button_limit) + break; + + status->bButton[buttons_done] = hat == SDL_HAT_LEFTUP; + + if (++buttons_done < button_limit) + break; + + status->bButton[buttons_done] = hat == SDL_HAT_LEFTDOWN; + + if (++buttons_done < button_limit) + break; + } // Blank the buttons that do not - for (int i = numButtons; i < 32; ++i) + for (size_t i = buttons_done; i < button_limit; ++i) status->bButton[i] = FALSE; return TRUE; diff --git a/src/Input.h b/src/Input.h index 4ad8aa7a..84008430 100644 --- a/src/Input.h +++ b/src/Input.h @@ -8,7 +8,7 @@ struct JOYSTICK_STATUS BOOL bRight; BOOL bUp; BOOL bDown; - BOOL bButton[32]; + BOOL bButton[32]; // The original `Input.cpp` assumed there were 32 buttons (because of DirectInput's `DIJOYSTATE` struct) }; void ReleaseDirectInput(void); From 08bd2e1e787008ac7b8961e2d71437552199c0f7 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sat, 4 Apr 2020 22:57:32 +0100 Subject: [PATCH 02/19] Cleanup --- src/Backends/SDL2/Controller.cpp | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/Backends/SDL2/Controller.cpp b/src/Backends/SDL2/Controller.cpp index b50b0f06..56fc11cb 100644 --- a/src/Backends/SDL2/Controller.cpp +++ b/src/Backends/SDL2/Controller.cpp @@ -11,8 +11,6 @@ #define DEADZONE 10000; static SDL_Joystick *joystick; -static int joystick_neutral_x; -static int joystick_neutral_y; BOOL ControllerBackend_Init(void) { @@ -43,10 +41,10 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) const Sint16 joystick_x = SDL_JoystickGetAxis(joystick, 0); const Sint16 joystick_y = SDL_JoystickGetAxis(joystick, 1); - status->bLeft = joystick_x < joystick_neutral_x - DEADZONE; - status->bRight = joystick_x > joystick_neutral_x + DEADZONE; - status->bUp = joystick_y < joystick_neutral_y - DEADZONE; - status->bDown = joystick_y > joystick_neutral_y + DEADZONE; + status->bLeft = joystick_x < -DEADZONE; + status->bRight = joystick_x > DEADZONE; + status->bUp = joystick_y < -DEADZONE; + status->bDown = joystick_y > DEADZONE; int total_buttons = SDL_JoystickNumButtons(joystick); int total_axes = SDL_JoystickNumAxes(joystick); @@ -134,8 +132,6 @@ BOOL ControllerBackend_ResetJoystickStatus(void) if (joystick == NULL) return FALSE; - // The code that would normally run here has been moved to JoystickCallback, to better-support hotplugging - return TRUE; } @@ -148,13 +144,7 @@ void ControllerBackend_JoystickConnect(Sint32 joystick_id) joystick = SDL_JoystickOpen(joystick_id); if (joystick != NULL) - { printf("Joystick #%d selected\n", joystick_id); - - // Reset default stick positions (this is performed in ResetJoystickStatus in vanilla Cave Story - joystick_neutral_x = SDL_JoystickGetAxis(joystick, 0); - joystick_neutral_y = SDL_JoystickGetAxis(joystick, 1); - } } } From ac533cb35e1da17c7bfa5edcee736231f7c6f3db Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sat, 4 Apr 2020 23:22:22 +0100 Subject: [PATCH 03/19] Add support to the GLFW3 backends as well Also fix the SDL2 backend wow --- src/Backends/GLFW3/Controller.cpp | 84 ++++++++++++++++++++++--------- src/Backends/SDL2/Controller.cpp | 29 ++++++----- 2 files changed, 73 insertions(+), 40 deletions(-) diff --git a/src/Backends/GLFW3/Controller.cpp b/src/Backends/GLFW3/Controller.cpp index bc378cdc..32ce0b4f 100644 --- a/src/Backends/GLFW3/Controller.cpp +++ b/src/Backends/GLFW3/Controller.cpp @@ -12,8 +12,6 @@ static BOOL joystick_connected; static int connected_joystick_id; -static int joystick_neutral_x; -static int joystick_neutral_y; static void JoystickCallback(int joystick_id, int event) { @@ -36,10 +34,6 @@ static void JoystickCallback(int joystick_id, int event) 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]; } } } @@ -76,8 +70,6 @@ void ControllerBackend_Deinit(void) joystick_connected = FALSE; connected_joystick_id = 0; - joystick_neutral_x = 0; - joystick_neutral_y = 0; } BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) @@ -85,29 +77,73 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) if (!joystick_connected) return FALSE; - // Read axis - int total_axis; - const float *axis = glfwGetJoystickAxes(connected_joystick_id, &total_axis); + const size_t button_limit = sizeof(status->bButton) / sizeof(status->bButton[0]); - status->bLeft = axis[0] < joystick_neutral_x - DEADZONE; - status->bRight = axis[0] > joystick_neutral_x + DEADZONE; - status->bUp = axis[1] < joystick_neutral_y - DEADZONE; - status->bDown = axis[1] > joystick_neutral_y + DEADZONE; - - // Read buttons int total_buttons; const unsigned char *buttons = glfwGetJoystickButtons(connected_joystick_id, &total_buttons); + total_buttons = 0; - // The original `Input.cpp` assumed there were 32 buttons (because of DirectInput's `DIJOYSTATE` struct) - if (total_buttons > 32) - total_buttons = 32; + int total_axes; + const float *axes = glfwGetJoystickAxes(connected_joystick_id, &total_axes); + + 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; + + + unsigned int buttons_done = 0; - // Read whatever buttons actually exist for (int i = 0; i < total_buttons; ++i) - status->bButton[i] = buttons[i] == GLFW_PRESS; + { + status->bButton[buttons_done] = buttons[i] == GLFW_PRESS; + + if (++buttons_done >= button_limit) + break; + } + + for (int i = 0; i < total_axes; ++i) + { + status->bButton[buttons_done] = axes[i] < -DEADZONE; + printf("\n%d %d\n", buttons_done, button_limit); + if (++buttons_done >= button_limit) + break; + + status->bButton[buttons_done] = axes[i] > DEADZONE; + printf("%d\n", buttons_done); + + if (++buttons_done >= button_limit) + break; + } + + for (int i = 0; i < total_axes; ++i) + { + status->bButton[buttons_done] = hats[i] == GLFW_HAT_UP; + + if (++buttons_done >= button_limit) + break; + + status->bButton[buttons_done] = hats[i] == GLFW_HAT_RIGHT; + + if (++buttons_done >= button_limit) + break; + + status->bButton[buttons_done] = hats[i] == GLFW_HAT_DOWN; + + if (++buttons_done >= button_limit) + break; + + status->bButton[buttons_done] = hats[i] == GLFW_HAT_LEFT; + + if (++buttons_done >= button_limit) + break; + } // Blank the buttons that do not - for (int i = total_buttons; i < 32; ++i) + for (size_t i = buttons_done; i < button_limit; ++i) status->bButton[i] = FALSE; return TRUE; @@ -118,7 +154,5 @@ BOOL ControllerBackend_ResetJoystickStatus(void) if (!joystick_connected) return FALSE; - // The code that would normally run here has been moved to JoystickCallback, to better-support hotplugging - return TRUE; } diff --git a/src/Backends/SDL2/Controller.cpp b/src/Backends/SDL2/Controller.cpp index 56fc11cb..21174f36 100644 --- a/src/Backends/SDL2/Controller.cpp +++ b/src/Backends/SDL2/Controller.cpp @@ -35,9 +35,8 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) if (joystick == NULL) return FALSE; - const size_t button_limit = sizeof(status->bButton) / sizeof(status->bButton); + const size_t button_limit = sizeof(status->bButton) / sizeof(status->bButton[0]); - // Read axis const Sint16 joystick_x = SDL_JoystickGetAxis(joystick, 0); const Sint16 joystick_y = SDL_JoystickGetAxis(joystick, 1); @@ -56,67 +55,67 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) { status->bButton[buttons_done] = SDL_JoystickGetButton(joystick, i); - if (++buttons_done < button_limit) + if (++buttons_done >= button_limit) break; } - for (int i = 0; i < total_axes && buttons_done < button_limit; ++i) + for (int i = 0; i < total_axes; ++i) { Sint16 axis = SDL_JoystickGetAxis(joystick, i); status->bButton[buttons_done] = axis < -DEADZONE; - if (++buttons_done < button_limit) + if (++buttons_done >= button_limit) break; status->bButton[buttons_done] = axis > DEADZONE; - if (++buttons_done < button_limit) + if (++buttons_done >= button_limit) break; } - for (int i = 0; i < total_axes && buttons_done < button_limit; ++i) + for (int i = 0; i < total_axes; ++i) { Uint8 hat = SDL_JoystickGetHat(joystick, i); status->bButton[buttons_done] = hat == SDL_HAT_UP; - if (++buttons_done < button_limit) + if (++buttons_done >= button_limit) break; status->bButton[buttons_done] = hat == SDL_HAT_RIGHT; - if (++buttons_done < button_limit) + if (++buttons_done >= button_limit) break; status->bButton[buttons_done] = hat == SDL_HAT_DOWN; - if (++buttons_done < button_limit) + if (++buttons_done >= button_limit) break; status->bButton[buttons_done] = hat == SDL_HAT_LEFT; - if (++buttons_done < button_limit) + if (++buttons_done >= button_limit) break; status->bButton[buttons_done] = hat == SDL_HAT_RIGHTUP; - if (++buttons_done < button_limit) + if (++buttons_done >= button_limit) break; status->bButton[buttons_done] = hat == SDL_HAT_RIGHTDOWN; - if (++buttons_done < button_limit) + if (++buttons_done >= button_limit) break; status->bButton[buttons_done] = hat == SDL_HAT_LEFTUP; - if (++buttons_done < button_limit) + if (++buttons_done >= button_limit) break; status->bButton[buttons_done] = hat == SDL_HAT_LEFTDOWN; - if (++buttons_done < button_limit) + if (++buttons_done >= button_limit) break; } From e0674d8f54ef1fe2a2bfd39a45ca845bf3d908dd Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 00:08:29 +0100 Subject: [PATCH 04/19] 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. --- src/Backends/GLFW3/Controller.cpp | 42 ++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/Backends/GLFW3/Controller.cpp b/src/Backends/GLFW3/Controller.cpp index 32ce0b4f..c21c8e40 100644 --- a/src/Backends/GLFW3/Controller.cpp +++ b/src/Backends/GLFW3/Controller.cpp @@ -2,6 +2,7 @@ #include #include +#include #define GLFW_INCLUDE_NONE #include @@ -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 @@ static void JoystickCallback(int joystick_id, int event) 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 @@ static void JoystickCallback(int joystick_id, int event) { printf("Joystick #%d disconnected\n", connected_joystick_id); joystick_connected = FALSE; + + free(axis_neutrals); } break; @@ -89,14 +103,23 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) 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 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) 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 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) 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; From 6bb240d335bd4357d8466e8adae45624800c0d99 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 00:11:52 +0100 Subject: [PATCH 05/19] Fixes --- src/Backends/GLFW3/Controller.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Backends/GLFW3/Controller.cpp b/src/Backends/GLFW3/Controller.cpp index c21c8e40..72e97780 100644 --- a/src/Backends/GLFW3/Controller.cpp +++ b/src/Backends/GLFW3/Controller.cpp @@ -25,10 +25,10 @@ static void JoystickCallback(int joystick_id, int event) if (!joystick_connected) { - int total_axis; - const float *axis = glfwGetJoystickAxes(joystick_id, &total_axis); + int total_axes; + const float *axes = glfwGetJoystickAxes(joystick_id, &total_axes); - if (total_axis >= 2) + if (total_axes >= 2) { #if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 3) if (glfwJoystickIsGamepad(joystick_id) == GLFW_TRUE) // Avoid selecting things like laptop touchpads @@ -39,9 +39,6 @@ static void JoystickCallback(int joystick_id, int event) 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) @@ -104,13 +101,13 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) const unsigned char *hats = glfwGetJoystickHats(connected_joystick_id, &total_hats); // Handle direction inputs - if (axes >= 1) + if (total_axes >= 1) { status->bLeft = axes[0] < -DEADZONE; status->bRight = axes[0] > DEADZONE; } - if (axes >= 2) + if (total_axes >= 2) { status->bUp = axes[1] < -DEADZONE; status->bDown = axes[1] > DEADZONE; From 8fdaea5d8b8cb218f5a3c1597eb267f1086d48de Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 00:16:19 +0100 Subject: [PATCH 06/19] More cleanup and fixes --- src/Backends/GLFW3/Controller.cpp | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/Backends/GLFW3/Controller.cpp b/src/Backends/GLFW3/Controller.cpp index 72e97780..30d48e23 100644 --- a/src/Backends/GLFW3/Controller.cpp +++ b/src/Backends/GLFW3/Controller.cpp @@ -28,7 +28,10 @@ static void JoystickCallback(int joystick_id, int event) int total_axes; const float *axes = glfwGetJoystickAxes(joystick_id, &total_axes); - if (total_axes >= 2) + int total_buttons; + const unsigned char *buttons = glfwGetJoystickButtons(connected_joystick_id, &total_buttons); + + if (total_axes >= 2 && total_buttons >= 6) { #if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 3) if (glfwJoystickIsGamepad(joystick_id) == GLFW_TRUE) // Avoid selecting things like laptop touchpads @@ -92,7 +95,6 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) int total_buttons; const unsigned char *buttons = glfwGetJoystickButtons(connected_joystick_id, &total_buttons); - total_buttons = 0; int total_axes; const float *axes = glfwGetJoystickAxes(connected_joystick_id, &total_axes); @@ -101,17 +103,10 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) const unsigned char *hats = glfwGetJoystickHats(connected_joystick_id, &total_hats); // Handle direction inputs - if (total_axes >= 1) - { - status->bLeft = axes[0] < -DEADZONE; - status->bRight = axes[0] > DEADZONE; - } - - if (total_axes >= 2) - { - status->bUp = axes[1] < -DEADZONE; - status->bDown = axes[1] > DEADZONE; - } + status->bLeft = axes[0] < -DEADZONE; + status->bRight = axes[0] > DEADZONE; + status->bUp = axes[1] < -DEADZONE; + status->bDown = axes[1] > DEADZONE; // Handle button inputs unsigned int buttons_done = 0; From c1876609045eb1cf8c4cd779edbc31b625b8f148 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 00:20:41 +0100 Subject: [PATCH 07/19] Hat cleanup --- src/Backends/GLFW3/Controller.cpp | 8 ++++---- src/Backends/SDL2/Controller.cpp | 28 ++++------------------------ 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/src/Backends/GLFW3/Controller.cpp b/src/Backends/GLFW3/Controller.cpp index 30d48e23..f4706057 100644 --- a/src/Backends/GLFW3/Controller.cpp +++ b/src/Backends/GLFW3/Controller.cpp @@ -137,22 +137,22 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) // Then the joystick hats 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; if (++buttons_done >= button_limit) break; - status->bButton[buttons_done] = hats[i] == GLFW_HAT_RIGHT; + status->bButton[buttons_done] = hats[i] & GLFW_HAT_RIGHT; if (++buttons_done >= button_limit) break; - status->bButton[buttons_done] = hats[i] == GLFW_HAT_DOWN; + status->bButton[buttons_done] = hats[i] & GLFW_HAT_DOWN; if (++buttons_done >= button_limit) break; - status->bButton[buttons_done] = hats[i] == GLFW_HAT_LEFT; + status->bButton[buttons_done] = hats[i] & GLFW_HAT_LEFT; if (++buttons_done >= button_limit) break; diff --git a/src/Backends/SDL2/Controller.cpp b/src/Backends/SDL2/Controller.cpp index 21174f36..35ca0bed 100644 --- a/src/Backends/SDL2/Controller.cpp +++ b/src/Backends/SDL2/Controller.cpp @@ -78,42 +78,22 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) { Uint8 hat = SDL_JoystickGetHat(joystick, i); - status->bButton[buttons_done] = hat == SDL_HAT_UP; + status->bButton[buttons_done] = hat == SDL_HAT_UP || hat == SDL_HAT_LEFTUP || hat == SDL_HAT_RIGHTUP; if (++buttons_done >= button_limit) break; - status->bButton[buttons_done] = hat == SDL_HAT_RIGHT; + status->bButton[buttons_done] = hat == SDL_HAT_RIGHT || hat == SDL_HAT_RIGHTUP || hat == SDL_HAT_RIGHTDOWN; if (++buttons_done >= button_limit) break; - status->bButton[buttons_done] = hat == SDL_HAT_DOWN; + status->bButton[buttons_done] = hat == SDL_HAT_DOWN || hat == SDL_HAT_LEFTDOWN || hat == SDL_HAT_RIGHTDOWN; if (++buttons_done >= button_limit) break; - status->bButton[buttons_done] = hat == SDL_HAT_LEFT; - - if (++buttons_done >= button_limit) - break; - - status->bButton[buttons_done] = hat == SDL_HAT_RIGHTUP; - - if (++buttons_done >= button_limit) - break; - - status->bButton[buttons_done] = hat == SDL_HAT_RIGHTDOWN; - - if (++buttons_done >= button_limit) - break; - - status->bButton[buttons_done] = hat == SDL_HAT_LEFTUP; - - if (++buttons_done >= button_limit) - break; - - status->bButton[buttons_done] = hat == SDL_HAT_LEFTDOWN; + status->bButton[buttons_done] = hat == SDL_HAT_LEFT || hat == SDL_HAT_LEFTUP || hat == SDL_HAT_LEFTDOWN; if (++buttons_done >= button_limit) break; From 26de0b6043726b7c10b22020f1ee81e0bd167192 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 00:30:58 +0100 Subject: [PATCH 08/19] Improve the SDL2 controller backend too --- src/Backends/GLFW3/Controller.cpp | 10 +++---- src/Backends/SDL2/Controller.cpp | 45 +++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/Backends/GLFW3/Controller.cpp b/src/Backends/GLFW3/Controller.cpp index f4706057..8a0de9c2 100644 --- a/src/Backends/GLFW3/Controller.cpp +++ b/src/Backends/GLFW3/Controller.cpp @@ -102,11 +102,11 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) 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; diff --git a/src/Backends/SDL2/Controller.cpp b/src/Backends/SDL2/Controller.cpp index 35ca0bed..7b2eae08 100644 --- 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,20 +39,23 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) 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); 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 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) 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 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) 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 @@ void ControllerBackend_JoystickConnect(Sint32 joystick_id) 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 @@ void ControllerBackend_JoystickDisconnect(Sint32 joystick_id) if (joystick_id == SDL_JoystickInstanceID(joystick)) { printf("Joystick #%d disconnected\n", joystick_id); + SDL_JoystickClose(joystick); joystick = NULL; + + free(axis_neutrals); } } From 67765fd0ad5b2804c0906d0c44864fe9ec45a146 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 00:54:45 +0100 Subject: [PATCH 09/19] Fix --- src/Backends/GLFW3/Controller.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Backends/GLFW3/Controller.cpp b/src/Backends/GLFW3/Controller.cpp index 8a0de9c2..8ce20273 100644 --- a/src/Backends/GLFW3/Controller.cpp +++ b/src/Backends/GLFW3/Controller.cpp @@ -29,7 +29,7 @@ static void JoystickCallback(int joystick_id, int event) const float *axes = glfwGetJoystickAxes(joystick_id, &total_axes); int total_buttons; - const unsigned char *buttons = glfwGetJoystickButtons(connected_joystick_id, &total_buttons); + const unsigned char *buttons = glfwGetJoystickButtons(joystick_id, &total_buttons); if (total_axes >= 2 && total_buttons >= 6) { From ee1e7aed11f7c04f51a06d0942174f67734e8f9e Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 00:58:56 +0100 Subject: [PATCH 10/19] Fix unfreed memory --- src/Backends/GLFW3/Controller.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Backends/GLFW3/Controller.cpp b/src/Backends/GLFW3/Controller.cpp index 8ce20273..8abd138e 100644 --- a/src/Backends/GLFW3/Controller.cpp +++ b/src/Backends/GLFW3/Controller.cpp @@ -84,6 +84,9 @@ void ControllerBackend_Deinit(void) joystick_connected = FALSE; connected_joystick_id = 0; + + free(axis_neutrals); + axis_neutrals = NULL; } BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) From b6398c58b476fd9577799c1b1695608fde0a36af Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 03:24:55 +0100 Subject: [PATCH 11/19] Disable GLFW hats in <3.3 Doesn't exist - causes Travis to fail --- src/Backends/GLFW3/Controller.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Backends/GLFW3/Controller.cpp b/src/Backends/GLFW3/Controller.cpp index 8abd138e..b5c55dc2 100644 --- a/src/Backends/GLFW3/Controller.cpp +++ b/src/Backends/GLFW3/Controller.cpp @@ -102,8 +102,10 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) int total_axes; const float *axes = glfwGetJoystickAxes(connected_joystick_id, &total_axes); +#if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 3) int total_hats; const unsigned char *hats = glfwGetJoystickHats(connected_joystick_id, &total_hats); +#endif // Handle directional inputs status->bLeft = axes[0] < axis_neutrals[0] - DEADZONE; @@ -137,6 +139,7 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) break; } +#if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 3) // Then the joystick hats for (int i = 0; i < total_axes; ++i) { @@ -160,6 +163,7 @@ BOOL ControllerBackend_GetJoystickStatus(JOYSTICK_STATUS *status) if (++buttons_done >= button_limit) break; } +#endif // Blank any remaining buttons for (size_t i = buttons_done; i < button_limit; ++i) From 80091c4be5eff953da4663eb57014bcab6151e4e Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 03:25:58 +0100 Subject: [PATCH 12/19] Simplify some logic --- src/Main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Main.cpp b/src/Main.cpp index 0980528f..125df8b6 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -384,7 +384,7 @@ BOOL SystemTask(void) for (unsigned int i = 0; i < BACKEND_KEYBOARD_TOTAL; ++i) { - if ((backend_keyboard_state[i] ^ backend_previous_keyboard_state[i]) & backend_keyboard_state[i]) + if (backend_keyboard_state[i] && !backend_previous_keyboard_state[i]) { switch (i) { @@ -470,7 +470,7 @@ BOOL SystemTask(void) break; } } - else if ((backend_keyboard_state[i] ^ backend_previous_keyboard_state[i]) & backend_previous_keyboard_state[i]) + else if (!backend_keyboard_state[i] && backend_previous_keyboard_state[i]) { switch (i) { From 393359e63f41450f2b132c9c015bc78130b6fcae Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 03:27:04 +0100 Subject: [PATCH 13/19] Add some sanity checks --- src/Main.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Main.cpp b/src/Main.cpp index 125df8b6..d29aef7a 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -277,13 +277,16 @@ int main(int argc, char *argv[]) size_t window_icon_resource_size; const unsigned char *window_icon_resource_data = FindResource("ICON_MINI", "ICON", &window_icon_resource_size); - unsigned int window_icon_width, window_icon_height; - unsigned char *window_icon_rgb_pixels = DecodeBitmap(window_icon_resource_data, window_icon_resource_size, &window_icon_width, &window_icon_height); - - if (window_icon_rgb_pixels != NULL) + if (window_icon_resource_data != NULL) { - Backend_SetWindowIcon(window_icon_rgb_pixels, window_icon_width, window_icon_height); - FreeBitmap(window_icon_rgb_pixels); + unsigned int window_icon_width, window_icon_height; + unsigned char *window_icon_rgb_pixels = DecodeBitmap(window_icon_resource_data, window_icon_resource_size, &window_icon_width, &window_icon_height); + + if (window_icon_rgb_pixels != NULL) + { + Backend_SetWindowIcon(window_icon_rgb_pixels, window_icon_width, window_icon_height); + FreeBitmap(window_icon_rgb_pixels); + } } #endif @@ -291,13 +294,16 @@ int main(int argc, char *argv[]) size_t cursor_resource_size; const unsigned char *cursor_resource_data = FindResource("CURSOR_NORMAL", "CURSOR", &cursor_resource_size); - unsigned int cursor_width, cursor_height; - unsigned char *cursor_rgb_pixels = DecodeBitmap(cursor_resource_data, cursor_resource_size, &cursor_width, &cursor_height); - - if (cursor_rgb_pixels != NULL) + if (cursor_resource_data != NULL) { - Backend_SetCursor(cursor_rgb_pixels, cursor_width, cursor_height); - FreeBitmap(cursor_rgb_pixels); + unsigned int cursor_width, cursor_height; + unsigned char *cursor_rgb_pixels = DecodeBitmap(cursor_resource_data, cursor_resource_size, &cursor_width, &cursor_height); + + if (cursor_rgb_pixels != NULL) + { + Backend_SetCursor(cursor_rgb_pixels, cursor_width, cursor_height); + FreeBitmap(cursor_rgb_pixels); + } } if (IsKeyFile("fps")) From e39c46fae9973ee796744bc59d44303b47c99e00 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 03:27:50 +0100 Subject: [PATCH 14/19] Force glad to be static Pretty sure it isn't set up to generate a proper DLL, so make sure it always produces a static library instead. --- external/glad/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/external/glad/CMakeLists.txt b/external/glad/CMakeLists.txt index e67a755f..c120bbb5 100644 --- a/external/glad/CMakeLists.txt +++ b/external/glad/CMakeLists.txt @@ -4,7 +4,7 @@ if(NOT TARGET glad) project(glad LANGUAGES C) -add_library(glad +add_library(glad STATIC "include/glad/glad.h" "include/KHR/khrplatform.h" "src/glad.c" From f75859b7591cf6433848af194cc7c53bfcca92da Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 03:35:43 +0100 Subject: [PATCH 15/19] Change constant ordering This was bugging the hell out of me --- src/Main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.cpp b/src/Main.cpp index 2544b3a1..5719d734 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -770,7 +770,7 @@ void JoystickProc(void) if (!GetJoystickStatus(&status)) return; - gKey &= (KEY_ESCAPE | KEY_F2 | KEY_F1); + gKey &= (KEY_ESCAPE | KEY_F1 | KEY_F2); // Set movement buttons if (status.bLeft) From aef0f81f28a51aa6e84b7ae4a5610bf46446a7c9 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 04:31:11 +0100 Subject: [PATCH 16/19] Offload keyboard tracker to the engine Centralising it in the backend breaks the enhanced branch's rebinding menu. --- src/Backends/GLFW3/Misc.cpp | 2 -- src/Backends/Misc.h | 1 - src/Backends/SDL2/Misc.cpp | 2 -- src/Main.cpp | 8 ++++++-- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Backends/GLFW3/Misc.cpp b/src/Backends/GLFW3/Misc.cpp index 0a45eff3..42b7650e 100644 --- a/src/Backends/GLFW3/Misc.cpp +++ b/src/Backends/GLFW3/Misc.cpp @@ -264,8 +264,6 @@ BOOL Backend_SystemTask(void) return FALSE; } - memcpy(backend_previous_keyboard_state, backend_keyboard_state, sizeof(backend_keyboard_state)); - glfwPollEvents(); while (!bActive) diff --git a/src/Backends/Misc.h b/src/Backends/Misc.h index e6d11124..d69f33d8 100644 --- a/src/Backends/Misc.h +++ b/src/Backends/Misc.h @@ -85,7 +85,6 @@ enum extern BOOL bActive; extern BOOL backend_keyboard_state[BACKEND_KEYBOARD_TOTAL]; -extern BOOL backend_previous_keyboard_state[BACKEND_KEYBOARD_TOTAL]; void Backend_Init(void); void Backend_Deinit(void); diff --git a/src/Backends/SDL2/Misc.cpp b/src/Backends/SDL2/Misc.cpp index bb858d90..8b1ad40a 100644 --- a/src/Backends/SDL2/Misc.cpp +++ b/src/Backends/SDL2/Misc.cpp @@ -97,8 +97,6 @@ void PlaybackBackend_EnableDragAndDrop(void) BOOL Backend_SystemTask(void) { - memcpy(backend_previous_keyboard_state, backend_keyboard_state, sizeof(backend_keyboard_state)); - while (SDL_PollEvent(NULL) || !bActive) { SDL_Event event; diff --git a/src/Main.cpp b/src/Main.cpp index 4b276eb5..2261f68f 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -385,12 +385,14 @@ void JoystickProc(void); BOOL SystemTask(void) { + static BOOL previous_keyboard_state[BACKEND_KEYBOARD_TOTAL]; + if (!Backend_SystemTask()) return FALSE; for (unsigned int i = 0; i < BACKEND_KEYBOARD_TOTAL; ++i) { - if (backend_keyboard_state[i] && !backend_previous_keyboard_state[i]) + if (backend_keyboard_state[i] && !previous_keyboard_state[i]) { switch (i) { @@ -476,7 +478,7 @@ BOOL SystemTask(void) break; } } - else if (!backend_keyboard_state[i] && backend_previous_keyboard_state[i]) + else if (!backend_keyboard_state[i] && previous_keyboard_state[i]) { switch (i) { @@ -560,6 +562,8 @@ BOOL SystemTask(void) } } + memcpy(previous_keyboard_state, backend_keyboard_state, sizeof(backend_keyboard_state)); + // Run joystick code if (gbUseJoystick) JoystickProc(); From eaef25d5dad09b1b1d4a5862abe6da94672e1c68 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 13:26:08 +0100 Subject: [PATCH 17/19] Change how backend keyboard status stuff works --- src/Backends/GLFW3/Misc.cpp | 11 ++++++++--- src/Backends/Misc.h | 2 +- src/Backends/SDL2/Misc.cpp | 11 ++++++++--- src/Main.cpp | 9 ++++++--- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/Backends/GLFW3/Misc.cpp b/src/Backends/GLFW3/Misc.cpp index 42b7650e..50945b37 100644 --- a/src/Backends/GLFW3/Misc.cpp +++ b/src/Backends/GLFW3/Misc.cpp @@ -20,12 +20,12 @@ #define DO_KEY(GLFW_KEY, BACKEND_KEY) \ case GLFW_KEY: \ - backend_keyboard_state[BACKEND_KEY] = action == GLFW_PRESS; \ + keyboard_state[BACKEND_KEY] = action == GLFW_PRESS; \ break; BOOL bActive = TRUE; -BOOL backend_keyboard_state[BACKEND_KEYBOARD_TOTAL]; -BOOL backend_previous_keyboard_state[BACKEND_KEYBOARD_TOTAL]; + +static BOOL keyboard_state[BACKEND_KEYBOARD_TOTAL]; static GLFWcursor* cursor; @@ -272,6 +272,11 @@ BOOL Backend_SystemTask(void) return TRUE; } +void Backend_GetKeyboardState(BOOL *out_keyboard_state) +{ + memcpy(out_keyboard_state, keyboard_state, sizeof(keyboard_state)); +} + void Backend_ShowMessageBox(const char *title, const char *message) { // GLFW3 doesn't have a message box diff --git a/src/Backends/Misc.h b/src/Backends/Misc.h index d69f33d8..7cdf3d2f 100644 --- a/src/Backends/Misc.h +++ b/src/Backends/Misc.h @@ -84,7 +84,6 @@ enum }; extern BOOL bActive; -extern BOOL backend_keyboard_state[BACKEND_KEYBOARD_TOTAL]; void Backend_Init(void); void Backend_Deinit(void); @@ -95,6 +94,7 @@ void Backend_SetWindowIcon(const unsigned char *rgb_pixels, unsigned int width, void Backend_SetCursor(const unsigned char *rgb_pixels, unsigned int width, unsigned int height); void PlaybackBackend_EnableDragAndDrop(void); BOOL Backend_SystemTask(void); +void Backend_GetKeyboardState(BOOL *keyboard_state); void Backend_ShowMessageBox(const char *title, const char *message); unsigned long Backend_GetTicks(void); void Backend_Delay(unsigned int ticks); diff --git a/src/Backends/SDL2/Misc.cpp b/src/Backends/SDL2/Misc.cpp index 8b1ad40a..0beec4b9 100644 --- a/src/Backends/SDL2/Misc.cpp +++ b/src/Backends/SDL2/Misc.cpp @@ -18,12 +18,12 @@ #define DO_KEY(SDL_KEY, BACKEND_KEY) \ case SDL_KEY: \ - backend_keyboard_state[BACKEND_KEY] = event.key.type == SDL_KEYDOWN; \ + keyboard_state[BACKEND_KEY] = event.key.type == SDL_KEYDOWN; \ break; BOOL bActive = TRUE; -BOOL backend_keyboard_state[BACKEND_KEYBOARD_TOTAL]; -BOOL backend_previous_keyboard_state[BACKEND_KEYBOARD_TOTAL]; + +static BOOL keyboard_state[BACKEND_KEYBOARD_TOTAL]; static SDL_Surface *cursor_surface; static SDL_Cursor *cursor; @@ -238,6 +238,11 @@ BOOL Backend_SystemTask(void) return TRUE; } +void Backend_GetKeyboardState(BOOL *out_keyboard_state) +{ + memcpy(out_keyboard_state, keyboard_state, sizeof(keyboard_state)); +} + void Backend_ShowMessageBox(const char *title, const char *message) { SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, message, window); diff --git a/src/Main.cpp b/src/Main.cpp index 2261f68f..4979237e 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -390,9 +390,12 @@ BOOL SystemTask(void) if (!Backend_SystemTask()) return FALSE; + BOOL keyboard_state[BACKEND_KEYBOARD_TOTAL]; + Backend_GetKeyboardState(keyboard_state); + for (unsigned int i = 0; i < BACKEND_KEYBOARD_TOTAL; ++i) { - if (backend_keyboard_state[i] && !previous_keyboard_state[i]) + if (keyboard_state[i] && !previous_keyboard_state[i]) { switch (i) { @@ -478,7 +481,7 @@ BOOL SystemTask(void) break; } } - else if (!backend_keyboard_state[i] && previous_keyboard_state[i]) + else if (!keyboard_state[i] && previous_keyboard_state[i]) { switch (i) { @@ -562,7 +565,7 @@ BOOL SystemTask(void) } } - memcpy(previous_keyboard_state, backend_keyboard_state, sizeof(backend_keyboard_state)); + memcpy(previous_keyboard_state, keyboard_state, sizeof(keyboard_state)); // Run joystick code if (gbUseJoystick) From 0aa65f7cd30862aedb3044bbf2b8b0caf7e74023 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 16:58:52 +0100 Subject: [PATCH 18/19] Fix pkg-config static linking For some reason, [LIB]_STATIC_LINK_LIBRARIES is blank. Thanks a lot, CMake. --- CMakeLists.txt | 12 ++++++------ DoConfig/CMakeLists.txt | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 13efa613..7a12b36d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -439,11 +439,11 @@ if(BACKEND_PLATFORM MATCHES "GLFW3") if (PKG_CONFIG_STATIC_LIBS) message(STATUS "Using system GLFW3 (pkg-config, static)") target_compile_options(CSE2 PRIVATE ${glfw3_STATIC_CFLAGS}) - target_link_libraries(CSE2 PRIVATE ${glfw3_STATIC_LINK_LIBRARIES}) + target_link_libraries(CSE2 PRIVATE ${glfw3_STATIC_LDFLAGS}) else() message(STATUS "Using system GLFW3 (pkg-config, dynamic)") target_compile_options(CSE2 PRIVATE ${glfw3_CFLAGS}) - target_link_libraries(CSE2 PRIVATE ${glfw3_LINK_LIBRARIES}) + target_link_libraries(CSE2 PRIVATE ${glfw3_LDFLAGS}) endif() elseif(TARGET glfw) # CMake @@ -475,11 +475,11 @@ if(BACKEND_PLATFORM MATCHES "SDL2" OR BACKEND_AUDIO MATCHES "SDL2") if (PKG_CONFIG_STATIC_LIBS) message(STATUS "Using system SDL2 (pkg-config, static)") target_compile_options(CSE2 PRIVATE ${sdl2_STATIC_CFLAGS}) - target_link_libraries(CSE2 PRIVATE ${sdl2_STATIC_LINK_LIBRARIES}) + target_link_libraries(CSE2 PRIVATE ${sdl2_STATIC_LDFLAGS}) else() message(STATUS "Using system SDL2 (pkg-config, dynamic)") target_compile_options(CSE2 PRIVATE ${sdl2_CFLAGS}) - target_link_libraries(CSE2 PRIVATE ${sdl2_LINK_LIBRARIES}) + target_link_libraries(CSE2 PRIVATE ${sdl2_LDFLAGS}) endif() elseif(TARGET SDL2::SDL2) # CMake-generated config (Arch, vcpkg, Raspbian) @@ -519,11 +519,11 @@ if(TARGET PkgConfig::freetype2) if (PKG_CONFIG_STATIC_LIBS) message(STATUS "Using system FreeType (pkg-config, static)") target_compile_options(CSE2 PRIVATE ${freetype2_STATIC_CFLAGS}) - target_link_libraries(CSE2 PRIVATE ${freetype2_STATIC_LINK_LIBRARIES}) + target_link_libraries(CSE2 PRIVATE ${freetype2_STATIC_LDFLAGS}) else() message(STATUS "Using system FreeType (pkg-config, dynamic)") target_compile_options(CSE2 PRIVATE ${freetype2_CFLAGS}) - target_link_libraries(CSE2 PRIVATE ${freetype2_LINK_LIBRARIES}) + target_link_libraries(CSE2 PRIVATE ${freetype2_LDFLAGS}) endif() elseif(FREETYPE_FOUND) message(STATUS "Using system FreeType (CMake)") diff --git a/DoConfig/CMakeLists.txt b/DoConfig/CMakeLists.txt index 1d23ce30..75bec4a5 100644 --- a/DoConfig/CMakeLists.txt +++ b/DoConfig/CMakeLists.txt @@ -95,11 +95,11 @@ if(TARGET PkgConfig::glfw3) if (PKG_CONFIG_STATIC_LIBS) message(STATUS "Using system GLFW3 (pkg-config, static)") target_compile_options(DoConfig PRIVATE ${glfw3_STATIC_CFLAGS}) - target_link_libraries(DoConfig PRIVATE ${glfw3_STATIC_LINK_LIBRARIES}) + target_link_libraries(DoConfig PRIVATE ${glfw3_STATIC_LDFLAGS}) else() message(STATUS "Using system GLFW3 (pkg-config, dynamic)") target_compile_options(DoConfig PRIVATE ${glfw3_CFLAGS}) - target_link_libraries(DoConfig PRIVATE ${glfw3_LINK_LIBRARIES}) + target_link_libraries(DoConfig PRIVATE ${glfw3_LDFLAGS}) endif() elseif(TARGET glfw) # CMake From bcd883e767526e7e5f4a8fae0c68853ee8ea1e00 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 5 Apr 2020 18:14:26 +0100 Subject: [PATCH 19/19] Fix weird buzzing in the software mixer Not really sure why this causes it, but apparently it does. --- src/Backends/Audio/SoftwareMixer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Backends/Audio/SoftwareMixer.cpp b/src/Backends/Audio/SoftwareMixer.cpp index afae1b4b..0b3c87b3 100644 --- a/src/Backends/Audio/SoftwareMixer.cpp +++ b/src/Backends/Audio/SoftwareMixer.cpp @@ -159,7 +159,7 @@ ATTR_HOT void Mixer_MixSounds(float *stream, unsigned int frames_total) const float sample2 = (sound->samples[(size_t)sound->position + 1] - 128.0f) / 128.0f; // Perform linear interpolation - const float interpolated_sample = sample1 + ((sample2 - sample1) * fmod((float)sound->position, 1.0f)); + const float interpolated_sample = sample1 + ((sample2 - sample1) * fmod(sound->position, 1.0)); *steam_pointer++ += interpolated_sample * sound->volume_l; *steam_pointer++ += interpolated_sample * sound->volume_r;