diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-06-27 21:42:29 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-06-27 21:42:29 +0200 |
| commit | 02ac0ab99c1a7b727dc40b32138730401cb94d89 (patch) | |
| tree | 7b72811c24701ef02e31a5c1b17221c6418cb488 /src | |
| parent | 1eac4cb95f7a7962238df1206174da891272dc17 (diff) | |
Collapse png.c Into Main
The `png` module was tiny and not worth being on its own. Instead
build the STB image code into a `lib/stbi.c`. This was prompted
because we wanted to lint with `clang-tidy` and it was pulling in
STB code because it is a single header library.
Diffstat (limited to 'src')
| -rw-r--r-- | src/picow.c | 134 | ||||
| -rw-r--r-- | src/png.c | 33 | ||||
| -rw-r--r-- | src/png.h | 24 | ||||
| -rw-r--r-- | src/sysroot.h | 4 |
4 files changed, 84 insertions, 111 deletions
diff --git a/src/picow.c b/src/picow.c index 0ad6041..8e18aa4 100644 --- a/src/picow.c +++ b/src/picow.c @@ -7,7 +7,9 @@ #include <SDL3/SDL.h> #include <SDL3/SDL_main.h> -#include "png.h" +#include <stb/stbi.h> +#include <wamr/wamr.h> + #include "sysroot.h" static void usage(char const *name) { @@ -74,15 +76,16 @@ struct pw_appstate { #define FAILF(msg, ...) \ (SDL_LogCritical(0, msg ". %s", __VA_ARGS__, SDL_GetError()), SDL_APP_FAILURE) -static bool init_pxtexture(SDL_Renderer *renderer, struct pw_pxtexture *texture, int w, int h) { - texture->handle = - SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, - SDL_TEXTUREACCESS_STREAMING, w, h); +static bool init_pxtexture(SDL_Renderer *renderer, struct pw_pxtexture *texture, + int w, int h) { + texture->handle = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, + SDL_TEXTUREACCESS_STREAMING, w, h); return texture->handle != NULL && - SDL_SetTextureScaleMode(texture->handle, SDL_SCALEMODE_NEAREST); + SDL_SetTextureScaleMode(texture->handle, SDL_SCALEMODE_NEAREST); } -static bool update_pxtexture_target(struct pw_pxtexture *texture, int w, int h) { +static bool update_pxtexture_target(struct pw_pxtexture *texture, int w, + int h) { float ww = (float)w, wh = (float)h; float fbw, fbh; if (!SDL_GetTextureSize(texture->handle, &fbw, &fbh)) { @@ -99,7 +102,7 @@ static bool update_pxtexture_target(struct pw_pxtexture *texture, int w, int h) return true; } -static bool blit_cart_png(struct pw_pxtexture *texture, struct pw_png *png) { +static bool blit_cart_png(struct pw_pxtexture *texture, uint32_t *png) { SDL_Rect region = {0, 0, 160, 205}; uint32_t *fb; int pitch; @@ -107,20 +110,37 @@ static bool blit_cart_png(struct pw_pxtexture *texture, struct pw_png *png) { return false; } - // clear the steganographic encoding of the cart data, we only want to show - // it as an error texture. + // clear the steganographic encoding of the cart data. for (size_t i = 0; i < 160 * 205; i++) { - size_t x = i % 160; - size_t y = i / 160; - fb[i] = png->image[y][x].raw & 0xfcfcfcfc; + fb[i] = png[i] & 0xfcfcfcfc; } SDL_UnlockTexture(texture->handle); return true; } -static bool load_cart(struct pw_pxtexture *texture, struct pw_cart *cart, char *file) { +static uint32_t *load_png(uint8_t const *data, size_t datasize) { + int x, y, n; + int ok = stbi_info_from_memory(data, datasize, &x, &y, &n); + if (!ok || x != 160 || y != 205) { + return false; + } + + stbi_uc *image = stbi_load_from_memory(data, datasize, &x, &y, &n, 4); + if (!image) { + return false; + } + + // The desired channels are set to 4, making the STB image library ensure + // that thre resulting image has exactly 4 8-bit colour channels per pixel; + // allowing us to treat the image as 160x205 32-bit pixels. + return (uint32_t *)image; +} + +static bool load_cart(struct pw_pxtexture *texture, struct pw_cart *cart, + char *file) { bool result = false; + uint32_t *png = NULL; SDL_IOStream *rom = SDL_IOFromFile(file, "r"); if (!rom) { @@ -140,22 +160,20 @@ static bool load_cart(struct pw_pxtexture *texture, struct pw_cart *cart, char * if (n != size) { goto cleanup; } - struct pw_png png; - if (!pw_png_decode(&png, data, n)) { + + if ((png = load_png(data, size)) == NULL) { SDL_SetError("failed to decode cart .png"); goto cleanup; } for (size_t i = 0; i < sizeof cart->rom; i++) { - size_t x = i % 160; - size_t y = i / 160; - union pw_pixel p = png.image[y][x]; - cart->rom[i] = ((p.ch.a & 0b11) << 6) | ((p.ch.r & 0b11) << 4) | - ((p.ch.g & 0b11) << 2) | ((p.ch.b & 0b11)); + uint32_t px = png[i]; + cart->rom[i] = ((px >> 18) & 0b11000000) | ((px << 4) & 0b00110000) | + ((px >> 6) & 0b00001100) | ((px >> 16) & 0b00000011); } - if (!blit_cart_png(texture, &png)) { - return false; + if (!blit_cart_png(texture, png)) { + goto cleanup; } SDL_memset(cart->name, 0, sizeof cart->name); @@ -172,21 +190,27 @@ static bool load_cart(struct pw_pxtexture *texture, struct pw_cart *cart, char * result = true; cleanup: + SDL_free(png); result &= SDL_CloseIO(rom); return result; } static bool load_unknown_png(struct pw_pxtexture *texture) { - struct pw_png png; - if (!pw_png_decode(&png, pw_sysroot_unknown_p8_png.data, pw_sysroot_unknown_p8_png.len)) { + uint32_t *png = + load_png(pw_sysroot_unknown_p8_png.data, pw_sysroot_unknown_p8_png.len); + if (png == NULL) { SDL_SetError("failed to decode unknown.p8.png"); return false; } - return blit_cart_png(texture, &png); + bool result = blit_cart_png(texture, png); + SDL_free(png); + + return result; } -static void update_gamepad_state(struct pw_appstate *state, union pw_gamepad_state *gamepad, +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: @@ -232,7 +256,8 @@ static void update_gamepad_state(struct pw_appstate *state, union pw_gamepad_sta } } -static SDL_AppResult update_target_rect(struct pw_appstate *state, int w, int h) { +static SDL_AppResult update_target_rect(struct pw_appstate *state, int w, + int h) { if (!update_pxtexture_target(&state->framebuffer, w, h)) { return FAIL("failed to update framebuffer target"); } @@ -285,7 +310,7 @@ static SDL_AppResult remove_gamepad(struct pw_appstate *state, } static union pw_gamepad_state *get_gamepad_state(struct pw_appstate *state, - SDL_JoystickID id) { + 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) { @@ -319,7 +344,8 @@ static SDL_AppResult handle_axis(struct pw_appstate *state, // 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) { + if (gamepad == &state->gamepads[0].state && + state->gamepads[1].handle == NULL) { gamepad++; switch (event->axis) { case SDL_GAMEPAD_AXIS_RIGHTX: @@ -400,47 +426,48 @@ static SDL_AppResult handle_keyboard(struct pw_appstate *state, case SDLK_Q: return SDL_APP_SUCCESS; case SDLK_Z: - update_gamepad_state(state, gamepad, PW_BUTTON_O, event->down); - break; + 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; + 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; + 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; + 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; + 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; + 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; + update_gamepad_state(state, gamepad, PW_BUTTON_MENU, event->down); + break; } 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}}; + 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.raw; + fb[x + y * 128] = px; } } } -static void render_pxtexture(SDL_Renderer *renderer, struct pw_pxtexture *texture) { +static void render_pxtexture(SDL_Renderer *renderer, + struct pw_pxtexture *texture) { SDL_RenderTexture(renderer, texture->handle, NULL, &texture->target); } @@ -504,7 +531,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { return FAIL("failed to create cart selection texture"); } - char **cartfiles = SDL_GlobDirectory(carts, "*.p8.png", SDL_GLOB_CASEINSENSITIVE, &state->ncarts); + char **cartfiles = SDL_GlobDirectory( + carts, "*.p8.png", SDL_GLOB_CASEINSENSITIVE, &state->ncarts); if (cartfiles == NULL) { return FAIL("failed to read carts directory"); } @@ -517,7 +545,8 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { SDL_strlcat(state->carts[i], cartfiles[i], sizeof *state->carts); } SDL_free(cartfiles); - if (state->ncarts > 0 && !load_cart(&state->cartpng, &state->cart, state->carts[0])) { + if (state->ncarts > 0 && + !load_cart(&state->cartpng, &state->cart, state->carts[0])) { return FAIL("unable to load cart"); } @@ -529,7 +558,8 @@ SDL_AppResult SDL_AppIterate(void *appstate) { if (state->cartidx != state->cartidx2) { state->cartidx = state->cartidx2; - if (!load_cart(&state->cartpng, &state->cart, state->carts[state->cartidx])) { + if (!load_cart(&state->cartpng, &state->cart, + state->carts[state->cartidx])) { return FAIL("failed to load cart png"); } } diff --git a/src/png.c b/src/png.c deleted file mode 100644 index c68649d..0000000 --- a/src/png.c +++ /dev/null @@ -1,33 +0,0 @@ -#include "png.h" - -#define STBI_ONLY_PNG -#define STBI_MAX_DIMENSIONS (160 * 205) -#define STBI_NO_FAILURE_STRINGS -#define STBI_NO_LINEAR -#define STBI_NO_STDIO -#define STBI_NO_THREAD_LOCALS -#define STB_IMAGE_IMPLEMENTATION -#include <stb/stb_image.h> - -_Static_assert(sizeof(union pw_pixel) == 4, "unexpected pixel size"); -_Static_assert(sizeof(struct pw_png) == 4 * 160 * 205, - "unexpected png image size"); - -bool pw_png_decode(struct pw_png *png, uint8_t const *data, size_t datasize) { - int x, y, n; - int ok = stbi_info_from_memory(data, datasize, &x, &y, &n); - if (!ok || x != 160 || y != 205) { - return false; - } - - unsigned char *image = stbi_load_from_memory(data, datasize, &x, &y, &n, 4); - if (!image) { - return false; - } - - // Using `memcpy` is OK here: we know that the images are same size. - memcpy(png->image, image, sizeof png->image); // NOLINT - stbi_image_free(image); - - return true; -} diff --git a/src/png.h b/src/png.h deleted file mode 100644 index c2d2c10..0000000 --- a/src/png.h +++ /dev/null @@ -1,24 +0,0 @@ -#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 diff --git a/src/sysroot.h b/src/sysroot.h index a739dbf..1507bbf 100644 --- a/src/sysroot.h +++ b/src/sysroot.h @@ -5,8 +5,8 @@ #include <stdint.h> struct pw_sysroot_data { - size_t len; - uint8_t data[]; + size_t len; + uint8_t data[]; }; extern struct pw_sysroot_data pw_sysroot_pico8_wasm; |
