From 381e246e73f271be2821dd9208edd2aa09bfc583 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Mon, 19 Oct 2026 20:55:01 +0200 Subject: Refactor Multicart Loading When starting to implement the ASM constants, I noticed that the multicart support would be _WAYYYYY_ easier if all the multicarts are just passed in as embedded cart data. Here, we modify the cart module to introduce a new "cartdat" type that represents a cart export (either a single cart, for the regular case, or multiple carts for the multicart case). This simplifies lots of code, and allows us to use the ASM constants for passing in cart data without additional glue logic. It mimics how the PICO-8 export works for multi-cart code as well. Lastly, cart names now include the `.p8` extension in the name, to match how they work with the multicart export. I'm not sure how this will interact with splore multi-cart loading (i.e. `LOAD("#SOMECART")`). --- src/cart.c | 190 +++++++++++++++++++++--------------------------------------- src/cart.h | 46 ++++++++------- src/env.c | 2 +- src/env.h | 2 +- src/game.c | 18 +++--- src/game.h | 4 +- src/menu.c | 28 ++++----- src/menu.h | 4 +- src/picow.c | 8 +-- 9 files changed, 124 insertions(+), 178 deletions(-) diff --git a/src/cart.c b/src/cart.c index a29ef11..994728e 100644 --- a/src/cart.c +++ b/src/cart.c @@ -67,7 +67,7 @@ struct pw_carts *pw_carts_list(char const *dir) { 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")) { + if (!cart_file_path(&entry->file, dir, cartfiles[i], ".png")) { goto error; } entry->multi = NULL; @@ -109,8 +109,7 @@ struct pw_carts *pw_carts_list(char const *dir) { } for (size_t j = 0; j < entry->nmulti; j++) { - if (!cart_file_path(&entry->multi[j], multidir, cartfiles[j], - ".p8.rom")) { + if (!cart_file_path(&entry->multi[j], multidir, cartfiles[j], ".rom")) { goto error; } } @@ -175,19 +174,19 @@ static bool blit_image(union pw_png_pixel const *image, SDL_Texture *texture) { return true; } -struct pw_cart *pw_cart_init(SDL_Renderer *renderer) { - struct pw_cart *cart = SDL_malloc(sizeof(struct pw_cart)); +struct pw_cart_png *pw_cart_png_init(SDL_Renderer *renderer) { + struct pw_cart_png *cart = SDL_malloc(sizeof(struct pw_cart_png)); if (!cart) { return NULL; } - cart->png = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, - SDL_TEXTUREACCESS_STREAMING, PW_PNG_WIDTH, - PW_PNG_HEIGHT); - if (!cart->png) { + cart->texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, + SDL_TEXTUREACCESS_STREAMING, PW_PNG_WIDTH, + PW_PNG_HEIGHT); + if (!cart->texture) { goto error; } - if (!SDL_SetTextureScaleMode(cart->png, SDL_SCALEMODE_NEAREST)) { + if (!SDL_SetTextureScaleMode(cart->texture, SDL_SCALEMODE_NEAREST)) { goto error; } @@ -197,7 +196,7 @@ struct pw_cart *pw_cart_init(SDL_Renderer *renderer) { goto error; } - bool ok = blit_image(image, cart->png); + bool ok = blit_image(image, cart->texture); SDL_free(image); if (!ok) { goto error; @@ -207,18 +206,18 @@ struct pw_cart *pw_cart_init(SDL_Renderer *renderer) { return cart; error: - pw_cart_free(cart); + pw_cart_png_free(cart); return NULL; } -void pw_cart_free(struct pw_cart *cart) { +void pw_cart_png_free(struct pw_cart_png *cart) { if (!cart) { SDL_InvalidParamError("cart"); return; } - if (cart->png) { - SDL_DestroyTexture(cart->png); + if (cart->texture) { + SDL_DestroyTexture(cart->texture); } SDL_free(cart); } @@ -284,7 +283,7 @@ cleanup: return result; } -bool pw_cart_load(struct pw_cart_entry *entry, struct pw_cart *cart) { +bool pw_cart_png_load(struct pw_cart_entry *entry, struct pw_cart_png *cart) { if (!entry) { SDL_InvalidParamError("entry"); return false; @@ -294,138 +293,81 @@ bool pw_cart_load(struct pw_cart_entry *entry, struct pw_cart *cart) { return false; } - return load_cart_png(entry->file.path, cart->png, cart->rom); + return load_cart_png(entry->file.path, cart->texture, cart->rom); } -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); -} - -struct pw_multicart *pw_multicart_init(struct pw_cart_entry *entry, - struct pw_cart *cart) { - if (!entry) { - SDL_InvalidParamError("entry"); - return NULL; - } - - struct pw_multicart *multi = SDL_malloc(sizeof(*multi)); - if (!multi) { - return NULL; +static bool load_cart_rom(char const *path, uint8_t rom[PW_ROM_SIZE]) { + SDL_IOStream *stream = SDL_IOFromFile(path, "rb"); + if (!stream) { + return false; } - struct pw_cart_entry *dst = &multi->entry; - if (entry->nmulti) { - dst->multi = SDL_malloc(entry->nmulti * sizeof(struct pw_cart_file)); - if (!dst->multi) { - goto error; - } + bool result = false; - for (size_t i = 0; i < entry->nmulti; ++i) { - SDL_memcpy(&dst->multi[i], &entry->multi[i], sizeof(struct pw_cart_file)); + Sint64 ssize = SDL_GetIOSize(stream); + if (ssize != PW_ROM_SIZE) { + if (ssize >= 0) { + SDL_SetError("cart ROM is incorrect size"); } - } else { - dst->multi = NULL; + goto cleanup; } - SDL_memcpy(&dst->file, &entry->file, sizeof(struct pw_cart_file)); - dst->nmulti = entry->nmulti; - if (cart) { - SDL_memcpy(multi->rom, cart->rom, PW_ROM_SIZE); - } else if (!load_cart_png(entry->file.path, NULL, multi->rom)) { - goto error; + size_t n = SDL_ReadIO(stream, rom, PW_ROM_SIZE); + if (n != PW_ROM_SIZE) { + goto cleanup; } - return multi; - -error: - pw_multicart_free(multi); - return NULL; -} - -void pw_multicart_free(struct pw_multicart *cart) { - if (!cart) { - SDL_InvalidParamError("cart"); - return; - } + result = true; - if (cart->entry.multi) { - SDL_free(cart->entry.multi); - } - SDL_free(cart); +cleanup: + result &= SDL_CloseIO(stream); + return result; } -bool pw_multicart_load(struct pw_multicart *cart, char const *name) { - if (!cart) { - SDL_InvalidParamError("cart"); - return false; - } - if (!name) { - SDL_InvalidParamError("name"); - return false; +struct pw_cart_data *pw_cart_data_load(struct pw_cart_entry *entry, + struct pw_cart_png *cart) { + if (!entry) { + SDL_InvalidParamError("entry"); + return NULL; } - // 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); + size_t count = entry->nmulti + 1; + struct pw_cart_data *cartdat = + SDL_malloc(sizeof(struct pw_cart_data) + count * sizeof(struct pw_cart)); + if (!cartdat) { + return NULL; } - // Check if we are loading the main cart. If so, fall back to loading the - // main cart PNG. - if (!SDL_strcmp(cart->entry.file.name, sanitized_name)) { - return load_cart_png(cart->entry.file.path, NULL, cart->rom); + SDL_strlcpy(cartdat->carts->name, entry->file.name, PW_NAME_MAX); + if (cart) { + SDL_memcpy(cartdat->carts->rom, cart->rom, PW_ROM_SIZE); + } else if (!load_cart_png(entry->file.path, NULL, cartdat->carts->rom)) { + goto error; } - // Find the multi-cart by name. - struct pw_cart_file *file = - SDL_bsearch(sanitized_name, cart->entry.multi, cart->entry.nmulti, - sizeof(*cart->entry.multi), cmp_cart_name); - if (!file) { - return false; - } + for (size_t i = 1; i < count; ++i) { + struct pw_cart *c = &cartdat->carts[i]; + struct pw_cart_file *f = &entry->multi[i - 1]; - SDL_IOStream *rom = SDL_IOFromFile(file->path, "rb"); - if (!rom) { - return false; + SDL_strlcpy(c->name, f->name, PW_NAME_MAX); + if (!load_cart_rom(f->path, c->rom)) { + goto error; + } } - bool result = false; + cartdat->count = count; + return cartdat; - Sint64 ssize = SDL_GetIOSize(rom); - if (ssize != PW_ROM_SIZE) { - if (ssize >= 0) { - SDL_SetError("cart ROM is incorrect size"); - } - goto cleanup; - } +error: + pw_cart_data_free(cartdat); + return NULL; +} - size_t n = SDL_ReadIO(rom, cart->rom, PW_ROM_SIZE); - if (n != PW_ROM_SIZE) { - goto cleanup; +void pw_cart_data_free(struct pw_cart_data *cartdat) { + if (!cartdat) { + SDL_InvalidParamError("cartdat"); + return; } - result = true; - -cleanup: - result &= SDL_CloseIO(rom); - return result; + SDL_free(cartdat); } diff --git a/src/cart.h b/src/cart.h index 2d34d9d..21eede6 100644 --- a/src/cart.h +++ b/src/cart.h @@ -50,21 +50,30 @@ struct pw_carts { }; /** - * A loaded PICO-8 cart, both the cart PNG image as well as its ROM. + * A loaded PICO-8 PNG cart, with both a texture for rendring the cart PNG + * as well as its steganographically encoded ROM. */ -struct pw_cart { - SDL_Texture *png; +struct pw_cart_png { + SDL_Texture *texture; uint8_t rom[PW_ROM_SIZE]; }; /** - * A selected multi-cart group for running in PICO-8. + * A PICO-8 cart data. */ -struct pw_multicart { - struct pw_cart_entry entry; +struct pw_cart { + char name[PW_NAME_MAX]; uint8_t rom[PW_ROM_SIZE]; }; +/** + * Embedded cart data for running in PICO-8. + */ +struct pw_cart_data { + size_t count; + struct pw_cart carts[]; +}; + /** * List all carts in the `dir`. Returns `NULL` on failure. */ @@ -78,12 +87,12 @@ void pw_carts_free(struct pw_carts *carts); /** * Initializes an empty cart. */ -struct pw_cart *pw_cart_init(SDL_Renderer *renderer); +struct pw_cart_png *pw_cart_png_init(SDL_Renderer *renderer); /** * Frees a cart and all of its resources. */ -void pw_cart_free(struct pw_cart *cart); +void pw_cart_png_free(struct pw_cart_png *cart); /** * Loads the main cart from an entry. @@ -91,26 +100,21 @@ void pw_cart_free(struct pw_cart *cart); * 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); +bool pw_cart_png_load(struct pw_cart_entry *entry, struct pw_cart_png *cart); /** - * Initializes a multi-cart from a cart entry. + * Loads cart data from a cart entry. It always has at least one cart (the main + * cart PNG from the entry), with an additional entry for each sub-cart in the + * case of a multi-cart. * - * Optionally specify a loaded cart to copy the initial ROM from. + * Optionally specify a loaded cart PNG to copy the initial ROM from. */ -struct pw_multicart *pw_multicart_init(struct pw_cart_entry *entry, - struct pw_cart *cart); +struct pw_cart_data *pw_cart_data_load(struct pw_cart_entry *entry, + struct pw_cart_png *cart); /** * Frees a multi-cart and all of its resources. */ -void pw_multicart_free(struct pw_multicart *cart); - -/** - * Loads a multi-cart ROM by name. - * - * The name is stripped of `#` and `.(p8|p8.png|p8.rom)` extensions. - */ -bool pw_multicart_load(struct pw_multicart *cart, char const *name); +void pw_cart_data_free(struct pw_cart_data *cart); #endif diff --git a/src/env.c b/src/env.c index 05cb0de..3b4ea11 100644 --- a/src/env.c +++ b/src/env.c @@ -95,7 +95,7 @@ bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context) { if (env->memory.size < ENV_MIN_PAGES * wasm2c_pico8_pagesize_env_memory) { return SDL_SetError("cannot write to fixed memory offsets"); } - if (!context.fb || !context.input || !context.carts || !context.instance) { + if (!context.fb || !context.input || !context.cartdat || !context.instance) { return SDL_InvalidParamError("context"); } diff --git a/src/env.h b/src/env.h index 9c9aebd..53e4191 100644 --- a/src/env.h +++ b/src/env.h @@ -16,7 +16,7 @@ struct pw_env_ctx { struct pw_framebuffer *fb; struct pw_input *input; - struct pw_multicart *carts; + struct pw_cart_data *cartdat; struct w2c_pico8 *instance; }; diff --git a/src/game.c b/src/game.c index 4b1483b..142a418 100644 --- a/src/game.c +++ b/src/game.c @@ -53,8 +53,8 @@ void pw_game_free(struct pw_game *game) { return; } - if (game->carts) { - pw_multicart_free(game->carts); + if (game->cartdat) { + pw_cart_data_free(game->cartdat); } pw_input_free(game->input); pw_fb_free(game->fb); @@ -62,25 +62,25 @@ void pw_game_free(struct pw_game *game) { SDL_free(game); } -bool pw_game_start(struct pw_game *game, struct pw_multicart *carts) { +bool pw_game_start(struct pw_game *game, struct pw_cart_data *cartdat) { if (!game) { SDL_InvalidParamError("game"); return false; } - if (!carts) { - SDL_InvalidParamError("carts"); + if (!cartdat) { + SDL_InvalidParamError("cartdat"); return false; } - if (game->carts) { - pw_multicart_free(game->carts); + if (game->cartdat) { + pw_cart_data_free(game->cartdat); } - game->carts = carts; + game->cartdat = cartdat; pw_env_link(game->env, (struct pw_env_ctx){ .fb = game->fb, .input = game->input, - .carts = game->carts, + .cartdat = game->cartdat, .instance = &game->instance, }); wasm2c_pico8_instantiate(&game->instance, game->env, NULL); diff --git a/src/game.h b/src/game.h index 21f931e..170775b 100644 --- a/src/game.h +++ b/src/game.h @@ -20,7 +20,7 @@ struct pw_game { struct w2c_env *env; struct pw_framebuffer *fb; struct pw_input *input; - struct pw_multicart *carts; + struct pw_cart_data *cartdat; }; /** @@ -37,7 +37,7 @@ void pw_game_free(struct pw_game *game); /** * Starts a PICO-8 game. */ -bool pw_game_start(struct pw_game *game, struct pw_multicart *carts); +bool pw_game_start(struct pw_game *game, struct pw_cart_data *cartdat); /** * Handles SDL events for game state. diff --git a/src/menu.c b/src/menu.c index daeaa48..1902f19 100644 --- a/src/menu.c +++ b/src/menu.c @@ -25,14 +25,14 @@ struct pw_menu *pw_menu_init(SDL_Renderer *renderer, char const *carts, goto error; } - menu->cart = pw_cart_init(renderer); + menu->cart = pw_cart_png_init(renderer); if (!menu->cart) { goto error; } if (!menu->carts->count) { SDL_LogWarn(0, "empty or missing carts directory"); - } else if (!pw_cart_load(&menu->carts->entries[0], menu->cart)) { + } else if (!pw_cart_png_load(&menu->carts->entries[0], menu->cart)) { goto error; } @@ -54,7 +54,7 @@ error: pw_carts_free(menu->carts); } if (menu->cart) { - pw_cart_free(menu->cart); + pw_cart_png_free(menu->cart); } SDL_free(menu); return NULL; @@ -67,7 +67,7 @@ void pw_menu_free(struct pw_menu *menu) { } pw_carts_free(menu->carts); - pw_cart_free(menu->cart); + pw_cart_png_free(menu->cart); SDL_free(menu); } @@ -176,40 +176,40 @@ bool pw_menu_event(struct pw_menu *menu, SDL_Event *event) { return true; } -bool pw_menu_iterate(struct pw_menu *menu, struct pw_multicart **cart) { +bool pw_menu_iterate(struct pw_menu *menu, struct pw_cart_data **cartdat) { if (!menu) { SDL_InvalidParamError("menu"); return false; } - if (!cart) { - SDL_InvalidParamError("cart"); + if (!cartdat) { + SDL_InvalidParamError("cartdat"); return false; } if (menu->selection.current != menu->selection.next) { - if (!pw_cart_load(&menu->carts->entries[menu->selection.next], - menu->cart)) { + if (!pw_cart_png_load(&menu->carts->entries[menu->selection.next], + menu->cart)) { return false; } menu->selection.current = menu->selection.next; } if (menu->selection.selected) { - *cart = pw_multicart_init(&menu->carts->entries[menu->selection.current], - menu->cart); - if (!*cart) { + *cartdat = pw_cart_data_load(&menu->carts->entries[menu->selection.current], + menu->cart); + if (!*cartdat) { return false; } menu->selection.selected = false; return true; } - *cart = NULL; + *cartdat = NULL; return true; } bool pw_menu_render(struct pw_menu *menu) { - SDL_Texture *png = menu->cart->png; + SDL_Texture *png = menu->cart->texture; SDL_Renderer *renderer = SDL_GetRendererFromTexture(png); if (!renderer) { return false; diff --git a/src/menu.h b/src/menu.h index a173033..513c303 100644 --- a/src/menu.h +++ b/src/menu.h @@ -12,7 +12,7 @@ */ struct pw_menu { struct pw_carts *carts; - struct pw_cart *cart; + struct pw_cart_png *cart; enum pw_scale_mode mode; struct { size_t current; @@ -41,7 +41,7 @@ bool pw_menu_event(struct pw_menu *menu, SDL_Event *event); * Iterate a new frame for the cart selection menu. Sets `cart` to non-`NULL` * if a cart was selected to run in the menu. */ -bool pw_menu_iterate(struct pw_menu *menu, struct pw_multicart **cart); +bool pw_menu_iterate(struct pw_menu *menu, struct pw_cart_data **cartdat); /** * Renders the cart selection menu. diff --git a/src/picow.c b/src/picow.c index a998307..8e383c2 100644 --- a/src/picow.c +++ b/src/picow.c @@ -154,12 +154,12 @@ SDL_AppResult SDL_AppIterate(void *appstate) { } if (state->scene == PW_SCENE_CART_SELECTION) { - struct pw_multicart *cart; - if (!pw_menu_iterate(state->menu, &cart)) { + struct pw_cart_data *cartdat; + if (!pw_menu_iterate(state->menu, &cartdat)) { return failf("failed to update cart selection menu state"); } - if (cart) { - if (!pw_game_start(state->game, cart)) { + if (cartdat) { + if (!pw_game_start(state->game, cartdat)) { return failf("failed to start selected cart"); } state->scene = PW_SCENE_GAME; -- cgit v1.2.3