aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-06-22 23:10:12 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-06-22 23:10:12 +0200
commitc2d3376d449cc3d41c33e1273745db906b5cf8e8 (patch)
tree7d286a293cb2b497a1cbf3c47b866aff36542662
parentacad294d255ca0a36b820a874bad735f8b4fd9d0 (diff)
Start Implementing Cart Selection
We now list carts and can cycle through them in the the interface. Next step is to actually start one.
-rw-r--r--src/picow.c172
1 files changed, 117 insertions, 55 deletions
diff --git a/src/picow.c b/src/picow.c
index 60bf80d..90c762c 100644
--- a/src/picow.c
+++ b/src/picow.c
@@ -50,7 +50,12 @@ struct pw_appstate {
SDL_Renderer *renderer;
struct pw_pxtexture framebuffer;
struct pw_pxtexture unknown;
+ struct pw_pxtexture cartpng;
struct pw_gamepad gamepads[8];
+ struct pw_cart cart;
+ char (*carts)[256];
+ int ncarts;
+ int cartidx;
};
#define FAIL(msg) \
@@ -58,7 +63,51 @@ struct pw_appstate {
#define FAILF(msg, ...) \
(SDL_LogCritical(0, msg ". %s", __VA_ARGS__, SDL_GetError()), SDL_APP_FAILURE)
-static bool load_cart(struct pw_png *png, struct pw_cart *cart, char *file) {
+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;
+}
+
+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)) {
+ return false;
+ }
+
+ float scale = SDL_floor(SDL_min(ww / fbw, wh / fbh));
+ texture->target.w = scale * fbw;
+ texture->target.h = scale * fbh;
+
+ texture->target.x = SDL_floor((ww - texture->target.w) / 2.0);
+ texture->target.y = SDL_floor((wh - texture->target.h) / 2.0);
+
+ return true;
+}
+
+static bool blit_cart_png(struct pw_pxtexture *texture, struct pw_png *png) {
+ SDL_Rect region = {0, 0, 160, 205};
+ uint32_t *fb;
+ int pitch;
+ if (!SDL_LockTexture(texture->handle, &region, (void **)&fb, &pitch)) {
+ return false;
+ }
+
+ // clear the steganographic encoding of the cart data, we only want to show
+ // it as an error texture.
+ 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;
+ }
+
+ SDL_UnlockTexture(texture->handle);
+ return true;
+}
+
+static bool load_cart(struct pw_pxtexture *texture, struct pw_cart *cart, char *file) {
bool result = false;
SDL_IOStream *rom = SDL_IOFromFile(file, "r");
@@ -79,7 +128,8 @@ static bool load_cart(struct pw_png *png, struct pw_cart *cart, char *file) {
if (n != size) {
goto cleanup;
}
- if (!pw_png_decode(png, data, n)) {
+ struct pw_png png;
+ if (!pw_png_decode(&png, data, n)) {
SDL_SetError("failed to decode cart .png");
goto cleanup;
}
@@ -87,11 +137,15 @@ static bool load_cart(struct pw_png *png, struct pw_cart *cart, char *file) {
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];
+ 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));
}
+ if (!blit_cart_png(texture, &png)) {
+ return false;
+ }
+
SDL_memset(cart->name, 0, sizeof cart->name);
char *name = SDL_strrchr(file, '/');
name = name ? name + 1 : file;
@@ -110,57 +164,14 @@ cleanup:
return result;
}
-static bool load_unknown_png(struct pw_png *png) {
- if (!pw_png_decode(png, pw_sysroot_unknown_p8_png.data, pw_sysroot_unknown_p8_png.len)) {
+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)) {
SDL_SetError("failed to decode unknown.p8.png");
return false;
}
- return true;
-}
-
-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;
-}
-
-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)) {
- return false;
- }
-
- float scale = SDL_floor(SDL_min(ww / fbw, wh / fbh));
- texture->target.w = scale * fbw;
- texture->target.h = scale * fbh;
-
- texture->target.x = SDL_floor((ww - texture->target.w) / 2.0);
- texture->target.y = SDL_floor((wh - texture->target.h) / 2.0);
-
- return true;
-}
-
-static bool blit_cart_png(struct pw_pxtexture *texture, struct pw_png *png) {
- SDL_Rect region = {0, 0, 160, 205};
- uint32_t *fb;
- int pitch;
- if (!SDL_LockTexture(texture->handle, &region, (void **)&fb, &pitch)) {
- return false;
- }
-
- // clear the steganographic encoding of the cart data, we only want to show
- // it as an error texture.
- 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;
- }
-
- SDL_UnlockTexture(texture->handle);
- return true;
+ return blit_cart_png(texture, &png);
}
static SDL_AppResult update_target_rect(struct pw_appstate *state, int w, int h) {
@@ -168,7 +179,10 @@ static SDL_AppResult update_target_rect(struct pw_appstate *state, int w, int h)
return FAIL("failed to update framebuffer target");
}
if (!update_pxtexture_target(&state->unknown, w, h)) {
- return FAIL("failed to update framebuffer target");
+ return FAIL("failed to update unknown cart target");
+ }
+ if (!update_pxtexture_target(&state->cartpng, w, h)) {
+ return FAIL("failed to update cart target");
}
return SDL_APP_CONTINUE;
}
@@ -313,6 +327,28 @@ static SDL_AppResult handle_keyboard(struct pw_appstate *state,
#undef MAP_BUTTON
+ if (state->ncarts > 0 && event->down) {
+ int cartidx = state->cartidx;
+ switch (event->key) {
+ case SDLK_LEFT:
+ state->cartidx = SDL_max(0, state->cartidx - 1);
+ break;
+ case SDLK_RIGHT:
+ state->cartidx = SDL_min(state->ncarts - 1, state->cartidx + 1);
+ break;
+ case SDLK_UP:
+ state->cartidx = SDL_max(0, state->cartidx - 10);
+ break;
+ case SDLK_DOWN:
+ state->cartidx = SDL_min(state->ncarts - 1, state->cartidx + 10);
+ break;
+ }
+
+ if (state->cartidx != cartidx && !load_cart(&state->cartpng, &state->cart, state->carts[state->cartidx])) {
+ return FAIL("failed to load cart png");
+ }
+ }
+
return SDL_APP_CONTINUE;
}
@@ -383,11 +419,31 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
if (!init_pxtexture(state->renderer, &state->unknown, 160, 205)) {
return FAIL("failed to create unknown cart texture");
}
- struct pw_png unknown;
- if (!load_unknown_png(&unknown) || !blit_cart_png(&state->unknown, &unknown)) {
+ if (!load_unknown_png(&state->unknown)) {
return FAIL("failed to load unknown cart image");
}
+ if (!init_pxtexture(state->renderer, &state->cartpng, 160, 205)) {
+ return FAIL("failed to create cart selection texture");
+ }
+
+ char **cartfiles = SDL_GlobDirectory(carts, "*.p8.png", SDL_GLOB_CASEINSENSITIVE, &state->ncarts);
+ if (cartfiles == NULL) {
+ return FAIL("failed to read carts directory");
+ }
+ // Unfortunately, SDL returns files without the leading path, so we need to
+ // manually pre-pend it to each found cart.
+ state->carts = SDL_calloc(state->ncarts, sizeof *state->carts);
+ for (int i = 0; i < state->ncarts; i++) {
+ SDL_strlcat(state->carts[i], carts, sizeof *state->carts);
+ SDL_strlcat(state->carts[i], "/", sizeof *state->carts);
+ 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])) {
+ return FAIL("unable to load cart");
+ }
+
return SDL_APP_CONTINUE;
}
@@ -404,7 +460,11 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
SDL_RenderClear(state->renderer);
render_pxtexture(state->renderer, &state->framebuffer);
- render_pxtexture(state->renderer, &state->unknown);
+ if (state->ncarts > 0) {
+ render_pxtexture(state->renderer, &state->cartpng);
+ } else {
+ render_pxtexture(state->renderer, &state->unknown);
+ }
SDL_RenderPresent(state->renderer);
return SDL_APP_CONTINUE;
@@ -440,10 +500,12 @@ void SDL_AppQuit(void *appstate, SDL_AppResult result) {
for (size_t i = 0; i < SDL_arraysize(state->gamepads); i++) {
SDL_CloseGamepad(state->gamepads[i].handle);
}
- SDL_DestroyTexture(state->framebuffer.handle);
+ SDL_DestroyTexture(state->cartpng.handle);
SDL_DestroyTexture(state->unknown.handle);
+ SDL_DestroyTexture(state->framebuffer.handle);
SDL_DestroyRenderer(state->renderer);
SDL_DestroyWindow(state->window);
+ SDL_free(state->carts);
SDL_free(state);
SDL_Log(0, "bye");