refactor: add helpers for file I/O, main script detection, safe strncpy, & make hal generic

This commit is contained in:
asrael 2025-12-06 15:04:53 -06:00
parent a33d4c0068
commit db82efe269
No known key found for this signature in database
GPG key ID: 2786557804DFAE24
19 changed files with 327 additions and 457 deletions

View file

@ -9,14 +9,13 @@
#include <sys/stat.h>
#include <unistd.h>
#include "pxl8_cart.h"
#include "pxl8_game.h"
#include "pxl8_hal.h"
#include "pxl8_log.h"
#include "pxl8_macros.h"
#include "pxl8_repl.h"
#include "pxl8_script.h"
#include "pxl8_sys.h"
#include "pxl8_types.h"
struct pxl8 {
pxl8_cart* cart;
@ -65,8 +64,6 @@ pxl8_result pxl8_init(pxl8* sys, i32 argc, char* argv[]) {
if (!sys || !sys->game) return PXL8_ERROR_INVALID_ARGUMENT;
pxl8_game* game = sys->game;
pxl8_pixel_mode pixel_mode = PXL8_PIXEL_INDEXED;
pxl8_resolution resolution = PXL8_RESOLUTION_640x360;
const char* script_arg = NULL;
bool bundle_mode = false;
bool pack_mode = false;
@ -116,13 +113,61 @@ pxl8_result pxl8_init(pxl8* sys, i32 argc, char* argv[]) {
return result;
}
if (game->repl_mode) {
pxl8_info("starting in REPL mode with script: %s", game->script_path);
pxl8_info("Starting up");
game->script = pxl8_script_create(game->repl_mode);
if (!game->script) {
pxl8_error("failed to initialize scripting: %s", pxl8_script_get_last_error(game->script));
return PXL8_ERROR_INITIALIZATION_FAILED;
}
pxl8_info("starting up");
bool has_embedded = pxl8_cart_has_embedded(argv[0]);
const char* cart_path = script_arg;
char* original_cwd = getcwd(NULL, 0);
sys->platform_data = sys->hal->create(pixel_mode, resolution, "pxl8", 1280, 720);
bool load_embedded = has_embedded && !script_arg;
bool load_from_path = false;
if (!load_embedded && (cart_path || !has_embedded)) {
if (!cart_path) cart_path = "demo";
struct stat st;
load_from_path = (stat(cart_path, &st) == 0 && S_ISDIR(st.st_mode)) ||
(cart_path && strstr(cart_path, ".pxc"));
}
if (load_embedded || load_from_path) {
sys->cart = pxl8_cart_create();
pxl8_result load_result = load_embedded
? pxl8_cart_load_embedded(sys->cart, argv[0])
: pxl8_cart_load(sys->cart, cart_path);
if (!sys->cart || load_result != PXL8_OK) {
pxl8_error("failed to load cart%s%s", load_from_path ? ": " : "", load_from_path ? cart_path : "");
if (sys->cart) pxl8_cart_destroy(sys->cart);
sys->cart = NULL;
free(original_cwd);
return PXL8_ERROR_INITIALIZATION_FAILED;
}
pxl8_cart_mount(sys->cart);
pxl8_script_load_cart_manifest(game->script, sys->cart);
if (load_from_path) {
pxl8_script_set_cart_path(game->script, pxl8_cart_get_base_path(sys->cart), original_cwd);
}
pxl8_strncpy(game->script_path, "main.fnl", sizeof(game->script_path));
} else if (script_arg) {
pxl8_strncpy(game->script_path, script_arg, sizeof(game->script_path));
}
free(original_cwd);
const char* window_title = pxl8_cart_get_title(sys->cart);
if (!window_title) window_title = "pxl8";
pxl8_resolution resolution = pxl8_cart_get_resolution(sys->cart);
pxl8_pixel_mode pixel_mode = pxl8_cart_get_pixel_mode(sys->cart);
pxl8_size window_size = pxl8_cart_get_window_size(sys->cart);
pxl8_size render_size = pxl8_get_resolution_dimensions(resolution);
sys->platform_data = sys->hal->create(render_size.w, render_size.h, window_title, window_size.w, window_size.h);
if (!sys->platform_data) {
pxl8_error("failed to create platform context");
return PXL8_ERROR_INITIALIZATION_FAILED;
@ -139,58 +184,8 @@ pxl8_result pxl8_init(pxl8* sys, i32 argc, char* argv[]) {
return PXL8_ERROR_INITIALIZATION_FAILED;
}
game->script = pxl8_script_create(game->repl_mode);
if (!game->script) {
pxl8_error("failed to initialize scripting: %s", pxl8_script_get_last_error(game->script));
return PXL8_ERROR_INITIALIZATION_FAILED;
}
bool has_embedded = pxl8_cart_has_embedded(argv[0]);
const char* cart_path = script_arg;
if (has_embedded && !script_arg) {
sys->cart = pxl8_cart_create();
if (!sys->cart) {
pxl8_error("failed to create cart");
return PXL8_ERROR_INITIALIZATION_FAILED;
}
if (pxl8_cart_load_embedded(sys->cart, argv[0]) == PXL8_OK) {
pxl8_cart_mount(sys->cart);
strncpy(game->script_path, "main.fnl", sizeof(game->script_path) - 1);
game->script_path[sizeof(game->script_path) - 1] = '\0';
pxl8_info("running embedded cart");
} else {
pxl8_error("failed to load embedded cart");
return PXL8_ERROR_INITIALIZATION_FAILED;
}
} else if (cart_path || !has_embedded) {
if (!cart_path) cart_path = "demo";
struct stat st;
bool is_cart = (stat(cart_path, &st) == 0 && S_ISDIR(st.st_mode)) ||
(cart_path && strstr(cart_path, ".pxc"));
if (is_cart) {
char* original_cwd = getcwd(NULL, 0);
sys->cart = pxl8_cart_create();
if (!sys->cart) {
pxl8_error("failed to create cart");
return PXL8_ERROR_INITIALIZATION_FAILED;
}
if (pxl8_cart_load(sys->cart, cart_path) == PXL8_OK) {
pxl8_script_set_cart_path(game->script, pxl8_cart_get_base_path(sys->cart), original_cwd);
pxl8_cart_mount(sys->cart);
strncpy(game->script_path, "main.fnl", sizeof(game->script_path) - 1);
game->script_path[sizeof(game->script_path) - 1] = '\0';
} else {
pxl8_error("failed to load cart: %s", cart_path);
return PXL8_ERROR_INITIALIZATION_FAILED;
}
free(original_cwd);
} else if (script_arg) {
strncpy(game->script_path, script_arg, sizeof(game->script_path) - 1);
game->script_path[sizeof(game->script_path) - 1] = '\0';
}
if (game->repl_mode) {
pxl8_info("starting in REPL mode with script: %s", game->script_path);
}
pxl8_script_set_gfx(game->script, game->gfx);
@ -202,7 +197,10 @@ pxl8_result pxl8_init(pxl8* sys, i32 argc, char* argv[]) {
game->script_loaded = (result == PXL8_OK);
if (game->script_loaded && !game->repl_mode) {
pxl8_script_call_function(game->script, "init");
pxl8_result init_result = pxl8_script_call_function(game->script, "init");
if (init_result != PXL8_OK) {
pxl8_script_error("%s", pxl8_script_get_last_error(game->script));
}
}
}
@ -305,7 +303,6 @@ pxl8_result pxl8_frame(pxl8* sys) {
pxl8_gfx_set_viewport(game->gfx, pxl8_gfx_viewport(bounds, pxl8_gfx_get_width(game->gfx), pxl8_gfx_get_height(game->gfx)));
pxl8_gfx_upload_framebuffer(game->gfx);
pxl8_gfx_upload_atlas(game->gfx);
pxl8_gfx_present(game->gfx);
memset(game->input.keys_pressed, 0, sizeof(game->input.keys_pressed));
@ -327,7 +324,7 @@ void pxl8_quit(pxl8* sys) {
pxl8_game* game = sys->game;
pxl8_info("shutting down");
pxl8_info("Shutting down");
if (sys->cart) {
pxl8_cart_unmount(sys->cart);