This commit is contained in:
asrael 2025-09-28 13:10:29 -05:00
parent 9f96626ea7
commit 6a02b24ae6
29 changed files with 653 additions and 583 deletions

View file

@ -32,13 +32,13 @@ typedef struct pxl8_repl_state {
bool running;
} pxl8_repl_state;
typedef struct pxl8_app_state {
typedef struct pxl8_state {
pxl8_cart* cart;
pxl8_color_mode color_mode;
pxl8_gfx_ctx gfx;
lua_State* lua;
pxl8_repl_state repl;
pxl8_resolution resolution;
pxl8_cart* cart;
f32 fps_timer;
i32 frame_count;
@ -52,7 +52,7 @@ typedef struct pxl8_app_state {
time_t script_mod_time;
pxl8_input_state input;
} pxl8_app_state;
} pxl8_state;
static void pxl8_repl_completion(const char* buf, linenoiseCompletions* lc) {
const char* fennel_keywords[] = {
@ -183,7 +183,7 @@ static pxl8_repl_command* pxl8_repl_pop_command(pxl8_repl_state* repl) {
return cmd;
}
static void load_script(pxl8_app_state* app) {
static void load_script(pxl8_state* app) {
const char* ext = strrchr(app->script_path, '.');
if (ext && strcmp(ext, ".fnl") == 0) {
@ -233,7 +233,7 @@ static time_t get_file_mod_time(const char* path) {
}
SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
static pxl8_app_state app = {0};
static pxl8_state app = {0};
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
pxl8_error("SDL_Init failed: %s", SDL_GetError());
@ -299,36 +299,40 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
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");
const char* cart_path = script_arg ? script_arg : "demo";
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';
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);
app.cart = calloc(1, sizeof(pxl8_cart));
if (!app.cart) {
pxl8_error("Failed to allocate memory for cart");
return false;
}
} else {
strcpy(app.script_path, "src/fnl/demo.fnl");
if (pxl8_cart_load(app.cart, cart_path) == 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", cart_path);
return SDL_APP_FAILURE;
}
free(original_cwd);
} else if (script_arg) {
strncpy(app.script_path, script_arg, sizeof(app.script_path) - 1);
app.script_path[sizeof(app.script_path) - 1] = '\0';
}
pxl8_lua_setup_contexts(app.lua, &app.gfx, &app.input);
app.script_mod_time = get_file_mod_time(app.script_path);
load_script(&app);
if (app.script_path[0] != '\0') {
app.script_mod_time = get_file_mod_time(app.script_path);
load_script(&app);
}
if (app.repl_mode) {
pxl8_repl_init(&app.repl);
@ -353,7 +357,7 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
}
SDL_AppResult SDL_AppIterate(void* appstate) {
pxl8_app_state* app = (pxl8_app_state*)appstate;
pxl8_state* app = (pxl8_state*)appstate;
int width, height;
SDL_GetWindowSize(app->gfx.window, &width, &height);
@ -465,7 +469,7 @@ SDL_AppResult SDL_AppIterate(void* appstate) {
}
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
pxl8_app_state* app = (pxl8_app_state*)appstate;
pxl8_state* app = (pxl8_state*)appstate;
switch (event->type) {
case SDL_EVENT_QUIT:
@ -502,7 +506,7 @@ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
}
void SDL_AppQuit(void* appstate, SDL_AppResult result) {
pxl8_app_state* app = (pxl8_app_state*)appstate;
pxl8_state* app = (pxl8_state*)appstate;
(void)result;
if (app) {
@ -511,7 +515,8 @@ void SDL_AppQuit(void* appstate, SDL_AppResult result) {
pxl8_repl_shutdown(&app->repl);
}
if (app->cart) {
pxl8_cart_destroy(app->cart);
pxl8_cart_unload(app->cart);
free(app->cart);
app->cart = NULL;
}
pxl8_lua_shutdown(app->lua);