Slight rework so that config can have a base dir but the program can start in another one.
This commit is contained in:
parent
d1c2352237
commit
3d4ddde96e
8 changed files with 32 additions and 13 deletions
|
@ -7,10 +7,6 @@
|
||||||
"file_name": "assets/shaders/ui_error.frag",
|
"file_name": "assets/shaders/ui_error.frag",
|
||||||
"type": "fragment"
|
"type": "fragment"
|
||||||
},
|
},
|
||||||
"rayview_sprites": {
|
|
||||||
"file_name": "assets/shaders/rayview_sprites.frag",
|
|
||||||
"type": "fragment"
|
|
||||||
},
|
|
||||||
"flame": {
|
"flame": {
|
||||||
"file_name": "assets/shaders/flame_trash.frag",
|
"file_name": "assets/shaders/flame_trash.frag",
|
||||||
"type": "fragment"
|
"type": "fragment"
|
||||||
|
|
12
config.cpp
12
config.cpp
|
@ -5,8 +5,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,11 @@ 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;
|
||||||
|
}
|
||||||
|
|
|
@ -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);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "guecs.hpp"
|
#include "guecs.hpp"
|
||||||
#include "shaders.hpp"
|
#include "shaders.hpp"
|
||||||
#include "sound.hpp"
|
#include "sound.hpp"
|
||||||
|
#include "config.hpp"
|
||||||
|
|
||||||
namespace guecs {
|
namespace guecs {
|
||||||
|
|
||||||
|
@ -108,7 +109,7 @@ namespace guecs {
|
||||||
}
|
}
|
||||||
|
|
||||||
UI::UI() {
|
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) {
|
void UI::position(int x, int y, int width, int height) {
|
||||||
|
|
6
main.cpp
6
main.cpp
|
@ -1,5 +1,6 @@
|
||||||
#include "builder.hpp"
|
#include "builder.hpp"
|
||||||
#include "gui.hpp"
|
#include "gui.hpp"
|
||||||
|
#include "config.hpp"
|
||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
#include "sound.hpp"
|
#include "sound.hpp"
|
||||||
#include "textures.hpp"
|
#include "textures.hpp"
|
||||||
|
@ -11,12 +12,15 @@ int main(int argc, char *argv[])
|
||||||
int opt = 0;
|
int opt = 0;
|
||||||
int timer_seconds = 60 * 60;
|
int timer_seconds = 60 * 60;
|
||||||
|
|
||||||
while((opt = getopt(argc, argv, "-t:")) != -1) {
|
while((opt = getopt(argc, argv, "t:c:")) != -1) {
|
||||||
switch(opt) {
|
switch(opt) {
|
||||||
case 't':
|
case 't':
|
||||||
timer_seconds = atoi(optarg) * 60;
|
timer_seconds = atoi(optarg) * 60;
|
||||||
fmt::println("Setting count down timer to {} seconds.", timer_seconds);
|
fmt::println("Setting count down timer to {} seconds.", timer_seconds);
|
||||||
break;
|
break;
|
||||||
|
case 'c':
|
||||||
|
Config::set_base_dir(optarg);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,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"];
|
std::string file_name = settings["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(Config::path_to(file_name),
|
||||||
|
sf::Shader::Type::Fragment);
|
||||||
|
|
||||||
if(good) {
|
if(good) {
|
||||||
configure_shader_defaults(ptr);
|
configure_shader_defaults(ptr);
|
||||||
|
|
|
@ -28,7 +28,7 @@ namespace sound {
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
if(!initialized) {
|
if(!initialized) {
|
||||||
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(), el.value());
|
||||||
|
@ -38,10 +38,11 @@ namespace sound {
|
||||||
}
|
}
|
||||||
|
|
||||||
void load(const std::string& name, const std::string& sound_path) {
|
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
|
// 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
|
// set it on the sound and keep in the sound map
|
||||||
auto sound = make_shared<sf::Sound>(*buffer);
|
auto sound = make_shared<sf::Sound>(*buffer);
|
||||||
|
|
|
@ -15,7 +15,7 @@ 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 texture = make_shared<sf::Texture>(Config::path_to(settings["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);
|
||||||
|
@ -66,7 +66,7 @@ namespace textures {
|
||||||
|
|
||||||
sf::Image load_image(std::string filename) {
|
sf::Image load_image(std::string filename) {
|
||||||
sf::Image texture;
|
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));
|
dbc::check(good, fmt::format("failed to load {}", filename));
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue