From 08939fb0a1e58a57118d28bb3f949fe4821c09d7 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Sat, 9 Aug 2025 11:14:14 +0200 Subject: Added support for multiple controllers (Keyboard, Xbox) --- README.md | 1 + main.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- stdlib/button.lua | 20 +++++++-------- 3 files changed, 81 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index e197ac7..b12ffa4 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,7 @@ close_window() ## Cool tools - https://hardwaretester.com/gamepad +- https://www.raylib.com/examples/core/loader.html?name=core_input_gamepad ## Development references diff --git a/main.c b/main.c index 3530dbc..3cbec87 100644 --- a/main.c +++ b/main.c @@ -19,6 +19,10 @@ #define DEBUG_LEVEL LOG_DEBUG #define FONT_IMPORT_SIZE 30 #define UID_LENGTH 36 +#define GAMEPAD_INDEX 0 +#define XBOX_ALIAS_1 "xbox" +#define XBOX_ALIAS_2 "x-box" +#define PS_ALIAS "playstation" typedef struct { char uid[UID_LENGTH + 1]; @@ -302,14 +306,76 @@ static int l_draw_triangle(lua_State *L) { } static int l_button_down(lua_State *L) { - int button = luaL_checknumber(L, 1); - lua_pushboolean(L, IsKeyDown(button)); + luaL_checktype(L, 1, LUA_TTABLE); + int keyboard = (int)lua_getfield_int(L, 1, "keyboard"); + int xbox = (int)lua_getfield_int(L, 1, "xbox"); + int playstation = (int)lua_getfield_int(L, 1, "playstation"); + bool is_active = false; + + // Check keyboard first. + if (IsKeyDown(keyboard)) { + is_active = true; + } else { + // FIXME: This does not work currently due to a bug in GLFW that Raylib + // is using. SDL could be used to compile Raylib but that doesn't + // statically link against SDL so it's not ideal. + + // Check for controllers otherwise. + if (IsGamepadAvailable(GAMEPAD_INDEX)) { + // Xbox controller. + if (TextFindIndex(TextToLower(GetGamepadName(GAMEPAD_INDEX)), XBOX_ALIAS_1) > -1 || TextFindIndex(TextToLower(GetGamepadName(GAMEPAD_INDEX)), XBOX_ALIAS_2) > -1) { + if (IsGamepadButtonDown(GAMEPAD_INDEX, xbox)) { + is_active = true; + } + } + + // Playstation controller. + if (TextFindIndex(TextToLower(GetGamepadName(GAMEPAD_INDEX)), PS_ALIAS) > -1) { + if (IsGamepadButtonDown(GAMEPAD_INDEX, playstation)) { + is_active = true; + } + } + } + } + + lua_pushboolean(L, is_active); return 1; } static int l_button_pressed(lua_State *L) { - int button = luaL_checknumber(L, 1); - lua_pushboolean(L, IsKeyPressed(button)); + luaL_checktype(L, 1, LUA_TTABLE); + int keyboard = (int)lua_getfield_int(L, 1, "keyboard"); + int xbox = (int)lua_getfield_int(L, 1, "xbox"); + int playstation = (int)lua_getfield_int(L, 1, "playstation"); + bool is_active = false; + + // Check keyboard first. + if (IsKeyPressed(keyboard)) { + is_active = true; + } else { + // FIXME: This does not work currently due to a bug in GLFW that Raylib + // is using. SDL could be used to compile Raylib but that doesn't + // statically link against SDL so it's not ideal. + + // Check for controllers otherwise. + if (IsGamepadAvailable(GAMEPAD_INDEX)) { + // Xbox controller. + if (TextFindIndex(TextToLower(GetGamepadName(GAMEPAD_INDEX)), XBOX_ALIAS_1) > -1 || TextFindIndex(TextToLower(GetGamepadName(GAMEPAD_INDEX)), XBOX_ALIAS_2) > -1) { + if (IsGamepadButtonPressed(GAMEPAD_INDEX, xbox)) { + is_active = true; + } + } + + // Playstation controller. + if (TextFindIndex(TextToLower(GetGamepadName(GAMEPAD_INDEX)), PS_ALIAS) > -1) { + if (IsGamepadButtonPressed(GAMEPAD_INDEX, playstation)) { + is_active = true; + } + } + } + } + + lua_pushboolean(L, is_active); return 1; } diff --git a/stdlib/button.lua b/stdlib/button.lua index e8b6318..02b8358 100644 --- a/stdlib/button.lua +++ b/stdlib/button.lua @@ -1,16 +1,16 @@ local button = {} -button.PAD_UP = 87 -- Key: W -button.PAD_DOWN = 83 -- Key: S -button.PAD_LEFT = 65 -- Key: A -button.PAD_RIGHT = 68 -- Key: D +button.PAD_UP = { keyboard = 87, xbox = 1, playstation = 0 } -- Key: W +button.PAD_DOWN = { keyboard = 83, xbox = 3, playstation = 0 } -- Key: S +button.PAD_LEFT = { keyboard = 65, xbox = 4, playstation = 0 } -- Key: A +button.PAD_RIGHT = { keyboard = 68, xbox = 2, playstation = 0 } -- Key: D -button.A = 76 -- Key: L -button.B = 80 -- Key: P -button.X = 75 -- Key: K -button.Y = 79 -- Key: O +button.A = { keyboard = 76, xbox = 7, playstation = 0 } -- Key: L +button.B = { keyboard = 80, xbox = 6, playstation = 0 } -- Key: P +button.X = { keyboard = 75, xbox = 8, playstation = 0 } -- Key: K +button.Y = { keyboard = 79, xbox = 5, playstation = 0 } -- Key: O -button.SELECT = 81 -- Key: Q -button.START = 69 -- Key: E +button.SELECT = { keyboard = 81, xbox = 13, playstation = 0 } -- Key: Q +button.START = { keyboard = 69, xbox = 15, playstation = 0 } -- Key: E return button -- cgit v1.2.3