aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2026-09-28 17:01:10 +0200
committerNicholas Rodrigues Lordello <[email protected]>2026-09-28 17:01:10 +0200
commit4a6403c6b682cf3b03f80a5ba3a6286e30555014 (patch)
treebf0205a31022882a2a5bac7394f62d3fb7fd2219 /src
parent7d54f3115d5acd58352a095206869d7b7d908d5f (diff)
Introduce Cart Module
In an effort to split up `picow.c` into smaller modules, add a `cart` module that includes logic for listing and loading cart files. We also added multi-cart support (by creating special `.p8.dir` directories for each individual parent cart that specifies which carts can be loaded in-game). Small fixes to the Makefile.
Diffstat (limited to 'src')
-rw-r--r--src/cart.c442
-rw-r--r--src/cart.h96
-rw-r--r--src/picow.c277
3 files changed, 606 insertions, 209 deletions
diff --git a/src/cart.c b/src/cart.c
new file mode 100644
index 0000000..da93178
--- /dev/null
+++ b/src/cart.c
@@ -0,0 +1,442 @@
+#include <SDL3/SDL_filesystem.h>
+#include <SDL3/SDL_surface.h>
+
+#include "cart.h"
+#include "sysroot.h"
+
+#define STBI_ONLY_PNG
+#define STBI_MAX_DIMENSIONS 205
+#define STBI_NO_FAILURE_STRINGS
+#define STBI_NO_LINEAR
+#define STBI_NO_STDIO
+#define STBI_NO_THREAD_LOCALS
+#define STBI_MALLOC(sz) SDL_malloc(sz)
+#define STBI_REALLOC(p, sz) SDL_realloc(p, sz)
+#define STBI_FREE(p) SDL_free(p)
+#define STB_IMAGE_IMPLEMENTATION
+#if defined(__GNUC__) || defined(__clang__)
+static int stbi__addints_valid(int, int) __attribute__((unused));
+static int stbi__mul2shorts_valid(int, int) __attribute__((unused));
+#endif
+#include <stb/stb_image.h>
+
+#ifdef SDL_PLATFORM_WINDOWS
+#define PATH_SEP '\\'
+#else
+#define PATH_SEP '/'
+#endif
+
+static bool name_strip_extension(char name[PW_NAME_MAX], char const *src,
+ char const *ext) {
+ size_t slen = SDL_strlen(src);
+ size_t elen = SDL_strlen(ext);
+
+ if (slen < elen) {
+ return false;
+ }
+
+ size_t len = slen - elen;
+ if (len >= PW_NAME_MAX || SDL_strcmp(src + len, ext)) {
+ return false;
+ }
+
+ SDL_strlcpy(name, src, len + 1);
+ return true;
+}
+
+static bool cart_file_path(struct pw_cart_file *file, char const *dir,
+ char const *filename, char const *ext) {
+ size_t n =
+ SDL_snprintf(file->path, PW_PATH_MAX, "%s%c%s", dir, PATH_SEP, filename);
+ if (n >= PW_PATH_MAX) {
+ SDL_SetError("cart path exceeds maximum length");
+ return false;
+ }
+
+ return name_strip_extension(file->name, filename, ext);
+}
+
+static int cmp_cart_entry(void const *a, void const *b) {
+ struct pw_cart_entry const *ea = a;
+ struct pw_cart_entry const *eb = b;
+ return SDL_strcmp(ea->file.name, eb->file.name);
+}
+
+static int cmp_cart_file(void const *a, void const *b) {
+ struct pw_cart_file const *fa = a;
+ struct pw_cart_file const *fb = b;
+ return SDL_strcmp(fa->name, fb->name);
+}
+
+struct pw_carts *pw_carts_list(char const *dir) {
+ struct pw_carts *carts = NULL;
+ char **cartfiles = NULL;
+
+ SDL_PathInfo pinfo;
+ if (!SDL_GetPathInfo(dir, &pinfo) || pinfo.type != SDL_PATHTYPE_DIRECTORY) {
+ carts = SDL_malloc(sizeof(struct pw_carts));
+ if (!carts) {
+ goto error;
+ }
+
+ SDL_SetError("missing carts directory");
+ carts->count = 0;
+ return carts;
+ }
+
+ int icount;
+ cartfiles =
+ SDL_GlobDirectory(dir, "*.p8.png", SDL_GLOB_CASEINSENSITIVE, &icount);
+ if (!cartfiles || icount < 0) {
+ goto error;
+ }
+
+ size_t count = (size_t)icount;
+ carts = SDL_malloc(sizeof(struct pw_carts) +
+ count * sizeof(struct pw_cart_entry));
+ if (!carts) {
+ goto error;
+ }
+ carts->count = 0;
+
+ for (size_t i = 0; i < count; i++) {
+ struct pw_cart_entry *entry = &carts->entries[i];
+ if (!cart_file_path(&entry->file, dir, cartfiles[i], ".p8.png")) {
+ goto error;
+ }
+ entry->multi = NULL;
+ entry->nmulti = 0;
+ }
+
+ carts->count = count;
+ SDL_qsort(carts->entries, carts->count, sizeof(*carts->entries),
+ cmp_cart_entry);
+
+ char multidir[PW_PATH_MAX];
+ for (size_t i = 0; i < count; i++) {
+ struct pw_cart_entry *entry = &carts->entries[i];
+
+ // We are _guaranteed_ that `len < PW_PATH_MAX` by construction, and that
+ // it has a `.p8.png` suffix. This allows us to easily add the `.p8.dir`
+ // suffix for the multi-cart directory. Skip carts without a multi-dir.
+ size_t len = SDL_strlcpy(multidir, entry->file.path, PW_PATH_MAX);
+ SDL_memcpy(multidir + len - 3, "dir", 4);
+ if (!SDL_GetPathInfo(multidir, &pinfo) ||
+ pinfo.type != SDL_PATHTYPE_DIRECTORY) {
+ continue;
+ }
+
+ SDL_free(cartfiles);
+ cartfiles = SDL_GlobDirectory(multidir, "*.p8.rom",
+ SDL_GLOB_CASEINSENSITIVE, &icount);
+ if (!cartfiles || icount < 0) {
+ goto error;
+ }
+ if (!icount) {
+ continue;
+ }
+
+ entry->nmulti = (size_t)icount;
+ entry->multi = SDL_malloc(entry->nmulti * sizeof(struct pw_cart_file));
+ if (!entry->multi) {
+ goto error;
+ }
+
+ for (size_t j = 0; j < entry->nmulti; j++) {
+ if (!cart_file_path(&entry->multi[j], multidir, cartfiles[j],
+ ".p8.rom")) {
+ goto error;
+ }
+ }
+
+ SDL_qsort(entry->multi, entry->nmulti, sizeof(*entry->multi),
+ cmp_cart_file);
+ }
+
+ SDL_free(cartfiles);
+ return carts;
+
+error:
+ if (carts) {
+ pw_carts_free(carts);
+ }
+ if (cartfiles) {
+ SDL_free(cartfiles);
+ }
+ return NULL;
+}
+
+void pw_carts_free(struct pw_carts *carts) {
+ if (!carts) {
+ SDL_InvalidParamError("carts");
+ return;
+ }
+
+ for (size_t i = 0; i < carts->count; i++) {
+ if (carts->entries[i].multi) {
+ SDL_free(carts->entries[i].multi);
+ }
+ }
+ SDL_free(carts);
+}
+
+union png_pixel {
+ uint32_t raw;
+ struct {
+ uint8_t r;
+ uint8_t g;
+ uint8_t b;
+ uint8_t a;
+ } ch;
+};
+
+static union png_pixel *load_png(uint8_t const *data, size_t sz) {
+ int x, y, n;
+ int ok = stbi_info_from_memory(data, sz, &x, &y, &n);
+ if (!ok || x != PW_PNG_WIDTH || y != PW_PNG_HEIGHT) {
+ SDL_SetError("malformed cart PNG header");
+ return NULL;
+ }
+
+ stbi_uc *image = stbi_load_from_memory(data, sz, &x, &y, &n, 4);
+ if (!image) {
+ SDL_SetError("malformed cart PNG");
+ return NULL;
+ }
+
+ // The desired channels are set to 4, making the STB image library ensure
+ // that the resulting image has exactly 4 8-bit colour channels per pixel;
+ // allowing us to treat the image as 160x205 32-bit pixels.
+ return (union png_pixel *)image;
+}
+
+static bool blit_image(union png_pixel const *image, SDL_Texture *texture) {
+ SDL_Surface *surface = NULL;
+ if (!SDL_LockTextureToSurface(texture, NULL, &surface)) {
+ return false;
+ }
+
+ if (surface->w != PW_PNG_WIDTH || surface->h != PW_PNG_HEIGHT ||
+ surface->pitch != PW_PNG_WIDTH * 4 ||
+ surface->format != SDL_PIXELFORMAT_RGBA32) {
+ SDL_UnlockTexture(texture);
+ SDL_SetError("invalid cart PNG texture size or format");
+ return false;
+ }
+
+ // Copy over the raw image data to the surface, while clearing the
+ // steganographic encoding of the cart data.
+ uint32_t *pixels = surface->pixels;
+ for (size_t i = 0; i < PW_PNG_WIDTH * PW_PNG_HEIGHT; i++) {
+ union png_pixel px = image[i];
+ px.raw &= 0xfcfcfcfc;
+ px.ch.a |= 0x03;
+ pixels[i] = px.raw;
+ }
+
+ SDL_UnlockTexture(texture);
+ return true;
+}
+
+struct pw_cart *pw_cart_init(SDL_Renderer *renderer) {
+ struct pw_cart *cart = SDL_malloc(sizeof(struct pw_cart));
+ if (!cart) {
+ return NULL;
+ }
+
+ cart->png = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32,
+ SDL_TEXTUREACCESS_STREAMING, PW_PNG_WIDTH,
+ PW_PNG_HEIGHT);
+ if (!cart->png) {
+ goto error;
+ }
+ if (!SDL_SetTextureScaleMode(cart->png, SDL_SCALEMODE_NEAREST)) {
+ goto error;
+ }
+
+ union png_pixel *image = load_png(unknown_p8_png.data, unknown_p8_png.size);
+ if (!image) {
+ goto error;
+ }
+
+ bool ok = blit_image(image, cart->png);
+ SDL_free(image);
+ if (!ok) {
+ goto error;
+ }
+
+ SDL_zeroa(cart->rom);
+ return cart;
+
+error:
+ pw_cart_free(cart);
+ return NULL;
+}
+
+bool pw_cart_load(struct pw_cart_entry *entry, struct pw_cart *cart) {
+ if (!entry) {
+ SDL_InvalidParamError("entry");
+ return false;
+ }
+ if (!cart) {
+ SDL_InvalidParamError("cart");
+ return false;
+ }
+
+ SDL_IOStream *png = SDL_IOFromFile(entry->file.path, "rb");
+ if (!png) {
+ return false;
+ }
+
+ bool result = false;
+ uint8_t *data = NULL;
+ union png_pixel *image = NULL;
+
+ int64_t ssize = SDL_GetIOSize(png);
+ if (ssize < 0) {
+ goto cleanup;
+ }
+ if (ssize > 1024 * 1024) {
+ SDL_SetError("cart PNG is too large");
+ goto cleanup;
+ }
+
+ size_t size = (size_t)ssize;
+ data = SDL_malloc(size);
+ if (!data) {
+ goto cleanup;
+ }
+
+ size_t n = SDL_ReadIO(png, data, size);
+ if (n != size) {
+ goto cleanup;
+ }
+
+ image = load_png(data, size);
+ if (!image) {
+ goto cleanup;
+ }
+
+ if (!blit_image(image, cart->png)) {
+ goto cleanup;
+ }
+
+ // Read steganographically encoded cart ROM.
+ for (size_t i = 0; i < sizeof cart->rom; i++) {
+ union png_pixel px = image[i];
+ px.raw &= 0x03030303;
+ cart->rom[i] = (px.ch.a << 6) | (px.ch.r << 4) | (px.ch.g << 2) | px.ch.b;
+ }
+
+ result = true;
+
+cleanup:
+ if (image) {
+ SDL_free(image);
+ }
+ if (data) {
+ SDL_free(data);
+ }
+ result &= SDL_CloseIO(png);
+ return result;
+}
+
+static int cmp_cart_name(void const *a, void const *b) {
+ char const *name = a;
+ struct pw_cart_file const *file = b;
+ return SDL_strcmp(name, file->name);
+}
+
+bool pw_cart_multi_load(struct pw_cart_entry *entry, struct pw_cart *cart,
+ char const *name) {
+ if (!entry) {
+ SDL_InvalidParamError("entry");
+ return false;
+ }
+ if (!cart) {
+ SDL_InvalidParamError("cart");
+ return false;
+ }
+ if (!name) {
+ SDL_InvalidParamError("name");
+ return false;
+ }
+
+ // First, we need to sanitize the name. This simplifies making the carts
+ // work with exported SPLORE carts (by removing the leading '#') and with
+ // carts that are created without manually mapping ROM paths.
+ char sanitized_name[PW_NAME_MAX] = {0};
+ if (name[0] == '#') {
+ size_t n = SDL_strlcpy(sanitized_name, name + 1, PW_NAME_MAX);
+ if (n >= PW_NAME_MAX) {
+ return false;
+ }
+ } else {
+ size_t n = SDL_strlen(name);
+ if (n >= 3 && !SDL_strcmp(name + n - 3, ".p8")) {
+ n -= 3;
+ } else if (n >= 7 && (!SDL_strcmp(name + n - 7, ".p8.png") ||
+ !SDL_strcmp(name + n - 7, ".p8.rom"))) {
+ n -= 7;
+ }
+ if (n >= PW_NAME_MAX) {
+ return false;
+ }
+ SDL_strlcpy(sanitized_name, name, n + 1);
+ }
+
+ // Check if we are loading the main cart. If so, fall back to loading the
+ // main cart PNG.
+ if (!SDL_strcmp(entry->file.name, sanitized_name)) {
+ return pw_cart_load(entry, cart);
+ }
+
+ // Find the multi-cart by name.
+ struct pw_cart_file *file =
+ SDL_bsearch(sanitized_name, entry->multi, entry->nmulti,
+ sizeof(*entry->multi), cmp_cart_name);
+ if (!file) {
+ return false;
+ }
+
+ SDL_IOStream *rom = SDL_IOFromFile(file->path, "rb");
+ if (!rom) {
+ return false;
+ }
+
+ bool result = false;
+
+ Sint64 ssize = SDL_GetIOSize(rom);
+ if (ssize != PW_ROM_SIZE) {
+ if (ssize >= 0) {
+ SDL_SetError("cart ROM is incorrect size");
+ }
+ goto cleanup;
+ }
+
+ size_t n = SDL_ReadIO(rom, cart->rom, PW_ROM_SIZE);
+ if (n != PW_ROM_SIZE) {
+ goto cleanup;
+ }
+
+ result = true;
+
+cleanup:
+ result &= SDL_CloseIO(rom);
+ return result;
+}
+
+/**
+ * Frees a cart and all of its resources.
+ */
+void pw_cart_free(struct pw_cart *cart) {
+ if (!cart) {
+ SDL_InvalidParamError("cart");
+ return;
+ }
+
+ if (cart->png) {
+ SDL_DestroyTexture(cart->png);
+ }
+ SDL_free(cart);
+}
diff --git a/src/cart.h b/src/cart.h
new file mode 100644
index 0000000..5d71be1
--- /dev/null
+++ b/src/cart.h
@@ -0,0 +1,96 @@
+#ifndef PW_CART_H
+#define PW_CART_H
+
+#include <SDL3/SDL_render.h>
+
+/** The maximum supported PICO-8 cart path size. */
+#define PW_PATH_MAX 992
+
+/** The maximum supported PICO-8 cart name size. */
+#define PW_NAME_MAX 32
+
+/** The size of a PICO-8 ROM. */
+#define PW_ROM_SIZE 32768
+
+/** The width in pixels of a PICO-8 PNG. */
+#define PW_PNG_WIDTH 160
+
+/** The height in pixels of a PICO-8 PNG. */
+#define PW_PNG_HEIGHT 205
+
+/**
+ * A PICO-8 cart file that can be loaded.
+ *
+ * Carts are identified by name for for `LOAD()`-ing within PICO-8, this
+ * enables multi-cart support.
+ */
+struct pw_cart_file {
+ char name[PW_NAME_MAX];
+ char path[PW_PATH_MAX];
+};
+
+/**
+ * A top-level loadable cart that can be selected from the cart menu.
+ *
+ * Each cart has a separate multi-cart sub-directory for adding additional
+ * carts that can be `LOAD()`-ed at runtime.
+ */
+struct pw_cart_entry {
+ struct pw_cart_file file;
+ struct pw_cart_file *multi;
+ size_t nmulti;
+};
+
+/**
+ * The list of carts read from the carts directory.
+ */
+struct pw_carts {
+ size_t count;
+ struct pw_cart_entry entries[];
+};
+
+/**
+ * A loaded PICO-8 cart, both the cart PNG image as well as its ROM.
+ */
+struct pw_cart {
+ SDL_Texture *png;
+ uint8_t rom[PW_ROM_SIZE];
+};
+
+/**
+ * List all carts in the `dir`. Returns `NULL` on failure.
+ */
+struct pw_carts *pw_carts_list(char const *dir);
+
+/**
+ * Free all memory associated with a carts list.
+ */
+void pw_carts_free(struct pw_carts *carts);
+
+/**
+ * Initializes an empty cart.
+ */
+struct pw_cart *pw_cart_init(SDL_Renderer *renderer);
+
+/**
+ * Loads the main cart from an entry.
+ *
+ * This will also decode and load the PNG to the cart `png` texture for use in
+ * rendering.
+ */
+bool pw_cart_load(struct pw_cart_entry *entry, struct pw_cart *cart);
+
+/**
+ * Loads a multi-cart by name from an entry. The PNG texture is left unchanged.
+ *
+ * The name is stripped of `#` and `.(p8|p8.png|p8.rom)` extensions.
+ */
+bool pw_cart_multi_load(struct pw_cart_entry *entry, struct pw_cart *cart,
+ char const *name);
+
+/**
+ * Frees a cart and all of its resources.
+ */
+void pw_cart_free(struct pw_cart *cart);
+
+#endif
diff --git a/src/picow.c b/src/picow.c
index 731eff1..b44a91d 100644
--- a/src/picow.c
+++ b/src/picow.c
@@ -1,7 +1,3 @@
-#include <limits.h>
-#include <stdbool.h>
-#include <stddef.h>
-#include <stdint.h>
#include <unistd.h>
#define SDL_MAIN_USE_CALLBACKS
@@ -11,6 +7,7 @@
#include <stb/stbi.h>
#include <wamr/wamr.h>
+#include "cart.h"
#include "sysroot.h"
static void usage(char const *name) {
@@ -21,16 +18,6 @@ static void usage(char const *name) {
// clang-format on
}
-struct pw_cart {
- char name[32];
- uint8_t rom[32 * 1024];
-};
-
-struct pw_pxtexture {
- SDL_Texture *handle;
- SDL_FRect target;
-};
-
enum pw_button {
PW_BUTTON_LEFT,
PW_BUTTON_RIGHT,
@@ -66,15 +53,13 @@ enum pw_scene {
struct pw_appstate {
SDL_Window *window;
SDL_Renderer *renderer;
- struct pw_pxtexture framebuffer;
- struct pw_pxtexture unknown;
- struct pw_pxtexture cartpng;
+ SDL_Texture *framebuffer;
+ SDL_FRect target;
struct pw_gamepad gamepads[8];
- struct pw_cart cart;
- char (*carts)[PATH_MAX];
- int ncarts;
- int cartidx;
- int cartidx2;
+ struct pw_carts *carts;
+ struct pw_cart *cart;
+ ssize_t cartidx;
+ ssize_t cartidx2;
wasm_module_t module;
wasm_module_inst_t instance;
enum pw_scene scene;
@@ -85,139 +70,35 @@ struct pw_appstate {
(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);
- return texture->handle != NULL &&
- SDL_SetTextureScaleMode(texture->handle, SDL_SCALEMODE_NEAREST);
+static SDL_Texture *init_pxtexture(SDL_Renderer *renderer, int w, int h) {
+ SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32,
+ SDL_TEXTUREACCESS_STREAMING, w, h);
+ if (texture && !SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST)) {
+ SDL_DestroyTexture(texture);
+ return NULL;
+ }
+
+ return texture;
}
-static bool update_pxtexture_target(struct pw_pxtexture *texture, int w,
- int h) {
+static bool update_pxtexture_target(SDL_Texture *texture, SDL_FRect *target,
+ int w, int h) {
float ww = (float)w, wh = (float)h;
float fbw, fbh;
- if (!SDL_GetTextureSize(texture->handle, &fbw, &fbh)) {
+ if (!SDL_GetTextureSize(texture, &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);
+ target->w = scale * fbw;
+ target->h = scale * fbh;
- return true;
-}
+ target->x = SDL_floor((ww - target->w) / 2.0);
+ target->y = SDL_floor((wh - target->h) / 2.0);
-static bool blit_cart_png(struct pw_pxtexture *texture, uint32_t *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.
- for (size_t i = 0; i < 160 * 205; i++) {
- fb[i] = png[i] & 0xfcfcfcfc;
- }
-
- SDL_UnlockTexture(texture->handle);
return true;
}
-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) {
- goto cleanup;
- }
-
- Sint64 ssize = SDL_GetIOSize(rom);
- if (ssize < 0) {
- goto cleanup;
- }
- if (ssize > 1024 * 1024) {
- SDL_SetError("cart .png is too large");
- goto cleanup;
- }
- uint8_t data[1024 * 1024];
- size_t size = (size_t)ssize;
- size_t n = SDL_ReadIO(rom, data, size);
- if (n != size) {
- goto cleanup;
- }
-
- 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++) {
- 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)) {
- goto cleanup;
- }
-
- SDL_memset(cart->name, 0, sizeof cart->name); // NOLINT
- char *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'", file, cart->name);
- result = true;
-
-cleanup:
- SDL_free(png);
- result &= SDL_CloseIO(rom);
- return result;
-}
-
-static bool load_unknown_png(struct pw_pxtexture *texture) {
- uint32_t *png = load_png(unknown_p8_png.data, unknown_p8_png.len);
- if (png == NULL) {
- SDL_SetError("failed to decode unknown.p8.png");
- return false;
- }
-
- 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,
enum pw_button button, bool pressed) {
@@ -245,20 +126,21 @@ static void update_gamepad_state(struct pw_appstate *state,
break;
}
- if (state->scene == PW_SCENE_CART_SELECTION && state->ncarts > 0 && pressed &&
+ ssize_t ncarts = (ssize_t)state->carts->count;
+ if (state->scene == PW_SCENE_CART_SELECTION && ncarts > 0 && pressed &&
gamepad == &state->gamepads[0].state) {
switch (button) {
case PW_BUTTON_LEFT:
state->cartidx2 = SDL_max(0, state->cartidx2 - 1);
break;
case PW_BUTTON_RIGHT:
- state->cartidx2 = SDL_min(state->ncarts - 1, state->cartidx2 + 1);
+ state->cartidx2 = SDL_min(ncarts - 1, state->cartidx2 + 1);
break;
case PW_BUTTON_UP:
state->cartidx2 = SDL_max(0, state->cartidx2 - 10);
break;
case PW_BUTTON_DOWN:
- state->cartidx2 = SDL_min(state->ncarts - 1, state->cartidx2 + 10);
+ state->cartidx2 = SDL_min(ncarts - 1, state->cartidx2 + 10);
break;
case PW_BUTTON_O:
case PW_BUTTON_X:
@@ -271,15 +153,22 @@ static void update_gamepad_state(struct pw_appstate *state,
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");
- }
- if (!update_pxtexture_target(&state->unknown, w, h)) {
- return FAIL("failed to update unknown cart target");
+ SDL_Texture *texture;
+ switch (state->scene) {
+ case PW_SCENE_CART_SELECTION:
+ texture = state->cart->png;
+ break;
+ case PW_SCENE_GAME:
+ texture = state->framebuffer;
+ break;
+ default:
+ return SDL_APP_CONTINUE;
}
- if (!update_pxtexture_target(&state->cartpng, w, h)) {
- return FAIL("failed to update cart target");
+
+ if (!update_pxtexture_target(texture, &state->target, w, h)) {
+ return FAIL("failed to update texture target");
}
+
return SDL_APP_CONTINUE;
}
@@ -479,11 +368,6 @@ static void blit_framebuffer(struct pw_appstate *state, uint32_t *fb) {
}
}
-static void render_pxtexture(SDL_Renderer *renderer,
- struct pw_pxtexture *texture) {
- SDL_RenderTexture(renderer, texture->handle, NULL, &texture->target);
-}
-
static void *wasm_malloc(unsigned int size) { return SDL_malloc((size_t)size); }
static void *wasm_realloc(void *ptr, unsigned int size) {
@@ -495,18 +379,13 @@ static void wasm_free(void *ptr) { SDL_free(ptr); }
static SDL_AppResult scene_cart_selection(struct pw_appstate *state) {
if (state->cartidx != state->cartidx2) {
state->cartidx = state->cartidx2;
- if (!load_cart(&state->cartpng, &state->cart,
- state->carts[state->cartidx])) {
+ if (!pw_cart_load(&state->carts->entries[state->cartidx], state->cart)) {
return FAIL("failed to load cart png");
}
}
SDL_RenderClear(state->renderer);
- if (state->ncarts > 0) {
- render_pxtexture(state->renderer, &state->cartpng);
- } else {
- render_pxtexture(state->renderer, &state->unknown);
- }
+ SDL_RenderTexture(state->renderer, state->cart->png, NULL, &state->target);
SDL_RenderPresent(state->renderer);
return SDL_APP_CONTINUE;
@@ -528,14 +407,14 @@ static SDL_AppResult scene_game_init(struct pw_appstate *state) {
static SDL_AppResult scene_game(struct pw_appstate *state) {
void *fb;
int pitch;
- if (!SDL_LockTexture(state->framebuffer.handle, NULL, &fb, &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.handle);
+ SDL_UnlockTexture(state->framebuffer);
SDL_RenderClear(state->renderer);
- render_pxtexture(state->renderer, &state->framebuffer);
+ SDL_RenderTexture(state->renderer, state->framebuffer, NULL, &state->target);
SDL_RenderPresent(state->renderer);
return SDL_APP_CONTINUE;
@@ -553,7 +432,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
SDL_LogPriority logp = SDL_GetLogPriority(0);
int c;
- while ((c = getopt(argc, argv, "hb:c:v")) != -1) {
+ while ((c = getopt(argc, argv, "hc:v")) != -1) {
switch (c) {
case 'h':
usage(argv[0]);
@@ -585,47 +464,23 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
return FAIL("failed to configure minimum window size");
}
- if (!init_pxtexture(state->renderer, &state->framebuffer, 128, 128)) {
+ state->framebuffer = init_pxtexture(state->renderer, 128, 128);
+ if (!state->framebuffer) {
return FAIL("failed to create framebuffer");
}
- if (!init_pxtexture(state->renderer, &state->unknown, 160, 205)) {
- return FAIL("failed to create unknown cart texture");
- }
- if (!load_unknown_png(&state->unknown)) {
- return FAIL("failed to load unknown cart image");
+ state->cart = pw_cart_init(state->renderer);
+ if (!state->cart) {
+ return FAIL("failed to initialize cart");
}
- 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 && state->ncarts > 0) {
- // 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);
- size_t clen =
- SDL_strlcat(state->carts[i], cartfiles[i], sizeof *state->carts);
- if (clen + 1 >= PATH_MAX) {
- return FAIL("invalid cart path '%s'", cartfiles[i]);
- }
- }
- SDL_free(cartfiles);
-
- if (!load_cart(&state->cartpng, &state->cart, state->carts[0])) {
- return FAIL("unable to load cart");
- }
- } else if (cartfiles != NULL) {
- SDL_LogWarn(0, "empty carts directory");
- SDL_free(cartfiles);
- } else {
- SDL_LogWarn(0, "failed to read carts directory. %s", SDL_GetError());
- state->ncarts = 0;
+ state->carts = pw_carts_list(carts);
+ if (!state->carts) {
+ return FAIL("failed to read carts directory");
+ } else if (!state->carts->count) {
+ SDL_LogWarn(0, "empty or missing carts directory");
+ } else if (!pw_cart_load(&state->carts->entries[0], state->cart)) {
+ return FAIL("failed to load startup cart");
}
struct RuntimeInitArgs init = {
@@ -647,7 +502,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) {
char err[1024];
state->module =
- wasm_runtime_load(pico8_wasm.data, pico8_wasm.len, err, sizeof err);
+ wasm_runtime_load(pico8_wasm.data, pico8_wasm.size, err, sizeof err);
if (state->module == NULL) {
SDL_SetError("%s", err);
return FAIL("failed to load the PICO-8 WebAssembly module");
@@ -672,6 +527,11 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
if (state->scene != state->scene2) {
state->scene = state->scene2;
+
+ int w, h;
+ SDL_GetWindowSizeInPixels(state->window, &w, &h);
+ update_target_rect(state, w, h);
+
switch (state->scene) {
case PW_SCENE_CART_SELECTION:
// TODO(nlordell): re-read cart directory?
@@ -727,12 +587,11 @@ 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->cartpng.handle);
- SDL_DestroyTexture(state->unknown.handle);
- SDL_DestroyTexture(state->framebuffer.handle);
+ pw_carts_free(state->carts);
+ pw_cart_free(state->cart);
+ SDL_DestroyTexture(state->framebuffer);
SDL_DestroyRenderer(state->renderer);
SDL_DestroyWindow(state->window);
- SDL_free(state->carts);
SDL_free(state);
SDL_Log(0, "bye");