From 530a65a48090997571d6d9047ee7a108eac9e23c Mon Sep 17 00:00:00 2001 From: PyryM Date: Thu, 10 Jun 2021 19:36:48 +0300 Subject: [PATCH] 0.1.1: More flexibility with physFS paths, deferred SDL init (#59) * Option whether to mount paths as relative or not * defer SDL init until window creation (scripts that don't ever create a window now never init SDL (prevents error spam on headless systems)) --- dist/include/truss_api.h | 2 +- dist/scripts/core/core.t | 2 +- src/addons/sdl/sdl_addon.cpp | 23 ++++++++++++++++------- src/addons/sdl/sdl_addon.h | 3 +++ src/truss/core.cpp | 10 +++++++--- src/truss/core.h | 2 +- src/truss/trussapi.cpp | 4 ++-- src/trussapi.h | 4 ++-- 8 files changed, 33 insertions(+), 17 deletions(-) diff --git a/dist/include/truss_api.h b/dist/include/truss_api.h index 5629f659..e2443403 100644 --- a/dist/include/truss_api.h +++ b/dist/include/truss_api.h @@ -48,7 +48,7 @@ const char* truss_get_file_real_path(const char* filename); truss_message* truss_load_file(const char* filename); int truss_save_file(const char* filename, truss_message* data); int truss_save_data(const char* filename, const char* data, unsigned int datalength); -int truss_add_fs_path(const char* path, const char* mountpath, int append); +int truss_add_fs_path(const char* path, const char* mountpath, int append, int relative); int truss_set_fs_savedir(const char* path); int truss_set_raw_write_dir(const char* path); int truss_list_directory(truss_interpreter_id interpreter, const char* path); diff --git a/dist/scripts/core/core.t b/dist/scripts/core/core.t index 529278f6..db44df45 100644 --- a/dist/scripts/core/core.t +++ b/dist/scripts/core/core.t @@ -492,7 +492,7 @@ local function add_paths() local physicalPath = truss.args[i+1] local mountPath = truss.args[i+2] log.info(("Adding path %s => %s"):format(physicalPath, mountPath)) - truss.C.add_fs_path(physicalPath, mountPath, 0) + truss.C.add_fs_path(physicalPath, mountPath, 0, 0) end end end diff --git a/src/addons/sdl/sdl_addon.cpp b/src/addons/sdl/sdl_addon.cpp index 3129c806..b50998a1 100644 --- a/src/addons/sdl/sdl_addon.cpp +++ b/src/addons/sdl/sdl_addon.cpp @@ -138,22 +138,29 @@ const std::string& SDLAddon::getVersion() { void SDLAddon::init(truss::Interpreter* owner) { owner_ = owner; + sdlIsInit_ = false; +} - // Init SDL - std::cout << "Going to create window; if you get an LLVM crash on linux" << - " at this point, the mostly likely reason is that you are using" << - " the mesa software renderer.\n"; +void SDLAddon::SDLinit() { + if (sdlIsInit_) { + return; + } + truss_log(TRUSS_LOG_DEBUG, "SDL Init. Segfaults near this point are likely due to video drivers that use LLVM."); SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0"); SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1); if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) != 0) { std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl; + return; } + sdlIsInit_ = true; } void SDLAddon::shutdown() { - destroyWindow(); - SDL_Quit(); + if (sdlIsInit_) { + destroyWindow(); + SDL_Quit(); + } } void copyKeyName(truss_sdl_event& newEvent, SDL_Event& event) { @@ -262,7 +269,7 @@ void SDLAddon::convertAndPushEvent_(SDL_Event& event) { } void SDLAddon::update(double dt) { - if (window_ == NULL) { + if (!sdlIsInit_ || window_ == NULL) { return; } @@ -276,6 +283,7 @@ void SDLAddon::update(double dt) { } void SDLAddon::createWindow(int width, int height, const char* name, int is_fullscreen, int display) { + SDLinit(); uint32_t flags = SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI; if (is_fullscreen > 0) { flags = flags | SDL_WINDOW_BORDERLESS; @@ -306,6 +314,7 @@ void SDLAddon::createWindow(int width, int height, const char* name, int is_full } void SDLAddon::createWindow(int x, int y, int w, int h, const char* name, int is_borderless) { + SDLinit(); uint32_t flags = SDL_WINDOW_SHOWN; if (is_borderless > 0) { flags = flags | SDL_WINDOW_BORDERLESS; diff --git a/src/addons/sdl/sdl_addon.h b/src/addons/sdl/sdl_addon.h index 75f9098c..1865dc17 100644 --- a/src/addons/sdl/sdl_addon.h +++ b/src/addons/sdl/sdl_addon.h @@ -97,6 +97,7 @@ class SDLAddon : public truss::Addon { const std::string& getHeader(); const std::string& getVersion(); void init(truss::Interpreter* owner); + void SDLinit(); void shutdown(); void update(double dt); @@ -132,6 +133,8 @@ class SDLAddon : public truss::Addon { std::string clipboard_; std::string filedrop_; + bool sdlIsInit_; + SDL_Window* window_; SDL_Event event_; truss::Interpreter* owner_; diff --git a/src/truss/core.cpp b/src/truss/core.cpp index 8ea8fb96..b67ff287 100644 --- a/src/truss/core.cpp +++ b/src/truss/core.cpp @@ -88,9 +88,12 @@ void Core::initFS(char* argv0, bool mountBaseDir) { physFSInitted_ = true; } -void Core::addFSPath(const char* pathname, const char* mountname, bool append) { +void Core::addFSPath(const char* pathname, const char* mountname, bool append, bool relative) { std::stringstream ss; - ss << PHYSFS_getBaseDir() << PHYSFS_getDirSeparator() << pathname; + if(relative){ + ss << PHYSFS_getBaseDir(); // includes path separator + } + ss << pathname; logPrint(TRUSS_LOG_DEBUG, "Adding physFS path: %s", ss.str().c_str()); int retval = PHYSFS_mount(ss.str().c_str(), mountname, append); @@ -131,7 +134,8 @@ void Core::extractLibraries() { void Core::setWriteDir(const char* writepath) { std::stringstream ss; - ss << PHYSFS_getBaseDir() << PHYSFS_getDirSeparator() << writepath; + // dir separator not needed, included in getBaseDir + ss << PHYSFS_getBaseDir() << writepath; logPrint(TRUSS_LOG_DEBUG, "Setting physFS write path: %s", ss.str().c_str()); int retval = PHYSFS_setWriteDir(ss.str().c_str()); diff --git a/src/truss/core.h b/src/truss/core.h index b8c82ab4..4eacafcd 100644 --- a/src/truss/core.h +++ b/src/truss/core.h @@ -20,7 +20,7 @@ class Core { // functions for dealing with physfs (you can also make direct physfs // calls if you need to, after you've called initFS) void initFS(char* argv0, bool mountBaseDir=true); - void addFSPath(const char* pathname, const char* mountname, bool append=true); + void addFSPath(const char* pathname, const char* mountname, bool append=true, bool relative=true); void setWriteDir(const char* writepath); void setRawWriteDir(const char* path, bool mount=true); void extractLibraries(); diff --git a/src/truss/trussapi.cpp b/src/truss/trussapi.cpp index d4f67906..0b8605f7 100644 --- a/src/truss/trussapi.cpp +++ b/src/truss/trussapi.cpp @@ -72,8 +72,8 @@ int truss_save_data(const char* filename, const char* data, unsigned int datalen return 0; } -int truss_add_fs_path(const char* path, const char* mountpath, int append) { - core().addFSPath(path, mountpath, append); +int truss_add_fs_path(const char* path, const char* mountpath, int append, int relative) { + core().addFSPath(path, mountpath, append > 0, relative > 0); return 0; } diff --git a/src/trussapi.h b/src/trussapi.h index b89932c1..6d006aef 100644 --- a/src/trussapi.h +++ b/src/trussapi.h @@ -6,7 +6,7 @@ #ifndef TRUSSAPI_H_HEADER_GUARD #define TRUSSAPI_H_HEADER_GUARD -#define TRUSS_VERSION_STRING "0.1.0" +#define TRUSS_VERSION_STRING "0.1.1" #include // Needed for uint64_t etc. #include // Needed for size_t etc. @@ -82,7 +82,7 @@ TRUSS_C_API const char* truss_get_file_real_path(const char* filename); TRUSS_C_API truss_message* truss_load_file(const char* filename); TRUSS_C_API int truss_save_file(const char* filename, truss_message* data); TRUSS_C_API int truss_save_data(const char* filename, const char* data, unsigned int datalength); -TRUSS_C_API int truss_add_fs_path(const char* path, const char* mountpath, int append); +TRUSS_C_API int truss_add_fs_path(const char* path, const char* mountpath, int append, int relative); TRUSS_C_API int truss_set_fs_savedir(const char* path); TRUSS_C_API int truss_set_raw_write_dir(const char* path); TRUSS_C_API int truss_list_directory(truss_interpreter_id interpreter, const char* path);