From b3bc4b21d17f7e434e21684ca6a2c111ea150fbb Mon Sep 17 00:00:00 2001 From: Ray Date: Fri, 14 Oct 2016 00:47:43 +0200 Subject: [PATCH] Working on better gamepad support --- src/core.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- src/raylib.h | 1 + 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/core.c b/src/core.c index a35dbae05..b07179d45 100644 --- a/src/core.c +++ b/src/core.c @@ -1166,6 +1166,17 @@ bool IsGamepadAvailable(int gamepad) return result; } +// Return gamepad internal name id +const char *GetGamepadName(int gamepad) +{ +#if defined(PLATFORM_DESKTOP) + if (glfwJoystickPresent(gamepad) == 1) return glfwGetJoystickName(gamepad); + else return NULL; +#else + return NULL; +#endif +} + // Return axis movement vector for a gamepad float GetGamepadAxisMovement(int gamepad, int axis) { @@ -1192,7 +1203,11 @@ float GetGamepadAxisMovement(int gamepad, int axis) bool IsGamepadButtonPressed(int gamepad, int button) { bool pressed = false; + + if ((currentGamepadState[button] != previousGamepadState[button]) && (currentGamepadState[button] == 1)) pressed = true; + else pressed = false; + /* currentGamepadState[button] = IsGamepadButtonDown(gamepad, button); if (currentGamepadState[button] != previousGamepadState[button]) @@ -1201,6 +1216,7 @@ bool IsGamepadButtonPressed(int gamepad, int button) previousGamepadState[button] = currentGamepadState[button]; } else pressed = false; + */ return pressed; } @@ -1222,6 +1238,8 @@ bool IsGamepadButtonDown(int gamepad, int button) if ((buttons != NULL) && (buttons[button] == GLFW_PRESS)) result = true; else result = false; + + //result = currentGamepadState[button]; #endif return result; @@ -1233,14 +1251,19 @@ bool IsGamepadButtonReleased(int gamepad, int button) bool released = false; currentGamepadState[button] = IsGamepadButtonUp(gamepad, button); + + if ((currentGamepadState[button] != previousGamepadState[button]) && (currentGamepadState[button] == 0)) released = true; + else released = false; + /* if (currentGamepadState[button] != previousGamepadState[button]) { if (currentGamepadState[button]) released = true; previousGamepadState[button] = currentGamepadState[button]; } else released = false; - + */ + return released; } @@ -1984,8 +2007,26 @@ static void PollInputEvents(void) previousMouseWheelY = currentMouseWheelY; currentMouseWheelY = 0; + + // Register previous gamepad states + for (int i = 0; i < 32; i++) previousGamepadState[i] = currentGamepadState[i]; + + // Get current gamepad state (no callback) + if (glfwJoystickPresent(GAMEPAD_PLAYER1)) + { + const unsigned char *buttons; + int buttonsCount; + + buttons = glfwGetJoystickButtons(GAMEPAD_PLAYER1, &buttonsCount); + + for (int i = 0; (buttons != NULL) && (buttonsCount < 32) && (i < buttonsCount); i++) + { + if (buttons[i] == GLFW_PRESS) currentGamepadState[i] = true; + else currentGamepadState[i] = false; + } + } - glfwPollEvents(); // Register keyboard/mouse events... and window events! + glfwPollEvents(); // Register keyboard/mouse events (callbacks)... and window events! #endif #if defined(PLATFORM_ANDROID) diff --git a/src/raylib.h b/src/raylib.h index 9bc891309..0d6f4326c 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -657,6 +657,7 @@ RLAPI int GetKeyPressed(void); // Get latest key RLAPI void SetExitKey(int key); // Set a custom key to exit program (default is ESC) RLAPI bool IsGamepadAvailable(int gamepad); // Detect if a gamepad is available +RLAPI const char *GetGamepadName(int gamepad); // Return gamepad internal name id RLAPI float GetGamepadAxisMovement(int gamepad, int axis); // Return axis movement value for a gamepad axis RLAPI bool IsGamepadButtonPressed(int gamepad, int button); // Detect if a gamepad button has been pressed once RLAPI bool IsGamepadButtonDown(int gamepad, int button); // Detect if a gamepad button is being pressed