Skip to content

Commit

Permalink
0.1.1: More flexibility with physFS paths, deferred SDL init (#59)
Browse files Browse the repository at this point in the history
* 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))
  • Loading branch information
PyryM authored Jun 10, 2021
1 parent 502126c commit 530a65a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dist/include/truss_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion dist/scripts/core/core.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 16 additions & 7 deletions src/addons/sdl/sdl_addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -262,7 +269,7 @@ void SDLAddon::convertAndPushEvent_(SDL_Event& event) {
}

void SDLAddon::update(double dt) {
if (window_ == NULL) {
if (!sdlIsInit_ || window_ == NULL) {
return;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/addons/sdl/sdl_addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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_;
Expand Down
10 changes: 7 additions & 3 deletions src/truss/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/truss/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/truss/trussapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/trussapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstdint> // Needed for uint64_t etc.
#include <cstddef> // Needed for size_t etc.
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 530a65a

Please sign in to comment.