pxl8/src/pxl8_script.c

1129 lines
41 KiB
C
Raw Normal View History

2025-11-13 07:15:41 -06:00
#include "pxl8_script.h"
2025-11-01 12:39:59 -05:00
#include <limits.h>
2025-10-04 04:13:48 -05:00
#include <stdlib.h>
#include <string.h>
2025-11-13 07:15:41 -06:00
#include <dirent.h>
2025-10-04 04:13:48 -05:00
#include <sys/stat.h>
#include <unistd.h>
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include "pxl8_cart.h"
2025-11-28 14:41:35 -06:00
#include "pxl8_embed.h"
#include "pxl8_gui.h"
#include "pxl8_log.h"
#include "pxl8_macros.h"
2025-10-04 04:13:48 -05:00
struct pxl8_script {
lua_State* L;
pxl8_gfx* gfx;
pxl8_input_state* input;
2025-11-28 14:41:35 -06:00
char last_error[PXL8_MAX_ERROR_SIZE];
char main_path[PXL8_MAX_PATH];
char watch_dir[PXL8_MAX_PATH];
2025-10-04 04:13:48 -05:00
time_t latest_mod_time;
2025-12-03 09:41:33 -06:00
int repl_env_ref;
bool repl_mode;
2025-10-04 04:13:48 -05:00
};
2025-11-01 12:39:59 -05:00
#define PXL8_MAX_REPL_COMMAND_SIZE 4096
static int pxl8_cart_loader(lua_State* L) {
const char* found_path = lua_tostring(L, lua_upvalueindex(1));
const char* code = lua_tostring(L, lua_upvalueindex(2));
size_t code_len = lua_objlen(L, lua_upvalueindex(2));
bool is_fennel = lua_toboolean(L, lua_upvalueindex(3));
if (is_fennel) {
lua_getglobal(L, "fennel");
lua_getfield(L, -1, "eval");
lua_pushlstring(L, code, code_len);
lua_createtable(L, 0, 1);
lua_pushstring(L, found_path);
lua_setfield(L, -2, "filename");
if (lua_pcall(L, 2, 1, 0) != 0) {
lua_remove(L, -2);
return lua_error(L);
}
lua_remove(L, -2);
} else {
if (luaL_loadbuffer(L, code, code_len, found_path) != 0) {
return lua_error(L);
}
if (lua_pcall(L, 0, 1, 0) != 0) {
return lua_error(L);
}
}
return 1;
}
static int pxl8_cart_searcher(lua_State* L) {
const char* modname = luaL_checkstring(L, 1);
pxl8_cart* cart = pxl8_cart_current();
if (!cart || !pxl8_cart_is_packed(cart)) {
lua_pushstring(L, "\n\tno packed cart mounted");
return 1;
}
char path[512];
size_t len = strlen(modname);
size_t j = 0;
for (size_t i = 0; i < len && j < sizeof(path) - 5; i++) {
if (modname[i] == '.') {
path[j++] = '/';
} else {
path[j++] = modname[i];
}
}
path[j] = '\0';
const char* extensions[] = {".fnl", ".lua", "/init.fnl", "/init.lua"};
u8* data = NULL;
u32 size = 0;
char found_path[512];
bool is_fennel = false;
for (int i = 0; i < 4; i++) {
snprintf(found_path, sizeof(found_path), "%s%s", path, extensions[i]);
if (pxl8_cart_read_file(cart, found_path, &data, &size) == PXL8_OK) {
is_fennel = (i == 0 || i == 2);
break;
}
}
if (!data) {
lua_pushfstring(L, "\n\tno file '%s' in cart", path);
return 1;
}
lua_pushstring(L, found_path);
lua_pushlstring(L, (const char*)data, size);
lua_pushboolean(L, is_fennel);
pxl8_cart_free_file(data);
lua_pushcclosure(L, pxl8_cart_loader, 3);
return 1;
}
2025-11-01 12:39:59 -05:00
static void pxl8_script_repl_promote_locals(const char* input, char* output, size_t output_size) {
size_t i = 0;
size_t j = 0;
bool in_string = false;
bool in_comment = false;
while (input[i] && j < output_size - 1) {
if (in_comment) {
output[j++] = input[i];
if (input[i] == '\n') {
in_comment = false;
}
i++;
continue;
}
if (input[i] == ';' && !in_string) {
in_comment = true;
output[j++] = input[i++];
continue;
}
if (input[i] == '"' && (i == 0 || input[i-1] != '\\')) {
in_string = !in_string;
output[j++] = input[i++];
continue;
}
if (!in_string && input[i] == '(' &&
strncmp(&input[i], "(local ", 7) == 0) {
memcpy(&output[j], "(global ", 8);
2025-11-01 12:39:59 -05:00
j += 8;
i += 7;
continue;
}
output[j++] = input[i++];
}
output[j] = '\0';
}
2025-10-04 04:13:48 -05:00
static const char* pxl8_ffi_cdefs =
"typedef uint8_t u8;\n"
"typedef uint16_t u16;\n"
"typedef uint32_t u32;\n"
"typedef uint64_t u64;\n"
"typedef int8_t i8;\n"
"typedef int16_t i16;\n"
"typedef int32_t i32;\n"
"typedef int64_t i64;\n"
"typedef float f32;\n"
"typedef double f64;\n"
2025-11-18 23:50:02 -06:00
"typedef struct pxl8 pxl8;\n"
2025-10-04 04:13:48 -05:00
"typedef struct pxl8_gfx pxl8_gfx;\n"
2025-10-04 11:55:04 -05:00
"typedef struct { int x, y, w, h; } pxl8_bounds;\n"
2025-10-04 04:13:48 -05:00
"typedef struct { int x, y; } pxl8_point;\n"
"\n"
2025-11-18 23:50:02 -06:00
"f32 pxl8_get_fps(const pxl8* sys);\n"
2025-11-11 23:26:51 -06:00
"\n"
2025-10-04 04:13:48 -05:00
"i32 pxl8_gfx_get_height(pxl8_gfx* ctx);\n"
"i32 pxl8_gfx_get_width(pxl8_gfx* ctx);\n"
"void pxl8_circle(pxl8_gfx* ctx, i32 x, i32 y, i32 r, u32 color);\n"
"void pxl8_circle_fill(pxl8_gfx* ctx, i32 x, i32 y, i32 r, u32 color);\n"
2025-11-15 11:55:00 -06:00
"void pxl8_clear(pxl8_gfx* ctx, u32 color);\n"
2025-10-04 04:13:48 -05:00
"u32 pxl8_get_pixel(pxl8_gfx* ctx, i32 x, i32 y);\n"
"void pxl8_line(pxl8_gfx* ctx, i32 x0, i32 y0, i32 x1, i32 y1, u32 color);\n"
"void pxl8_pixel(pxl8_gfx* ctx, i32 x, i32 y, u32 color);\n"
"void pxl8_rect(pxl8_gfx* ctx, i32 x, i32 y, i32 w, i32 h, u32 color);\n"
"void pxl8_rect_fill(pxl8_gfx* ctx, i32 x, i32 y, i32 w, i32 h, u32 color);\n"
"void pxl8_sprite(pxl8_gfx* ctx, i32 id, i32 x, i32 y, i32 w, i32 h, bool flip_x, bool flip_y);\n"
"void pxl8_text(pxl8_gfx* ctx, const char* str, i32 x, i32 y, u32 color);\n"
"void pxl8_gfx_color_ramp(pxl8_gfx* ctx, u8 start, u8 count, u32 from_color, u32 to_color);\n"
"void pxl8_gfx_cycle_palette(pxl8_gfx* ctx, u8 start, u8 count, i32 step);\n"
2025-10-04 04:13:48 -05:00
"void pxl8_gfx_fade_palette(pxl8_gfx* ctx, u8 start, u8 count, f32 amount, u32 target_color);\n"
"i32 pxl8_gfx_add_palette_cycle(pxl8_gfx* ctx, u8 start_index, u8 end_index, f32 speed);\n"
"void pxl8_gfx_remove_palette_cycle(pxl8_gfx* ctx, i32 cycle_id);\n"
"void pxl8_gfx_set_palette_cycle_speed(pxl8_gfx* ctx, i32 cycle_id, f32 speed);\n"
"void pxl8_gfx_clear_palette_cycles(pxl8_gfx* ctx);\n"
"void pxl8_gfx_update(pxl8_gfx* ctx, f32 dt);\n"
2025-10-04 04:13:48 -05:00
"i32 pxl8_gfx_load_palette(pxl8_gfx* ctx, const char* filepath);\n"
"i32 pxl8_gfx_load_sprite(pxl8_gfx* ctx, const char* filepath, u32* sprite_id);\n"
2025-10-05 16:25:17 -05:00
"i32 pxl8_gfx_create_texture(pxl8_gfx* ctx, const u8* pixels, u32 width, u32 height);\n"
2025-10-04 04:13:48 -05:00
"typedef struct pxl8_input_state pxl8_input_state;\n"
2025-10-06 18:14:07 -05:00
"bool pxl8_key_down(const pxl8_input_state* input, const char* key_name);\n"
"bool pxl8_key_pressed(const pxl8_input_state* input, const char* key_name);\n"
"bool pxl8_key_released(const pxl8_input_state* input, const char* key_name);\n"
"bool pxl8_mouse_pressed(const pxl8_input_state* input, i32 button);\n"
"bool pxl8_mouse_released(const pxl8_input_state* input, i32 button);\n"
2025-10-06 18:14:07 -05:00
"int pxl8_mouse_wheel_x(const pxl8_input_state* input);\n"
"int pxl8_mouse_wheel_y(const pxl8_input_state* input);\n"
"int pxl8_mouse_x(const pxl8_input_state* input);\n"
"int pxl8_mouse_y(const pxl8_input_state* input);\n"
2025-11-19 22:18:08 -06:00
"int pxl8_mouse_dx(const pxl8_input_state* input);\n"
"int pxl8_mouse_dy(const pxl8_input_state* input);\n"
"typedef enum { PXL8_CURSOR_ARROW = 0, PXL8_CURSOR_HAND = 1 } pxl8_cursor;\n"
"void pxl8_center_cursor(pxl8* sys);\n"
"void pxl8_set_cursor(pxl8* sys, pxl8_cursor cursor);\n"
2025-11-19 22:18:08 -06:00
"void pxl8_set_relative_mouse_mode(pxl8* sys, bool enabled);\n"
"void pxl8_set_running(pxl8* sys, bool running);\n"
2025-12-03 09:41:33 -06:00
"void pxl8_lua_log(int level, const char* file, int line, const char* msg);\n"
2025-11-01 12:39:59 -05:00
"typedef u32 pxl8_tile;\n"
2025-10-04 04:13:48 -05:00
"typedef struct pxl8_tilemap pxl8_tilemap;\n"
"typedef struct pxl8_tilesheet pxl8_tilesheet;\n"
"pxl8_tilemap* pxl8_tilemap_create(u32 width, u32 height, u32 tile_size);\n"
"void pxl8_tilemap_destroy(pxl8_tilemap* tilemap);\n"
2025-11-01 12:39:59 -05:00
"u32 pxl8_tilemap_get_height(const pxl8_tilemap* tilemap);\n"
"pxl8_tile pxl8_tilemap_get_tile(const pxl8_tilemap* tilemap, u32 layer, u32 x, u32 y);\n"
"u32 pxl8_tilemap_get_tile_size(const pxl8_tilemap* tilemap);\n"
"void* pxl8_tilemap_get_tile_user_data(const pxl8_tilemap* tilemap, u16 tile_id);\n"
"u32 pxl8_tilemap_get_width(const pxl8_tilemap* tilemap);\n"
"void pxl8_tilemap_set_tile_user_data(pxl8_tilemap* tilemap, u16 tile_id, void* user_data);\n"
2025-10-04 04:13:48 -05:00
"bool pxl8_tilemap_check_collision(const pxl8_tilemap* tilemap, i32 x, i32 y, i32 w, i32 h);\n"
"u16 pxl8_tilemap_get_tile_id(const pxl8_tilemap* tilemap, u32 layer, u32 x, u32 y);\n"
"bool pxl8_tilemap_is_solid(const pxl8_tilemap* tilemap, u32 x, u32 y);\n"
"void pxl8_tilemap_render(const pxl8_tilemap* tilemap, pxl8_gfx* gfx);\n"
"void pxl8_tilemap_render_layer(const pxl8_tilemap* tilemap, pxl8_gfx* gfx, u32 layer);\n"
"void pxl8_tilemap_set_camera(pxl8_tilemap* tilemap, i32 x, i32 y);\n"
"void pxl8_tilemap_set_tile(pxl8_tilemap* tilemap, u32 layer, u32 x, u32 y, u16 tile_id, u8 flags);\n"
"i32 pxl8_tilemap_set_tilesheet(pxl8_tilemap* tilemap, pxl8_tilesheet* tilesheet);\n"
2025-11-01 12:39:59 -05:00
"i32 pxl8_tilemap_load_ase(pxl8_tilemap* tilemap, const char* filepath, u32 layer);\n"
2025-10-04 04:13:48 -05:00
"pxl8_tilesheet* pxl8_tilesheet_create(u32 tile_size);\n"
"void pxl8_tilesheet_destroy(pxl8_tilesheet* tilesheet);\n"
"i32 pxl8_tilesheet_load(pxl8_tilesheet* tilesheet, const char* filepath, pxl8_gfx* gfx);\n"
"\n"
"typedef struct {\n"
" float angle;\n"
" float ax, ay, az;\n"
" unsigned int color;\n"
" unsigned int end_color;\n"
" unsigned char flags;\n"
" float life;\n"
" float max_life;\n"
" float size;\n"
" float spin;\n"
" unsigned int start_color;\n"
" float vx, vy, vz;\n"
" float x, y, z;\n"
"} pxl8_particle;\n"
"\n"
"typedef struct {\n"
" float amplitude;\n"
" float base_y;\n"
" unsigned int color;\n"
" unsigned int fade_color;\n"
" int height;\n"
" float phase;\n"
" float speed;\n"
"} pxl8_raster_bar;\n"
"\n"
"typedef struct pxl8_particles pxl8_particles;\n"
"pxl8_particles* pxl8_particles_create(u32 max_count);\n"
"void pxl8_particles_destroy(pxl8_particles* particles);\n"
"void pxl8_particles_clear(pxl8_particles* particles);\n"
"void pxl8_particles_emit(pxl8_particles* particles, u32 count);\n"
"void pxl8_particles_render(pxl8_particles* particles, pxl8_gfx* gfx);\n"
"void pxl8_particles_update(pxl8_particles* particles, float dt);\n"
"void pxl8_vfx_explosion(pxl8_particles* particles, int x, int y, unsigned int color, float force);\n"
"void pxl8_vfx_fire(pxl8_particles* particles, int x, int y, int width, unsigned char palette_start);\n"
"void pxl8_vfx_plasma(pxl8_gfx* ctx, f32 time, f32 scale1, f32 scale2, u8 palette_offset);\n"
"void pxl8_vfx_rain(pxl8_particles* particles, int width, float wind);\n"
"void pxl8_vfx_raster_bars(pxl8_gfx* ctx, pxl8_raster_bar* bars, u32 bar_count, f32 time);\n"
"void pxl8_vfx_rotozoom(pxl8_gfx* ctx, f32 angle, f32 zoom, i32 cx, i32 cy);\n"
"void pxl8_vfx_smoke(pxl8_particles* particles, int x, int y, unsigned char color);\n"
"void pxl8_vfx_snow(pxl8_particles* particles, int width, float wind);\n"
"void pxl8_vfx_sparks(pxl8_particles* particles, int x, int y, unsigned int color);\n"
"void pxl8_vfx_starfield(pxl8_particles* particles, float speed, float spread);\n"
"void pxl8_vfx_tunnel(pxl8_gfx* ctx, f32 time, f32 speed, f32 twist);\n"
"void pxl8_vfx_water_ripple(pxl8_gfx* ctx, f32* height_map, i32 drop_x, i32 drop_y, f32 damping);\n"
"\n"
"typedef struct pxl8_transition pxl8_transition;\n"
"typedef struct pxl8_anim pxl8_anim;\n"
"\n"
"pxl8_transition* pxl8_transition_create(i32 type, f32 duration);\n"
"void pxl8_transition_destroy(pxl8_transition* transition);\n"
"f32 pxl8_transition_get_progress(const pxl8_transition* transition);\n"
"bool pxl8_transition_is_active(const pxl8_transition* transition);\n"
"bool pxl8_transition_is_complete(const pxl8_transition* transition);\n"
"void pxl8_transition_render(const pxl8_transition* transition, pxl8_gfx* gfx);\n"
"void pxl8_transition_reset(pxl8_transition* transition);\n"
"void pxl8_transition_set_color(pxl8_transition* transition, u32 color);\n"
"void pxl8_transition_set_reverse(pxl8_transition* transition, bool reverse);\n"
"void pxl8_transition_start(pxl8_transition* transition);\n"
"void pxl8_transition_stop(pxl8_transition* transition);\n"
"void pxl8_transition_update(pxl8_transition* transition, f32 dt);\n"
"\n"
"pxl8_anim* pxl8_anim_create(const u32* frame_ids, const u16* frame_durations, u16 frame_count);\n"
"pxl8_anim* pxl8_anim_create_from_ase(pxl8_gfx* gfx, const char* path);\n"
"void pxl8_anim_destroy(pxl8_anim* anim);\n"
"i32 pxl8_anim_add_state(pxl8_anim* anim, const char* name, pxl8_anim* state_anim);\n"
"u16 pxl8_anim_get_current_frame(const pxl8_anim* anim);\n"
"u32 pxl8_anim_get_current_frame_id(const pxl8_anim* anim);\n"
"const char* pxl8_anim_get_state(const pxl8_anim* anim);\n"
"bool pxl8_anim_has_state_machine(const pxl8_anim* anim);\n"
"bool pxl8_anim_is_complete(const pxl8_anim* anim);\n"
"bool pxl8_anim_is_playing(const pxl8_anim* anim);\n"
"void pxl8_anim_pause(pxl8_anim* anim);\n"
"void pxl8_anim_play(pxl8_anim* anim);\n"
"void pxl8_anim_render_sprite(const pxl8_anim* anim, pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h);\n"
"void pxl8_anim_reset(pxl8_anim* anim);\n"
"void pxl8_anim_set_frame(pxl8_anim* anim, u16 frame);\n"
"void pxl8_anim_set_loop(pxl8_anim* anim, bool loop);\n"
"void pxl8_anim_set_reverse(pxl8_anim* anim, bool reverse);\n"
"void pxl8_anim_set_speed(pxl8_anim* anim, f32 speed);\n"
"i32 pxl8_anim_set_state(pxl8_anim* anim, const char* name);\n"
"void pxl8_anim_stop(pxl8_anim* anim);\n"
"void pxl8_anim_update(pxl8_anim* anim, f32 dt);\n"
"\n"
2025-10-04 04:13:48 -05:00
"typedef struct { float x, y, z; } pxl8_vec3;\n"
"typedef struct { float x, y, z, w; } pxl8_vec4;\n"
"typedef struct { float m[16]; } pxl8_mat4;\n"
"\n"
"void pxl8_3d_clear_zbuffer(pxl8_gfx* gfx);\n"
"void pxl8_3d_draw_line_3d(pxl8_gfx* gfx, pxl8_vec3 p0, pxl8_vec3 p1, u32 color);\n"
"void pxl8_3d_draw_triangle_raw(pxl8_gfx* gfx, pxl8_vec3 v0, pxl8_vec3 v1, pxl8_vec3 v2, u32 color);\n"
2025-10-05 16:25:17 -05:00
"void pxl8_3d_draw_triangle_textured(pxl8_gfx* gfx, pxl8_vec3 v0, pxl8_vec3 v1, pxl8_vec3 v2, f32 u0, f32 v0, f32 u1, f32 v1, f32 u2, f32 v2, u32 texture_id);\n"
"void pxl8_3d_set_affine_textures(pxl8_gfx* gfx, bool affine);\n"
2025-10-04 04:13:48 -05:00
"void pxl8_3d_set_backface_culling(pxl8_gfx* gfx, bool culling);\n"
"void pxl8_3d_set_model(pxl8_gfx* gfx, pxl8_mat4 mat);\n"
"void pxl8_3d_set_projection(pxl8_gfx* gfx, pxl8_mat4 mat);\n"
"void pxl8_3d_set_view(pxl8_gfx* gfx, pxl8_mat4 mat);\n"
"void pxl8_3d_set_wireframe(pxl8_gfx* gfx, bool wireframe);\n"
"pxl8_mat4 pxl8_mat4_identity(void);\n"
"pxl8_mat4 pxl8_mat4_lookat(pxl8_vec3 eye, pxl8_vec3 center, pxl8_vec3 up);\n"
"pxl8_mat4 pxl8_mat4_multiply(pxl8_mat4 a, pxl8_mat4 b);\n"
"pxl8_mat4 pxl8_mat4_ortho(float left, float right, float bottom, float top, float near, float far);\n"
"pxl8_mat4 pxl8_mat4_perspective(float fov, float aspect, float near, float far);\n"
"pxl8_mat4 pxl8_mat4_rotate_x(float angle);\n"
"pxl8_mat4 pxl8_mat4_rotate_y(float angle);\n"
"pxl8_mat4 pxl8_mat4_rotate_z(float angle);\n"
"pxl8_mat4 pxl8_mat4_scale(float x, float y, float z);\n"
2025-10-04 11:55:04 -05:00
"pxl8_mat4 pxl8_mat4_translate(float x, float y, float z);\n"
"\n"
2025-11-09 06:30:17 -06:00
"typedef enum pxl8_procgen_type {\n"
2025-11-20 20:55:45 -06:00
" PXL8_PROCGEN_ROOMS = 0,\n"
" PXL8_PROCGEN_TERRAIN = 1\n"
2025-11-09 06:30:17 -06:00
"} pxl8_procgen_type;\n"
"\n"
"typedef struct pxl8_procgen_params {\n"
" pxl8_procgen_type type;\n"
" int width;\n"
" int height;\n"
" int depth;\n"
" unsigned int seed;\n"
2025-11-20 20:55:45 -06:00
" int min_room_size;\n"
" int max_room_size;\n"
" int num_rooms;\n"
2025-11-09 06:30:17 -06:00
"} pxl8_procgen_params;\n"
"\n"
"typedef struct pxl8_procgen_tex_params {\n"
" char name[16];\n"
" unsigned int seed;\n"
" int width;\n"
" int height;\n"
" float scale;\n"
" float roughness;\n"
" unsigned char base_color;\n"
" unsigned char variation;\n"
"} pxl8_procgen_tex_params;\n"
"\n"
"void pxl8_procgen_tex(u8* buffer, const pxl8_procgen_tex_params* params);\n"
"\n"
"typedef struct pxl8_bsp pxl8_bsp;\n"
"typedef struct pxl8_bsp_face pxl8_bsp_face;\n"
"typedef bool (*pxl8_texture_rule)(const pxl8_vec3* normal, const pxl8_bsp_face* face, const pxl8_bsp* bsp);\n"
"\n"
"typedef struct pxl8_world_texture {\n"
" char name[16];\n"
" unsigned int texture_id;\n"
" pxl8_texture_rule rule;\n"
"} pxl8_world_texture;\n"
"\n"
2025-10-07 10:32:48 -05:00
"typedef struct pxl8_world pxl8_world;\n"
"pxl8_world* pxl8_world_create(void);\n"
"void pxl8_world_destroy(pxl8_world* world);\n"
"int pxl8_world_generate(pxl8_world* world, pxl8_gfx* gfx, const pxl8_procgen_params* params);\n"
2025-11-21 16:44:42 -06:00
"int pxl8_world_load(pxl8_world* world, const char* path);\n"
"void pxl8_world_unload(pxl8_world* world);\n"
"int pxl8_world_apply_textures(pxl8_world* world, const pxl8_world_texture* textures, unsigned int count);\n"
2025-11-21 16:44:42 -06:00
"bool pxl8_world_check_collision(const pxl8_world* world, pxl8_vec3 pos, float radius);\n"
2025-11-09 06:30:17 -06:00
"bool pxl8_world_is_loaded(const pxl8_world* world);\n"
2025-10-07 10:32:48 -05:00
"void pxl8_world_render(pxl8_world* world, pxl8_gfx* gfx, pxl8_vec3 camera_pos);\n"
2025-11-21 16:44:42 -06:00
"pxl8_vec3 pxl8_world_resolve_collision(const pxl8_world* world, pxl8_vec3 from, pxl8_vec3 to, float radius);\n"
2025-10-07 10:32:48 -05:00
"\n"
"typedef struct { i32 cursor_x; i32 cursor_y; bool cursor_down; bool cursor_clicked; u32 hot_id; u32 active_id; } pxl8_gui_state;\n"
"pxl8_gui_state* pxl8_gui_state_create(void);\n"
"void pxl8_gui_state_destroy(pxl8_gui_state* state);\n"
"void pxl8_gui_begin_frame(pxl8_gui_state* state);\n"
"void pxl8_gui_end_frame(pxl8_gui_state* state);\n"
"void pxl8_gui_cursor_move(pxl8_gui_state* state, i32 x, i32 y);\n"
"void pxl8_gui_cursor_down(pxl8_gui_state* state);\n"
"void pxl8_gui_cursor_up(pxl8_gui_state* state);\n"
"bool pxl8_gui_button(pxl8_gui_state* state, pxl8_gfx* gfx, u32 id, i32 x, i32 y, i32 w, i32 h, const char* label);\n"
"void pxl8_gui_window(pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h, const char* title);\n"
"void pxl8_gui_label(pxl8_gfx* gfx, i32 x, i32 y, const char* text, u8 color);\n"
"bool pxl8_gui_is_hovering(const pxl8_gui_state* state);\n"
"void pxl8_gui_get_cursor_pos(const pxl8_gui_state* state, i32* x, i32* y);\n"
"\n"
"typedef struct pxl8_save pxl8_save;\n"
"pxl8_save* pxl8_save_create(const char* game_name, u32 magic, u32 version);\n"
"void pxl8_save_destroy(pxl8_save* save);\n"
"i32 pxl8_save_write(pxl8_save* save, u8 slot, const u8* data, u32 size);\n"
"i32 pxl8_save_read(pxl8_save* save, u8 slot, u8** data_out, u32* size_out);\n"
"void pxl8_save_free(u8* data);\n"
"bool pxl8_save_exists(pxl8_save* save, u8 slot);\n"
"i32 pxl8_save_delete(pxl8_save* save, u8 slot);\n"
"const char* pxl8_save_get_directory(pxl8_save* save);\n";
2025-10-04 04:13:48 -05:00
2025-12-03 09:41:33 -06:00
void pxl8_lua_log(int level, const char* file, int line, const char* msg) {
if (file && (file[0] == '?' || file[0] == '\0')) file = NULL;
switch (level) {
case PXL8_LOG_LEVEL_TRACE: pxl8_log_write_trace(file, line, "%s", msg); break;
case PXL8_LOG_LEVEL_DEBUG: pxl8_log_write_debug(file, line, "%s", msg); break;
case PXL8_LOG_LEVEL_INFO: pxl8_log_write_info("%s", msg); break;
case PXL8_LOG_LEVEL_WARN: pxl8_log_write_warn(file, line, "%s", msg); break;
case PXL8_LOG_LEVEL_ERROR: pxl8_log_write_error(file, line, "%s", msg); break;
2025-12-03 09:41:33 -06:00
}
2025-10-04 04:13:48 -05:00
}
static void pxl8_script_set_error(pxl8_script* script, const char* error) {
if (!script) return;
if (!error) {
snprintf(script->last_error, sizeof(script->last_error), "Unknown error");
return;
}
const char* src = error;
char* dst = script->last_error;
char* end = dst + sizeof(script->last_error) - 1;
while (*src && dst < end) {
if (strncmp(src, "[string \"", 9) == 0) {
src += 9;
const char* mod_start = src;
while (*src && !(*src == '"' && *(src+1) == ']')) src++;
size_t mod_len = src - mod_start;
if (mod_len > 4 && strncmp(mod_start, "pxl8", 4) == 0) {
const char* prefix = "src/lua/";
while (*prefix && dst < end) *dst++ = *prefix++;
for (size_t i = 0; i < mod_len && dst < end; i++) {
*dst++ = (mod_start[i] == '.') ? '/' : mod_start[i];
}
const char* suffix = ".lua";
while (*suffix && dst < end) *dst++ = *suffix++;
} else {
for (size_t i = 0; i < mod_len && dst < end; i++) {
*dst++ = mod_start[i];
}
}
if (*src == '"' && *(src+1) == ']') src += 2;
} else {
*dst++ = *src++;
}
}
*dst = '\0';
2025-10-04 04:13:48 -05:00
}
const char* pxl8_script_get_last_error(pxl8_script* script) {
return script ? script->last_error : "Invalid script context";
}
2025-11-28 14:41:35 -06:00
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(bool repl_mode) {
2025-10-17 17:54:33 -05:00
pxl8_script* script = (pxl8_script*)calloc(1, sizeof(pxl8_script));
2025-10-04 04:13:48 -05:00
if (!script) return NULL;
script->repl_mode = repl_mode;
2025-10-04 04:13:48 -05:00
script->L = luaL_newstate();
if (!script->L) {
2025-10-17 17:54:33 -05:00
free(script);
2025-10-04 04:13:48 -05:00
return NULL;
}
luaL_openlibs(script->L);
lua_getglobal(script->L, "require");
lua_pushstring(script->L, "ffi");
if (lua_pcall(script->L, 1, 1, 0) != 0) {
pxl8_script_set_error(script, lua_tostring(script->L, -1));
pxl8_script_destroy(script);
return NULL;
}
lua_getfield(script->L, -1, "cdef");
lua_pushstring(script->L, pxl8_ffi_cdefs);
if (lua_pcall(script->L, 1, 0, 0) != 0) {
pxl8_script_set_error(script, lua_tostring(script->L, -1));
pxl8_script_destroy(script);
return NULL;
}
lua_pop(script->L, 1);
2025-11-28 14:41:35 -06:00
pxl8_install_embed_searcher(script->L);
2025-10-04 04:13:48 -05:00
2025-11-28 14:41:35 -06:00
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");
2025-10-06 18:14:07 -05:00
2025-11-28 14:41:35 -06:00
lua_getglobal(script->L, "fennel");
lua_getfield(script->L, -1, "install");
if (lua_isfunction(script->L, -1)) {
lua_newtable(script->L);
lua_pushboolean(script->L, 1);
lua_setfield(script->L, -2, "correlate");
2025-12-03 09:41:33 -06:00
lua_pushboolean(script->L, 1);
lua_setfield(script->L, -2, "lambdaAsFn");
if (script->repl_mode) {
lua_pushboolean(script->L, 1);
lua_setfield(script->L, -2, "useMetadata");
lua_pushboolean(script->L, 1);
lua_setfield(script->L, -2, "assertAsRepl");
}
if (lua_pcall(script->L, 1, 0, 0) != 0) {
2025-11-28 14:41:35 -06:00
pxl8_warn("Failed to install fennel searcher: %s", lua_tostring(script->L, -1));
lua_pop(script->L, 1);
}
lua_getglobal(script->L, "package");
lua_getfield(script->L, -1, "loaders");
if (lua_isnil(script->L, -1)) {
lua_pop(script->L, 1);
lua_getfield(script->L, -1, "searchers");
}
if (lua_istable(script->L, -1)) {
int n = (int)lua_objlen(script->L, -1);
for (int i = n; i >= 2; i--) {
lua_rawgeti(script->L, -1, i);
lua_rawseti(script->L, -2, i + 1);
}
lua_pushcfunction(script->L, pxl8_cart_searcher);
lua_rawseti(script->L, -2, 2);
}
lua_pop(script->L, 2);
2025-11-28 14:41:35 -06:00
} else {
2025-10-06 18:14:07 -05:00
lua_pop(script->L, 1);
}
2025-11-28 14:41:35 -06:00
lua_pop(script->L, 1);
2025-10-06 18:14:07 -05:00
} else {
2025-11-28 14:41:35 -06:00
pxl8_warn("Failed to execute fennel: %s", lua_tostring(script->L, -1));
2025-10-06 18:14:07 -05:00
lua_pop(script->L, 1);
}
2025-10-04 04:13:48 -05:00
}
script->last_error[0] = '\0';
2025-12-03 09:41:33 -06:00
script->repl_env_ref = LUA_NOREF;
if (script->repl_mode) {
lua_newtable(script->L);
lua_newtable(script->L);
lua_getglobal(script->L, "_G");
lua_setfield(script->L, -2, "__index");
lua_setmetatable(script->L, -2);
script->repl_env_ref = luaL_ref(script->L, LUA_REGISTRYINDEX);
}
2025-10-04 04:13:48 -05:00
return script;
}
void pxl8_script_destroy(pxl8_script* script) {
if (!script) return;
if (script->L) {
2025-12-03 09:41:33 -06:00
if (script->repl_env_ref != LUA_NOREF) {
luaL_unref(script->L, LUA_REGISTRYINDEX, script->repl_env_ref);
}
2025-10-04 04:13:48 -05:00
lua_close(script->L);
}
2025-10-17 17:54:33 -05:00
free(script);
2025-10-04 04:13:48 -05:00
}
void pxl8_script_set_gfx(pxl8_script* script, pxl8_gfx* gfx) {
if (!script) return;
script->gfx = gfx;
if (script->L && gfx) {
lua_pushlightuserdata(script->L, gfx);
lua_setglobal(script->L, "_pxl8_gfx");
}
}
void pxl8_script_set_input(pxl8_script* script, pxl8_input_state* input) {
if (!script) return;
script->input = input;
if (script->L && input) {
lua_pushlightuserdata(script->L, input);
lua_setglobal(script->L, "_pxl8_input");
}
}
2025-11-18 23:50:02 -06:00
void pxl8_script_set_sys(pxl8_script* script, void* sys) {
2025-11-11 23:26:51 -06:00
if (!script) return;
2025-11-18 23:50:02 -06:00
if (script->L && sys) {
lua_pushlightuserdata(script->L, sys);
lua_setglobal(script->L, "_pxl8_sys");
2025-11-11 23:26:51 -06:00
}
}
2025-11-01 12:39:59 -05:00
static pxl8_result pxl8_script_prepare_path(pxl8_script* script, const char* filename, char* out_basename, size_t basename_size) {
char filename_copy[PATH_MAX];
pxl8_strncpy(filename_copy, filename, sizeof(filename_copy));
2025-11-01 12:39:59 -05:00
2025-10-04 04:13:48 -05:00
char* last_slash = strrchr(filename_copy, '/');
if (last_slash) {
*last_slash = '\0';
2025-11-01 12:39:59 -05:00
char script_dir[PATH_MAX];
char original_cwd[PATH_MAX];
if (realpath(filename_copy, script_dir) && getcwd(original_cwd, sizeof(original_cwd))) {
2025-10-04 04:13:48 -05:00
chdir(script_dir);
pxl8_script_set_cart_path(script, script_dir, original_cwd);
2025-11-01 12:39:59 -05:00
strncpy(out_basename, last_slash + 1, basename_size - 1);
out_basename[basename_size - 1] = '\0';
} else {
strncpy(out_basename, filename, basename_size - 1);
out_basename[basename_size - 1] = '\0';
2025-10-04 04:13:48 -05:00
}
2025-11-01 12:39:59 -05:00
} else {
strncpy(out_basename, filename, basename_size - 1);
out_basename[basename_size - 1] = '\0';
2025-10-04 04:13:48 -05:00
}
2025-10-04 11:55:04 -05:00
return PXL8_OK;
}
pxl8_result pxl8_script_run_file(pxl8_script* script, const char* filename) {
if (!script || !script->L || !filename) return PXL8_ERROR_NULL_POINTER;
2025-11-01 12:39:59 -05:00
char basename[PATH_MAX];
pxl8_script_prepare_path(script, filename, basename, sizeof(basename));
2025-10-04 11:55:04 -05:00
2025-10-04 04:13:48 -05:00
pxl8_result result = PXL8_OK;
if (luaL_dofile(script->L, basename) != 0) {
pxl8_script_set_error(script, lua_tostring(script->L, -1));
lua_pop(script->L, 1);
result = PXL8_ERROR_SCRIPT_ERROR;
} else {
script->last_error[0] = '\0';
}
return result;
}
void pxl8_script_set_cart_path(pxl8_script* script, const char* cart_path, const char* original_cwd) {
if (!script || !script->L || !cart_path || !original_cwd) return;
lua_getglobal(script->L, "package");
lua_getfield(script->L, -1, "path");
const char* current_path = lua_tostring(script->L, -1);
char new_path[2048];
snprintf(new_path, sizeof(new_path),
"%s/?.lua;%s/?/init.lua;%s/src/?.lua;%s/src/?/init.lua;%s/src/lua/?.lua;%s",
cart_path, cart_path, cart_path, cart_path, original_cwd,
current_path ? current_path : "");
lua_pushstring(script->L, new_path);
lua_setfield(script->L, -3, "path");
lua_pop(script->L, 2);
lua_getglobal(script->L, "fennel");
if (!lua_isnil(script->L, -1)) {
lua_getfield(script->L, -1, "path");
const char* fennel_path = lua_tostring(script->L, -1);
snprintf(new_path, sizeof(new_path),
"%s/?.fnl;%s/?/init.fnl;%s/src/?.fnl;%s/src/?/init.fnl;%s",
cart_path, cart_path, cart_path, cart_path,
fennel_path ? fennel_path : "");
lua_pushstring(script->L, new_path);
lua_setfield(script->L, -3, "path");
lua_pop(script->L, 1);
}
lua_pop(script->L, 1);
}
pxl8_result pxl8_script_run_fennel_file(pxl8_script* script, const char* filename) {
if (!script || !script->L || !filename) return PXL8_ERROR_NULL_POINTER;
2025-11-01 12:39:59 -05:00
char basename[PATH_MAX];
pxl8_script_prepare_path(script, filename, basename, sizeof(basename));
2025-10-04 04:13:48 -05:00
lua_getglobal(script->L, "fennel");
if (lua_isnil(script->L, -1)) {
pxl8_script_set_error(script, "Fennel not loaded");
lua_pop(script->L, 1);
return PXL8_ERROR_SCRIPT_ERROR;
}
pxl8_cart* cart = pxl8_cart_current();
2025-10-04 04:13:48 -05:00
pxl8_result result = PXL8_OK;
if (cart && pxl8_cart_is_packed(cart)) {
u8* data = NULL;
u32 size = 0;
if (pxl8_cart_read_file(cart, basename, &data, &size) != PXL8_OK) {
pxl8_script_set_error(script, "Failed to read script from cart");
lua_pop(script->L, 1);
return PXL8_ERROR_FILE_NOT_FOUND;
}
lua_getfield(script->L, -1, "eval");
lua_pushlstring(script->L, (const char*)data, size);
lua_createtable(script->L, 0, 1);
lua_pushstring(script->L, basename);
lua_setfield(script->L, -2, "filename");
pxl8_cart_free_file(data);
if (lua_pcall(script->L, 2, 0, 0) != 0) {
pxl8_script_set_error(script, lua_tostring(script->L, -1));
lua_pop(script->L, 1);
result = PXL8_ERROR_SCRIPT_ERROR;
} else {
script->last_error[0] = '\0';
}
2025-10-04 04:13:48 -05:00
} else {
lua_getfield(script->L, -1, "dofile");
lua_pushstring(script->L, basename);
if (lua_pcall(script->L, 1, 0, 0) != 0) {
pxl8_script_set_error(script, lua_tostring(script->L, -1));
lua_pop(script->L, 1);
result = PXL8_ERROR_SCRIPT_ERROR;
} else {
script->last_error[0] = '\0';
}
2025-10-04 04:13:48 -05:00
}
lua_pop(script->L, 1);
return result;
}
2025-11-01 12:39:59 -05:00
static pxl8_result pxl8_script_eval_internal(pxl8_script* script, const char* code, bool repl_mode) {
2025-10-04 04:13:48 -05:00
if (!script || !script->L || !code) return PXL8_ERROR_NULL_POINTER;
2025-11-01 12:39:59 -05:00
char transformed_code[PXL8_MAX_REPL_COMMAND_SIZE];
if (repl_mode) {
pxl8_script_repl_promote_locals(code, transformed_code, sizeof(transformed_code));
code = transformed_code;
}
2025-10-04 04:13:48 -05:00
lua_getglobal(script->L, "fennel");
if (lua_isnil(script->L, -1)) {
pxl8_script_set_error(script, "Fennel not loaded");
lua_pop(script->L, 1);
return PXL8_ERROR_SCRIPT_ERROR;
}
lua_getfield(script->L, -1, "eval");
lua_pushstring(script->L, code);
2025-11-01 12:39:59 -05:00
lua_newtable(script->L);
lua_pushboolean(script->L, true);
2025-12-03 09:41:33 -06:00
lua_setfield(script->L, -2, "useMetadata");
if (repl_mode && script->repl_env_ref != LUA_NOREF) {
lua_rawgeti(script->L, LUA_REGISTRYINDEX, script->repl_env_ref);
lua_setfield(script->L, -2, "env");
lua_pushboolean(script->L, false);
lua_setfield(script->L, -2, "allowedGlobals");
}
2025-11-01 12:39:59 -05:00
if (lua_pcall(script->L, 2, 1, 0) != 0) {
const char* error = lua_tostring(script->L, -1);
if (error) {
char cleaned_error[2048];
size_t j = 0;
for (size_t i = 0; error[i] && j < sizeof(cleaned_error) - 1; i++) {
if (error[i] == '\t') {
cleaned_error[j++] = ' ';
} else {
cleaned_error[j++] = error[i];
}
}
cleaned_error[j] = '\0';
pxl8_script_set_error(script, cleaned_error);
} else {
pxl8_script_set_error(script, "Unknown error");
}
lua_pop(script->L, 2);
2025-10-04 04:13:48 -05:00
return PXL8_ERROR_SCRIPT_ERROR;
}
2025-11-01 12:39:59 -05:00
if (!lua_isnil(script->L, -1)) {
lua_pushvalue(script->L, -1);
lua_setglobal(script->L, "_");
lua_getglobal(script->L, "tostring");
lua_pushvalue(script->L, -2);
if (lua_pcall(script->L, 1, 1, 0) == 0) {
const char* result = lua_tostring(script->L, -1);
if (result && strlen(result) > 0) {
printf("=> %s\n", result);
fflush(stdout);
}
lua_pop(script->L, 1);
}
}
lua_pop(script->L, 2);
2025-10-04 04:13:48 -05:00
script->last_error[0] = '\0';
return PXL8_OK;
}
2025-11-01 12:39:59 -05:00
pxl8_result pxl8_script_eval(pxl8_script* script, const char* code) {
return pxl8_script_eval_internal(script, code, false);
}
pxl8_result pxl8_script_eval_repl(pxl8_script* script, const char* code) {
return pxl8_script_eval_internal(script, code, true);
}
2025-10-04 04:13:48 -05:00
pxl8_result pxl8_script_load_module(pxl8_script* script, const char* module_name) {
if (!script || !script->L || !module_name) return PXL8_ERROR_NULL_POINTER;
lua_getglobal(script->L, "require");
lua_pushstring(script->L, module_name);
if (lua_pcall(script->L, 1, 1, 0) != 0) {
pxl8_script_set_error(script, lua_tostring(script->L, -1));
lua_pop(script->L, 1);
return PXL8_ERROR_SCRIPT_ERROR;
}
lua_setglobal(script->L, module_name);
script->last_error[0] = '\0';
return PXL8_OK;
}
pxl8_result pxl8_script_call_function(pxl8_script* script, const char* name) {
if (!script || !script->L || !name) return PXL8_ERROR_NULL_POINTER;
lua_getglobal(script->L, name);
if (!lua_isfunction(script->L, -1)) {
lua_pop(script->L, 1);
return PXL8_OK;
}
if (lua_pcall(script->L, 0, 0, 0) != 0) {
pxl8_script_set_error(script, lua_tostring(script->L, -1));
lua_pop(script->L, 1);
return PXL8_ERROR_SCRIPT_ERROR;
}
script->last_error[0] = '\0';
return PXL8_OK;
}
pxl8_result pxl8_script_call_function_f32(pxl8_script* script, const char* name, f32 arg) {
if (!script || !script->L || !name) return PXL8_ERROR_NULL_POINTER;
lua_getglobal(script->L, name);
if (!lua_isfunction(script->L, -1)) {
lua_pop(script->L, 1);
return PXL8_OK;
}
lua_pushnumber(script->L, arg);
if (lua_pcall(script->L, 1, 0, 0) != 0) {
pxl8_script_set_error(script, lua_tostring(script->L, -1));
lua_pop(script->L, 1);
return PXL8_ERROR_SCRIPT_ERROR;
}
script->last_error[0] = '\0';
return PXL8_OK;
}
static time_t get_file_mod_time(const char* path) {
struct stat file_stat;
if (stat(path, &file_stat) == 0) {
return file_stat.st_mtime;
}
return 0;
}
static time_t get_latest_script_mod_time(const char* dir_path) {
DIR* dir = opendir(dir_path);
if (!dir) return 0;
time_t latest = 0;
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] == '.') continue;
char full_path[512];
snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, entry->d_name);
2025-10-04 04:13:48 -05:00
struct stat st;
if (stat(full_path, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
time_t subdir_time = get_latest_script_mod_time(full_path);
if (subdir_time > latest) {
latest = subdir_time;
}
} else {
size_t len = strlen(entry->d_name);
bool is_script = (len > 4 && strcmp(entry->d_name + len - 4, ".fnl") == 0) ||
(len > 4 && strcmp(entry->d_name + len - 4, ".lua") == 0);
if (is_script) {
time_t mod_time = get_file_mod_time(full_path);
if (mod_time > latest) {
latest = mod_time;
}
}
2025-10-04 04:13:48 -05:00
}
}
}
closedir(dir);
return latest;
}
pxl8_result pxl8_script_load_main(pxl8_script* script, const char* path) {
if (!script || !path) return PXL8_ERROR_NULL_POINTER;
pxl8_strncpy(script->main_path, path, sizeof(script->main_path));
2025-10-04 04:13:48 -05:00
char* last_slash = strrchr(script->main_path, '/');
if (last_slash) {
size_t dir_len = last_slash - script->main_path;
strncpy(script->watch_dir, script->main_path, dir_len);
script->watch_dir[dir_len] = '\0';
} else {
2025-11-28 14:41:35 -06:00
script->watch_dir[0] = '.';
script->watch_dir[1] = '\0';
2025-10-04 04:13:48 -05:00
}
script->latest_mod_time = get_latest_script_mod_time(script->watch_dir);
const char* ext = strrchr(path, '.');
pxl8_result result = PXL8_ERROR_INVALID_FORMAT;
if (ext && strcmp(ext, ".fnl") == 0) {
result = pxl8_script_run_fennel_file(script, path);
} else if (ext && strcmp(ext, ".lua") == 0) {
result = pxl8_script_run_file(script, path);
} else {
pxl8_script_set_error(script, "Unknown script type (expected .fnl or .lua)");
return PXL8_ERROR_INVALID_FORMAT;
}
if (result == PXL8_OK) {
pxl8_info("Loaded script: %s", path);
} else {
pxl8_error("Failed to load script: %s", script->last_error);
2025-10-04 04:13:48 -05:00
}
return result;
}
bool pxl8_script_check_reload(pxl8_script* script) {
if (!script || script->main_path[0] == '\0') return false;
time_t current_mod_time = get_latest_script_mod_time(script->watch_dir);
if (current_mod_time > script->latest_mod_time && current_mod_time != 0) {
pxl8_info("Script files modified, reloading: %s", script->main_path);
script->latest_mod_time = current_mod_time;
const char* ext = strrchr(script->main_path, '.');
if (ext && strcmp(ext, ".fnl") == 0) {
if (pxl8_script_run_fennel_file(script, script->main_path) == PXL8_OK) {
pxl8_script_call_function(script, "init");
return true;
}
} else if (ext && strcmp(ext, ".lua") == 0) {
if (pxl8_script_run_file(script, script->main_path) == PXL8_OK) {
pxl8_script_call_function(script, "init");
return true;
}
}
}
return false;
}
2025-10-17 17:54:33 -05:00
2025-11-01 12:39:59 -05:00
static bool pxl8_script_is_incomplete_error(const char* error) {
if (!error) return false;
return strstr(error, "expected closing delimiter") != NULL ||
strstr(error, "unexpected end of source") != NULL ||
strstr(error, "expected whitespace before") != NULL ||
strstr(error, "unexpected end") != NULL;
}
bool pxl8_script_is_incomplete_input(pxl8_script* script) {
if (!script) return false;
return pxl8_script_is_incomplete_error(script->last_error);
2025-10-17 17:54:33 -05:00
}
static pxl8_resolution parse_resolution(const char* str) {
if (strcmp(str, "240x160") == 0) return PXL8_RESOLUTION_240x160;
if (strcmp(str, "320x180") == 0) return PXL8_RESOLUTION_320x180;
if (strcmp(str, "320x240") == 0) return PXL8_RESOLUTION_320x240;
if (strcmp(str, "640x360") == 0) return PXL8_RESOLUTION_640x360;
if (strcmp(str, "640x480") == 0) return PXL8_RESOLUTION_640x480;
if (strcmp(str, "800x600") == 0) return PXL8_RESOLUTION_800x600;
if (strcmp(str, "960x540") == 0) return PXL8_RESOLUTION_960x540;
return PXL8_RESOLUTION_640x360;
}
static pxl8_pixel_mode parse_pixel_mode(const char* str) {
if (strcmp(str, "indexed") == 0) return PXL8_PIXEL_INDEXED;
if (strcmp(str, "hicolor") == 0) return PXL8_PIXEL_HICOLOR;
return PXL8_PIXEL_INDEXED;
}
pxl8_result pxl8_script_load_cart_manifest(pxl8_script* script, pxl8_cart* cart) {
if (!script || !script->L || !cart) return PXL8_ERROR_NULL_POINTER;
if (!pxl8_cart_file_exists(cart, "cart.fnl")) {
return PXL8_OK;
}
u8* data = NULL;
u32 size = 0;
if (pxl8_cart_read_file(cart, "cart.fnl", &data, &size) != PXL8_OK) {
return PXL8_OK;
}
lua_getglobal(script->L, "fennel");
if (lua_isnil(script->L, -1)) {
lua_pop(script->L, 1);
pxl8_cart_free_file(data);
return PXL8_OK;
}
lua_getfield(script->L, -1, "eval");
lua_pushlstring(script->L, (const char*)data, size);
lua_createtable(script->L, 0, 1);
lua_pushstring(script->L, "cart.fnl");
lua_setfield(script->L, -2, "filename");
pxl8_cart_free_file(data);
if (lua_pcall(script->L, 2, 1, 0) != 0) {
pxl8_warn("Failed to load cart.fnl: %s", lua_tostring(script->L, -1));
lua_pop(script->L, 2);
return PXL8_OK;
}
if (lua_istable(script->L, -1)) {
lua_getfield(script->L, -1, "title");
if (lua_isstring(script->L, -1)) {
pxl8_cart_set_title(cart, lua_tostring(script->L, -1));
}
lua_pop(script->L, 1);
lua_getfield(script->L, -1, "resolution");
if (lua_isstring(script->L, -1)) {
pxl8_cart_set_resolution(cart, parse_resolution(lua_tostring(script->L, -1)));
}
lua_pop(script->L, 1);
lua_getfield(script->L, -1, "pixel-mode");
if (lua_isstring(script->L, -1)) {
pxl8_cart_set_pixel_mode(cart, parse_pixel_mode(lua_tostring(script->L, -1)));
}
lua_pop(script->L, 1);
lua_getfield(script->L, -1, "window-size");
if (lua_istable(script->L, -1)) {
lua_rawgeti(script->L, -1, 1);
lua_rawgeti(script->L, -2, 2);
if (lua_isnumber(script->L, -2) && lua_isnumber(script->L, -1)) {
pxl8_size size = {(i32)lua_tonumber(script->L, -2), (i32)lua_tonumber(script->L, -1)};
pxl8_cart_set_window_size(cart, size);
}
lua_pop(script->L, 2);
}
lua_pop(script->L, 1);
}
lua_pop(script->L, 2);
return PXL8_OK;
}