From d9bf936f0d382bd8e6acdfe4f11105aa8f595c59 Mon Sep 17 00:00:00 2001 From: Sven Balzer <4653051+Kyuusokuna@users.noreply.github.com> Date: Thu, 20 Mar 2025 20:22:23 +0100 Subject: [PATCH] add basic gamepad support --- src/main.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index a7c0514..a09ef95 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1094,7 +1094,7 @@ int main(int argc, char **argv) { SDL_SetHint(SDL_HINT_VIDEO_DRIVER, "wayland,x11"); #endif - if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) { + if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_GAMEPAD)) { log_error("Failed to initialize SDL (%s). Exiting.", SDL_GetError()); return 1; } @@ -1488,7 +1488,28 @@ int main(int argc, char **argv) { } dragging_tile_change = false; - } break;; + } break; + + case SDL_EVENT_GAMEPAD_BUTTON_DOWN: { + if (io.WantCaptureKeyboard) + continue; + + if (event.gbutton.button == SDL_GAMEPAD_BUTTON_DPAD_UP) { + player.pos_y = clamp(0, player.pos_y + 1, map_height - 2); + } + + if (event.gbutton.button == SDL_GAMEPAD_BUTTON_DPAD_LEFT) { + player.pos_x = clamp(0, player.pos_x - 1, map_width - 2); + } + + if (event.gbutton.button == SDL_GAMEPAD_BUTTON_DPAD_DOWN) { + player.pos_y = clamp(0, player.pos_y - 1, map_height - 2); + } + + if (event.gbutton.button == SDL_GAMEPAD_BUTTON_DPAD_RIGHT) { + player.pos_x = clamp(0, player.pos_x + 1, map_width - 2); + } + } break; } } }