diff --git a/lua-bindings/surface.cpp b/lua-bindings/surface.cpp index 92b92c6..a02c674 100644 --- a/lua-bindings/surface.cpp +++ b/lua-bindings/surface.cpp @@ -1,5 +1,7 @@ #include "../luablitlib.hpp" +extern std::string base_path; + using namespace blit; static Surface *to_surface(lua_State *L, int arg) { @@ -237,7 +239,7 @@ static int surface_load_sprites(lua_State *L, Surface *surface, int first_arg) { delete surface->sprites; surface->sprites = nullptr; } - surface->sprites = Surface::load(filename); + surface->sprites = Surface::load(base_path + "/" + filename); return 0; } diff --git a/lua/CMakeLists.txt b/lua/CMakeLists.txt index a670ba4..21c65c9 100644 --- a/lua/CMakeLists.txt +++ b/lua/CMakeLists.txt @@ -34,11 +34,14 @@ add_library(lua STATIC ldblib.c liolib.c loslib.c - wrap/stdio.cpp ) target_include_directories(lua PRIVATE ${32BLIT_DIR}/32blit) -if(32BLIT_HW) +set(LUA_PATH "\"/lib/?.lua\;/lib/?/init.lua\;./?.lua\;./?/init.lua\"") +target_compile_definitions(lua PRIVATE "LUA_PATH_DEFAULT=${LUA_PATH}") + +if(32BLIT_HW OR CMAKE_SYSTEM_NAME STREQUAL "PICO") target_include_directories(lua SYSTEM PUBLIC wrap) + target_sources(lua PRIVATE wrap/stdio.cpp) endif() \ No newline at end of file diff --git a/lua/wrap/stdio.cpp b/lua/wrap/stdio.cpp index a2ec3b5..893295a 100644 --- a/lua/wrap/stdio.cpp +++ b/lua/wrap/stdio.cpp @@ -1,11 +1,11 @@ -#ifdef TARGET_32BLIT_HW - #include #include #include #include "engine/file.hpp" +extern std::string base_path; + // wrap stdio funcs around blit:: funcs wrap_FILE *wrap_stdin = nullptr; wrap_FILE *wrap_stdout = nullptr; @@ -46,8 +46,14 @@ wrap_FILE *wrap_fopen(const char *filename, const char *mode) blit_mode |= blit::OpenMode::read; } + std::string filename_str = filename; + + // adjust relative filenames to root + if(filename[0] == '.' && filename[1] == '/') + filename_str = base_path + (filename + 1); + auto ret = new wrap_FILE; - ret->file.open(filename, blit_mode); + ret->file.open(filename_str, blit_mode); ret->offset = 0; ret->getc_buf_len = ret->getc_buf_off = 0; @@ -213,4 +219,7 @@ int wrap_fflush(wrap_FILE *file) return 0; } -#endif \ No newline at end of file +wrap_FILE *wrap_tmpfile() +{ + return nullptr; +} diff --git a/lua/wrap/stdio.h b/lua/wrap/stdio.h index 36820b2..bd37b30 100644 --- a/lua/wrap/stdio.h +++ b/lua/wrap/stdio.h @@ -29,6 +29,8 @@ int wrap_ferror(wrap_FILE *file); int wrap_clearerr(wrap_FILE *file); int wrap_fflush(wrap_FILE *file); +wrap_FILE *wrap_tmpfile(); + extern wrap_FILE *wrap_stdin; extern wrap_FILE *wrap_stdout; extern wrap_FILE *wrap_stderr; @@ -54,6 +56,7 @@ extern wrap_FILE *wrap_stderr; #define ferror wrap_ferror #define clearerr wrap_clearerr #define fflush wrap_fflush +#define tmpfile wrap_tmpfile #ifdef __cplusplus } diff --git a/main.cpp b/main.cpp index 5b768b9..8104043 100644 --- a/main.cpp +++ b/main.cpp @@ -8,6 +8,8 @@ lua_State *L; bool has_update = true; bool has_render = true; +std::string base_path; + void init() { set_screen_mode(ScreenMode::lores); screen.pen = Pen(0, 0, 0, 255); @@ -20,9 +22,18 @@ void init() { lua_blit_update_state(L); auto launchPath = blit::get_launch_path(); + if(!launchPath) { launchPath = "main.lua"; } + + + // set base path from dir of main script + auto pos = std::string_view(launchPath).find_last_of('/'); + if(pos != std::string_view::npos) + base_path = std::string_view(launchPath).substr(0, pos); + + // load the script auto err = luaL_loadfile(L, launchPath); if(err) {