diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-06-21 14:21:32 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-06-21 14:21:32 +0200 |
| commit | a290eaab66eda29da69c9d5f28b8753fb4d7a65e (patch) | |
| tree | 428c280fb4ce7847ceaa9c7f230ae84865d47579 | |
| parent | 25d6188009ce36e0a59edb52b192c0738b4123cc (diff) | |
Just Build It
I had tried to split the PICO-w stuff into a separate library
and have individual platform code, but I will instead embrace the
SDL and just build on top of it.
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | Makefile | 8 | ||||
| -rw-r--r-- | include/picow.h | 49 | ||||
| -rw-r--r-- | platform/sdl/main.c | 366 | ||||
| -rw-r--r-- | src/picow.c | 396 | ||||
| -rw-r--r-- | src/png.c | 2 | ||||
| -rw-r--r-- | src/png.h | 24 |
7 files changed, 423 insertions, 426 deletions
@@ -1,9 +1,9 @@ carts/*.html carts/*.js -carts/*.wasm carts/*.p8.png carts/*.p8.rom -platform/sdl/picow +carts/*.wasm +picow src/sysroot.c system/ target/ @@ -2,18 +2,18 @@ SRC = src/picow.c src/png.c src/sysroot.c OBJ = $(patsubst %.c,%.o,$(SRC)) .PHONY: all -all: platform/sdl/picow ; +all: picow ; include makefiles/system.mk include makefiles/tools.mk include makefiles/wamr.mk -platform/sdl/picow: platform/sdl/main.c $(OBJ) $(WAMR_OBJ) - $(CC) -o $@ $^ -Iinclude/ $$(pkg-config SDL3 --cflags --libs) +picow: $(OBJ) $(WAMR_OBJ) + $(CC) -o $@ $^ $$(pkg-config SDL3 --libs) .PHONY: clean clean: clean-system clean-tools clean-wamr rm -f platform/sdl/picow src/picow.o src/png.o src/sysroot.c src/%.o: src/%.c - $(CC) -c -o $@ $< -Iinclude/ -Ilib/ + $(CC) -c -o $@ $< -Ilib/ $$(pkg-config SDL3 --cflags) diff --git a/include/picow.h b/include/picow.h deleted file mode 100644 index 5daec57..0000000 --- a/include/picow.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef _PICOW_H_ -#define _PICOW_H_ - -#include <stdbool.h> -#include <stddef.h> -#include <stdint.h> - -union pw_pixel { - uint32_t raw; - struct pw_pixel_channels { - uint8_t r; - uint8_t g; - uint8_t b; - uint8_t a; - } ch; -}; - -struct pw_png { - union pw_pixel image[205][160]; -}; - -bool pw_png_decode(struct pw_png *png, uint8_t const *data, size_t datasize); - -struct pw_cart { - char name[32]; - uint8_t rom[32 * 1024]; -}; - -void pw_load_png_cart(struct pw_cart *cart, struct pw_png *png); - -union pw_gamepad { - 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; -}; - -struct pw_state { - union pw_gamepad gamepads[8]; -}; - -#endif diff --git a/platform/sdl/main.c b/platform/sdl/main.c deleted file mode 100644 index c475214..0000000 --- a/platform/sdl/main.c +++ /dev/null @@ -1,366 +0,0 @@ -#include <unistd.h> - -#define SDL_MAIN_USE_CALLBACKS -#include <SDL3/SDL.h> -#include <SDL3/SDL_main.h> - -#include "picow.h" - -static void usage(char const *name) { - // clang-format off - printf("Usage: %s [-s <sysroot>] [-c <carts>] [-v[v[v]]]\n", name); - printf(" -c <carts> Carts directory. Defaults to 'carts'.\n"); - printf(" -v[v[v]] Increasingly verbose logging.\n"); - // clang-format on -} - -struct appstate { - struct pw_state picow; - - SDL_Window *window; - SDL_Renderer *renderer; - SDL_Texture *framebuffer; - SDL_FRect target; - SDL_Gamepad *gamepads[8]; -}; - -#define FAIL(msg) \ - (SDL_LogCritical(0, msg ". %s", SDL_GetError()), SDL_APP_FAILURE) -#define FAILF(msg, ...) \ - (SDL_LogCritical(0, msg ". %s", __VA_ARGS__, SDL_GetError()), SDL_APP_FAILURE) - -static bool load_cart(struct pw_cart *cart, char *s) { - bool result = false; - - char *saveptr; - char *file = SDL_strtok_r(s, ":", &saveptr); - char *ext = SDL_strrchr(file, '.'); - char *name = SDL_strtok_r(NULL, "", &saveptr); - - SDL_IOStream *rom = SDL_IOFromFile(file, "r"); - if (!rom) { - goto cleanup; - } - - if (ext && SDL_strcmp(ext, ".png") == 0) { - Sint64 size = SDL_GetIOSize(rom); - if (size < 0) { - goto cleanup; - } - if (size > 1024 * 1024) { - SDL_SetError("cart .png is too large"); - goto cleanup; - } - uint8_t data[1024 * 1024]; - size_t n = SDL_ReadIO(rom, data, (size_t)size); - if (n != size) { - goto cleanup; - } - struct pw_png png; - if (!pw_png_decode(&png, data, n)) { - SDL_SetError("failed to decode cart .png"); - goto cleanup; - } - pw_load_png_cart(cart, &png); - } else { - size_t n = SDL_ReadIO(rom, cart->rom, sizeof cart->rom); - if (n != sizeof cart->rom) { - if (n > 0) { - SDL_SetError("cart rom is less than 32kB"); - } - goto cleanup; - } - n = SDL_ReadIO(rom, cart->name, 1); - if (SDL_GetIOStatus(rom) != SDL_IO_STATUS_EOF) { - if (n > 0) { - SDL_SetError("cart rom is more than 32kB"); - } - goto cleanup; - } - } - - SDL_memset(cart->name, 0, sizeof cart->name); - if (name) { - SDL_strlcpy(cart->name, name, sizeof cart->name); - } else { - name = SDL_strrchr(file, '/'); - name = name ? name + 1 : file; - char *ext = SDL_strrchr(name, '.'); - size_t len = ext ? (size_t)(ext - name + 1) : SDL_strlen(name); - if (len >= sizeof cart->name) { - len = sizeof cart->name; - } - SDL_strlcpy(cart->name, name, len); - } - - SDL_LogDebug(0, "loaded cart '%s' as '%s'", s, cart->name); - result = true; - -cleanup: - result &= SDL_CloseIO(rom); - 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->framebuffer, &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"); - } - - SDL_LogDebug(0, "connected gamepad for player %zu", i + 1); - 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]); - SDL_LogDebug(0, "disconnected gamepad for player %zu", i + 1); - state->picow.gamepads[i].raw = 0; - break; - } - } - return SDL_APP_CONTINUE; -} - -static union pw_gamepad *get_gamepad_state(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]) == id) { - return &state->picow.gamepads[i]; - } - } - return NULL; -} - -static SDL_AppResult handle_axis(struct appstate *state, - SDL_GamepadAxisEvent *event) { - union pw_gamepad *gamepad = get_gamepad_state(state, event->which); - if (gamepad == NULL) { - return SDL_APP_CONTINUE; - } - -#define MAP_AXIS(sdlaxis, minus, plus) \ - case sdlaxis: \ - gamepad->b.minus = event->value < -10000; \ - gamepad->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->picow.gamepads[0] && state->gamepads[1] == 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_button(struct appstate *state, - SDL_GamepadButtonEvent *event) { - union pw_gamepad *gamepad = get_gamepad_state(state, event->which); - if (gamepad == NULL) { - return SDL_APP_CONTINUE; - } - -#define MAP_BUTTON(sdlbtn, gbtn) \ - case sdlbtn: \ - gamepad->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) { - switch (event->key) { - case SDLK_ESCAPE: - case SDLK_Q: - return SDL_APP_SUCCESS; - } - return SDL_APP_CONTINUE; -} - -static void blit_framebuffer(struct appstate *state, uint32_t *fb) { - uint8_t r = state->picow.gamepads[0].raw << 1; - uint8_t g = state->picow.gamepads[1].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++) { - fb[x + y * 128] = px.raw; - } - } -} - -SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { - struct appstate *state = SDL_calloc(1, sizeof *state); - *appstate = state; - - if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD)) { - return FAIL("failed to initialize SDL"); - } - - char *carts = "carts"; - SDL_LogPriority logp = SDL_GetLogPriority(0); - - int c; - while ((c = getopt(argc, argv, "hb:c:v")) != -1) { - switch (c) { - case 'h': - usage(argv[0]); - return SDL_APP_SUCCESS; - case 'c': - carts = optarg; - break; - case 'v': - logp--; - if (logp == SDL_LOG_PRIORITY_INVALID) { - fprintf(stderr, "ERROR: specified -v too many times\n"); - return SDL_APP_FAILURE; - } - break; - default: - fprintf(stderr, "ERROR: unknown option -%c\n", c); - return SDL_APP_FAILURE; - } - } - SDL_SetLogPriority(0, logp); - - if (!SDL_CreateWindowAndRenderer("PICO-w", 512, 512, - SDL_WINDOW_HIGH_PIXEL_DENSITY | - SDL_WINDOW_RESIZABLE, - &state->window, &state->renderer)) { - return FAIL("failed to create window"); - } - - state->framebuffer = - SDL_CreateTexture(state->renderer, SDL_PIXELFORMAT_RGBA32, - SDL_TEXTUREACCESS_STREAMING, 128, 128); - if (state->framebuffer == NULL || - !SDL_SetWindowMinimumSize(state->window, 128, 128)) { - return FAIL("failed to create framebuffer"); - } - - return SDL_APP_CONTINUE; -} - -SDL_AppResult SDL_AppIterate(void *appstate) { - struct appstate *state = appstate; - - void *fb; - int pitch; - if (!SDL_LockTexture(state->framebuffer, NULL, &fb, &pitch)) { - return FAIL("failed to lock framebuffer"); - } - blit_framebuffer(state, (uint32_t *)fb); - SDL_UnlockTexture(state->framebuffer); - - SDL_RenderClear(state->renderer); - SDL_RenderTexture(state->renderer, state->framebuffer, NULL, &state->target); - SDL_RenderPresent(state->renderer); - - return SDL_APP_CONTINUE; -} - -SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { - struct appstate *state = appstate; - - switch (event->type) { - case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED: - return update_target_rect(state, event->window.data1, event->window.data2); - 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); - case SDL_EVENT_KEY_DOWN: - case SDL_EVENT_KEY_UP: - return handle_keyboard(state, &event->key); - } - return SDL_APP_CONTINUE; -} - -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_DestroyTexture(state->framebuffer); - SDL_DestroyRenderer(state->renderer); - SDL_DestroyWindow(state->window); - SDL_free(state); - - SDL_Log(0, "bye"); -} diff --git a/src/picow.c b/src/picow.c index a153c7f..c7d2bf1 100644 --- a/src/picow.c +++ b/src/picow.c @@ -1,9 +1,61 @@ -#include "picow.h" +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <unistd.h> -_Static_assert(sizeof(struct pw_gamepad_buttons) == 1, - "unexpected button state size"); +#define SDL_MAIN_USE_CALLBACKS +#include <SDL3/SDL.h> +#include <SDL3/SDL_main.h> -void pw_load_png_cart(struct pw_cart *cart, struct pw_png *png) { +#include "png.h" +#include "sysroot.h" + +static void usage(char const *name) { + // clang-format off + printf("Usage: %s [-s <sysroot>] [-c <carts>] [-v[v[v]]]\n", name); + printf(" -c <carts> Carts directory. Defaults to 'carts'.\n"); + printf(" -v[v[v]] Increasingly verbose logging.\n"); + // clang-format on +} + +struct pw_cart { + char name[32]; + uint8_t rom[32 * 1024]; +}; + +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; +}; + +struct pw_gamepad { + SDL_Gamepad *handle; + union pw_gamepad_state state; +}; + +struct pw_appstate { + SDL_Window *window; + SDL_Renderer *renderer; + SDL_Texture *framebuffer; + SDL_FRect target; + struct pw_gamepad gamepads[8]; +}; + +#define FAIL(msg) \ + (SDL_LogCritical(0, msg ". %s", SDL_GetError()), SDL_APP_FAILURE) +#define FAILF(msg, ...) \ + (SDL_LogCritical(0, msg ". %s", __VA_ARGS__, SDL_GetError()), SDL_APP_FAILURE) + +static void load_png_cart(struct pw_cart *cart, struct pw_png *png) { for (size_t i = 0; i < sizeof cart->rom; i++) { size_t x = i % 160; size_t y = i / 160; @@ -12,3 +64,339 @@ void pw_load_png_cart(struct pw_cart *cart, struct pw_png *png) { ((p.ch.g & 0b11) << 2) | ((p.ch.b & 0b11)); } } + +static bool load_cart(struct pw_cart *cart, char *s) { + bool result = false; + + char *saveptr; + char *file = SDL_strtok_r(s, ":", &saveptr); + char *ext = SDL_strrchr(file, '.'); + char *name = SDL_strtok_r(NULL, "", &saveptr); + + SDL_IOStream *rom = SDL_IOFromFile(file, "r"); + if (!rom) { + goto cleanup; + } + + if (ext && SDL_strcmp(ext, ".png") == 0) { + Sint64 size = SDL_GetIOSize(rom); + if (size < 0) { + goto cleanup; + } + if (size > 1024 * 1024) { + SDL_SetError("cart .png is too large"); + goto cleanup; + } + uint8_t data[1024 * 1024]; + size_t n = SDL_ReadIO(rom, data, (size_t)size); + if (n != size) { + goto cleanup; + } + struct pw_png png; + if (!pw_png_decode(&png, data, n)) { + SDL_SetError("failed to decode cart .png"); + goto cleanup; + } + load_png_cart(cart, &png); + } else { + size_t n = SDL_ReadIO(rom, cart->rom, sizeof cart->rom); + if (n != sizeof cart->rom) { + if (n > 0) { + SDL_SetError("cart rom is less than 32kB"); + } + goto cleanup; + } + n = SDL_ReadIO(rom, cart->name, 1); + if (SDL_GetIOStatus(rom) != SDL_IO_STATUS_EOF) { + if (n > 0) { + SDL_SetError("cart rom is more than 32kB"); + } + goto cleanup; + } + } + + SDL_memset(cart->name, 0, sizeof cart->name); + if (name) { + SDL_strlcpy(cart->name, name, sizeof cart->name); + } else { + name = SDL_strrchr(file, '/'); + name = name ? name + 1 : file; + char *ext = SDL_strrchr(name, '.'); + size_t len = ext ? (size_t)(ext - name + 1) : SDL_strlen(name); + if (len >= sizeof cart->name) { + len = sizeof cart->name; + } + SDL_strlcpy(cart->name, name, len); + } + + SDL_LogDebug(0, "loaded cart '%s' as '%s'", s, cart->name); + result = true; + +cleanup: + result &= SDL_CloseIO(rom); + return result; +} + +static SDL_AppResult update_target_rect(struct pw_appstate *state, int w, int h) { + float ww = (float)w, wh = (float)h; + float fbw, fbh; + if (!SDL_GetTextureSize(state->framebuffer, &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 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; + } + } + + if (i >= ngamepads) { + return SDL_APP_CONTINUE; + } + + SDL_Gamepad *gamepad = SDL_OpenGamepad(event->which); + if (gamepad == NULL) { + return FAIL("failed to open gamepad"); + } + + SDL_LogDebug(0, "connected gamepad for player %zu", i + 1); + state->gamepads[i].handle = gamepad; + return SDL_APP_CONTINUE; +} + +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 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 handle_axis(struct pw_appstate *state, + SDL_GamepadAxisEvent *event) { + union pw_gamepad_state *gamepad = get_gamepad_state(state, event->which); + if (gamepad == NULL) { + return SDL_APP_CONTINUE; + } + +#define MAP_AXIS(sdlaxis, minus, plus) \ + case sdlaxis: \ + gamepad->b.minus = event->value < -10000; \ + gamepad->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 && state->gamepads[1].handle == 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_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; + } + +#define MAP_BUTTON(sdlbtn, gbtn) \ + case sdlbtn: \ + gamepad->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 pw_appstate *state, + SDL_KeyboardEvent *event) { + switch (event->key) { + case SDLK_ESCAPE: + case SDLK_Q: + return SDL_APP_SUCCESS; + } + return SDL_APP_CONTINUE; +} + +static void blit_framebuffer(struct pw_appstate *state, uint32_t *fb) { + uint8_t r = state->gamepads[0].state.raw << 1; + uint8_t g = state->gamepads[1].state.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++) { + fb[x + y * 128] = px.raw; + } + } +} + +SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { + struct pw_appstate *state = SDL_calloc(1, sizeof *state); + *appstate = state; + + if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD)) { + return FAIL("failed to initialize SDL"); + } + + char *carts = "carts"; + SDL_LogPriority logp = SDL_GetLogPriority(0); + + int c; + while ((c = getopt(argc, argv, "hb:c:v")) != -1) { + switch (c) { + case 'h': + usage(argv[0]); + return SDL_APP_SUCCESS; + case 'c': + carts = optarg; + break; + case 'v': + logp--; + if (logp == SDL_LOG_PRIORITY_INVALID) { + fprintf(stderr, "ERROR: specified -v too many times\n"); + return SDL_APP_FAILURE; + } + break; + default: + fprintf(stderr, "ERROR: unknown option -%c\n", c); + return SDL_APP_FAILURE; + } + } + SDL_SetLogPriority(0, logp); + + if (!SDL_CreateWindowAndRenderer("PICO-w", 512, 512, + SDL_WINDOW_HIGH_PIXEL_DENSITY | + SDL_WINDOW_RESIZABLE, + &state->window, &state->renderer)) { + return FAIL("failed to create window"); + } + + state->framebuffer = + SDL_CreateTexture(state->renderer, SDL_PIXELFORMAT_RGBA32, + SDL_TEXTUREACCESS_STREAMING, 128, 128); + if (state->framebuffer == NULL || + !SDL_SetWindowMinimumSize(state->window, 128, 128)) { + return FAIL("failed to create framebuffer"); + } + + return SDL_APP_CONTINUE; +} + +SDL_AppResult SDL_AppIterate(void *appstate) { + struct pw_appstate *state = appstate; + + void *fb; + int pitch; + if (!SDL_LockTexture(state->framebuffer, NULL, &fb, &pitch)) { + return FAIL("failed to lock framebuffer"); + } + blit_framebuffer(state, (uint32_t *)fb); + SDL_UnlockTexture(state->framebuffer); + + SDL_RenderClear(state->renderer); + SDL_RenderTexture(state->renderer, state->framebuffer, NULL, &state->target); + SDL_RenderPresent(state->renderer); + + return SDL_APP_CONTINUE; +} + +SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { + struct pw_appstate *state = appstate; + + switch (event->type) { + case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED: + return update_target_rect(state, event->window.data1, event->window.data2); + 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); + case SDL_EVENT_KEY_DOWN: + case SDL_EVENT_KEY_UP: + return handle_keyboard(state, &event->key); + } + return SDL_APP_CONTINUE; +} + +void SDL_AppQuit(void *appstate, SDL_AppResult result) { + struct pw_appstate *state = appstate; + + for (size_t i = 0; i < SDL_arraysize(state->gamepads); i++) { + SDL_CloseGamepad(state->gamepads[i].handle); + } + SDL_DestroyTexture(state->framebuffer); + SDL_DestroyRenderer(state->renderer); + SDL_DestroyWindow(state->window); + SDL_free(state); + + SDL_Log(0, "bye"); +} @@ -1,4 +1,4 @@ -#include "picow.h" +#include "png.h" #define STBI_ONLY_PNG #define STBI_MAX_DIMENSIONS (160 * 205) diff --git a/src/png.h b/src/png.h new file mode 100644 index 0000000..c2d2c10 --- /dev/null +++ b/src/png.h @@ -0,0 +1,24 @@ +#ifndef _PICOW_PNG_H_ +#define _PICOW_PNG_H_ + +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +union pw_pixel { + uint32_t raw; + struct pw_pixel_channels { + uint8_t r; + uint8_t g; + uint8_t b; + uint8_t a; + } ch; +}; + +struct pw_png { + union pw_pixel image[205][160]; +}; + +bool pw_png_decode(struct pw_png *png, uint8_t const *data, size_t datasize); + +#endif |
