Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lua-bindings/surface.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "../luablitlib.hpp"

extern std::string base_path;

using namespace blit;

static Surface *to_surface(lua_State *L, int arg) {
Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 5 additions & 2 deletions lua/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
17 changes: 13 additions & 4 deletions lua/wrap/stdio.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifdef TARGET_32BLIT_HW

#include <cstdarg>
#include <stdio.h>
#include <cstring>

#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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -213,4 +219,7 @@ int wrap_fflush(wrap_FILE *file)
return 0;
}

#endif
wrap_FILE *wrap_tmpfile()
{
return nullptr;
}
3 changes: 3 additions & 0 deletions lua/wrap/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down