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:
parent
4d71f552aa
commit
de0d957c66
6 changed files with 28 additions and 8 deletions
|
@ -2,8 +2,10 @@
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <codecvt>
|
#include <codecvt>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
struct Config {
|
struct Config {
|
||||||
|
static std::filesystem::path BASE_DIR;
|
||||||
nlohmann::json $config;
|
nlohmann::json $config;
|
||||||
std::string $src_path;
|
std::string $src_path;
|
||||||
|
|
||||||
|
@ -16,4 +18,7 @@ struct Config {
|
||||||
nlohmann::json &json() { return $config; };
|
nlohmann::json &json() { return $config; };
|
||||||
std::wstring wstring(const std::string main_key, const std::string sub_key);
|
std::wstring wstring(const std::string main_key, const std::string sub_key);
|
||||||
std::vector<std::string> keys();
|
std::vector<std::string> keys();
|
||||||
|
|
||||||
|
static void set_base_dir(const char *optarg);
|
||||||
|
static std::filesystem::path path_to(const std::string& path);
|
||||||
};
|
};
|
||||||
|
|
|
@ -16,7 +16,7 @@ namespace sound {
|
||||||
};
|
};
|
||||||
|
|
||||||
void init();
|
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(const std::string& name, bool loop=false);
|
||||||
void play_at(const std::string& name, float x, float y, float z);
|
void play_at(const std::string& name, float x, float y, float z);
|
||||||
void stop(const std::string& name);
|
void stop(const std::string& name);
|
||||||
|
|
|
@ -4,8 +4,10 @@
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
using fmt::format;
|
using fmt::format;
|
||||||
|
|
||||||
|
std::filesystem::path Config::BASE_DIR{"."};
|
||||||
|
|
||||||
Config::Config(const std::string src_path) : $src_path(src_path) {
|
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);
|
$config = json::parse(infile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,3 +35,12 @@ std::vector<std::string> Config::keys() {
|
||||||
|
|
||||||
return the_fucking_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;
|
||||||
|
}
|
||||||
|
|
|
@ -16,9 +16,10 @@ namespace shaders {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool load_shader(std::string name, nlohmann::json& settings) {
|
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>();
|
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) {
|
if(good) {
|
||||||
configure_shader_defaults(ptr);
|
configure_shader_defaults(ptr);
|
||||||
|
|
|
@ -30,13 +30,13 @@ namespace sound {
|
||||||
Config assets("assets/config.json");
|
Config assets("assets/config.json");
|
||||||
|
|
||||||
for(auto& el : assets["sounds"].items()) {
|
for(auto& el : assets["sounds"].items()) {
|
||||||
load(el.key(), el.value());
|
load(el.key(), Config::path_to(el.value()));
|
||||||
}
|
}
|
||||||
initialized = true;
|
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");
|
assert(fs::exists(sound_path) && "sound file does not exist");
|
||||||
|
|
||||||
// create the buffer and keep in the buffer map
|
// create the buffer and keep in the buffer map
|
||||||
|
|
|
@ -14,7 +14,9 @@ namespace textures {
|
||||||
Config assets("assets/config.json");
|
Config assets("assets/config.json");
|
||||||
|
|
||||||
for(auto& [name, settings] : assets["sprites"].items()) {
|
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"]);
|
texture->setSmooth(assets["graphics"]["smooth_textures"]);
|
||||||
auto sprite = make_shared<sf::Sprite>(*texture);
|
auto sprite = make_shared<sf::Sprite>(*texture);
|
||||||
|
@ -51,7 +53,8 @@ namespace textures {
|
||||||
|
|
||||||
sf::Image load_image(const std::string& filename) {
|
sf::Image load_image(const std::string& filename) {
|
||||||
sf::Image texture;
|
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");
|
assert(good && "failed to load image file");
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue