aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2025-05-08 14:33:11 +0200
committerNicholas Rodrigues Lordello <[email protected]>2025-05-08 14:33:11 +0200
commit94191d18cd4e60ab614d77e80dc135d8fea8d9ae (patch)
tree7d62e314f9b8fd3c86366d5a961ca4b604d56235
parent872ddab387bee8076fe2a1eba3d923cee7f69e7f (diff)
Add Gamepad Support
We now have working gamepads, thought the button states should probably not belong in the "app".
-rw-r--r--include/picow.h14
-rw-r--r--platform/sdl/main.c134
2 files changed, 88 insertions, 60 deletions
diff --git a/include/picow.h b/include/picow.h
index cc6286b..d88dd5e 100644
--- a/include/picow.h
+++ b/include/picow.h
@@ -27,4 +27,18 @@ struct pw_png {
bool pw_png_decode(struct pw_png *png, uint8_t const *data, size_t datasize);
void pw_png_load_rom(struct pw_png *png, struct pw_cart *cart);
+union pw_buttons {
+ uint8_t raw;
+ struct {
+ 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;
+};
+
#endif
diff --git a/platform/sdl/main.c b/platform/sdl/main.c
index f2b6d5d..060860c 100644
--- a/platform/sdl/main.c
+++ b/platform/sdl/main.c
@@ -30,19 +30,8 @@ struct appstate {
struct gamepad {
SDL_Gamepad *device;
- union buttons {
- uint8_t raw;
- struct buttonflags {
- 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;
- } buttons;
+ // TODO(nlordell): This should probably be `pw_state`.
+ union pw_buttons buttons;
} gamepads[8];
};
@@ -115,6 +104,7 @@ static bool load_cart(struct pw_cart *cart, char *s) {
SDL_strlcpy(cart->name, name, len);
}
+ SDL_LogDebug(0, "loaded cart '%s' as '%s'", s, cart->name);
result = true;
cleanup:
@@ -159,6 +149,7 @@ static SDL_AppResult add_gamepad(struct appstate *state,
return FAIL("failed to open gamepad");
}
+ SDL_LogDebug(0, "connected gamepad to player %zu", i + 1);
state->gamepads[i].device = gamepad;
return SDL_APP_CONTINUE;
}
@@ -173,14 +164,6 @@ static struct gamepad *find_gamepad(struct appstate *state, SDL_JoystickID id) {
return NULL;
}
-static struct gamepad *get_gamepad2(struct appstate *state,
- struct gamepad *g1) {
- if (g1 == &state->gamepads[0] && state->gamepads[1].device == NULL) {
- return &state->gamepads[1];
- }
- return NULL;
-}
-
static SDL_AppResult remove_gamepad(struct appstate *state,
SDL_GamepadDeviceEvent *event) {
struct gamepad *gamepad = find_gamepad(state, event->which);
@@ -192,60 +175,82 @@ static SDL_AppResult remove_gamepad(struct appstate *state,
return SDL_APP_CONTINUE;
}
-static SDL_AppResult handle_button(struct appstate *state,
- SDL_GamepadButtonEvent *event) {
+static SDL_AppResult handle_axis(struct appstate *state,
+ SDL_GamepadAxisEvent *event) {
struct gamepad *gamepad = find_gamepad(state, event->which);
- struct gamepad *gamepad2 = get_gamepad2(state, gamepad);
+ if (gamepad == NULL) {
+ return SDL_APP_CONTINUE;
+ }
+
+#define MAP_AXIS(sdlaxis, minus, plus) \
+ case sdlaxis: \
+ gamepad->buttons.b.minus = event->value < -10000; \
+ gamepad->buttons.b.plus = event->value > 10000; \
+ break
+
+ switch (event->axis) {
+ MAP_AXIS(SDL_GAMEPAD_AXIS_LEFTX, left, right);
+ MAP_AXIS(SDL_GAMEPAD_AXIS_LEFTY, up, down);
+ }
+
+ // 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->gamepads[1].device == NULL) {
+ gamepad++;
+ switch (event->axis) {
+ MAP_AXIS(SDL_GAMEPAD_AXIS_RIGHTX, left, right);
+ MAP_AXIS(SDL_GAMEPAD_AXIS_RIGHTY, up, down);
+ MAP_AXIS(SDL_GAMEPAD_AXIS_LEFT_TRIGGER, reserved, o);
+ MAP_AXIS(SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, reserved, x);
+ }
+ }
+
+#undef MAP_AXIS
return SDL_APP_CONTINUE;
}
-static SDL_AppResult handle_axis(struct appstate *state,
- SDL_GamepadAxisEvent *event) {
+static SDL_AppResult handle_button(struct appstate *state,
+ SDL_GamepadButtonEvent *event) {
struct gamepad *gamepad = find_gamepad(state, event->which);
- struct gamepad *gamepad2 = get_gamepad2(state, gamepad);
+ if (gamepad == NULL) {
+ return SDL_APP_CONTINUE;
+ }
+
+#define MAP_BUTTON(sdlbtn, gbtn) \
+ case sdlbtn: \
+ gamepad->buttons.b.gbtn = event->down; \
+ break
+
+ switch (event->button) {
+ MAP_BUTTON(SDL_GAMEPAD_BUTTON_SOUTH, o);
+ MAP_BUTTON(SDL_GAMEPAD_BUTTON_EAST, x);
+ MAP_BUTTON(SDL_GAMEPAD_BUTTON_WEST, x);
+ MAP_BUTTON(SDL_GAMEPAD_BUTTON_NORTH, o);
+ MAP_BUTTON(SDL_GAMEPAD_BUTTON_BACK, menu);
+ MAP_BUTTON(SDL_GAMEPAD_BUTTON_START, menu);
+ MAP_BUTTON(SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, o);
+ MAP_BUTTON(SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, x);
+ MAP_BUTTON(SDL_GAMEPAD_BUTTON_DPAD_UP, up);
+ MAP_BUTTON(SDL_GAMEPAD_BUTTON_DPAD_DOWN, down);
+ MAP_BUTTON(SDL_GAMEPAD_BUTTON_DPAD_LEFT, left);
+ MAP_BUTTON(SDL_GAMEPAD_BUTTON_DPAD_RIGHT, right);
+ }
+
+#undef MAP_BUTTON
return SDL_APP_CONTINUE;
}
static SDL_AppResult handle_keyboard(struct appstate *state,
SDL_KeyboardEvent *event) {
-#define MAP_KEY(key, player, button) \
- case key: \
- state->gamepads[player].buttons.b.button = event->down; \
- break
-
- switch (event->key) {
- MAP_KEY(SDLK_LEFT, 0, left);
- MAP_KEY(SDLK_RIGHT, 0, right);
- MAP_KEY(SDLK_UP, 0, up);
- MAP_KEY(SDLK_DOWN, 0, down);
- MAP_KEY(SDLK_Z, 0, o);
- MAP_KEY(SDLK_X, 0, x);
- MAP_KEY(SDLK_C, 0, o);
- MAP_KEY(SDLK_V, 0, x);
- MAP_KEY(SDLK_N, 0, o);
- MAP_KEY(SDLK_M, 0, x);
- MAP_KEY(SDLK_S, 1, left);
- MAP_KEY(SDLK_F, 1, right);
- MAP_KEY(SDLK_E, 1, up);
- MAP_KEY(SDLK_D, 1, down);
- MAP_KEY(SDLK_TAB, 1, o);
- MAP_KEY(SDLK_Q, 1, x);
- case SDLK_ESCAPE:
- if (event->down) {
- state->gamepads[0].buttons.b.menu = true;
- }
- break;
- }
-
-#undef MAP_KEY
return SDL_APP_CONTINUE;
}
static void blit_framebuffer(struct appstate *state, uint32_t *fb) {
uint8_t r = state->gamepads[0].buttons.raw << 1;
- uint8_t g = state->gamepads[1].buttons.raw << 1;
+ uint8_t g = state->gamepads[1].buttons.raw << 2;
union pw_pixel px = {.ch = {r, g, 0xff, 0xff}};
for (int x = 0; x < 128; x++) {
for (int y = 0; y < 128; y++) {
@@ -258,6 +263,10 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[argc + 1]) {
struct appstate *state = SDL_calloc(1, sizeof *state);
*appstate = state;
+ if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD)) {
+ return FAIL("failed to initialize SDL");
+ }
+
char *sysroot = "system";
SDL_LogPriority logp = SDL_GetLogPriority(0);
@@ -296,7 +305,6 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[argc + 1]) {
if (!load_cart(&state->carts[i], s)) {
return FAILF("could not load cart '%s'", s);
}
- SDL_LogDebug(0, "loaded cart '%s' as '%s'", s, state->carts->name);
}
if (!SDL_CreateWindowAndRenderer("PICO-w", 512, 512,
@@ -345,10 +353,16 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
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);
case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP:
switch (event->key.key) {
- case SDLK_BACKSPACE:
+ case SDLK_ESCAPE:
+ case SDLK_Q:
return SDL_APP_SUCCESS;
default:
return handle_keyboard(state, &event->key);