Updated the SFML support stuff to use the Config that can be oriented at a BASE_DIR to find stuff relative to the config file.

This commit is contained in:
Zed A. Shaw 2025-05-13 02:02:47 -04:00
parent 4d71f552aa
commit de0d957c66
6 changed files with 28 additions and 8 deletions

View file

@ -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<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;
}

View file

@ -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<sf::Shader>();
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);

View file

@ -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

View file

@ -14,7 +14,9 @@ namespace textures {
Config assets("assets/config.json");
for(auto& [name, settings] : assets["sprites"].items()) {
auto texture = make_shared<sf::Texture>(settings["path"]);
auto file_name = settings["path"];
auto file_path = Config::path_to(file_name);
auto texture = make_shared<sf::Texture>(file_path);
texture->setSmooth(assets["graphics"]["smooth_textures"]);
auto sprite = make_shared<sf::Sprite>(*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;
}