From ba3e3cc2b489b1de3823e535621b07551938e595 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Thu, 9 Nov 2023 15:00:00 +0000 Subject: [PATCH 1/5] Use the stdio wrapper for pico builds too Also the check out of the cpp --- lua/CMakeLists.txt | 4 ++-- lua/wrap/stdio.cpp | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/lua/CMakeLists.txt b/lua/CMakeLists.txt index a670ba4..2659afd 100644 --- a/lua/CMakeLists.txt +++ b/lua/CMakeLists.txt @@ -34,11 +34,11 @@ add_library(lua STATIC ldblib.c liolib.c loslib.c - wrap/stdio.cpp ) target_include_directories(lua PRIVATE ${32BLIT_DIR}/32blit) -if(32BLIT_HW) +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..a22639a 100644 --- a/lua/wrap/stdio.cpp +++ b/lua/wrap/stdio.cpp @@ -1,5 +1,3 @@ -#ifdef TARGET_32BLIT_HW - #include #include #include @@ -212,5 +210,3 @@ int wrap_fflush(wrap_FILE *file) { return 0; } - -#endif \ No newline at end of file From cc3c889744c5a307f663b8fddc0ba8f1152b1657 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Sat, 14 Sep 2024 15:24:49 +0100 Subject: [PATCH 2/5] Stub tmpfile This is still broken but now it compiles --- lua/wrap/stdio.cpp | 5 +++++ lua/wrap/stdio.h | 3 +++ 2 files changed, 8 insertions(+) diff --git a/lua/wrap/stdio.cpp b/lua/wrap/stdio.cpp index a22639a..d3049f5 100644 --- a/lua/wrap/stdio.cpp +++ b/lua/wrap/stdio.cpp @@ -210,3 +210,8 @@ int wrap_fflush(wrap_FILE *file) { return 0; } + +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 } From 66a5210afdd58a6a991273f0025e2d65437cf380 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Sat, 14 Sep 2024 15:27:46 +0100 Subject: [PATCH 3/5] Simplify search path Looking in /usr/local/share on a blit is not so useful. (For SDL this would be [game dir]/usr/local/,,, which is about as bad) --- lua/CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lua/CMakeLists.txt b/lua/CMakeLists.txt index 2659afd..21c65c9 100644 --- a/lua/CMakeLists.txt +++ b/lua/CMakeLists.txt @@ -38,6 +38,9 @@ add_library(lua STATIC target_include_directories(lua PRIVATE ${32BLIT_DIR}/32blit) +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) From eaddd34bf6c80897b8afe1a1be5e93771c4416ee Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Sat, 14 Sep 2024 15:45:34 +0100 Subject: [PATCH 4/5] Resolve reletive paths relative to the launch script This means that require "thing" now works --- lua/wrap/stdio.cpp | 10 +++++++++- main.cpp | 11 +++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lua/wrap/stdio.cpp b/lua/wrap/stdio.cpp index d3049f5..893295a 100644 --- a/lua/wrap/stdio.cpp +++ b/lua/wrap/stdio.cpp @@ -4,6 +4,8 @@ #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; @@ -44,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; 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) { From 295e8ec8bad1106e1e8c7073f02e242cda7fb19a Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Sat, 14 Sep 2024 17:06:39 +0100 Subject: [PATCH 5/5] Also load sprites relative to the launched script --- lua-bindings/surface.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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; }