diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-09 09:29:28 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-09 10:11:21 +0200 |
| commit | c62d8a1a22379a04e09623b60f8bd88a3e042040 (patch) | |
| tree | 2cfaaff0092b261f524616c507895963e95dabec /src | |
| parent | 36213f7dfd59ab0f3e99b88e14b653723ca37d64 (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')
| -rw-r--r-- | src/input.c | 248 | ||||
| -rw-r--r-- | src/input.h | 61 | ||||
| -rw-r--r-- | src/picow.c | 362 |
3 files changed, 400 insertions, 271 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); +} diff --git a/src/input.h b/src/input.h new file mode 100644 index 0000000..996daf9 --- /dev/null +++ b/src/input.h @@ -0,0 +1,61 @@ +#ifndef PW_INPUT_H +#define PW_INPUT_H + +#include <SDL3/SDL_events.h> +#include <SDL3/SDL_gamepad.h> + +#define PW_PLAYERS_MAX 8 + +/** + * PICO-8 controller buttons. + */ +enum pw_button { + PW_BUTTON_NONE = 0, + PW_BUTTON_LEFT = 1 << 0, + PW_BUTTON_RIGHT = 1 << 1, + PW_BUTTON_UP = 1 << 2, + PW_BUTTON_DOWN = 1 << 3, + PW_BUTTON_O = 1 << 4, + PW_BUTTON_X = 1 << 5, + PW_BUTTON_MENU = 1 << 6, + PW_BUTTON_ALL = 0x7f, +}; + +/** + * A PICO-8 controller. + */ +struct pw_controller { + SDL_Gamepad *gamepad; + enum pw_button button; +}; + +/** + * PICO-8 controllers. + */ +struct pw_controllers { + struct pw_controller players[PW_PLAYERS_MAX]; +}; + +/** + * Initializes the controller state. + */ +bool pw_controllers_init(struct pw_controllers *controllers); + +/** + * Cleanup controller gamepad handles. + */ +void pw_controllers_destroy(struct pw_controllers *controllers); + +/** + * Handles an SDL gamepad events for controller input. Converts the SDL event + * into a PICO-w controller update. + */ +bool pw_controllers_update(struct pw_controllers *controllers, + SDL_Event *event); + +/** + * Gets the button state for a player. + */ +uint8_t pw_controller_button(struct pw_controllers *controllers, size_t player); + +#endif diff --git a/src/picow.c b/src/picow.c index 9faa524..a2ad5c0 100644 --- a/src/picow.c +++ b/src/picow.c @@ -1,3 +1,4 @@ +#include "SDL3/SDL_init.h" #include <unistd.h> #define SDL_MAIN_USE_CALLBACKS @@ -7,6 +8,7 @@ #include <wamr/wamr.h> #include "cart.h" +#include "input.h" #include "path.h" #include "sysroot.h" @@ -42,33 +44,6 @@ static SDL_AppResult failf(char const *fmt, ...) { return SDL_APP_FAILURE; } -enum pw_button { - PW_BUTTON_LEFT, - PW_BUTTON_RIGHT, - PW_BUTTON_UP, - PW_BUTTON_DOWN, - PW_BUTTON_O, - PW_BUTTON_X, - PW_BUTTON_MENU, -}; - -struct pw_gamepad { - SDL_Gamepad *handle; - union pw_gamepad_state { - uint8_t raw; - struct pw_gamepad_buttons { - bool left : 1; - bool right : 1; - bool up : 1; - bool down : 1; - bool o : 1; - bool x : 1; - bool menu : 1; - bool reserved : 1; - } b; - } state; -}; - enum pw_scene { PW_SCENE_CART_SELECTION, PW_SCENE_GAME, @@ -79,7 +54,7 @@ struct pw_appstate { SDL_Renderer *renderer; SDL_Texture *framebuffer; SDL_FRect target; - struct pw_gamepad gamepads[8]; + struct pw_controllers controllers; struct pw_carts *carts; struct pw_cart *cart; ssize_t cartidx; @@ -123,58 +98,6 @@ static bool update_pxtexture_target(SDL_Texture *texture, SDL_FRect *target, return true; } -static void update_gamepad_state(struct pw_appstate *state, - union pw_gamepad_state *gamepad, - enum pw_button button, bool pressed) { - switch (button) { - case PW_BUTTON_LEFT: - gamepad->b.left = pressed; - break; - case PW_BUTTON_RIGHT: - gamepad->b.right = pressed; - break; - case PW_BUTTON_UP: - gamepad->b.up = pressed; - break; - case PW_BUTTON_DOWN: - gamepad->b.down = pressed; - break; - case PW_BUTTON_O: - gamepad->b.o = pressed; - break; - case PW_BUTTON_X: - gamepad->b.x = pressed; - break; - case PW_BUTTON_MENU: - gamepad->b.menu = pressed; - break; - } - - ssize_t ncarts = (ssize_t)state->carts->count; - if (state->scene == PW_SCENE_CART_SELECTION && ncarts > 0 && pressed && - gamepad == &state->gamepads[0].state) { - switch (button) { - case PW_BUTTON_LEFT: - state->cartidx2 = SDL_max(0, state->cartidx2 - 1); - break; - case PW_BUTTON_RIGHT: - state->cartidx2 = SDL_min(ncarts - 1, state->cartidx2 + 1); - break; - case PW_BUTTON_UP: - state->cartidx2 = SDL_max(0, state->cartidx2 - 10); - break; - case PW_BUTTON_DOWN: - state->cartidx2 = SDL_min(ncarts - 1, state->cartidx2 + 10); - break; - case PW_BUTTON_O: - case PW_BUTTON_X: - case PW_BUTTON_MENU: - state->scene2 = PW_SCENE_GAME; - break; - } - } -} - static SDL_AppResult update_target_rect(struct pw_appstate *state, int w, int h) { SDL_Texture *texture; @@ -196,210 +119,54 @@ static SDL_AppResult update_target_rect(struct pw_appstate *state, int w, return SDL_APP_CONTINUE; } -static SDL_AppResult add_gamepad(struct pw_appstate *state, - SDL_GamepadDeviceEvent *event) { - size_t ngamepads = SDL_arraysize(state->gamepads); - - size_t i; - for (i = 0; i < ngamepads; i++) { - if (state->gamepads[i].handle == NULL) { - break; +static void blit_framebuffer(struct pw_appstate *state, uint32_t *fb) { + uint32_t r = (uint32_t)pw_controller_button(&state->controllers, 0) << 1; + uint32_t g = (uint32_t)pw_controller_button(&state->controllers, 1) << 9; + uint32_t px = r | g | 0xffff0000; + for (int x = 0; x < 128; x++) { + for (int y = 0; y < 128; y++) { + fb[x + y * 128] = px; } } +} - if (i >= ngamepads) { - return SDL_APP_CONTINUE; - } - - SDL_Gamepad *gamepad = SDL_OpenGamepad(event->which); - if (gamepad == NULL) { - return failf("failed to open gamepad"); - } +static void *wasm_malloc(unsigned int size) { return SDL_malloc((size_t)size); } - SDL_LogDebug(0, "connected gamepad for player %zu", i + 1); - state->gamepads[i].handle = gamepad; - return SDL_APP_CONTINUE; +static void *wasm_realloc(void *ptr, unsigned int size) { + return SDL_realloc(ptr, (size_t)size); } -static SDL_AppResult remove_gamepad(struct pw_appstate *state, - SDL_GamepadDeviceEvent *event) { - size_t ngamepads = SDL_arraysize(state->gamepads); - for (size_t i = 0; i < ngamepads; i++) { - if (SDL_GetGamepadID(state->gamepads[i].handle) == event->which) { - SDL_CloseGamepad(state->gamepads[i].handle); - SDL_LogDebug(0, "disconnected gamepad for player %zu", i + 1); - state->gamepads[i].state.raw = 0; - break; - } - } - return SDL_APP_CONTINUE; -} +static void wasm_free(void *ptr) { SDL_free(ptr); } -static union pw_gamepad_state *get_gamepad_state(struct pw_appstate *state, - SDL_JoystickID id) { - size_t ngamepads = SDL_arraysize(state->gamepads); - for (size_t i = 0; i < ngamepads; i++) { - if (SDL_GetGamepadID(state->gamepads[i].handle) == id) { - return &state->gamepads[i].state; - } - } - return NULL; -} +static SDL_AppResult scene_cart_selection_event(struct pw_appstate *state) { + // TODO(nlordell): This is slightly incorrect, we actually want to keep track + // of general button states. Fix once we have a proper scene abstraction. -static SDL_AppResult handle_axis(struct pw_appstate *state, - SDL_GamepadAxisEvent *event) { - union pw_gamepad_state *gamepad = get_gamepad_state(state, event->which); - if (gamepad == NULL) { + ssize_t ncarts = (ssize_t)state->carts->count; + if (ncarts <= 0) { return SDL_APP_CONTINUE; } - bool minus = event->value < -10000; - bool plus = event->value > 10000; - - switch (event->axis) { - case SDL_GAMEPAD_AXIS_LEFTX: - update_gamepad_state(state, gamepad, PW_BUTTON_LEFT, minus); - update_gamepad_state(state, gamepad, PW_BUTTON_RIGHT, plus); - break; - case SDL_GAMEPAD_AXIS_LEFTY: - update_gamepad_state(state, gamepad, PW_BUTTON_UP, minus); - update_gamepad_state(state, gamepad, PW_BUTTON_DOWN, plus); - break; + uint8_t button = pw_controller_button(&state->controllers, 0); + if (button & PW_BUTTON_LEFT) { + state->cartidx2 = SDL_max(0, state->cartidx2 - 1); } - - // We also update the gamepad for player 2, which receives - // keypresses from the player 1's device to make playing games like - // POOM more fun. - if (gamepad == &state->gamepads[0].state && - state->gamepads[1].handle == NULL) { - gamepad++; - switch (event->axis) { - case SDL_GAMEPAD_AXIS_RIGHTX: - update_gamepad_state(state, gamepad, PW_BUTTON_LEFT, minus); - update_gamepad_state(state, gamepad, PW_BUTTON_RIGHT, plus); - break; - case SDL_GAMEPAD_AXIS_RIGHTY: - update_gamepad_state(state, gamepad, PW_BUTTON_UP, minus); - update_gamepad_state(state, gamepad, PW_BUTTON_DOWN, plus); - break; - case SDL_GAMEPAD_AXIS_LEFT_TRIGGER: - update_gamepad_state(state, gamepad, PW_BUTTON_O, plus); - break; - case SDL_GAMEPAD_AXIS_RIGHT_TRIGGER: - update_gamepad_state(state, gamepad, PW_BUTTON_X, plus); - break; - } + if (button & PW_BUTTON_RIGHT) { + state->cartidx2 = SDL_min(ncarts - 1, state->cartidx2 + 1); } - - return SDL_APP_CONTINUE; -} - -static SDL_AppResult handle_button(struct pw_appstate *state, - SDL_GamepadButtonEvent *event) { - union pw_gamepad_state *gamepad = get_gamepad_state(state, event->which); - if (gamepad == NULL) { - return SDL_APP_CONTINUE; + if (button & PW_BUTTON_UP) { + state->cartidx2 = SDL_max(0, state->cartidx2 - 10); } - - switch (event->button) { - case SDL_GAMEPAD_BUTTON_SOUTH: - update_gamepad_state(state, gamepad, PW_BUTTON_O, event->down); - break; - case SDL_GAMEPAD_BUTTON_EAST: - update_gamepad_state(state, gamepad, PW_BUTTON_X, event->down); - break; - case SDL_GAMEPAD_BUTTON_WEST: - update_gamepad_state(state, gamepad, PW_BUTTON_X, event->down); - break; - case SDL_GAMEPAD_BUTTON_NORTH: - update_gamepad_state(state, gamepad, PW_BUTTON_O, event->down); - break; - case SDL_GAMEPAD_BUTTON_BACK: - update_gamepad_state(state, gamepad, PW_BUTTON_MENU, event->down); - break; - case SDL_GAMEPAD_BUTTON_START: - update_gamepad_state(state, gamepad, PW_BUTTON_MENU, event->down); - break; - case SDL_GAMEPAD_BUTTON_LEFT_SHOULDER: - update_gamepad_state(state, gamepad, PW_BUTTON_O, event->down); - break; - case SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER: - update_gamepad_state(state, gamepad, PW_BUTTON_X, event->down); - break; - case SDL_GAMEPAD_BUTTON_DPAD_UP: - update_gamepad_state(state, gamepad, PW_BUTTON_UP, event->down); - break; - case SDL_GAMEPAD_BUTTON_DPAD_DOWN: - update_gamepad_state(state, gamepad, PW_BUTTON_DOWN, event->down); - break; - case SDL_GAMEPAD_BUTTON_DPAD_LEFT: - update_gamepad_state(state, gamepad, PW_BUTTON_LEFT, event->down); - break; - case SDL_GAMEPAD_BUTTON_DPAD_RIGHT: - update_gamepad_state(state, gamepad, PW_BUTTON_RIGHT, event->down); - break; + if (button & PW_BUTTON_DOWN) { + state->cartidx2 = SDL_min(ncarts - 1, state->cartidx2 + 10); } - - return SDL_APP_CONTINUE; -} - -static SDL_AppResult handle_keyboard(struct pw_appstate *state, - SDL_KeyboardEvent *event) { - union pw_gamepad_state *gamepad = &state->gamepads[0].state; - - switch (event->key) { - case SDLK_ESCAPE: - case SDLK_Q: - return SDL_APP_SUCCESS; - case SDLK_Z: - update_gamepad_state(state, gamepad, PW_BUTTON_O, event->down); - break; - case SDLK_X: - update_gamepad_state(state, gamepad, PW_BUTTON_X, event->down); - break; - case SDLK_UP: - case SDLK_K: - update_gamepad_state(state, gamepad, PW_BUTTON_UP, event->down); - break; - case SDLK_DOWN: - case SDLK_J: - update_gamepad_state(state, gamepad, PW_BUTTON_DOWN, event->down); - break; - case SDLK_LEFT: - case SDLK_H: - update_gamepad_state(state, gamepad, PW_BUTTON_LEFT, event->down); - break; - case SDLK_RIGHT: - case SDLK_L: - update_gamepad_state(state, gamepad, PW_BUTTON_RIGHT, event->down); - break; - case SDLK_RETURN: - update_gamepad_state(state, gamepad, PW_BUTTON_MENU, event->down); - break; + if (button & (PW_BUTTON_O | PW_BUTTON_X | PW_BUTTON_MENU)) { + state->scene2 = PW_SCENE_GAME; } return SDL_APP_CONTINUE; } -static void blit_framebuffer(struct pw_appstate *state, uint32_t *fb) { - uint32_t r = (uint32_t)(state->gamepads[0].state.raw << 1); - uint32_t g = (uint32_t)(state->gamepads[1].state.raw << 9); - uint32_t px = r | g | 0xffff0000; - for (int x = 0; x < 128; x++) { - for (int y = 0; y < 128; y++) { - fb[x + y * 128] = px; - } - } -} - -static void *wasm_malloc(unsigned int size) { return SDL_malloc((size_t)size); } - -static void *wasm_realloc(void *ptr, unsigned int size) { - return SDL_realloc(ptr, (size_t)size); -} - -static void wasm_free(void *ptr) { SDL_free(ptr); } - static SDL_AppResult scene_cart_selection(struct pw_appstate *state) { if (state->cartidx != state->cartidx2) { state->cartidx = state->cartidx2; @@ -599,6 +366,10 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { return failf("failed to create framebuffer"); } + if (!pw_controllers_init(&state->controllers)) { + return failf("failed to initialize player controllers"); + } + state->cart = pw_cart_init(state->renderer); if (!state->cart) { return failf("failed to initialize cart"); @@ -681,6 +452,54 @@ SDL_AppResult SDL_AppIterate(void *appstate) { } } +static SDL_AppResult HACK_gamepad_state(struct pw_appstate *state, + enum pw_button button, bool pressed) { + // TODO(nlordell): Just a hack for now before we implement a keyboard + // abstraction and split out the scenes. + + enum pw_button *target = &state->controllers.players[0].button; + if (pressed) { + *target |= button; + } else { + *target &= ~button; + } + + if (state->scene != PW_SCENE_CART_SELECTION) { + return SDL_APP_CONTINUE; + } + + return scene_cart_selection_event(state); +} + +static SDL_AppResult handle_keyboard(struct pw_appstate *state, + SDL_KeyboardEvent *event) { + switch (event->key) { + case SDLK_ESCAPE: + case SDLK_Q: + return SDL_APP_SUCCESS; + case SDLK_Z: + return HACK_gamepad_state(state, PW_BUTTON_O, event->down); + case SDLK_X: + return HACK_gamepad_state(state, PW_BUTTON_X, event->down); + case SDLK_UP: + case SDLK_K: + return HACK_gamepad_state(state, PW_BUTTON_UP, event->down); + case SDLK_DOWN: + case SDLK_J: + return HACK_gamepad_state(state, PW_BUTTON_DOWN, event->down); + case SDLK_LEFT: + case SDLK_H: + return HACK_gamepad_state(state, PW_BUTTON_LEFT, event->down); + case SDLK_RIGHT: + case SDLK_L: + return HACK_gamepad_state(state, PW_BUTTON_RIGHT, event->down); + case SDLK_RETURN: + return HACK_gamepad_state(state, PW_BUTTON_MENU, event->down); + } + + return SDL_APP_CONTINUE; +} + SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { struct pw_appstate *state = appstate; @@ -690,14 +509,17 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { case SDL_EVENT_QUIT: return SDL_APP_SUCCESS; case SDL_EVENT_GAMEPAD_ADDED: - return add_gamepad(state, &event->gdevice); case SDL_EVENT_GAMEPAD_REMOVED: - return remove_gamepad(state, &event->gdevice); case SDL_EVENT_GAMEPAD_AXIS_MOTION: - return handle_axis(state, &event->gaxis); case SDL_EVENT_GAMEPAD_BUTTON_UP: case SDL_EVENT_GAMEPAD_BUTTON_DOWN: - return handle_button(state, &event->gbutton); + if (!pw_controllers_update(&state->controllers, event)) { + return SDL_APP_FAILURE; + }; + if (state->scene == PW_SCENE_CART_SELECTION) { + return scene_cart_selection_event(state); + } + break; case SDL_EVENT_KEY_DOWN: case SDL_EVENT_KEY_UP: return handle_keyboard(state, &event->key); @@ -716,9 +538,7 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result) { wasm_runtime_unload(state->module); wasm_runtime_destroy(); } - for (size_t i = 0; i < SDL_arraysize(state->gamepads); i++) { - SDL_CloseGamepad(state->gamepads[i].handle); - } + pw_controllers_destroy(&state->controllers); pw_carts_free(state->carts); pw_cart_free(state->cart); SDL_DestroyTexture(state->framebuffer); |
