diff --git a/include/guecs/sfml/config.hpp b/include/guecs/sfml/config.hpp index 52bf664..2ca8d52 100644 --- a/include/guecs/sfml/config.hpp +++ b/include/guecs/sfml/config.hpp @@ -2,8 +2,10 @@ #include #include #include +#include struct Config { + static std::filesystem::path BASE_DIR; nlohmann::json $config; std::string $src_path; @@ -16,4 +18,7 @@ struct Config { nlohmann::json &json() { return $config; }; std::wstring wstring(const std::string main_key, const std::string sub_key); std::vector keys(); + + static void set_base_dir(const char *optarg); + static std::filesystem::path path_to(const std::string& path); }; diff --git a/include/guecs/sfml/sound.hpp b/include/guecs/sfml/sound.hpp index 3dc21cc..80241cf 100644 --- a/include/guecs/sfml/sound.hpp +++ b/include/guecs/sfml/sound.hpp @@ -16,7 +16,7 @@ namespace sound { }; void init(); - void load(const std::string& name, const std::string& path); + void load(const std::string& name, const std::filesystem::path& path); void play(const std::string& name, bool loop=false); void play_at(const std::string& name, float x, float y, float z); void stop(const std::string& name); diff --git a/src/guecs/sfml/config.cpp b/src/guecs/sfml/config.cpp index 3118126..8ecd101 100644 --- a/src/guecs/sfml/config.cpp +++ b/src/guecs/sfml/config.cpp @@ -4,8 +4,10 @@ using nlohmann::json; using fmt::format; +std::filesystem::path Config::BASE_DIR{"."}; + Config::Config(const std::string src_path) : $src_path(src_path) { - std::ifstream infile($src_path); + std::ifstream infile(Config::path_to($src_path)); $config = json::parse(infile); } @@ -33,3 +35,12 @@ std::vector Config::keys() { return the_fucking_keys; } + + +void Config::set_base_dir(const char *optarg) { + Config::BASE_DIR.assign(optarg); +} + +std::filesystem::path Config::path_to(const std::string& path) { + return Config::BASE_DIR / path; +} diff --git a/src/guecs/sfml/shaders.cpp b/src/guecs/sfml/shaders.cpp index d6194ac..a235454 100644 --- a/src/guecs/sfml/shaders.cpp +++ b/src/guecs/sfml/shaders.cpp @@ -16,9 +16,10 @@ namespace shaders { } bool load_shader(std::string name, nlohmann::json& settings) { - std::string file_name = settings["file_name"]; + auto file_name = settings["file_name"]; + auto file_path = Config::path_to(file_name); auto ptr = std::make_shared(); - bool good = ptr->loadFromFile(file_name, sf::Shader::Type::Fragment); + bool good = ptr->loadFromFile(file_path, sf::Shader::Type::Fragment); if(good) { configure_shader_defaults(ptr); diff --git a/src/guecs/sfml/sound.cpp b/src/guecs/sfml/sound.cpp index 9e1812a..3fcb2e3 100644 --- a/src/guecs/sfml/sound.cpp +++ b/src/guecs/sfml/sound.cpp @@ -30,13 +30,13 @@ namespace sound { Config assets("assets/config.json"); for(auto& el : assets["sounds"].items()) { - load(el.key(), el.value()); + load(el.key(), Config::path_to(el.value())); } initialized = true; } } - void load(const std::string& name, const std::string& sound_path) { + void load(const std::string& name, const std::filesystem::path& sound_path) { assert(fs::exists(sound_path) && "sound file does not exist"); // create the buffer and keep in the buffer map diff --git a/src/guecs/sfml/textures.cpp b/src/guecs/sfml/textures.cpp index cd6b2f1..c4cecdc 100644 --- a/src/guecs/sfml/textures.cpp +++ b/src/guecs/sfml/textures.cpp @@ -14,7 +14,9 @@ namespace textures { Config assets("assets/config.json"); for(auto& [name, settings] : assets["sprites"].items()) { - auto texture = make_shared(settings["path"]); + auto file_name = settings["path"]; + auto file_path = Config::path_to(file_name); + auto texture = make_shared(file_path); texture->setSmooth(assets["graphics"]["smooth_textures"]); auto sprite = make_shared(*texture); @@ -51,7 +53,8 @@ namespace textures { sf::Image load_image(const std::string& filename) { sf::Image texture; - bool good = texture.loadFromFile(filename); + auto file_path = Config::path_to(filename); + bool good = texture.loadFromFile(file_path); assert(good && "failed to load image file"); return texture; }