add basic gamepad support

This commit is contained in:
Sven Balzer 2025-03-20 20:22:23 +01:00
parent 19e75d3807
commit d9bf936f0d

View File

@ -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;
}
}
}