diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2025-08-09 11:14:14 +0200 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2025-08-09 11:14:14 +0200 |
| commit | 08939fb0a1e58a57118d28bb3f949fe4821c09d7 (patch) | |
| tree | c5f859688e7da2c408cf540b96b7a6a80c786bc4 /main.c | |
| parent | d06d50075b536cecb04ceaf6453980e398d68cb8 (diff) | |
| download | bidi-08939fb0a1e58a57118d28bb3f949fe4821c09d7.tar.gz | |
Added support for multiple controllers (Keyboard, Xbox)
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 74 |
1 files changed, 70 insertions, 4 deletions
@@ -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; } |
