Gamepad Support

This commit is contained in:
Reece Mackie 2019-04-25 19:09:55 +01:00
parent 108f02e279
commit 19cdcbe7a0
4 changed files with 70 additions and 39 deletions

View File

@ -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();

View File

@ -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.

View File

@ -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

View File

@ -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?
#endif