diff options
| -rw-r--r-- | platform/sdl/main.c | 248 |
1 files changed, 169 insertions, 79 deletions
diff --git a/platform/sdl/main.c b/platform/sdl/main.c index 23a0aa8..f2b6d5d 100644 --- a/platform/sdl/main.c +++ b/platform/sdl/main.c @@ -1,22 +1,10 @@ -#include <SDL3/SDL.h> #include <unistd.h> -#include "picow.h" - #define SDL_MAIN_USE_CALLBACKS +#include <SDL3/SDL.h> #include <SDL3/SDL_main.h> -struct appstate { - size_t ncarts; - struct pw_cart *carts; - - SDL_Window *window; - SDL_Renderer *renderer; - SDL_Texture *fb; - SDL_FRect target; - - SDL_Gamepad *gamepads[8]; -}; +#include "picow.h" static void usage(char const *name) { // clang-format off @@ -31,6 +19,33 @@ static void usage(char const *name) { // clang-format on } +struct appstate { + size_t ncarts; + struct pw_cart *carts; + + SDL_Window *window; + SDL_Renderer *renderer; + SDL_Texture *fb; + SDL_FRect target; + + 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; + } gamepads[8]; +}; + #define FAIL(msg) \ (SDL_LogCritical(0, msg ". %s", SDL_GetError()), SDL_APP_FAILURE) #define FAILF(msg, ...) \ @@ -107,6 +122,138 @@ cleanup: return result; } +static SDL_AppResult update_target_rect(struct appstate *state, int w, int h) { + float ww = (float)w, wh = (float)h; + float fbw, fbh; + if (!SDL_GetTextureSize(state->fb, &fbw, &fbh)) { + return FAIL("failed to lock framebuffer"); + } + + float scale = SDL_floor(SDL_min(ww / fbw, wh / fbh)); + state->target.w = scale * fbw; + state->target.h = scale * fbh; + + state->target.x = SDL_floor((ww - state->target.w) / 2.0); + state->target.y = SDL_floor((wh - state->target.h) / 2.0); + + return SDL_APP_CONTINUE; +} + +static SDL_AppResult add_gamepad(struct 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].device == NULL) { + break; + } + } + + if (i >= ngamepads) { + return SDL_APP_CONTINUE; + } + + SDL_Gamepad *gamepad = SDL_OpenGamepad(event->which); + if (gamepad == NULL) { + return FAIL("failed to open gamepad"); + } + + state->gamepads[i].device = gamepad; + return SDL_APP_CONTINUE; +} + +static struct gamepad *find_gamepad(struct 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].device) == id) { + return &state->gamepads[i]; + } + } + 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); + if (gamepad != NULL) { + SDL_CloseGamepad(gamepad->device); + gamepad->device = NULL; + gamepad->buttons.raw = 0; + } + return SDL_APP_CONTINUE; +} + +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); + + return SDL_APP_CONTINUE; +} + +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); + + 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; + union pw_pixel px = {.ch = {r, g, 0xff, 0xff}}; + for (int x = 0; x < 128; x++) { + for (int y = 0; y < 128; y++) { + fb[x + y * 128] = px.raw; + } + } +} + SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[argc + 1]) { struct appstate *state = SDL_calloc(1, sizeof *state); *appstate = state; @@ -171,18 +318,12 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[argc + 1]) { SDL_AppResult SDL_AppIterate(void *appstate) { struct appstate *state = appstate; - void *pixels; + void *fb; int pitch; - if (!SDL_LockTexture(state->fb, NULL, &pixels, &pitch)) { + if (!SDL_LockTexture(state->fb, NULL, &fb, &pitch)) { return FAIL("failed to lock framebuffer"); } - uint8_t ch = (uint8_t)((SDL_GetTicks() / 10) % 0xff); - union pw_pixel px = {.ch = {ch, ch, ch, 0xff}}; - for (int x = 0; x < 128; x++) { - for (int y = 0; y < 128; y++) { - ((uint32_t *)pixels)[x + y * 128] = px.raw; - } - } + blit_framebuffer(state, (uint32_t *)fb); SDL_UnlockTexture(state->fb); SDL_RenderClear(state->renderer); @@ -192,59 +333,6 @@ SDL_AppResult SDL_AppIterate(void *appstate) { return SDL_APP_CONTINUE; } -static SDL_AppResult update_target_rect(struct appstate *state, int w, int h) { - float ww = (float)w, wh = (float)h; - float fbw, fbh; - if (!SDL_GetTextureSize(state->fb, &fbw, &fbh)) { - return FAIL("failed to lock framebuffer"); - } - - float scale = SDL_floor(SDL_min(ww / fbw, wh / fbh)); - state->target.w = scale * fbw; - state->target.h = scale * fbh; - - state->target.x = SDL_floor((ww - state->target.w) / 2.0); - state->target.y = SDL_floor((wh - state->target.h) / 2.0); - - return SDL_APP_CONTINUE; -} - -static SDL_AppResult add_gamepad(struct 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] == NULL) { - break; - } - } - - if (i >= ngamepads) { - return SDL_APP_CONTINUE; - } - - SDL_Gamepad *gamepad = SDL_OpenGamepad(event->which); - if (gamepad == NULL) { - return FAIL("failed to open gamepad"); - } - - state->gamepads[i] = gamepad; - return SDL_APP_CONTINUE; -} - -static SDL_AppResult remove_gamepad(struct 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]) == event->which) { - SDL_CloseGamepad(state->gamepads[i]); - state->gamepads[i] = NULL; - break; - } - } - return SDL_APP_CONTINUE; -} - SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { struct appstate *state = appstate; @@ -258,10 +346,12 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { case SDL_EVENT_GAMEPAD_REMOVED: return remove_gamepad(state, &event->gdevice); case SDL_EVENT_KEY_DOWN: + case SDL_EVENT_KEY_UP: switch (event->key.key) { - case SDLK_ESCAPE: - case SDLK_Q: + case SDLK_BACKSPACE: return SDL_APP_SUCCESS; + default: + return handle_keyboard(state, &event->key); } } return SDL_APP_CONTINUE; @@ -271,7 +361,7 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result) { struct appstate *state = appstate; for (size_t i = 0; i < SDL_arraysize(state->gamepads); i++) { - SDL_CloseGamepad(state->gamepads[i]); + SDL_CloseGamepad(state->gamepads[i].device); } SDL_DestroyTexture(state->fb); SDL_DestroyRenderer(state->renderer); |
