From 19cdcbe7a0203f48ba7c0f2b5fcc6b77d8fa1f10 Mon Sep 17 00:00:00 2001 From: Reece Mackie <20544390+Rover656@users.noreply.github.com> Date: Thu, 25 Apr 2019 19:09:55 +0100 Subject: [PATCH] Gamepad Support --- projects/VS2017.UWP/raylib.App.UWP/App.cpp | 4 +- projects/VS2017.UWP/raylib.App.UWP/BaseApp.h | 55 +++++++++----------- src/core.c | 44 +++++++++++++--- src/raylib.h | 6 ++- 4 files changed, 70 insertions(+), 39 deletions(-) diff --git a/projects/VS2017.UWP/raylib.App.UWP/App.cpp b/projects/VS2017.UWP/raylib.App.UWP/App.cpp index e56c0e84c..7dc318a27 100644 --- a/projects/VS2017.UWP/raylib.App.UWP/App.cpp +++ b/projects/VS2017.UWP/raylib.App.UWP/App.cpp @@ -62,13 +62,13 @@ void App::Update() if (IsKeyDown(KEY_S)) DrawCircle(100, 100, 100, BLUE); - if (IsKeyPressed(KEY_A)) + if (IsKeyPressed(KEY_A) || IsGamepadButtonPressed(0, GAMEPAD_XBOX_BUTTON_LEFT)) { posX -= 50; EnableCursor(); } - if (IsKeyPressed(KEY_D)) + if (IsKeyPressed(KEY_D) || IsGamepadButtonPressed(0, GAMEPAD_XBOX_BUTTON_RIGHT)) { posX += 50; DisableCursor(); diff --git a/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h b/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h index f46812413..b7794a1d5 100644 --- a/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h +++ b/projects/VS2017.UWP/raylib.App.UWP/BaseApp.h @@ -63,9 +63,9 @@ TODO list: */ // Stand-ins for "core.c" variables -//#define MAX_GAMEPADS 4 // Max number of gamepads supported -//#define MAX_GAMEPAD_BUTTONS 32 // Max bumber of buttons supported (per gamepad) -//#define MAX_GAMEPAD_AXIS 8 // Max number of axis supported (per gamepad) +#define MAX_GAMEPADS 4 // Max number of gamepads supported +#define MAX_GAMEPAD_BUTTONS 32 // Max bumber of buttons supported (per gamepad) +#define MAX_GAMEPAD_AXIS 8 // Max number of axis supported (per gamepad) //static bool gamepadReady[MAX_GAMEPADS] = { false }; // Flag to know if gamepad is ready //static float gamepadAxisState[MAX_GAMEPADS][MAX_GAMEPAD_AXIS]; // Gamepad axis state //static char previousGamepadState[MAX_GAMEPADS][MAX_GAMEPAD_BUTTONS]; // Previous gamepad buttons state @@ -231,7 +231,7 @@ namespace raylibUWP } // Process Gamepads - /*{ + { // Check if gamepads are ready for (int i = 0; i < MAX_GAMEPADS; i++) { @@ -239,46 +239,43 @@ namespace raylibUWP // connected gamepads with their spot in the list, but this has serious robustness problems // e.g. player 1, 2, and 3 are playing a game - if player2 disconnects, p3's controller would now be mapped to p2's character since p3 is now second in the list. - gamepadReady[i] = (i < Gamepad::Gamepads->Size); + UWPGamepadActive(i, i < Gamepad::Gamepads->Size); } // Get current gamepad state for (int i = 0; i < MAX_GAMEPADS; i++) { - if (gamepadReady[i]) + if (IsGamepadAvailable(i)) { - // Register previous gamepad states - for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) previousGamepadState[i][k] = currentGamepadState[i][k]; - // Get current gamepad state auto gamepad = Gamepad::Gamepads->GetAt(i); GamepadReading reading = gamepad->GetCurrentReading(); // NOTE: Maybe it would be wiser to redefine the gamepad button mappings in "raylib.h" for the UWP platform instead of remapping them manually - currentGamepadState[i][GAMEPAD_XBOX_BUTTON_A] = ((reading.Buttons & GamepadButtons::A) == GamepadButtons::A); - currentGamepadState[i][GAMEPAD_XBOX_BUTTON_B] = ((reading.Buttons & GamepadButtons::B) == GamepadButtons::B); - currentGamepadState[i][GAMEPAD_XBOX_BUTTON_X] = ((reading.Buttons & GamepadButtons::X) == GamepadButtons::X); - currentGamepadState[i][GAMEPAD_XBOX_BUTTON_Y] = ((reading.Buttons & GamepadButtons::Y) == GamepadButtons::Y); - currentGamepadState[i][GAMEPAD_XBOX_BUTTON_LB] = ((reading.Buttons & GamepadButtons::LeftShoulder) == GamepadButtons::LeftShoulder); - currentGamepadState[i][GAMEPAD_XBOX_BUTTON_RB] = ((reading.Buttons & GamepadButtons::RightShoulder) == GamepadButtons::RightShoulder); - currentGamepadState[i][GAMEPAD_XBOX_BUTTON_SELECT] = ((reading.Buttons & GamepadButtons::View) == GamepadButtons::View); // Changed for XB1 Controller - currentGamepadState[i][GAMEPAD_XBOX_BUTTON_START] = ((reading.Buttons & GamepadButtons::Menu) == GamepadButtons::Menu); // Changed for XB1 Controller - currentGamepadState[i][GAMEPAD_XBOX_BUTTON_UP] = ((reading.Buttons & GamepadButtons::DPadUp) == GamepadButtons::DPadUp); - currentGamepadState[i][GAMEPAD_XBOX_BUTTON_RIGHT] = ((reading.Buttons & GamepadButtons::DPadRight) == GamepadButtons::DPadRight); - currentGamepadState[i][GAMEPAD_XBOX_BUTTON_DOWN] = ((reading.Buttons & GamepadButtons::DPadLeft) == GamepadButtons::DPadDown); - currentGamepadState[i][GAMEPAD_XBOX_BUTTON_LEFT] = ((reading.Buttons & GamepadButtons::DPadDown) == GamepadButtons::DPadLeft); - currentGamepadState[i][GAMEPAD_XBOX_BUTTON_HOME] = false; // Home button not supported by UWP + UWPGamepadButton(i, GAMEPAD_XBOX_BUTTON_A, ((reading.Buttons & GamepadButtons::A) == GamepadButtons::A)); + UWPGamepadButton(i, GAMEPAD_XBOX_BUTTON_B, ((reading.Buttons & GamepadButtons::B) == GamepadButtons::B)); + UWPGamepadButton(i, GAMEPAD_XBOX_BUTTON_X, ((reading.Buttons & GamepadButtons::X) == GamepadButtons::X)); + UWPGamepadButton(i, GAMEPAD_XBOX_BUTTON_Y, ((reading.Buttons & GamepadButtons::Y) == GamepadButtons::Y)); + UWPGamepadButton(i, GAMEPAD_XBOX_BUTTON_LB, ((reading.Buttons & GamepadButtons::LeftShoulder) == GamepadButtons::LeftShoulder)); + UWPGamepadButton(i, GAMEPAD_XBOX_BUTTON_RB, ((reading.Buttons & GamepadButtons::RightShoulder) == GamepadButtons::RightShoulder)); + UWPGamepadButton(i, GAMEPAD_XBOX_BUTTON_SELECT, ((reading.Buttons & GamepadButtons::View) == GamepadButtons::View)); // Changed for XB1 Controller + UWPGamepadButton(i, GAMEPAD_XBOX_BUTTON_START, ((reading.Buttons & GamepadButtons::Menu) == GamepadButtons::Menu)); // Changed for XB1 Controller + UWPGamepadButton(i, GAMEPAD_XBOX_BUTTON_UP, ((reading.Buttons & GamepadButtons::DPadUp) == GamepadButtons::DPadUp)); + UWPGamepadButton(i, GAMEPAD_XBOX_BUTTON_RIGHT, ((reading.Buttons & GamepadButtons::DPadRight) == GamepadButtons::DPadRight)); + UWPGamepadButton(i, GAMEPAD_XBOX_BUTTON_DOWN, ((reading.Buttons & GamepadButtons::DPadDown) == GamepadButtons::DPadDown)); + UWPGamepadButton(i, GAMEPAD_XBOX_BUTTON_LEFT, ((reading.Buttons & GamepadButtons::DPadLeft) == GamepadButtons::DPadLeft)); + UWPGamepadButton(i, GAMEPAD_XBOX_BUTTON_HOME, false); // Home button not supported by UWP // Get current axis state - gamepadAxisState[i][GAMEPAD_XBOX_AXIS_LEFT_X] = reading.LeftThumbstickX; - gamepadAxisState[i][GAMEPAD_XBOX_AXIS_LEFT_Y] = reading.LeftThumbstickY; - gamepadAxisState[i][GAMEPAD_XBOX_AXIS_RIGHT_X] = reading.RightThumbstickX; - gamepadAxisState[i][GAMEPAD_XBOX_AXIS_RIGHT_Y] = reading.RightThumbstickY; - gamepadAxisState[i][GAMEPAD_XBOX_AXIS_LT] = reading.LeftTrigger; - gamepadAxisState[i][GAMEPAD_XBOX_AXIS_RT] = reading.RightTrigger; + UWPGamepadAxis(i, GAMEPAD_XBOX_AXIS_LEFT_X, (float)reading.LeftThumbstickX); + UWPGamepadAxis(i, GAMEPAD_XBOX_AXIS_LEFT_Y, (float)reading.LeftThumbstickY); + UWPGamepadAxis(i, GAMEPAD_XBOX_AXIS_RIGHT_X, (float)reading.RightThumbstickX); + UWPGamepadAxis(i, GAMEPAD_XBOX_AXIS_RIGHT_Y, (float)reading.RightThumbstickY); + UWPGamepadAxis(i, GAMEPAD_XBOX_AXIS_LT, (float)reading.LeftTrigger); + UWPGamepadAxis(i, GAMEPAD_XBOX_AXIS_RT, (float)reading.RightTrigger); } } - }*/ + } } // Application lifecycle event handlers. diff --git a/src/core.c b/src/core.c index 863f844f1..7e0f65348 100644 --- a/src/core.c +++ b/src/core.c @@ -3096,6 +3096,14 @@ static void PollInputEvents(void) // Register previous keys states for (int i = 0; i < 512; i++) previousKeyState[i] = currentKeyState[i]; + for (int i = 0; i < MAX_GAMEPADS; i++) + { + if (gamepadReady[i]) + { + for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) previousGamepadState[i][k] = currentGamepadState[i][k]; + } + } + // Register previous mouse states previousMouseWheelY = currentMouseWheelY; currentMouseWheelY = 0; @@ -4593,12 +4601,14 @@ static void *GamepadThread(void *arg) #if defined(PLATFORM_UWP) -void UWPRegisterKey(int key, char action) { +void UWPRegisterKey(int key, char action) +{ //Convert from virtualKey int actualKey = -1; - switch (key) { + switch (key) + { case 0x08: actualKey = KEY_BACKSPACE; break; case 0x20: actualKey = KEY_SPACE; break; case 0x1B: actualKey = KEY_ESCAPE; break; @@ -4668,23 +4678,45 @@ void UWPRegisterKey(int key, char action) { currentKeyState[actualKey] = action; } -void UWPRegisterClick(int btn, char action) { +void UWPRegisterClick(int btn, char action) +{ currentMouseState[btn] = action; } -void UWPScrollWheel(int delta) { +void UWPScrollWheel(int delta) +{ currentMouseWheelY += delta; } -void UWPMousePosition(float x, float y) { +void UWPMousePosition(float x, float y) +{ mousePosition.x = x; mousePosition.y = y; } -void UWPMarkCursor(bool hidden) { +void UWPMarkCursor(bool hidden) +{ cursorHidden = hidden; } +void UWPGamepadActive(int gamepad, bool active) +{ + if (gamepad < MAX_GAMEPADS) + gamepadReady[gamepad] = active; +} + +void UWPGamepadButton(int gamepad, int button, char action) +{ + if (gamepad < MAX_GAMEPADS && button < MAX_GAMEPAD_BUTTONS) + currentGamepadState[gamepad][button] = action; +} + +void UWPGamepadAxis(int gamepad, int axis, float value) +{ + if (gamepad < MAX_GAMEPADS && axis < MAX_GAMEPAD_AXIS) + gamepadAxisState[gamepad][axis] = value; +} + #endif // Plays raylib logo appearing animation diff --git a/src/raylib.h b/src/raylib.h index e9b6e62d8..8080fb389 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -921,9 +921,11 @@ RLAPI void UWPRegisterClick(int btn, char action); RLAPI void UWPScrollWheel(int delta); RLAPI void UWPMousePosition(float x, float y); RLAPI void UWPMarkCursor(bool hidden); +RLAPI void UWPGamepadActive(int gamepad, bool active); +RLAPI void UWPGamepadButton(int gamepad, int button, char action); +RLAPI void UWPGamepadAxis(int gamepad, int axis, float value); -//Get task/input for allowing C to call UWP/cursor functions -//Gamepad functionality? +//Get task/input for allowing C to call UWP/cursor functions #endif