diff --git a/config.cpp b/config.cpp new file mode 100644 index 0000000..ca36bc1 --- /dev/null +++ b/config.cpp @@ -0,0 +1,15 @@ +#include "config.hpp" + +Config::Config(const std::string src_path) : $src_path(src_path) { + std::ifstream infile($src_path); + $config = json::parse(infile); +} + +json &Config::operator[](const std::string &key) { + return $config[key]; +} + +std::wstring Config::wstring(const std::string main_key, const std::string sub_key) { + const std::string& str_val = $config[main_key][sub_key]; + return $converter.from_bytes(str_val); +} diff --git a/config.hpp b/config.hpp new file mode 100644 index 0000000..8c2b504 --- /dev/null +++ b/config.hpp @@ -0,0 +1,19 @@ +#pragma once +#include +#include +#include + +using namespace nlohmann; + +struct Config { + std::wstring_convert> $converter; + json $config; + std::string $src_path = "./config.json"; + + Config(const std::string src_path); + Config(Config &other) = delete; + + json &operator[](const std::string &key); + + std::wstring wstring(const std::string main_key, const std::string sub_key); +}; diff --git a/main.cpp b/main.cpp index 7c2c0ff..e57b6d9 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,7 @@ #include "dbc.hpp" #include "collider.hpp" #include "render.hpp" +#include "config.hpp" #include "ftxui/screen/terminal.hpp" // for SetColorSupport, Color, TrueColor @@ -53,11 +54,14 @@ void configure_world(DinkyECS::World &world, Map &game_map) { int main() { Terminal::SetColorSupport(Terminal::Color::TrueColor); DinkyECS::World world; + Map game_map(GAME_MAP_X, GAME_MAP_Y); game_map.generate(); configure_world(world, game_map); System::init_positions(world); + GUI gui(world, game_map); + return gui.main(); } diff --git a/meson.build b/meson.build index 317f936..59bc3ea 100644 --- a/meson.build +++ b/meson.build @@ -20,7 +20,7 @@ runtests = executable('runtests', [ 'sound.cpp', 'collider.cpp', 'ansi_parser.cpp', - #'render.cpp', + 'config.cpp', 'tests/fsm.cpp', 'tests/dbc.cpp', 'tests/map.cpp', @@ -28,6 +28,7 @@ runtests = executable('runtests', [ 'tests/sound.cpp', 'tests/dinkyecs.cpp', 'tests/ansi_parser.cpp', + 'tests/config.cpp', ], dependencies: dependencies) diff --git a/tests/ansi_parser.cpp b/tests/ansi_parser.cpp index 8cb96b4..0c7108a 100644 --- a/tests/ansi_parser.cpp +++ b/tests/ansi_parser.cpp @@ -59,10 +59,6 @@ TEST_CASE("test out ragel parser", "[gui]") { bool good = parser.parse(colors, [&](sf::Color bgcolor, sf::Color color, wchar_t ch) { bool correct_char = ch == '#' || ch == ' ' || ch == '\n' || ch == '\r'; - // println("FG: {},{},{},{}; BG: {},{},{},{}; ch: {}", - // color.r, color.g, color.b, color.a, - // bgcolor.r, bgcolor.g, bgcolor.b, bgcolor.a, - // int(ch)); REQUIRE(correct_char); }); diff --git a/tests/config.cpp b/tests/config.cpp new file mode 100644 index 0000000..681ea21 --- /dev/null +++ b/tests/config.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include "config.hpp" +#include +#include "dinkyecs.hpp" + +using namespace fmt; +using std::string; + +TEST_CASE("basic configuration system", "[config]") { + Config config("./tests/config.json"); + + auto not_found = config["types"]["NOTFOUND"]; + REQUIRE(not_found == nullptr); + + auto test_string = config["types"]["STRING"]; + REQUIRE(test_string == L"\u2849█Ω♣"); + + std::wstring test_wstring = config.wstring("types", "STRING"); + REQUIRE(test_wstring == L"\u2849█Ω♣"); + + wchar_t chr0 = test_wstring[0]; + REQUIRE(chr0 == L'\u2849'); + + auto test_num = config["types"]["NUMBER"]; + REQUIRE(test_num == 1234); + + auto test_float = config["types"]["FLOAT"]; + REQUIRE(test_num >= 0.1233f); + + auto test_obj = config["types"]["OBJECT"]; + REQUIRE(test_obj["name"] == "Zed"); +} + +void test_func(Config &ref) { + REQUIRE(ref["types"]["OBJECT"]["name"] == "Zed"); +} + +TEST_CASE("store config in any", "[config]") { + Config config("./tests/config.json"); + test_func(config); +} diff --git a/tests/config.json b/tests/config.json new file mode 100644 index 0000000..faa1733 --- /dev/null +++ b/tests/config.json @@ -0,0 +1,10 @@ +{ + "types": { + "NUMBER": 1234, + "STRING": "\u2849█Ω♣", + "FLOAT": 0.1234, + "OBJECT": { + "name": "Zed" + } + } +}