Basic config system from a json file but it's got serious issues. Can't copy construct it because of the variable blocking copying, and it can't even be put into the dinkyecs in any way.

main
Zed A. Shaw 3 weeks ago
parent 24b1e4a500
commit 0ba789697a
  1. 15
      config.cpp
  2. 19
      config.hpp
  3. 4
      main.cpp
  4. 3
      meson.build
  5. 4
      tests/ansi_parser.cpp
  6. 43
      tests/config.cpp
  7. 10
      tests/config.json

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

@ -0,0 +1,19 @@
#pragma once
#include <nlohmann/json.hpp>
#include <fstream>
#include <codecvt>
using namespace nlohmann;
struct Config {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $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);
};

@ -6,6 +6,7 @@
#include "dbc.hpp" #include "dbc.hpp"
#include "collider.hpp" #include "collider.hpp"
#include "render.hpp" #include "render.hpp"
#include "config.hpp"
#include "ftxui/screen/terminal.hpp" // for SetColorSupport, Color, TrueColor #include "ftxui/screen/terminal.hpp" // for SetColorSupport, Color, TrueColor
@ -53,11 +54,14 @@ void configure_world(DinkyECS::World &world, Map &game_map) {
int main() { int main() {
Terminal::SetColorSupport(Terminal::Color::TrueColor); Terminal::SetColorSupport(Terminal::Color::TrueColor);
DinkyECS::World world; DinkyECS::World world;
Map game_map(GAME_MAP_X, GAME_MAP_Y); Map game_map(GAME_MAP_X, GAME_MAP_Y);
game_map.generate(); game_map.generate();
configure_world(world, game_map); configure_world(world, game_map);
System::init_positions(world); System::init_positions(world);
GUI gui(world, game_map); GUI gui(world, game_map);
return gui.main(); return gui.main();
} }

@ -20,7 +20,7 @@ runtests = executable('runtests', [
'sound.cpp', 'sound.cpp',
'collider.cpp', 'collider.cpp',
'ansi_parser.cpp', 'ansi_parser.cpp',
#'render.cpp', 'config.cpp',
'tests/fsm.cpp', 'tests/fsm.cpp',
'tests/dbc.cpp', 'tests/dbc.cpp',
'tests/map.cpp', 'tests/map.cpp',
@ -28,6 +28,7 @@ runtests = executable('runtests', [
'tests/sound.cpp', 'tests/sound.cpp',
'tests/dinkyecs.cpp', 'tests/dinkyecs.cpp',
'tests/ansi_parser.cpp', 'tests/ansi_parser.cpp',
'tests/config.cpp',
], ],
dependencies: dependencies) dependencies: dependencies)

@ -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 good = parser.parse(colors, [&](sf::Color bgcolor, sf::Color color, wchar_t ch) {
bool correct_char = ch == '#' || ch == ' ' || ch == '\n' || ch == '\r'; 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); REQUIRE(correct_char);
}); });

@ -0,0 +1,43 @@
#include <catch2/catch_test_macros.hpp>
#include <fmt/core.h>
#include <string>
#include "config.hpp"
#include <any>
#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);
}

@ -0,0 +1,10 @@
{
"types": {
"NUMBER": 1234,
"STRING": "\u2849█Ω♣",
"FLOAT": 0.1234,
"OBJECT": {
"name": "Zed"
}
}
}
Loading…
Cancel
Save