aboutsummaryrefslogtreecommitdiff
path: root/src/input.c
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-09 09:29:28 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-09 10:11:21 +0200
commitc62d8a1a22379a04e09623b60f8bd88a3e042040 (patch)
tree2cfaaff0092b261f524616c507895963e95dabec /src/input.c
parent36213f7dfd59ab0f3e99b88e14b653723ca37d64 (diff)
Factor Out Controller Logic
Start factoring out controller logic, note that we need to manage controllers at the app-level and not the scene level, as the different scenes need gamepad events (for controlling cart selection and passing to the game respectively). There are some hacks to work around how the new abstraction does not exactly fit the previous model, we will fix that up once we wire up the rest of input handling: PICO-8 needs keyboard and mouse events only from the host, and they need to be in SDL 1.3 format (the Emscripten SDL builtin that PICO-8 uses).
Diffstat (limited to 'src/input.c')
-rw-r--r--src/input.c248
1 files changed, 248 insertions, 0 deletions
diff --git a/src/input.c b/src/input.c
new file mode 100644
index 0000000..70eab8b
--- /dev/null
+++ b/src/input.c
@@ -0,0 +1,248 @@
+#include <SDL3/SDL_log.h>
+
+#include "input.h"
+
+bool pw_controllers_init(struct pw_controllers *controllers) {
+ if (!controllers) {
+ SDL_InvalidParamError("controllers");
+ return false;
+ }
+
+ for (size_t i = 0; i < PW_PLAYERS_MAX; ++i) {
+ controllers->players[i].gamepad = NULL;
+ controllers->players[i].button = PW_BUTTON_NONE;
+ }
+ return true;
+}
+
+void pw_controllers_destroy(struct pw_controllers *controllers) {
+ if (!controllers) {
+ SDL_InvalidParamError("controllers");
+ return;
+ }
+
+ for (size_t i = 0; i < PW_PLAYERS_MAX; ++i) {
+ SDL_Gamepad *gamepad = controllers->players[i].gamepad;
+ if (gamepad) {
+ SDL_CloseGamepad(gamepad);
+ }
+ }
+}
+
+static bool add_gamepad(struct pw_controllers *controllers,
+ SDL_GamepadDeviceEvent *event) {
+ size_t i;
+ for (i = 0; i < PW_PLAYERS_MAX; ++i) {
+ if (controllers->players[i].gamepad == NULL) {
+ break;
+ }
+ }
+
+ if (i >= PW_PLAYERS_MAX) {
+ return true;
+ }
+
+ SDL_Gamepad *gamepad = SDL_OpenGamepad(event->which);
+ if (gamepad == NULL) {
+ return false;
+ }
+
+ SDL_LogDebug(0, "connected gamepad for player %zu", i + 1);
+ controllers->players[i].gamepad = gamepad;
+ controllers->players[i].button = PW_BUTTON_NONE;
+
+ return true;
+}
+
+static struct pw_controller *get_controller(struct pw_controllers *controllers,
+ SDL_JoystickID id, size_t *player) {
+ for (size_t i = 0; i < PW_PLAYERS_MAX; i++) {
+ if (SDL_GetGamepadID(controllers->players[i].gamepad) == id) {
+ if (player) {
+ *player = i;
+ }
+ return &controllers->players[i];
+ }
+ }
+ return NULL;
+}
+
+static bool remove_gamepad(struct pw_controllers *controllers,
+ SDL_GamepadDeviceEvent *event) {
+ size_t player;
+ struct pw_controller *controller =
+ get_controller(controllers, event->which, &player);
+ if (!controller) {
+ SDL_SetError("unknown gamepad");
+ return false;
+ }
+
+ SDL_CloseGamepad(controller->gamepad);
+
+ SDL_LogDebug(0, "disconnected gamepad for player %zu", player + 1);
+ controller->gamepad = NULL;
+ controller->button = PW_BUTTON_NONE;
+
+ return true;
+}
+
+#define AXIS_THRESHOLD 10000
+#define TRIGGER_THRESHOLD 16000
+
+static void set_button(struct pw_controller *controller, enum pw_button button,
+ bool pressed) {
+ if (pressed) {
+ controller->button |= button;
+ } else {
+ controller->button &= ~button;
+ }
+}
+
+static bool handle_axis(struct pw_controllers *controllers,
+ SDL_GamepadAxisEvent *event) {
+ size_t player;
+ struct pw_controller *controller =
+ get_controller(controllers, event->which, &player);
+ if (!controller) {
+ SDL_SetError("unknown gamepad");
+ return false;
+ }
+
+ switch (event->axis) {
+ case SDL_GAMEPAD_AXIS_LEFTX:
+ set_button(controller, PW_BUTTON_LEFT, event->value < -AXIS_THRESHOLD);
+ set_button(controller, PW_BUTTON_RIGHT, event->value > AXIS_THRESHOLD);
+ break;
+ case SDL_GAMEPAD_AXIS_LEFTY:
+ set_button(controller, PW_BUTTON_UP, event->value < -AXIS_THRESHOLD);
+ set_button(controller, PW_BUTTON_DOWN, event->value > AXIS_THRESHOLD);
+ break;
+ }
+
+ // Some special POOM handling, which uses both player's inputs for controls.
+ // This allows one player's directions be used for turning, and another for
+ // strafing.
+ if (!player && !controllers->players[1].gamepad) {
+ struct pw_controller *p2 = &controllers->players[1];
+ switch (event->axis) {
+ case SDL_GAMEPAD_AXIS_RIGHTX:
+ set_button(p2, PW_BUTTON_LEFT, event->value < -AXIS_THRESHOLD);
+ set_button(p2, PW_BUTTON_RIGHT, event->value > AXIS_THRESHOLD);
+ break;
+ case SDL_GAMEPAD_AXIS_RIGHTY:
+ set_button(p2, PW_BUTTON_UP, event->value < -AXIS_THRESHOLD);
+ set_button(p2, PW_BUTTON_DOWN, event->value > AXIS_THRESHOLD);
+ break;
+ case SDL_GAMEPAD_AXIS_LEFT_TRIGGER:
+ set_button(p2, PW_BUTTON_O, event->value > TRIGGER_THRESHOLD);
+ break;
+ case SDL_GAMEPAD_AXIS_RIGHT_TRIGGER:
+ set_button(p2, PW_BUTTON_X, event->value > TRIGGER_THRESHOLD);
+ break;
+ }
+ }
+
+ return true;
+}
+
+static bool handle_button(struct pw_controllers *controllers,
+ SDL_GamepadButtonEvent *event) {
+ size_t player;
+ struct pw_controller *controller =
+ get_controller(controllers, event->which, &player);
+ if (!controller) {
+ SDL_SetError("unknown gamepad");
+ return false;
+ }
+
+ switch (event->button) {
+ case SDL_GAMEPAD_BUTTON_SOUTH:
+ set_button(controller, PW_BUTTON_O, event->down);
+ break;
+ case SDL_GAMEPAD_BUTTON_EAST:
+ set_button(controller, PW_BUTTON_X, event->down);
+ break;
+ case SDL_GAMEPAD_BUTTON_WEST:
+ set_button(controller, PW_BUTTON_X, event->down);
+ break;
+ case SDL_GAMEPAD_BUTTON_NORTH:
+ set_button(controller, PW_BUTTON_O, event->down);
+ break;
+ case SDL_GAMEPAD_BUTTON_BACK:
+ set_button(controller, PW_BUTTON_MENU, event->down);
+ break;
+ case SDL_GAMEPAD_BUTTON_START:
+ set_button(controller, PW_BUTTON_MENU, event->down);
+ break;
+ case SDL_GAMEPAD_BUTTON_LEFT_SHOULDER:
+ set_button(controller, PW_BUTTON_O, event->down);
+ break;
+ case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER:
+ set_button(controller, PW_BUTTON_X, event->down);
+ break;
+ case SDL_GAMEPAD_BUTTON_DPAD_UP:
+ set_button(controller, PW_BUTTON_UP, event->down);
+ break;
+ case SDL_GAMEPAD_BUTTON_DPAD_DOWN:
+ set_button(controller, PW_BUTTON_DOWN, event->down);
+ break;
+ case SDL_GAMEPAD_BUTTON_DPAD_LEFT:
+ set_button(controller, PW_BUTTON_LEFT, event->down);
+ break;
+ case SDL_GAMEPAD_BUTTON_DPAD_RIGHT:
+ set_button(controller, PW_BUTTON_RIGHT, event->down);
+ break;
+ }
+
+ return true;
+}
+
+bool pw_controllers_update(struct pw_controllers *controllers,
+ SDL_Event *event) {
+ if (!controllers) {
+ SDL_InvalidParamError("controllers");
+ return false;
+ }
+ if (!event) {
+ SDL_InvalidParamError("event");
+ return false;
+ }
+
+ switch (event->type) {
+ case SDL_EVENT_GAMEPAD_ADDED:
+ return add_gamepad(controllers, &event->gdevice);
+ case SDL_EVENT_GAMEPAD_REMOVED:
+ return remove_gamepad(controllers, &event->gdevice);
+ case SDL_EVENT_GAMEPAD_AXIS_MOTION:
+ return handle_axis(controllers, &event->gaxis);
+ case SDL_EVENT_GAMEPAD_BUTTON_UP:
+ case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
+ return handle_button(controllers, &event->gbutton);
+ default:
+ return true;
+ }
+}
+
+uint8_t pw_controller_button(struct pw_controllers *controllers,
+ size_t player) {
+ if (!controllers) {
+ SDL_InvalidParamError("controllers");
+ return PW_BUTTON_NONE;
+ }
+ if (player >= PW_PLAYERS_MAX) {
+ SDL_InvalidParamError("player");
+ return PW_BUTTON_NONE;
+ }
+
+ enum pw_button button = controllers->players[player].button;
+
+ // PICO-8 only checks the menu state for player 1, so for that player, the
+ // menu needs to be set if it is set for any player.
+ if (!player) {
+ for (size_t i = 1; i < PW_PLAYERS_MAX; i++) {
+ button |= controllers->players[player].button & PW_BUTTON_MENU;
+ }
+ }
+
+ return (uint8_t)(button & PW_BUTTON_ALL);
+}