add cartridge system

This commit is contained in:
asrael 2025-09-27 11:03:36 -05:00
parent ff698730f1
commit 98ca54e920
25 changed files with 968 additions and 315 deletions

View file

@ -12,6 +12,7 @@
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include "pxl8_cart.h"
#include "pxl8_lua.h"
#include "pxl8_macros.h"
#include "pxl8_types.h"
@ -37,12 +38,13 @@ typedef struct pxl8_app_state {
lua_State* lua;
pxl8_repl_state repl;
pxl8_resolution resolution;
pxl8_cart* cart;
f32 fps_timer;
i32 frame_count;
u64 last_time;
f32 time;
bool repl_mode;
bool running;
bool script_loaded;
@ -240,21 +242,32 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
app.color_mode = PXL8_COLOR_MODE_MEGA;
app.resolution = PXL8_RESOLUTION_640x360;
const char* script_arg = NULL;
bool pack_mode = false;
const char* pack_input = NULL;
const char* pack_output = NULL;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--repl") == 0) {
app.repl_mode = true;
} else if (strcmp(argv[i], "--pack") == 0) {
pack_mode = true;
if (i + 2 < argc) {
pack_input = argv[++i];
pack_output = argv[++i];
} else {
pxl8_error("--pack requires <folder> <output.pxc>");
return SDL_APP_FAILURE;
}
} else if (!script_arg) {
script_arg = argv[i];
}
}
if (script_arg) {
strncpy(app.script_path, script_arg, sizeof(app.script_path) - 1);
app.script_path[sizeof(app.script_path) - 1] = '\0';
} else {
strcpy(app.script_path, "src/fnl/demo.fnl");
if (pack_mode) {
pxl8_result result = pxl8_cart_pack(pack_input, pack_output);
return (result == PXL8_OK) ? SDL_APP_SUCCESS : SDL_APP_FAILURE;
}
@ -285,6 +298,32 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
pxl8_gfx_shutdown(&app.gfx);
return SDL_APP_FAILURE;
}
if (script_arg) {
struct stat st;
bool is_cart = (stat(script_arg, &st) == 0 && S_ISDIR(st.st_mode)) ||
strstr(script_arg, ".pxc");
if (is_cart) {
char* original_cwd = getcwd(NULL, 0);
app.cart = pxl8_cart_new();
if (pxl8_cart_load(app.cart, script_arg) == PXL8_OK) {
pxl8_lua_setup_cart_path(app.lua, app.cart->base_path, original_cwd);
pxl8_cart_mount(app.cart);
strcpy(app.script_path, "main.fnl");
pxl8_info("Loaded cart: %s", app.cart->name);
} else {
pxl8_error("Failed to load cart: %s", script_arg);
return SDL_APP_FAILURE;
}
free(original_cwd);
} else {
strncpy(app.script_path, script_arg, sizeof(app.script_path) - 1);
app.script_path[sizeof(app.script_path) - 1] = '\0';
}
} else {
strcpy(app.script_path, "src/fnl/demo.fnl");
}
pxl8_lua_setup_contexts(app.lua, &app.gfx, &app.input);
@ -471,6 +510,10 @@ void SDL_AppQuit(void* appstate, SDL_AppResult result) {
if (app->repl_mode) {
pxl8_repl_shutdown(&app->repl);
}
if (app->cart) {
pxl8_cart_destroy(app->cart);
app->cart = NULL;
}
pxl8_lua_shutdown(app->lua);
pxl8_gfx_shutdown(&app->gfx);
}