Slight rework so that config can have a base dir but the program can start in another one.

master
Zed A. Shaw 21 hours ago
parent d1c2352237
commit 3d4ddde96e
  1. 4
      assets/shaders.json
  2. 12
      config.cpp
  3. 5
      config.hpp
  4. 3
      guecs.cpp
  5. 6
      main.cpp
  6. 4
      shaders.cpp
  7. 7
      sound.cpp
  8. 4
      textures.cpp

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

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

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

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

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

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

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

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

Loading…
Cancel
Save