diff --git a/Makefile b/Makefile index ece7c1d..a3f6721 100644 --- a/Makefile +++ b/Makefile @@ -60,7 +60,7 @@ clean: meson compile --clean -C builddir debug_test: build - gdb --nx -x .gdbinit --ex run --ex bt --ex q --args builddir/runtests -e + gdb --nx -x .gdbinit --ex run --ex bt --ex q --args builddir/runtests -e "[lighting]" win_installer: powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" scripts\win_installer.ifp' diff --git a/ai.cpp b/ai.cpp index c6b93e6..fa72b81 100644 --- a/ai.cpp +++ b/ai.cpp @@ -66,7 +66,7 @@ namespace ai { void init(std::string config_path) { if(!initialized) { - Config config(config_path); + auto config = settings::get(config_path); // profile specifies what keys (bitset indexes) are allowed // and how they map to the bitset of State diff --git a/animation.cpp b/animation.cpp index f0dd2c4..7075ba8 100644 --- a/animation.cpp +++ b/animation.cpp @@ -97,8 +97,8 @@ namespace animation { void init() { if(!initialized) { - Config animations("assets/animations.json"); - Config config("assets/config.json"); + auto animations = settings::get("animations"); + auto config = settings::get("config"); auto& sprites = config["sprites"]; for(auto& [name, data] : animations.json().items()) { diff --git a/assets/config.json b/assets/config.json index abd8a82..1ebfcc2 100644 --- a/assets/config.json +++ b/assets/config.json @@ -265,5 +265,8 @@ "text_size": 20, "label_size": 20, "font_file_name": "assets/text.otf" + }, + "player": { + "hands": "male_hand" } } diff --git a/backend.cpp b/backend.cpp index ef0e8e3..d62904e 100644 --- a/backend.cpp +++ b/backend.cpp @@ -47,7 +47,7 @@ namespace sfml { guecs::Theme Backend::theme() { palette::init(); - auto config = Config("assets/config.json")["theme"]; + auto config = settings::Config("assets/config.json")["theme"]; guecs::Theme theme { .BLACK=palette::get("gui/theme:black"), @@ -71,7 +71,7 @@ namespace sfml { theme.BG_COLOR = palette::get("gui/theme:bg_color"); theme.BORDER_COLOR = palette::get("gui/theme:border_color"); theme.BG_COLOR_DARK = palette::get("gui/theme:bg_color_dark"); - theme.FONT_FILE_NAME = Config::path_to(config["font_file_name"]).string(); + theme.FONT_FILE_NAME = settings::Config::path_to(config["font_file_name"]).string(); return theme; } diff --git a/components.hpp b/components.hpp index 30336fb..1585645 100644 --- a/components.hpp +++ b/components.hpp @@ -52,13 +52,13 @@ namespace components { }; struct GameConfig { - Config game; - Config enemies; - Config items; - Config tiles; - Config devices; - Config bosses; - Config rituals; + settings::Config game; + settings::Config enemies; + settings::Config items; + settings::Config tiles; + settings::Config devices; + settings::Config bosses; + settings::Config rituals; }; struct Personality { diff --git a/config.cpp b/config.cpp index d549f3a..8fd53cf 100644 --- a/config.cpp +++ b/config.cpp @@ -2,51 +2,66 @@ #include "dbc.hpp" #include -using nlohmann::json; -using fmt::format; +namespace settings { + using nlohmann::json; + using fmt::format; -std::filesystem::path Config::BASE_DIR{"."}; + std::filesystem::path Config::BASE_DIR{"."}; -Config::Config(const std::string src_path) : $src_path(src_path) { - auto path_to = Config::path_to($src_path); - dbc::check(std::filesystem::exists(path_to), - fmt::format("requested config file {} doesn't exist", path_to.string())); - std::ifstream infile(path_to); - $config = json::parse(infile); -} - -nlohmann::json &Config::operator[](size_t key) { - return $config[key]; -} - -json &Config::operator[](const std::string &key) { - dbc::check($config.contains(key), fmt::format("ERROR in config, key {} doesn't exist.", key)); - return $config[key]; -} - -std::wstring Config::wstring(const std::string main_key, const std::string sub_key) { - dbc::check($config.contains(main_key), fmt::format("ERROR wstring main/key in config, main_key {} doesn't exist.", main_key)); - dbc::check($config[main_key].contains(sub_key), fmt::format("ERROR wstring in config, main_key/key {}/{} doesn't exist.", main_key, sub_key)); - - const std::string& str_val = $config[main_key][sub_key]; - std::wstring_convert> $converter; - return $converter.from_bytes(str_val); -} - -std::vector Config::keys() { - std::vector the_fucking_keys; - - for(auto& [key, value] : $config.items()) { - the_fucking_keys.push_back(key); + Config::Config(const std::string src_path) : $src_path(src_path) { + auto path_to = Config::path_to($src_path); + dbc::check(std::filesystem::exists(path_to), + fmt::format("requested config file {} doesn't exist", path_to.string())); + std::ifstream infile(path_to); + $config = json::parse(infile); } - return the_fucking_keys; -} + nlohmann::json &Config::operator[](size_t key) { + return $config[key]; + } -void Config::set_base_dir(const char *optarg) { - Config::BASE_DIR.assign(optarg); -} + json &Config::operator[](const std::string &key) { + dbc::check($config.contains(key), fmt::format("ERROR in config, key {} doesn't exist.", key)); + return $config[key]; + } -std::filesystem::path Config::path_to(const std::string& path) { - return Config::BASE_DIR / path; + std::wstring Config::wstring(const std::string main_key, const std::string sub_key) { + dbc::check($config.contains(main_key), fmt::format("ERROR wstring main/key in config, main_key {} doesn't exist.", main_key)); + dbc::check($config[main_key].contains(sub_key), fmt::format("ERROR wstring in config, main_key/key {}/{} doesn't exist.", main_key, sub_key)); + + const std::string& str_val = $config[main_key][sub_key]; + std::wstring_convert> $converter; + return $converter.from_bytes(str_val); + } + + std::vector Config::keys() { + std::vector the_fucking_keys; + + for(auto& [key, value] : $config.items()) { + the_fucking_keys.push_back(key); + } + + 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; + } + + Config get(const std::string& name) { + if(name.ends_with(".json")) { + return {name}; + } else { + auto path = Config::BASE_DIR / fmt::format("assets/{}.json", name); + + dbc::check(std::filesystem::exists(path), fmt::format( + "config file {} does not exist", path.string())); + + return {path.string()}; + } + } } diff --git a/config.hpp b/config.hpp index d26f5a2..2512e3d 100644 --- a/config.hpp +++ b/config.hpp @@ -4,22 +4,26 @@ #include #include -struct Config { - static std::filesystem::path BASE_DIR; - nlohmann::json $config; - std::string $src_path; +namespace settings { + struct Config { + static std::filesystem::path BASE_DIR; + nlohmann::json $config; + std::string $src_path; - Config(const std::string src_path); + Config(const std::string src_path); - Config(nlohmann::json config, std::string src_path) - : $config(config), $src_path(src_path) {} + Config(nlohmann::json config, std::string src_path) + : $config(config), $src_path(src_path) {} - nlohmann::json &operator[](size_t); - nlohmann::json &operator[](const std::string &key); - nlohmann::json &json() { return $config; }; - std::wstring wstring(const std::string main_key, const std::string sub_key); - std::vector keys(); + nlohmann::json &operator[](size_t); + nlohmann::json &operator[](const std::string &key); + nlohmann::json &json() { return $config; }; + std::wstring wstring(const std::string main_key, const std::string sub_key); + std::vector keys(); - static void set_base_dir(const char *optarg); - static std::filesystem::path path_to(const std::string& path); -}; + static void set_base_dir(const char *optarg); + static std::filesystem::path path_to(const std::string& path); + }; + + Config get(const std::string &name); +} diff --git a/game_level.cpp b/game_level.cpp index c5fb221..243a16f 100644 --- a/game_level.cpp +++ b/game_level.cpp @@ -2,7 +2,6 @@ #include "components.hpp" #include "worldbuilder.hpp" #include "constants.hpp" -#include "save.hpp" #include "systems.hpp" #include "components.hpp" #include "rituals.hpp" @@ -21,7 +20,7 @@ inline shared_ptr clone_load_world(shared_ptr auto world = make_shared(); if(prev_world == nullptr) { - save::load_configs(*world); + GameDB::load_configs(*world); } else { prev_world->clone_into(*world); } @@ -134,4 +133,16 @@ namespace GameDB { dbc::check(initialized, "Forgot to call GameDB::init()"); return current_level().player; } + + void load_configs(DinkyECS::World &world) { + world.set_the({ + settings::get("config"), + settings::get("enemies"), + settings::get("items"), + settings::get("tiles"), + settings::get("devices"), + settings::get("bosses"), + settings::get("rituals") + }); + } } diff --git a/game_level.hpp b/game_level.hpp index 6db3470..3cc50e7 100644 --- a/game_level.hpp +++ b/game_level.hpp @@ -12,7 +12,6 @@ namespace components { struct Position; } - namespace GameDB { struct Level { size_t index; @@ -31,4 +30,6 @@ namespace GameDB { std::shared_ptr current_world(); components::Position& player_position(); DinkyECS::Entity the_player(); + + void load_configs(DinkyECS::World &world); } diff --git a/gui/main_ui.cpp b/gui/main_ui.cpp index 9c7cadf..5968e48 100644 --- a/gui/main_ui.cpp +++ b/gui/main_ui.cpp @@ -11,12 +11,15 @@ namespace gui { MainUI::MainUI(sf::RenderWindow& window) : $window(window), - $rayview(std::make_shared(RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT)), - $hand(textures::get_sprite("female_hand")), - $hand_anim(animation::load("female_hand")) + $rayview(std::make_shared(RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT)) { $window.setVerticalSyncEnabled(VSYNC); $window.setFramerateLimit(FRAME_LIMIT); + + auto config = settings::get("config"); + + $hand = textures::get_sprite(config["player"]["hands"]); + $hand_anim = animation::load(config["player"]["hands"]); } void MainUI::dirty() { @@ -34,7 +37,6 @@ namespace gui { $overlay_ui.init(); } - void MainUI::render() { if($needs_render) $rayview->render(); $rayview->draw($window); diff --git a/main.cpp b/main.cpp index 20ee840..b403ebc 100644 --- a/main.cpp +++ b/main.cpp @@ -14,7 +14,7 @@ int main(int argc, char* argv[]) { components::init(); sfml::Backend backend; guecs::init(&backend); - ai::init("assets/ai.json"); + ai::init("ai"); animation::init(); GameDB::init(); diff --git a/meson.build b/meson.build index 9b8bacc..d4edac4 100644 --- a/meson.build +++ b/meson.build @@ -115,7 +115,6 @@ sources = [ 'rand.cpp', 'raycaster.cpp', 'rituals.cpp', - 'save.cpp', 'shaders.cpp', 'shiterator.hpp', 'sound.cpp', diff --git a/palette.cpp b/palette.cpp index 04823c8..f40bd2d 100644 --- a/palette.cpp +++ b/palette.cpp @@ -21,7 +21,7 @@ namespace palette { COLOR.initialized = true; COLOR.config = json_file; - Config config(json_file); + auto config = settings::get(json_file); json& colors = config.json(); for(auto [key, value_specs] : colors.items()) { diff --git a/palette.hpp b/palette.hpp index 03686d1..ff70da7 100644 --- a/palette.hpp +++ b/palette.hpp @@ -4,7 +4,7 @@ namespace palette { using std::string; - void init(const std::string &config="assets/palette.json"); + void init(const std::string &config="palette"); sf::Color get(const string &key); diff --git a/rituals.hpp b/rituals.hpp index 46cba11..197fa82 100644 --- a/rituals.hpp +++ b/rituals.hpp @@ -52,7 +52,7 @@ namespace ritual { }; struct Engine { - Config $config; + settings::Config $config; ai::AIProfile $profile; std::unordered_map $actions; std::unordered_map $states; diff --git a/save.cpp b/save.cpp deleted file mode 100644 index e91f21a..0000000 --- a/save.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "save.hpp" -#include -#include "dbc.hpp" -#include -#include "config.hpp" -#include - -using namespace components; -using namespace fmt; - -void save::load_configs(DinkyECS::World &world) { - Config game("./assets/config.json"); - Config enemies("./assets/enemies.json"); - Config items("./assets/items.json"); - Config tiles("./assets/tiles.json"); - Config devices("./assets/devices.json"); - Config bosses("./assets/bosses.json"); - Config rituals("./assets/rituals.json"); - - world.set_the({ - game, enemies, items, tiles, devices, bosses, rituals - }); -} diff --git a/save.hpp b/save.hpp deleted file mode 100644 index 5e3df38..0000000 --- a/save.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include "components.hpp" -#include "map.hpp" -#include "dinkyecs.hpp" -#include -#include -#include - -namespace save { - namespace fs = std::filesystem; - - void load_configs(DinkyECS::World &world); -} diff --git a/shaders.cpp b/shaders.cpp index dbc161f..be86529 100644 --- a/shaders.cpp +++ b/shaders.cpp @@ -33,7 +33,7 @@ namespace shaders { if(!INITIALIZED) { dbc::check(sf::Shader::isAvailable(), "no shaders?!"); INITIALIZED = true; - Config config("assets/shaders.json"); + auto config = settings::get("shaders"); bool good = load_shader("ERROR", config["ERROR"]); dbc::check(good, "Failed to load ERROR shader. Look in assets/shaders.json"); diff --git a/sound.cpp b/sound.cpp index cf8c48a..369d0f3 100644 --- a/sound.cpp +++ b/sound.cpp @@ -28,7 +28,7 @@ namespace sound { void init() { if(!initialized) { - Config assets("assets/config.json"); + auto assets = settings::get("config"); for(auto& el : assets["sounds"].items()) { load(el.key(), el.value()); diff --git a/tests/ai.cpp b/tests/ai.cpp index 763aa44..0f55350 100644 --- a/tests/ai.cpp +++ b/tests/ai.cpp @@ -130,7 +130,7 @@ TEST_CASE("ai as a module like sound/sprites", "[ai]") { TEST_CASE("ai autowalker ai test", "[ai]") { ai::reset(); - ai::init("assets/ai.json"); + ai::init("ai"); auto start = ai::load_state("Host::initial_state"); auto goal = ai::load_state("Host::final_state"); int enemy_count = 5; @@ -170,7 +170,7 @@ TEST_CASE("ai autowalker ai test", "[ai]") { TEST_CASE("Confirm EntityAI behaves as expected", "[ai]") { ai::reset(); - ai::init("assets/ai.json"); + ai::init("ai"); auto ai_start = ai::load_state("Enemy::initial_state"); auto ai_goal = ai::load_state("Enemy::final_state"); diff --git a/tests/battle.cpp b/tests/battle.cpp index 21498b1..a479b55 100644 --- a/tests/battle.cpp +++ b/tests/battle.cpp @@ -9,7 +9,7 @@ using namespace combat; TEST_CASE("battle operations fantasy", "[combat-battle]") { ai::reset(); - ai::init("assets/ai.json"); + ai::init("ai"); auto ai_start = ai::load_state("Enemy::initial_state"); auto ai_goal = ai::load_state("Enemy::final_state"); diff --git a/tests/components.cpp b/tests/components.cpp index bafb750..3106c4f 100644 --- a/tests/components.cpp +++ b/tests/components.cpp @@ -16,7 +16,7 @@ TEST_CASE("confirm component loading works", "[components]") { DinkyECS::World world; for(auto test_data : test_list) { - Config config(test_data); + auto config = settings::get(test_data); auto data_list = config.json(); for(auto& [key, data] : data_list.items()) { @@ -31,7 +31,7 @@ TEST_CASE("confirm component loading works", "[components]") { } TEST_CASE("make sure json_mods works", "[components]") { - Config config("assets/bosses.json"); + auto config = settings::get("bosses"); // this confirms that loading something with an optional // field works with the json conversions in json_mods.hpp for(auto& comp_data : config["RAT_KING"]["components"]) { diff --git a/tests/config.cpp b/tests/config.cpp index 4b49d72..1ac674e 100644 --- a/tests/config.cpp +++ b/tests/config.cpp @@ -3,8 +3,8 @@ #include TEST_CASE("confirm basic config loader ops", "[config]") { - Config::set_base_dir("./"); - Config config("assets/devices.json"); + settings::Config::set_base_dir("./"); + auto config = settings::get("devices"); auto data_list = config.json(); auto the_keys = config.keys(); @@ -19,7 +19,7 @@ TEST_CASE("confirm basic config loader ops", "[config]") { } } - Config indexed("tests/config_test.json"); + auto indexed = settings::get("tests/config_test.json"); auto& test_0 = indexed[0]; REQUIRE(test_0["test"] == 0); diff --git a/tests/loot.cpp b/tests/loot.cpp index c7d52ee..d37f3e8 100644 --- a/tests/loot.cpp +++ b/tests/loot.cpp @@ -8,7 +8,7 @@ using namespace fmt; using namespace components; TEST_CASE("test the loot ui", "[loot]") { - Config items("assets/items.json"); + auto items = settings::get("assets/items.json"); DinkyECS::World world; auto torch = world.entity(); auto& data = items["TORCH_BAD"]; diff --git a/textures.cpp b/textures.cpp index 73af589..994c481 100644 --- a/textures.cpp +++ b/textures.cpp @@ -39,12 +39,12 @@ namespace textures { } void load_sprites() { - Config sprites("assets/config.json"); + auto sprites = settings::get("config"); bool smooth = sprites["graphics"]["smooth_textures"]; load_sprite_textures(TMGR.sprite_textures, sprites["sprites"], smooth); - Config icons("assets/icons.json"); + auto icons = settings::get("assets/icons.json"); load_sprite_textures(TMGR.icon_textures, icons.json(), smooth); } @@ -56,7 +56,7 @@ namespace textures { } void load_tiles() { - Config assets("assets/tiles.json"); + auto assets = settings::get("tiles"); auto &tiles = assets.json(); resize_shit(tiles.size()); @@ -93,7 +93,7 @@ namespace textures { } void load_map_tiles() { - Config config("./assets/map_tiles.json"); + auto config = settings::get("map_tiles"); json& tiles = config.json(); for(auto tile : tiles) { diff --git a/tools/arena.cpp b/tools/arena.cpp index 40eb826..c2adf3e 100644 --- a/tools/arena.cpp +++ b/tools/arena.cpp @@ -12,7 +12,7 @@ int main(int argc, char* argv[]) { textures::init(); sound::init(); - ai::init("assets/ai.json"); + ai::init("ai"); animation::init(); sound::mute(false); diff --git a/tools/icongen.cpp b/tools/icongen.cpp index 3ef2021..f83a44c 100644 --- a/tools/icongen.cpp +++ b/tools/icongen.cpp @@ -203,7 +203,7 @@ struct MapTileBuilder { void load_config(MapConfig& config, bool is_centered, std::string path, std::function finder) { - Config tiles(path); + auto tiles = settings::get(path); for(auto [key, val] : tiles.json().items()) { config.it.next(); @@ -248,13 +248,13 @@ int main() { palette::init(); MapConfig config; - load_config(config, false, "./assets/tiles.json", [](json& val) -> json& { + load_config(config, false, "tiles", [](json& val) -> json& { return val; }); - load_config(config, true, "./assets/items.json", component_display); - load_config(config, true, "./assets/devices.json", component_display); - load_config(config, true, "./assets/enemies.json", component_display); + load_config(config, true, "items", component_display); + load_config(config, true, "devices", component_display); + load_config(config, true, "enemies", component_display); fmt::println("-----------------------------------------"); MapTileBuilder builder(ICONGEN_MAP_TILE_DIM, ICONGEN_MAP_TILE_DIM); diff --git a/worldbuilder.cpp b/worldbuilder.cpp index 176ed74..bdbc3a5 100644 --- a/worldbuilder.cpp +++ b/worldbuilder.cpp @@ -15,7 +15,7 @@ using namespace components; void WorldBuilder::stylize_rooms() { auto& tiles = $map.tiles(); - Config style_config("assets/styles.json"); + auto style_config = settings::get("styles"); json& styles = style_config.json(); for(auto& room : $map.rooms()) {