From 9dbd30c44bd64fbefc78dce32128e196cb7f0da5 Mon Sep 17 00:00:00 2001 From: Sam C Date: Wed, 29 Nov 2017 20:43:41 -0800 Subject: [PATCH] Add UWP gamepad input polling Add a basic UWP gampead poll function with zero changes to code outside of App.cpp --- project/vs2015.UWP/raylib.App.UWP/App.cpp | 65 +++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/project/vs2015.UWP/raylib.App.UWP/App.cpp b/project/vs2015.UWP/raylib.App.UWP/App.cpp index 7d98d707..8fe9bc19 100644 --- a/project/vs2015.UWP/raylib.App.UWP/App.cpp +++ b/project/vs2015.UWP/raylib.App.UWP/App.cpp @@ -10,12 +10,77 @@ using namespace Windows::UI::Core; using namespace Windows::UI::Input; using namespace Windows::Foundation; using namespace Windows::Foundation::Collections; +using namespace Windows::Gaming::Input; using namespace Windows::Graphics::Display; using namespace Microsoft::WRL; using namespace Platform; using namespace raylibUWP; +/* GAMEPAD CODE */ + +// 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) + +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 +static char currentGamepadState[MAX_GAMEPADS][MAX_GAMEPAD_BUTTONS]; // Current gamepad buttons state + +void UWP_PollInput() +{ + // Check if gamepads are ready + for (int i = 0; i < MAX_GAMEPADS; i++) + { + // HACK: UWP keeps a contiguous list of gamepads. For the interest of time I'm just doing a 1:1 mapping of + // 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); + } + + // Get current gamepad state + for (int i = 0; i < MAX_GAMEPADS; i++) + { + if (gamepadReady[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 doing this + 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 + + // 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; + } + } +} + +/* OTHER CODE*/ + // Helper to convert a length in device-independent pixels (DIPs) to a length in physical pixels. inline float ConvertDipsToPixels(float dips, float dpi) {