true 16-bit color... glorious

This commit is contained in:
asrael 2025-11-28 14:41:35 -06:00
parent 3dccce8a81
commit b1e8525c3e
30 changed files with 678 additions and 652 deletions

View file

@ -12,6 +12,7 @@
#include <lua.h>
#include <lualib.h>
#include "pxl8_embed.h"
#include "pxl8_macros.h"
#include "pxl8_gui.h"
@ -19,9 +20,9 @@ struct pxl8_script {
lua_State* L;
pxl8_gfx* gfx;
pxl8_input_state* input;
char last_error[2048];
char main_path[256];
char watch_dir[256];
char last_error[PXL8_MAX_ERROR_SIZE];
char main_path[PXL8_MAX_PATH];
char watch_dir[PXL8_MAX_PATH];
time_t latest_mod_time;
};
@ -353,6 +354,39 @@ const char* pxl8_script_get_last_error(pxl8_script* script) {
return script ? script->last_error : "Invalid script context";
}
static int pxl8_embed_searcher(lua_State* L) {
const char* name = luaL_checkstring(L, 1);
const pxl8_embed* e = pxl8_embed_find(name);
if (!e) {
lua_pushfstring(L, "\n\tno embedded module '%s'", name);
return 1;
}
if (luaL_loadbuffer(L, e->data, e->size, name) != 0) {
return lua_error(L);
}
lua_pushstring(L, name);
return 2;
}
static void pxl8_install_embed_searcher(lua_State* L) {
lua_getglobal(L, "package");
lua_getfield(L, -1, "searchers");
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_getfield(L, -1, "loaders");
}
int len = (int)lua_objlen(L, -1);
for (int i = len; i >= 2; i--) {
lua_rawgeti(L, -1, i);
lua_rawseti(L, -2, i + 1);
}
lua_pushcfunction(L, pxl8_embed_searcher);
lua_rawseti(L, -2, 2);
lua_pop(L, 2);
}
pxl8_script* pxl8_script_create(void) {
pxl8_script* script = (pxl8_script*)calloc(1, sizeof(pxl8_script));
if (!script) return NULL;
@ -383,29 +417,28 @@ pxl8_script* pxl8_script_create(void) {
lua_pop(script->L, 1);
lua_getglobal(script->L, "package");
lua_getfield(script->L, -1, "path");
const char* current_path = lua_tostring(script->L, -1);
lua_pop(script->L, 1);
pxl8_install_embed_searcher(script->L);
lua_pushfstring(script->L, "%s;src/lua/?.lua", current_path);
lua_setfield(script->L, -2, "path");
lua_pop(script->L, 1);
const pxl8_embed* fennel = pxl8_embed_find("fennel");
if (fennel && luaL_loadbuffer(script->L, fennel->data, fennel->size, "fennel") == 0) {
if (lua_pcall(script->L, 0, 1, 0) == 0) {
lua_setglobal(script->L, "fennel");
if (luaL_dofile(script->L, "lib/fennel/fennel.lua") == 0) {
lua_setglobal(script->L, "fennel");
lua_getglobal(script->L, "fennel");
lua_getfield(script->L, -1, "install");
if (lua_isfunction(script->L, -1)) {
if (lua_pcall(script->L, 0, 0, 0) != 0) {
pxl8_warn("Failed to install fennel searcher: %s", lua_tostring(script->L, -1));
lua_getglobal(script->L, "fennel");
lua_getfield(script->L, -1, "install");
if (lua_isfunction(script->L, -1)) {
if (lua_pcall(script->L, 0, 0, 0) != 0) {
pxl8_warn("Failed to install fennel searcher: %s", lua_tostring(script->L, -1));
lua_pop(script->L, 1);
}
} else {
lua_pop(script->L, 1);
}
lua_pop(script->L, 1);
} else {
pxl8_warn("Failed to execute fennel: %s", lua_tostring(script->L, -1));
lua_pop(script->L, 1);
}
lua_pop(script->L, 1);
}
script->last_error[0] = '\0';
@ -746,7 +779,8 @@ pxl8_result pxl8_script_load_main(pxl8_script* script, const char* path) {
strncpy(script->watch_dir, script->main_path, dir_len);
script->watch_dir[dir_len] = '\0';
} else {
strcpy(script->watch_dir, ".");
script->watch_dir[0] = '.';
script->watch_dir[1] = '\0';
}
script->latest_mod_time = get_latest_script_mod_time(script->watch_dir);