add pxl8.get_title() API to expose cart title to Lua

This commit is contained in:
asrael 2025-12-07 15:52:54 -06:00
parent db82efe269
commit 99d9c43ea3
No known key found for this signature in database
GPG key ID: 2786557804DFAE24
9 changed files with 75 additions and 22 deletions

View file

@ -2,6 +2,7 @@
#define PXL8_COPYRIGHT "Copyright (c) 2024-2025 pxl8.org"
#define PXL8_VERSION "0.1.0"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -60,6 +61,20 @@ void pxl8_destroy(pxl8* sys) {
free(sys);
}
static void pxl8_print_help(void) {
printf("pxl8 %s - pixel art game framework\n", PXL8_VERSION);
printf("%s\n\n", PXL8_COPYRIGHT);
printf("Usage: pxl8 [path] [--repl]\n\n");
printf(" pxl8 Run main.fnl from current directory\n");
printf(" pxl8 ./game Run game from folder\n");
printf(" pxl8 game.pxc Run packed cart file\n");
printf(" pxl8 --repl Run with REPL enabled\n\n");
printf("Other commands:\n");
printf(" pxl8 pack <folder> <out.pxc> Pack folder into cart file\n");
printf(" pxl8 bundle <in> <out> Bundle cart into executable\n");
printf(" pxl8 help Show this help\n");
}
pxl8_result pxl8_init(pxl8* sys, i32 argc, char* argv[]) {
if (!sys || !sys->game) return PXL8_ERROR_INVALID_ARGUMENT;
@ -67,28 +82,36 @@ pxl8_result pxl8_init(pxl8* sys, i32 argc, char* argv[]) {
const char* script_arg = NULL;
bool bundle_mode = false;
bool pack_mode = false;
bool run_mode = false;
const char* pack_input = NULL;
const char* pack_output = NULL;
bool has_embedded = pxl8_cart_has_embedded(argv[0]);
for (i32 i = 1; i < argc; i++) {
if (strcmp(argv[i], "--repl") == 0) {
if (strcmp(argv[i], "help") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
pxl8_print_help();
return PXL8_ERROR_INVALID_ARGUMENT;
} else if (strcmp(argv[i], "run") == 0) {
run_mode = true;
} else if (strcmp(argv[i], "--repl") == 0) {
game->repl_mode = true;
} else if (strcmp(argv[i], "--bundle") == 0) {
} else if (strcmp(argv[i], "bundle") == 0 || strcmp(argv[i], "--bundle") == 0) {
bundle_mode = true;
if (i + 2 < argc) {
pack_input = argv[++i];
pack_output = argv[++i];
} else {
pxl8_error("--bundle requires <folder|.pxc> <output>");
pxl8_error("bundle requires <folder|.pxc> <output>");
return PXL8_ERROR_INVALID_ARGUMENT;
}
} else if (strcmp(argv[i], "--pack") == 0) {
} else if (strcmp(argv[i], "pack") == 0 || 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>");
pxl8_error("pack requires <folder> <output.pxc>");
return PXL8_ERROR_INVALID_ARGUMENT;
}
} else if (!script_arg) {
@ -96,6 +119,10 @@ pxl8_result pxl8_init(pxl8* sys, i32 argc, char* argv[]) {
}
}
if (!run_mode && !bundle_mode && !pack_mode) {
run_mode = true;
}
if (bundle_mode) {
char exe_path[1024];
ssize_t len = readlink("/proc/self/exe", exe_path, sizeof(exe_path) - 1);
@ -121,18 +148,25 @@ pxl8_result pxl8_init(pxl8* sys, i32 argc, char* argv[]) {
return PXL8_ERROR_INITIALIZATION_FAILED;
}
bool has_embedded = pxl8_cart_has_embedded(argv[0]);
const char* cart_path = script_arg;
char* original_cwd = getcwd(NULL, 0);
bool load_embedded = has_embedded && !script_arg;
bool load_embedded = has_embedded && !run_mode;
bool load_from_path = false;
if (!load_embedded && (cart_path || !has_embedded)) {
if (!cart_path) cart_path = "demo";
if (!load_embedded && run_mode) {
if (!cart_path) {
if (access("main.fnl", F_OK) == 0 || access("main.lua", F_OK) == 0) {
cart_path = ".";
} else {
pxl8_error("no main.fnl or main.lua found in current directory");
free(original_cwd);
return PXL8_ERROR_INITIALIZATION_FAILED;
}
}
struct stat st;
load_from_path = (stat(cart_path, &st) == 0 && S_ISDIR(st.st_mode)) ||
(cart_path && strstr(cart_path, ".pxc"));
strstr(cart_path, ".pxc");
}
if (load_embedded || load_from_path) {