From 708890cb4a3057cf89fb5246c7b2ea1fb882a288 Mon Sep 17 00:00:00 2001 From: milerius Date: Mon, 7 Oct 2019 23:59:36 +0200 Subject: [PATCH] feat(config): add game config maker + unit tests --- .../assets/config/game.config.maker.json | 7 ++ modules/config/CMakeLists.txt | 6 +- .../config/antara.config.game.maker.tests.cpp | 52 ++++++++++++ .../config/antara.config.game.tests.cpp | 1 + .../antara/gaming/config/config.game.hpp | 62 -------------- .../gaming/config/config.game.maker.cpp | 78 +++++++++++++++++ .../gaming/config/config.game.maker.hpp | 47 +++++++++++ .../antara/gaming/config/config.loading.hpp | 84 +++++++++++++++++++ .../antara/gaming/sfml/graphic.system.cpp | 11 +-- .../world/antara/gaming/world/world.app.cpp | 10 ++- .../world/antara/gaming/world/world.app.hpp | 2 +- 11 files changed, 288 insertions(+), 72 deletions(-) create mode 100644 examples/sfml/tic-tac-toe/assets/config/game.config.maker.json create mode 100644 modules/config/antara/gaming/config/antara.config.game.maker.tests.cpp create mode 100644 modules/config/antara/gaming/config/config.game.maker.cpp create mode 100644 modules/config/antara/gaming/config/config.game.maker.hpp create mode 100644 modules/config/antara/gaming/config/config.loading.hpp diff --git a/examples/sfml/tic-tac-toe/assets/config/game.config.maker.json b/examples/sfml/tic-tac-toe/assets/config/game.config.maker.json new file mode 100644 index 00000000..b4f27a76 --- /dev/null +++ b/examples/sfml/tic-tac-toe/assets/config/game.config.maker.json @@ -0,0 +1,7 @@ +{ + "canvas_height": 1080.0, + "canvas_width": 1920.0, + "custom_canvas_height": true, + "custom_canvas_width": true, + "scale_mode": "crop" +} \ No newline at end of file diff --git a/modules/config/CMakeLists.txt b/modules/config/CMakeLists.txt index 59737269..68d80757 100644 --- a/modules/config/CMakeLists.txt +++ b/modules/config/CMakeLists.txt @@ -1,7 +1,8 @@ ## shared sources between the module and his unit tests add_library(antara_config_shared_sources STATIC) target_sources(antara_config_shared_sources PRIVATE - antara/gaming/config/config.game.cpp) + antara/gaming/config/config.game.cpp + antara/gaming/config/config.game.maker.cpp) target_include_directories(antara_config_shared_sources PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(antara_config_shared_sources PUBLIC antara::default_settings nlohmann_json::nlohmann_json) add_library(antara::config ALIAS antara_config_shared_sources) @@ -11,7 +12,8 @@ if (ANTARA_BUILD_UNIT_TESTS) add_executable(antara_config_tests) target_sources(antara_config_tests PUBLIC antara/gaming/config/antara.config.tests.cpp - antara/gaming/config/antara.config.game.tests.cpp) + antara/gaming/config/antara.config.game.tests.cpp + antara/gaming/config/antara.config.game.maker.tests.cpp) target_link_libraries(antara_config_tests PRIVATE doctest PUBLIC antara::config) set_target_properties(antara_config_tests PROPERTIES diff --git a/modules/config/antara/gaming/config/antara.config.game.maker.tests.cpp b/modules/config/antara/gaming/config/antara.config.game.maker.tests.cpp new file mode 100644 index 00000000..9b7264b8 --- /dev/null +++ b/modules/config/antara/gaming/config/antara.config.game.maker.tests.cpp @@ -0,0 +1,52 @@ +/****************************************************************************** + * Copyright © 2013-2019 The Komodo Platform Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * Komodo Platform software, including this file may be copied, modified, * + * propagated or distributed except according to the terms contained in the * + * LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include +#include "antara/gaming/config/config.loading.hpp" +#include "antara/gaming/config/config.game.maker.hpp" + +namespace antara::gaming::config::tests +{ + TEST_CASE ("game maker config from json") + { + auto json_game_cfg = R"( + { + "custom_canvas_width": true, + "custom_canvas_height": true, + "canvas_width": 1280.0, + "canvas_height": 720.0, + "scale_mode": "crop"})"_json; + game_maker_cfg game_maker_config{}; + CHECK_NOTHROW(from_json(json_game_cfg, game_maker_config)); + CHECK_EQ(game_maker_config, game_maker_cfg{true, true, 1280.f, 720.f, crop}); + CHECK_NE(game_maker_config, game_maker_cfg{}); + } + + TEST_CASE ("game maker config to json") + { + auto json_game_cfg = R"( + { + "custom_canvas_width": true, + "custom_canvas_height": true, + "canvas_width": 1280.0, + "canvas_height": 720.0, + "scale_mode": "crop"})"_json; + game_maker_cfg game_maker_config{true, true, 1280.f, 720.f, crop}; + nlohmann::json json_data; + CHECK_NOTHROW(to_json(json_data, game_maker_config)); + CHECK_EQ(json_game_cfg, json_data); + } +} \ No newline at end of file diff --git a/modules/config/antara/gaming/config/antara.config.game.tests.cpp b/modules/config/antara/gaming/config/antara.config.game.tests.cpp index 942c7a35..a7c38399 100644 --- a/modules/config/antara/gaming/config/antara.config.game.tests.cpp +++ b/modules/config/antara/gaming/config/antara.config.game.tests.cpp @@ -15,6 +15,7 @@ ******************************************************************************/ #include +#include "antara/gaming/config/config.loading.hpp" #include "antara/gaming/config/config.game.hpp" namespace antara::gaming::config::tests diff --git a/modules/config/antara/gaming/config/config.game.hpp b/modules/config/antara/gaming/config/config.game.hpp index 1c29bfe7..9ebf8f66 100644 --- a/modules/config/antara/gaming/config/config.game.hpp +++ b/modules/config/antara/gaming/config/config.game.hpp @@ -18,8 +18,6 @@ #include #include -#include -#include #include #include @@ -68,64 +66,4 @@ namespace antara::gaming::config void from_json(const nlohmann::json &json_data, game_cfg &game_cfg); void to_json(nlohmann::json &json_data, const game_cfg &game_cfg); - - namespace details - { - template - TConfig create_configuration(const std::filesystem::path &config_path, - const std::filesystem::path &full_path) noexcept - { - TConfig config_to_export{}; - std::error_code ec; - std::filesystem::create_directories(config_path, ec); - if (ec) { - return config_to_export; - } - std::ofstream ofs(full_path); - assert(ofs.is_open()); - nlohmann::json config_json_data; - config_json_data = config_to_export; - ofs << config_json_data; - return config_to_export; - } - - template - TConfig load_config(const std::filesystem::path &full_path) noexcept - { - TConfig config_to_fill{}; - std::ifstream ifs(full_path); - assert(ifs.is_open()); - nlohmann::json config_json_data; - ifs >> config_json_data; - config_to_fill = config_json_data; - return config_to_fill; - } - } - - /** - * @brief This function allows us to load a configuration through a `path` and `filename`. - * There are three different behaviors in this function: - * - if the parameter path does not exist the function will attempt to create the directories of the given `path`. - * - if the configuration does not exist a default one will be **created**. - * - if the `path` and the `name` of the file exists, the contents of the configuration will be **loaded**. - * - * @tparam TConfig the type of template you want to load - * @param config_path the path to the configuration you want to load - * @param filename the name of the configuration you want to load. - * @return a loaded/created configuration. - * - * Example: - * @code{.cpp} - * auto cfg = config::load_configuration(std::filesystem::current_path() / "assets/config", "my_game.config.json"); - * @endcode - */ - template - TConfig load_configuration(std::filesystem::path &&config_path, std::string filename) noexcept - { - const auto &full_path = config_path / std::move(filename); - if (!std::filesystem::exists(config_path) || !std::filesystem::exists(full_path)) { - return details::create_configuration(config_path, full_path); - } - return details::load_config(full_path); - } } \ No newline at end of file diff --git a/modules/config/antara/gaming/config/config.game.maker.cpp b/modules/config/antara/gaming/config/config.game.maker.cpp new file mode 100644 index 00000000..27ee750c --- /dev/null +++ b/modules/config/antara/gaming/config/config.game.maker.cpp @@ -0,0 +1,78 @@ +/****************************************************************************** + * Copyright © 2013-2019 The Komodo Platform Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * Komodo Platform software, including this file may be copied, modified, * + * propagated or distributed except according to the terms contained in the * + * LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#include "antara/gaming/config/config.game.maker.hpp" + +namespace antara::gaming::config +{ + void from_json(const nlohmann::json &json_data, game_maker_cfg &game_maker_cfg) + { + auto scale_mode_str = json_data.at("scale_mode").get(); + if (scale_mode_str == "none") { + game_maker_cfg.scale_mode = scale_mode::none; + } else if (scale_mode_str == "stretch") { + game_maker_cfg.scale_mode = scale_mode::stretch; + } else if (scale_mode_str == "crop") { + game_maker_cfg.scale_mode = scale_mode::crop; + } else if (scale_mode_str == "fit") { + game_maker_cfg.scale_mode = scale_mode::fit; + } + + json_data.at("canvas_height").get_to(game_maker_cfg.canvas_height); + json_data.at("canvas_width").get_to(game_maker_cfg.canvas_width); + json_data.at("custom_canvas_height").get_to(game_maker_cfg.custom_canvas_height); + json_data.at("custom_canvas_width").get_to(game_maker_cfg.custom_canvas_width); + } + + void to_json(nlohmann::json &json_data, const game_maker_cfg &game_maker_cfg) + { + json_data["canvas_height"] = game_maker_cfg.canvas_height; + json_data["canvas_width"] = game_maker_cfg.canvas_width; + json_data["custom_canvas_height"] = game_maker_cfg.custom_canvas_height; + json_data["custom_canvas_width"] = game_maker_cfg.custom_canvas_width; + + switch (game_maker_cfg.scale_mode) { + case none: + json_data["scale_mode"] = "none"; + break; + case stretch: + json_data["scale_mode"] = "stretch"; + break; + case crop: + json_data["scale_mode"] = "crop"; + break; + case fit: + json_data["scale_mode"] = "fit"; + break; + default: + json_data["scale_mode"] = "crop"; + } + } + + bool game_maker_cfg::operator==(const game_maker_cfg &rhs) const + { + return custom_canvas_width == rhs.custom_canvas_width && + custom_canvas_height == rhs.custom_canvas_height && + canvas_width == rhs.canvas_width && + canvas_height == rhs.canvas_height && + scale_mode == rhs.scale_mode; + } + + bool game_maker_cfg::operator!=(const game_maker_cfg &rhs) const + { + return !(rhs == *this); + } +} \ No newline at end of file diff --git a/modules/config/antara/gaming/config/config.game.maker.hpp b/modules/config/antara/gaming/config/config.game.maker.hpp new file mode 100644 index 00000000..d419e2a0 --- /dev/null +++ b/modules/config/antara/gaming/config/config.game.maker.hpp @@ -0,0 +1,47 @@ +/****************************************************************************** + * Copyright © 2013-2019 The Komodo Platform Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * Komodo Platform software, including this file may be copied, modified, * + * propagated or distributed except according to the terms contained in the * + * LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#pragma once + +#include +#include "config.game.hpp" + +namespace antara::gaming::config +{ + enum scale_mode + { + none, + stretch, + crop, + fit + }; + + struct game_maker_cfg + { + bool operator==(const game_maker_cfg &rhs) const; + + bool operator!=(const game_maker_cfg &rhs) const; + + mutable bool custom_canvas_width{true}; + mutable bool custom_canvas_height{true}; + mutable float canvas_width{1920.0f}; + mutable float canvas_height{1080.0f}; + mutable scale_mode scale_mode{crop}; + }; + + void from_json(const nlohmann::json &json_data, game_maker_cfg &game_maker_cfg); + void to_json(nlohmann::json &json_data, const game_maker_cfg &game_maker_cfg); +} \ No newline at end of file diff --git a/modules/config/antara/gaming/config/config.loading.hpp b/modules/config/antara/gaming/config/config.loading.hpp new file mode 100644 index 00000000..3e7c58cf --- /dev/null +++ b/modules/config/antara/gaming/config/config.loading.hpp @@ -0,0 +1,84 @@ +/****************************************************************************** + * Copyright © 2013-2019 The Komodo Platform Developers. * + * * + * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * + * the top-level directory of this distribution for the individual copyright * + * holder information and the developer policies on copyright and licensing. * + * * + * Unless otherwise agreed in a custom licensing agreement, no part of the * + * Komodo Platform software, including this file may be copied, modified, * + * propagated or distributed except according to the terms contained in the * + * LICENSE file * + * * + * Removal or modification of this copyright notice is prohibited. * + * * + ******************************************************************************/ + +#pragma once + +#include +#include +#include + +namespace antara::gaming::config +{ + namespace details + { + template + TConfig create_configuration(const std::filesystem::path &config_path, + const std::filesystem::path &full_path) noexcept + { + TConfig config_to_export{}; + std::error_code ec; + std::filesystem::create_directories(config_path, ec); + if (ec) { + return config_to_export; + } + std::ofstream ofs(full_path); + assert(ofs.is_open()); + nlohmann::json config_json_data; + config_json_data = config_to_export; + ofs << config_json_data; + return config_to_export; + } + + template + TConfig load_config(const std::filesystem::path &full_path) noexcept + { + TConfig config_to_fill{}; + std::ifstream ifs(full_path); + assert(ifs.is_open()); + nlohmann::json config_json_data; + ifs >> config_json_data; + config_to_fill = config_json_data; + return config_to_fill; + } + } + +/** + * @brief This function allows us to load a configuration through a `path` and `filename`. + * There are three different behaviors in this function: + * - if the parameter path does not exist the function will attempt to create the directories of the given `path`. + * - if the configuration does not exist a default one will be **created**. + * - if the `path` and the `name` of the file exists, the contents of the configuration will be **loaded**. + * + * @tparam TConfig the type of template you want to load + * @param config_path the path to the configuration you want to load + * @param filename the name of the configuration you want to load. + * @return a loaded/created configuration. + * + * Example: + * @code{.cpp} + * auto cfg = config::load_configuration(std::filesystem::current_path() / "assets/config", "my_game.config.json"); + * @endcode + */ + template + TConfig load_configuration(std::filesystem::path &&config_path, std::string filename) noexcept + { + const auto &full_path = config_path / std::move(filename); + if (!std::filesystem::exists(config_path) || !std::filesystem::exists(full_path)) { + return details::create_configuration(config_path, full_path); + } + return details::load_config(full_path); + } +} \ No newline at end of file diff --git a/modules/sfml/antara/gaming/sfml/graphic.system.cpp b/modules/sfml/antara/gaming/sfml/graphic.system.cpp index 983b771d..15b327c0 100644 --- a/modules/sfml/antara/gaming/sfml/graphic.system.cpp +++ b/modules/sfml/antara/gaming/sfml/graphic.system.cpp @@ -14,6 +14,7 @@ * * ******************************************************************************/ +#include "antara/gaming/config/config.game.maker.hpp" #include "antara/gaming/event/canvas.resized.hpp" #include "antara/gaming/ecs/component.position.hpp" #include "antara/gaming/ecs/component.layer.hpp" @@ -31,11 +32,11 @@ namespace antara::gaming::sfml void graphic_system::refresh_render_texture() noexcept { // User config - const bool custom_canvas_width = true; - const bool custom_canvas_height = true; - const float canvas_width = 1920.0f; - const float canvas_height = 1080.0f; - const int scale_mode = 3; // 0 - None, 1 - Stretch, 2 - Crop, 3 - Fit, user choice + auto &&[custom_canvas_width, + custom_canvas_height, + canvas_width, + canvas_height, + scale_mode] = this->entity_registry_.ctx(); // Set the Render Texture size diff --git a/modules/world/antara/gaming/world/world.app.cpp b/modules/world/antara/gaming/world/world.app.cpp index 1da9367c..5a2368f9 100644 --- a/modules/world/antara/gaming/world/world.app.cpp +++ b/modules/world/antara/gaming/world/world.app.cpp @@ -19,7 +19,9 @@ #endif #include "antara/gaming/core/real.path.hpp" +#include "antara/gaming/config/config.loading.hpp" #include "antara/gaming/config/config.game.hpp" +#include "antara/gaming/config/config.game.maker.hpp" #include "antara/gaming/event/start.game.hpp" #include "antara/gaming/world/world.app.hpp" @@ -32,10 +34,14 @@ void emscripten_antara_loop(void *world) namespace antara::gaming::world { //! Constructor - app::app(std::string config_name) noexcept + app::app(std::string config_name, std::string config_maker_name) noexcept { - auto cfg = config::load_configuration(core::assets_real_path() / "config", std::move(config_name)); + auto cfg = config::load_configuration(core::assets_real_path() / "config", + std::move(config_name)); + auto cfg_maker = config::load_configuration(core::assets_real_path() / "config", + std::move(config_maker_name)); this->entity_registry_.set(cfg); + this->entity_registry_.set(cfg_maker); dispatcher_.sink().connect<&app::receive_quit_game>(*this); } diff --git a/modules/world/antara/gaming/world/world.app.hpp b/modules/world/antara/gaming/world/world.app.hpp index 86ab2db2..a54639b4 100644 --- a/modules/world/antara/gaming/world/world.app.hpp +++ b/modules/world/antara/gaming/world/world.app.hpp @@ -28,7 +28,7 @@ namespace antara::gaming::world class app { public: - app(std::string config_name = "game.config.json") noexcept; + app(std::string config_name = "game.config.json", std::string config_maker_name = "game.config.maker.json") noexcept; //! Public callbacks void receive_quit_game(const event::quit_game &evt) noexcept;