From db4585b3e23cd3c5aa87da21aedc36fd8be21739 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 16 Mar 2016 17:52:09 +0100 Subject: [PATCH] Improved gamepad support Now it works ok also in RaspberryPi --- src/core.c | 46 ++++++++++++++-------------------------------- src/raylib.h | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/src/core.c b/src/core.c index 6b2321c2..5bb59faa 100644 --- a/src/core.c +++ b/src/core.c @@ -127,6 +127,7 @@ #define MOUSE_SENSITIVITY 0.8f #define MAX_GAMEPAD_BUTTONS 11 + #define MAX_GAMEPAD_AXIS 5 #endif //---------------------------------------------------------------------------------- @@ -168,8 +169,7 @@ static bool gamepadReady = false; // Flag to know if gamepad is re pthread_t gamepadThreadId; // Gamepad reading thread id int gamepadButtons[MAX_GAMEPAD_BUTTONS]; -int gamepadAxisX = 0; -int gamepadAxisY = 0; +float gamepadAxisValues[MAX_GAMEPAD_AXIS]; #endif #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) @@ -1177,30 +1177,22 @@ bool IsGamepadAvailable(int gamepad) } // Return axis movement vector for a gamepad -Vector2 GetGamepadMovement(int gamepad) +float GetGamepadAxisMovement(int gamepad, int axis) { - Vector2 vec = { 0, 0 }; - - const float *axes; - int axisCount = 0; + float value = 0; #if defined(PLATFORM_RPI) - // TODO: Get gamepad axis information - // Use gamepadAxisX, gamepadAxisY + if (axis < MAX_GAMEPAD_AXIS) value = gamepadAxisValues[axis]; #else + const float *axes; + int axisCount = 0; + axes = glfwGetJoystickAxes(gamepad, &axisCount); -#endif - if (axisCount >= 2) - { - vec.x = axes[0]; // Left joystick X - vec.y = axes[1]; // Left joystick Y - - //vec.x = axes[2]; // Right joystick X - //vec.x = axes[3]; // Right joystick Y - } + if (axis < axisCount) value = axes[axis]; +#endif - return vec; + return value; } // Detect if a gamepad button has been pressed once @@ -2484,10 +2476,6 @@ static void *GamepadThread(void *arg) unsigned char number; // event axis/button number }; - // These values are sensible on Logitech Dual Action Rumble and Xbox360 controller - const int joystickAxisX = 0; - const int joystickAxisY = 1; - // Read gamepad event struct js_event gamepadEvent; @@ -2512,17 +2500,11 @@ static void *GamepadThread(void *arg) { TraceLog(DEBUG, "Gamepad axis: %i, value: %i", gamepadEvent.number, gamepadEvent.value); - if (gamepadEvent.number == joystickAxisX) gamepadAxisX = (int)gamepadEvent.value; - if (gamepadEvent.number == joystickAxisY) gamepadAxisY = (int)gamepadEvent.value; - /* - switch (gamepadEvent.number) + if (gamepadEvent.number < MAX_GAMEPAD_AXIS) { - case 0: // 1st Axis X - case 1: // 1st Axis Y - case 2: // 2st Axis X - case 3: // 2st Axis Y + // NOTE: Scaling of gamepadEvent.value to get values between -1..1 + gamepadAxisValues[gamepadEvent.number] = (float)gamepadEvent.value/32768; } - */ } } } diff --git a/src/raylib.h b/src/raylib.h index ddfccea7..d891467e 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -190,7 +190,35 @@ #define GAMEPAD_BUTTON_SELECT 9 #define GAMEPAD_BUTTON_START 10 -// TODO: Review Xbox360 USB Controller Buttons +// Xbox360 USB Controller Buttons +#define GAMEPAD_XBOX_BUTTON_A 0 +#define GAMEPAD_XBOX_BUTTON_B 1 +#define GAMEPAD_XBOX_BUTTON_X 2 +#define GAMEPAD_XBOX_BUTTON_Y 3 +#define GAMEPAD_XBOX_BUTTON_LB 4 +#define GAMEPAD_XBOX_BUTTON_RB 5 +#define GAMEPAD_XBOX_BUTTON_SELECT 6 +#define GAMEPAD_XBOX_BUTTON_START 7 + +#if defined(PLATFORM_RPI) + #define GAMEPAD_XBOX_AXIS_DPAD_X 32 + #define GAMEPAD_XBOX_AXIS_DPAD_Y 64 + #define GAMEPAD_XBOX_AXIS_RIGHT_X 3 + #define GAMEPAD_XBOX_AXIS_RIGHT_Y 4 + #define GAMEPAD_XBOX_AXIS_LT 2 + #define GAMEPAD_XBOX_AXIS_RT 5 +#else + #define GAMEPAD_XBOX_BUTTON_UP 10 + #define GAMEPAD_XBOX_BUTTON_DOWN 12 + #define GAMEPAD_XBOX_BUTTON_LEFT 13 + #define GAMEPAD_XBOX_BUTTON_RIGHT 11 + #define GAMEPAD_XBOX_AXIS_RIGHT_X 4 + #define GAMEPAD_XBOX_AXIS_RIGHT_Y 3 + #define GAMEPAD_XBOX_AXIS_LT_RT 2 +#endif + +#define GAMEPAD_XBOX_AXIS_LEFT_X 0 +#define GAMEPAD_XBOX_AXIS_LEFT_Y 1 // Android Physic Buttons #define ANDROID_BACK 4 @@ -592,7 +620,7 @@ void DisableCursor(void); // Disables cursor bool IsCursorHidden(void); // Returns true if cursor is not visible bool IsGamepadAvailable(int gamepad); // Detect if a gamepad is available -Vector2 GetGamepadMovement(int gamepad); // Return axis movement vector for a gamepad +float GetGamepadAxisMovement(int gamepad, int axis); // Return axis movement value for a gamepad axis bool IsGamepadButtonPressed(int gamepad, int button); // Detect if a gamepad button has been pressed once bool IsGamepadButtonDown(int gamepad, int button); // Detect if a gamepad button is being pressed bool IsGamepadButtonReleased(int gamepad, int button); // Detect if a gamepad button has been released once