Skip to content

Commit

Permalink
Adjust lua path for SDL builds to root everything to game dir
Browse files Browse the repository at this point in the history
Gets us path handling consistent with device builds without having to use the stdio wrapper.
  • Loading branch information
Daft-Freak committed Sep 15, 2024
1 parent 9ea88ce commit 77e34fb
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
#include "lua/lua.hpp"
#include "luablitlib.hpp"

// really wants to be "if SDL"
#if !defined(TARGET_32BLIT_HW) && !defined(PICO_BUILD)
#include "SDL.h"
#define TARGET_SDL
#endif

using namespace blit;

lua_State *L;
Expand All @@ -10,6 +16,35 @@ bool has_render = true;

std::string base_path;

static void setup_paths() {
#ifdef TARGET_SDL
// get SDL base path and prepend to what we calculated from the launch path
auto full_base_path = base_path;

auto sdl_base_path = SDL_GetBasePath();
if(sdl_base_path)
full_base_path = std::string(sdl_base_path) + full_base_path;
SDL_free(sdl_base_path);

if(full_base_path.back() == '/')
full_base_path.pop_back();

// construct lua path rooted at base dir
std::string lua_path;

lua_path.append(full_base_path).append("/lib/?.lua;")
.append(full_base_path).append("/lib/?/init.lua;")
.append(full_base_path).append("/?.lua;")
.append(full_base_path).append("/?/init.lua;");

// set package.path
lua_getglobal(L, "package");
lua_pushstring(L, lua_path.c_str());
lua_setfield(L, -2, "path");
lua_pop(L, 1);
#endif
}

void init() {
set_screen_mode(ScreenMode::lores);
screen.pen = Pen(0, 0, 0, 255);
Expand All @@ -33,6 +68,8 @@ void init() {
if(pos != std::string_view::npos)
base_path = std::string_view(launchPath).substr(0, pos);

setup_paths();

// load the script
auto err = luaL_loadfile(L, launchPath);

Expand Down

0 comments on commit 77e34fb

Please sign in to comment.