diff --git a/assets/shaders.json b/assets/shaders.json index 70fb534..fe50f0e 100644 --- a/assets/shaders.json +++ b/assets/shaders.json @@ -7,10 +7,6 @@ "file_name": "assets/shaders/ui_error.frag", "type": "fragment" }, - "rayview_sprites": { - "file_name": "assets/shaders/rayview_sprites.frag", - "type": "fragment" - }, "flame": { "file_name": "assets/shaders/flame_trash.frag", "type": "fragment" diff --git a/config.cpp b/config.cpp index ea3ef62..d6478d8 100644 --- a/config.cpp +++ b/config.cpp @@ -5,8 +5,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,11 @@ std::vector<std::string> 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/config.hpp b/config.hpp index 52bf664..2ca8d52 100644 --- a/config.hpp +++ b/config.hpp @@ -2,8 +2,10 @@ #include <nlohmann/json.hpp> #include <fstream> #include <codecvt> +#include <filesystem> 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<std::string> keys(); + + static void set_base_dir(const char *optarg); + static std::filesystem::path path_to(const std::string& path); }; diff --git a/guecs.cpp b/guecs.cpp index 5512002..9ba642b 100644 --- a/guecs.cpp +++ b/guecs.cpp @@ -1,6 +1,7 @@ #include "guecs.hpp" #include "shaders.hpp" #include "sound.hpp" +#include "config.hpp" namespace guecs { @@ -108,7 +109,7 @@ namespace guecs { } UI::UI() { - $font = make_shared<sf::Font>(FONT_FILE_NAME); + $font = make_shared<sf::Font>(Config::path_to(FONT_FILE_NAME)); } void UI::position(int x, int y, int width, int height) { diff --git a/main.cpp b/main.cpp index 9721ade..091800f 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include "builder.hpp" #include "gui.hpp" +#include "config.hpp" #include <fmt/core.h> #include "sound.hpp" #include "textures.hpp" @@ -11,12 +12,15 @@ int main(int argc, char *argv[]) int opt = 0; int timer_seconds = 60 * 60; - while((opt = getopt(argc, argv, "-t:")) != -1) { + while((opt = getopt(argc, argv, "t:c:")) != -1) { switch(opt) { case 't': timer_seconds = atoi(optarg) * 60; fmt::println("Setting count down timer to {} seconds.", timer_seconds); break; + case 'c': + Config::set_base_dir(optarg); + break; } } diff --git a/shaders.cpp b/shaders.cpp index 011a73a..6ff8d2b 100644 --- a/shaders.cpp +++ b/shaders.cpp @@ -18,8 +18,10 @@ namespace shaders { bool load_shader(std::string name, nlohmann::json& settings) { std::string file_name = settings["file_name"]; + auto ptr = std::make_shared<sf::Shader>(); - bool good = ptr->loadFromFile(file_name, sf::Shader::Type::Fragment); + bool good = ptr->loadFromFile(Config::path_to(file_name), + sf::Shader::Type::Fragment); if(good) { configure_shader_defaults(ptr); diff --git a/sound.cpp b/sound.cpp index cf8c48a..94966f8 100644 --- a/sound.cpp +++ b/sound.cpp @@ -28,7 +28,7 @@ namespace sound { void init() { if(!initialized) { - Config assets("assets/config.json"); + Config assets("./assets/config.json"); for(auto& el : assets["sounds"].items()) { load(el.key(), el.value()); @@ -38,10 +38,11 @@ namespace sound { } void load(const std::string& name, const std::string& sound_path) { - dbc::check(fs::exists(sound_path), fmt::format("sound file {} does not exist", sound_path)); + dbc::check(fs::exists(Config::path_to(sound_path)), + fmt::format("sound file {} does not exist", sound_path)); // create the buffer and keep in the buffer map - auto buffer = make_shared<sf::SoundBuffer>(sound_path); + auto buffer = make_shared<sf::SoundBuffer>(Config::path_to(sound_path)); // set it on the sound and keep in the sound map auto sound = make_shared<sf::Sound>(*buffer); diff --git a/textures.cpp b/textures.cpp index eb067dd..de7c581 100644 --- a/textures.cpp +++ b/textures.cpp @@ -15,7 +15,7 @@ namespace textures { Config assets("assets/config.json"); for(auto& [name, settings] : assets["sprites"].items()) { - auto texture = make_shared<sf::Texture>(settings["path"]); + auto texture = make_shared<sf::Texture>(Config::path_to(settings["path"])); texture->setSmooth(assets["graphics"]["smooth_textures"]); auto sprite = make_shared<sf::Sprite>(*texture); @@ -66,7 +66,7 @@ namespace textures { sf::Image load_image(std::string filename) { sf::Image texture; - bool good = texture.loadFromFile(filename); + bool good = texture.loadFromFile(Config::path_to(filename)); dbc::check(good, fmt::format("failed to load {}", filename)); return texture; }