From 96db787657313c671ff618c23ffc91638cbc72b0 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 31 Jan 2021 03:07:26 +0100 Subject: [PATCH] REVIEWED: Gamepad system, specially for RPI --- src/core.c | 50 +++++++++++++++++++++++++------------------------- src/raylib.h | 6 +++--- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/core.c b/src/core.c index 3e22f436..6b3906e7 100644 --- a/src/core.c +++ b/src/core.c @@ -2860,9 +2860,7 @@ bool IsGamepadAvailable(int gamepad) { bool result = false; -#if !defined(PLATFORM_ANDROID) if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad]) result = true; -#endif return result; } @@ -2871,13 +2869,10 @@ bool IsGamepadAvailable(int gamepad) bool IsGamepadName(int gamepad, const char *name) { bool result = false; - -#if !defined(PLATFORM_ANDROID) const char *currentName = NULL; if (CORE.Input.Gamepad.ready[gamepad]) currentName = GetGamepadName(gamepad); if ((name != NULL) && (currentName != NULL)) result = (strcmp(name, currentName) == 0); -#endif return result; } @@ -2905,6 +2900,7 @@ int GetGamepadAxisCount(int gamepad) if (CORE.Input.Gamepad.ready[gamepad]) ioctl(CORE.Input.Gamepad.streamId[gamepad], JSIOCGAXES, &axisCount); CORE.Input.Gamepad.axisCount = axisCount; #endif + return CORE.Input.Gamepad.axisCount; } @@ -2913,11 +2909,8 @@ float GetGamepadAxisMovement(int gamepad, int axis) { float value = 0; -#if !defined(PLATFORM_ANDROID) if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (axis < MAX_GAMEPAD_AXIS) && - ((axis == GAMEPAD_AXIS_LEFT_TRIGGER) || (axis == GAMEPAD_AXIS_RIGHT_TRIGGER) || - (fabsf(CORE.Input.Gamepad.axisState[gamepad][axis]) >= 0.2f))) value = CORE.Input.Gamepad.axisState[gamepad][axis]; -#endif + (fabsf(CORE.Input.Gamepad.axisState[gamepad][axis]) > 0.1f)) value = CORE.Input.Gamepad.axisState[gamepad][axis]; // 0.1f = GAMEPAD_AXIS_MINIMUM_DRIFT/DELTA return value; } @@ -2927,11 +2920,9 @@ bool IsGamepadButtonPressed(int gamepad, int button) { bool pressed = false; -#if !defined(PLATFORM_ANDROID) if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS) && - (CORE.Input.Gamepad.currentState[gamepad][button] != CORE.Input.Gamepad.previousState[gamepad][button]) && - (CORE.Input.Gamepad.currentState[gamepad][button] == 1)) pressed = true; -#endif + (CORE.Input.Gamepad.previousState[gamepad][button] == 0) && (CORE.Input.Gamepad.currentState[gamepad][button] == 1)) pressed = true; + else pressed = false; return pressed; } @@ -2941,10 +2932,8 @@ bool IsGamepadButtonDown(int gamepad, int button) { bool result = false; -#if !defined(PLATFORM_ANDROID) if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS) && (CORE.Input.Gamepad.currentState[gamepad][button] == 1)) result = true; -#endif return result; } @@ -2954,11 +2943,9 @@ bool IsGamepadButtonReleased(int gamepad, int button) { bool released = false; -#if !defined(PLATFORM_ANDROID) if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS) && - (CORE.Input.Gamepad.currentState[gamepad][button] != CORE.Input.Gamepad.previousState[gamepad][button]) && - (CORE.Input.Gamepad.currentState[gamepad][button] == 0)) released = true; -#endif + (CORE.Input.Gamepad.previousState[gamepad][button] == 1) && (CORE.Input.Gamepad.currentState[gamepad][button] == 0)) released = true; + else released = false; return released; } @@ -2968,10 +2955,8 @@ bool IsGamepadButtonUp(int gamepad, int button) { bool result = false; -#if !defined(PLATFORM_ANDROID) if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (button < MAX_GAMEPAD_BUTTONS) && (CORE.Input.Gamepad.currentState[gamepad][button] == 0)) result = true; -#endif return result; } @@ -3293,7 +3278,11 @@ static bool InitGraphicsDevice(int width, int height) else glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_FALSE); #endif - if (CORE.Window.flags & FLAG_MSAA_4X_HINT) glfwWindowHint(GLFW_SAMPLES, 4); // Tries to enable multisampling x4 (MSAA), default is 0 + if (CORE.Window.flags & FLAG_MSAA_4X_HINT) + { + TRACELOG(LOG_INFO, "DISPLAY: Trying to enable MSAA x4"); + glfwWindowHint(GLFW_SAMPLES, 4); // Tries to enable multisampling x4 (MSAA), default is 0 + } // NOTE: When asking for an OpenGL context version, most drivers provide highest supported version // with forward compatibility to older OpenGL versions. @@ -4246,7 +4235,8 @@ static void Wait(float ms) // Get gamepad button generic to all platforms static int GetGamepadButton(int button) { - int btn = GAMEPAD_BUTTON_UNKNOWN; + int btn = -1; + #if defined(PLATFORM_DESKTOP) switch (button) { @@ -4335,6 +4325,16 @@ static void PollInputEvents(void) CORE.Input.Mouse.previousButtonState[i] = CORE.Input.Mouse.currentButtonState[i]; CORE.Input.Mouse.currentButtonState[i] = CORE.Input.Mouse.currentButtonStateEvdev[i]; } + + // Register gamepads buttons events + for (int i = 0; i < MAX_GAMEPADS; i++) + { + if (CORE.Input.Gamepad.ready[i]) // Check if gamepad is available + { + // Register previous gamepad states + for (int k = 0; k < MAX_GAMEPAD_BUTTONS; k++) CORE.Input.Gamepad.previousState[i][k] = CORE.Input.Gamepad.currentState[i][k]; + } + } #endif #if defined(PLATFORM_UWP) @@ -5904,7 +5904,7 @@ static void *GamepadThread(void *arg) // Process gamepad events by type if (gamepadEvent.type == JS_EVENT_BUTTON) { - TRACELOGD("RPI: Gamepad button: %i, value: %i", gamepadEvent.number, gamepadEvent.value); + //TRACELOG(LOG_WARNING, "RPI: Gamepad button: %i, value: %i", gamepadEvent.number, gamepadEvent.value); if (gamepadEvent.number < MAX_GAMEPAD_BUTTONS) { @@ -5917,7 +5917,7 @@ static void *GamepadThread(void *arg) } else if (gamepadEvent.type == JS_EVENT_AXIS) { - TRACELOGD("RPI: Gamepad axis: %i, value: %i", gamepadEvent.number, gamepadEvent.value); + //TRACELOG(LOG_WARNING, "RPI: Gamepad axis: %i, value: %i", gamepadEvent.number, gamepadEvent.value); if (gamepadEvent.number < MAX_GAMEPAD_AXIS) { diff --git a/src/raylib.h b/src/raylib.h index a2e5c63b..d693f026 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -674,9 +674,9 @@ typedef enum { GAMEPAD_BUTTON_RIGHT_TRIGGER_2, // These are buttons in the center of the gamepad - GAMEPAD_BUTTON_MIDDLE_LEFT, //PS3 Select - GAMEPAD_BUTTON_MIDDLE, //PS Button/XBOX Button - GAMEPAD_BUTTON_MIDDLE_RIGHT, //PS3 Start + GAMEPAD_BUTTON_MIDDLE_LEFT, // PS3 Select + GAMEPAD_BUTTON_MIDDLE, // PS Button/XBOX Button + GAMEPAD_BUTTON_MIDDLE_RIGHT, // PS3 Start // These are the joystick press in buttons GAMEPAD_BUTTON_LEFT_THUMB,