aboutsummaryrefslogtreecommitdiff
path: root/src/input.c
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-10-09 16:57:26 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-10-09 16:57:26 +0200
commit0a98b48593e4ad561c7732387a688797ad5ee1ce (patch)
tree39d2d83a7a0c0b7307725286beafeb399350078a /src/input.c
parentc62d8a1a22379a04e09623b60f8bd88a3e042040 (diff)
Player and Framebuffer Management
We now handle player controller connection/disconnection in a separate component (since, for scene selection we actually don't want 1:1 PICO-8 input semantics). We also added a new `pw_framebuffer` type for managing the framebuffer that can be written to by PICO-8. The target rect is updated every frame, and we provide utilities for mapping window coordinates to clamped framebuffer coordinates (used for the mouse). Next up is actually implementing the scenes in their separate file, at which point we can move the FB logic into our stub PICO-8 WAT implementation and wireup the events correctly!
Diffstat (limited to 'src/input.c')
-rw-r--r--src/input.c348
1 files changed, 213 insertions, 135 deletions
diff --git a/src/input.c b/src/input.c
index 70eab8b..46c8491 100644
--- a/src/input.c
+++ b/src/input.c
@@ -1,143 +1,74 @@
#include <SDL3/SDL_log.h>
+#include "fb.h"
#include "input.h"
-bool pw_controllers_init(struct pw_controllers *controllers) {
- if (!controllers) {
- SDL_InvalidParamError("controllers");
+struct pw_input *pw_input_init(void) {
+ struct pw_input *input = SDL_malloc(sizeof(*input));
+ if (!input) {
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;
+ SDL_zerop(input);
+ return input;
}
-void pw_controllers_destroy(struct pw_controllers *controllers) {
- if (!controllers) {
- SDL_InvalidParamError("controllers");
+void pw_input_free(struct pw_input *input) {
+ if (!input) {
+ SDL_InvalidParamError("input");
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;
+ SDL_free(input);
}
-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,
+static void set_button(enum pw_button *player, enum pw_button button,
bool pressed) {
if (pressed) {
- controller->button |= button;
+ *player |= button;
} else {
- controller->button &= ~button;
+ *player &= ~button;
}
}
-static bool handle_axis(struct pw_controllers *controllers,
+static bool handle_axis(struct pw_input *input, struct pw_players *players,
SDL_GamepadAxisEvent *event) {
- size_t player;
- struct pw_controller *controller =
- get_controller(controllers, event->which, &player);
- if (!controller) {
- SDL_SetError("unknown gamepad");
- return false;
+ size_t i = pw_player_find(players, event->which);
+ if (i >= PW_PLAYERS_MAX) {
+ return true;
}
+ enum pw_button *player = &input->players[i];
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);
+ set_button(player, PW_BUTTON_LEFT, event->value < -PW_AXIS_THRESHOLD);
+ set_button(player, PW_BUTTON_RIGHT, event->value > PW_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);
+ set_button(player, PW_BUTTON_UP, event->value < -PW_AXIS_THRESHOLD);
+ set_button(player, PW_BUTTON_DOWN, event->value > PW_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];
+ if (!i && pw_players_count(players) == 1) {
+ enum pw_button *p2 = &input->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);
+ set_button(p2, PW_BUTTON_LEFT, event->value < -PW_AXIS_THRESHOLD);
+ set_button(p2, PW_BUTTON_RIGHT, event->value > PW_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);
+ set_button(p2, PW_BUTTON_UP, event->value < -PW_AXIS_THRESHOLD);
+ set_button(p2, PW_BUTTON_DOWN, event->value > PW_AXIS_THRESHOLD);
break;
case SDL_GAMEPAD_AXIS_LEFT_TRIGGER:
- set_button(p2, PW_BUTTON_O, event->value > TRIGGER_THRESHOLD);
+ set_button(p2, PW_BUTTON_O, event->value > PW_TRIGGER_THRESHOLD);
break;
case SDL_GAMEPAD_AXIS_RIGHT_TRIGGER:
- set_button(p2, PW_BUTTON_X, event->value > TRIGGER_THRESHOLD);
+ set_button(p2, PW_BUTTON_X, event->value > PW_TRIGGER_THRESHOLD);
break;
}
}
@@ -145,62 +76,127 @@ static bool handle_axis(struct pw_controllers *controllers,
return true;
}
-static bool handle_button(struct pw_controllers *controllers,
+static bool handle_button(struct pw_input *input, struct pw_players *players,
SDL_GamepadButtonEvent *event) {
- size_t player;
- struct pw_controller *controller =
- get_controller(controllers, event->which, &player);
- if (!controller) {
- SDL_SetError("unknown gamepad");
- return false;
+ size_t i = pw_player_find(players, event->which);
+ if (i >= PW_PLAYERS_MAX) {
+ return true;
}
+ enum pw_button *player = &input->players[i];
switch (event->button) {
case SDL_GAMEPAD_BUTTON_SOUTH:
- set_button(controller, PW_BUTTON_O, event->down);
+ set_button(player, PW_BUTTON_O, event->down);
break;
case SDL_GAMEPAD_BUTTON_EAST:
- set_button(controller, PW_BUTTON_X, event->down);
+ set_button(player, PW_BUTTON_X, event->down);
break;
case SDL_GAMEPAD_BUTTON_WEST:
- set_button(controller, PW_BUTTON_X, event->down);
+ set_button(player, PW_BUTTON_X, event->down);
break;
case SDL_GAMEPAD_BUTTON_NORTH:
- set_button(controller, PW_BUTTON_O, event->down);
+ set_button(player, PW_BUTTON_O, event->down);
break;
case SDL_GAMEPAD_BUTTON_BACK:
- set_button(controller, PW_BUTTON_MENU, event->down);
+ set_button(player, PW_BUTTON_MENU, event->down);
break;
case SDL_GAMEPAD_BUTTON_START:
- set_button(controller, PW_BUTTON_MENU, event->down);
+ set_button(player, PW_BUTTON_MENU, event->down);
break;
case SDL_GAMEPAD_BUTTON_LEFT_SHOULDER:
- set_button(controller, PW_BUTTON_O, event->down);
+ set_button(player, PW_BUTTON_O, event->down);
break;
case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER:
- set_button(controller, PW_BUTTON_X, event->down);
+ set_button(player, PW_BUTTON_X, event->down);
break;
case SDL_GAMEPAD_BUTTON_DPAD_UP:
- set_button(controller, PW_BUTTON_UP, event->down);
+ set_button(player, PW_BUTTON_UP, event->down);
break;
case SDL_GAMEPAD_BUTTON_DPAD_DOWN:
- set_button(controller, PW_BUTTON_DOWN, event->down);
+ set_button(player, PW_BUTTON_DOWN, event->down);
break;
case SDL_GAMEPAD_BUTTON_DPAD_LEFT:
- set_button(controller, PW_BUTTON_LEFT, event->down);
+ set_button(player, PW_BUTTON_LEFT, event->down);
break;
case SDL_GAMEPAD_BUTTON_DPAD_RIGHT:
- set_button(controller, PW_BUTTON_RIGHT, event->down);
+ set_button(player, PW_BUTTON_RIGHT, event->down);
+ break;
+ }
+
+ return true;
+}
+
+static bool enqueue_item(struct pw_keyboard_queue *queue, int16_t item) {
+ size_t next = (queue->end + 1) % PW_KEYBOARD_BUFFER_SIZE;
+ if (next == queue->start) {
+ return false;
+ }
+
+ queue->items[queue->end] = item;
+ queue->end = next;
+ return true;
+}
+
+static int8_t scancode_to_p8scii(SDL_Scancode sc, SDL_Keymod mod) {
+ switch (sc) {
+ case SDL_SCANCODE_TAB:
+ return 9;
+ case SDL_SCANCODE_RETURN:
+ case SDL_SCANCODE_KP_ENTER:
+ return 13;
+ case SDL_SCANCODE_BACKSPACE:
+ return 8;
+ case SDL_SCANCODE_ESCAPE:
+ return 27;
+ default:
break;
}
+ // Clear other modifiers, PICO-8 requests things like Ctrl and Alt through a
+ // separate `pw_input_keymod` call.
+ mod &= SDL_KMOD_SHIFT | SDL_KMOD_CAPS;
+
+ SDL_Keycode keycode = SDL_GetKeyFromScancode(sc, mod, false);
+ if (keycode < 32 || keycode > 126) {
+ // Non-ASCII characters (including the exceptions above) are mapped to
+ // negative values which are ignored. Note that we _still_ need to push
+ // these to the queue, as they trigger some generic "key pressed" code
+ // in the PICO-8 runtime.
+ return -1;
+ }
+
+ return (int8_t)keycode;
+}
+
+static bool handle_key(struct pw_input *input, SDL_KeyboardEvent *event) {
+ if (event->scancode < 0 || event->scancode > INT16_MAX) {
+ return false;
+ }
+
+ int16_t scancode = (int16_t)event->scancode;
+ if (!event->down) {
+ scancode = -scancode;
+ }
+ if (!enqueue_item(&input->keyboard.scancodes, scancode)) {
+ SDL_LogDebug(0, "keyboard event queue full, dropping scancode");
+ }
+
+ int16_t key = (int16_t)scancode_to_p8scii(event->scancode, event->mod);
+ if (!enqueue_item(&input->keyboard.text, key)) {
+ SDL_LogDebug(0, "keyboard text queue full, dropping key");
+ }
+
return true;
}
-bool pw_controllers_update(struct pw_controllers *controllers,
- SDL_Event *event) {
- if (!controllers) {
- SDL_InvalidParamError("controllers");
+bool pw_input_event(struct pw_input *input, struct pw_players *players,
+ SDL_Event *event) {
+ if (!input) {
+ SDL_InvalidParamError("input");
+ return false;
+ }
+ if (!players) {
+ SDL_InvalidParamError("input");
return false;
}
if (!event) {
@@ -209,24 +205,88 @@ bool pw_controllers_update(struct pw_controllers *controllers,
}
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);
+ return handle_axis(input, players, &event->gaxis);
case SDL_EVENT_GAMEPAD_BUTTON_UP:
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
- return handle_button(controllers, &event->gbutton);
+ return handle_button(input, players, &event->gbutton);
+ case SDL_EVENT_KEY_DOWN:
+ case SDL_EVENT_KEY_UP:
+ return handle_key(input, &event->key);
default:
return true;
}
}
-uint8_t pw_controller_button(struct pw_controllers *controllers,
- size_t player) {
- if (!controllers) {
- SDL_InvalidParamError("controllers");
+static void update_keymod(struct pw_input *input) {
+ input->keyboard.mod = PW_KEYMOD_NONE;
+
+ SDL_Keymod state = SDL_GetModState();
+ if (state & SDL_KMOD_LSHIFT) {
+ input->keyboard.mod |= PW_KEYMOD_LSHIFT;
+ }
+ if (state & SDL_KMOD_RSHIFT) {
+ input->keyboard.mod |= PW_KEYMOD_LSHIFT;
+ }
+ if (state & SDL_KMOD_LCTRL) {
+ input->keyboard.mod |= PW_KEYMOD_LSHIFT;
+ }
+ if (state & SDL_KMOD_RCTRL) {
+ input->keyboard.mod |= PW_KEYMOD_LSHIFT;
+ }
+ if (state & SDL_KMOD_LALT) {
+ input->keyboard.mod |= PW_KEYMOD_LSHIFT;
+ }
+ if (state & SDL_KMOD_RALT) {
+ input->keyboard.mod |= PW_KEYMOD_LSHIFT;
+ }
+}
+
+static bool update_mouse(struct pw_input *input, struct pw_framebuffer *fb) {
+ float x, y;
+ SDL_MouseButtonFlags flags = SDL_GetMouseState(&x, &y);
+
+ struct pw_coordinates new;
+ if (!pw_fb_coordinates(fb, x, y, &new)) {
+ return false;
+ }
+
+ input->mouse.button = PW_MOUSE_BUTTON_NONE;
+ if (flags & SDL_BUTTON_LMASK) {
+ input->mouse.button |= PW_MOUSE_BUTTON_LEFT;
+ }
+ if (flags & SDL_BUTTON_MMASK) {
+ input->mouse.button |= PW_MOUSE_BUTTON_MIDDLE;
+ }
+ if (flags & SDL_BUTTON_RMASK) {
+ input->mouse.button |= PW_MOUSE_BUTTON_RIGHT;
+ }
+
+ input->mouse.xrel = (int32_t)new.x - (int32_t)input->mouse.pos.x;
+ input->mouse.yrel = (int32_t)new.y - (int32_t)input->mouse.pos.y;
+ input->mouse.pos.x = new.x;
+ input->mouse.pos.y = new.y;
+
+ return true;
+}
+
+bool pw_input_iterate(struct pw_input *input, struct pw_framebuffer *fb) {
+ if (!input) {
+ SDL_InvalidParamError("input");
+ return false;
+ }
+ if (!fb) {
+ SDL_InvalidParamError("fb");
+ return false;
+ }
+
+ update_keymod(input);
+ return update_mouse(input, fb);
+}
+
+enum pw_button pw_input_btn(struct pw_input *input, size_t player) {
+ if (!input) {
+ SDL_InvalidParamError("input");
return PW_BUTTON_NONE;
}
if (player >= PW_PLAYERS_MAX) {
@@ -234,15 +294,33 @@ uint8_t pw_controller_button(struct pw_controllers *controllers,
return PW_BUTTON_NONE;
}
- enum pw_button button = controllers->players[player].button;
+ enum pw_button button = input->players[player];
// 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;
+ button |= input->players[player] & PW_BUTTON_MENU;
}
}
- return (uint8_t)(button & PW_BUTTON_ALL);
+ return button & PW_BUTTON_ALL;
+}
+
+enum pw_keymod pw_input_keymod(struct pw_input *input) {
+ if (!input) {
+ SDL_InvalidParamError("input");
+ return PW_KEYMOD_NONE;
+ }
+
+ return input->keyboard.mod;
+}
+
+enum pw_mouse_button pw_input_mouse(struct pw_input *input) {
+ if (!input) {
+ SDL_InvalidParamError("input");
+ return PW_MOUSE_BUTTON_NONE;
+ }
+
+ return input->mouse.button;
}