From 62f63f9e485fdffa1e981ae4ae58f5eb8ccfff8e Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 2 Jan 2023 17:06:52 +0100 Subject: [PATCH] REVIEWED: Avoid possible gamepad index as `-1` #2839 WARNING: It could require further review of `GamepadThread()` function where `js_event gamepadEvent.number` detecting current pressed button could generate a missmatch with index 0 (reserved for button unknow). Or maybe `0` could just be `GAMEPAD_BUTTON_NONE`? In that case, consistency with other inputs should be carefully considered... --- src/raylib.h | 4 ++-- src/rcore.c | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/raylib.h b/src/raylib.h index 5a230a3ac..1c3f77669 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -716,7 +716,7 @@ typedef enum { // Material map index typedef enum { - MATERIAL_MAP_ALBEDO = 0, // Albedo material (same as: MATERIAL_MAP_DIFFUSE) + MATERIAL_MAP_ALBEDO = 0, // Albedo material (same as: MATERIAL_MAP_DIFFUSE) MATERIAL_MAP_METALNESS, // Metalness material (same as: MATERIAL_MAP_SPECULAR) MATERIAL_MAP_NORMAL, // Normal material MATERIAL_MAP_ROUGHNESS, // Roughness material @@ -862,7 +862,7 @@ typedef enum { } BlendMode; // Gesture -// NOTE: It could be used as flags to enable only some gestures +// NOTE: Provided as bit-wise flags to enable only desired gestures typedef enum { GESTURE_NONE = 0, // No gesture GESTURE_TAP = 1, // Tap gesture diff --git a/src/rcore.c b/src/rcore.c index b88d6e9f2..0e9aa76ec 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -765,7 +765,7 @@ void InitWindow(int width, int height, const char *title) CORE.Input.Keyboard.exitKey = KEY_ESCAPE; CORE.Input.Mouse.scale = (Vector2){ 1.0f, 1.0f }; CORE.Input.Mouse.cursor = MOUSE_CURSOR_ARROW; - CORE.Input.Gamepad.lastButtonPressed = -1; + CORE.Input.Gamepad.lastButtonPressed = 0; // GAMEPAD_BUTTON_UNKNOWN #if defined(SUPPORT_EVENTS_WAITING) CORE.Window.eventWaiting = true; #endif @@ -4871,7 +4871,7 @@ void PollInputEvents(void) #if !(defined(PLATFORM_RPI) || defined(PLATFORM_DRM)) // Reset last gamepad button/axis registered state - CORE.Input.Gamepad.lastButtonPressed = -1; + CORE.Input.Gamepad.lastButtonPressed = 0; // GAMEPAD_BUTTON_UNKNOWN CORE.Input.Gamepad.axisCount = 0; #endif @@ -5714,14 +5714,15 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event) CORE.Input.Gamepad.ready[0] = true; GamepadButton button = AndroidTranslateGamepadButton(keycode); - if (button == GAMEPAD_BUTTON_UNKNOWN) - return 1; + + if (button == GAMEPAD_BUTTON_UNKNOWN) return 1; if (AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_DOWN) { CORE.Input.Gamepad.currentButtonState[0][button] = 1; } else CORE.Input.Gamepad.currentButtonState[0][button] = 0; // Key up + return 1; // Handled gamepad button } @@ -6685,7 +6686,7 @@ static void *GamepadThread(void *arg) CORE.Input.Gamepad.currentButtonState[i][gamepadEvent.number] = (int)gamepadEvent.value; if ((int)gamepadEvent.value == 1) CORE.Input.Gamepad.lastButtonPressed = gamepadEvent.number; - else CORE.Input.Gamepad.lastButtonPressed = -1; + else CORE.Input.Gamepad.lastButtonPressed = 0; // GAMEPAD_BUTTON_UNKNOWN } } else if (gamepadEvent.type == JS_EVENT_AXIS)