Skip to content

Commit

Permalink
feat(config): add game config maker + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Milerius committed Oct 7, 2019
1 parent 8e47aa4 commit 708890c
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"canvas_height": 1080.0,
"canvas_width": 1920.0,
"custom_canvas_height": true,
"custom_canvas_width": true,
"scale_mode": "crop"
}
6 changes: 4 additions & 2 deletions modules/config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <doctest/doctest.h>
#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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
******************************************************************************/

#include <doctest/doctest.h>
#include "antara/gaming/config/config.loading.hpp"
#include "antara/gaming/config/config.game.hpp"

namespace antara::gaming::config::tests
Expand Down
62 changes: 0 additions & 62 deletions modules/config/antara/gaming/config/config.game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

#include <cassert>
#include <cstddef>
#include <fstream>
#include <filesystem>
#include <string>
#include <nlohmann/json.hpp>

Expand Down Expand Up @@ -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<typename TConfig>
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<typename TConfig>
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<my_game::config>(std::filesystem::current_path() / "assets/config", "my_game.config.json");
* @endcode
*/
template<typename TConfig>
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<TConfig>(config_path, full_path);
}
return details::load_config<TConfig>(full_path);
}
}
78 changes: 78 additions & 0 deletions modules/config/antara/gaming/config/config.game.maker.cpp
Original file line number Diff line number Diff line change
@@ -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<std::string>();
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);
}
}
47 changes: 47 additions & 0 deletions modules/config/antara/gaming/config/config.game.maker.hpp
Original file line number Diff line number Diff line change
@@ -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 <nlohmann/json.hpp>
#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);
}
84 changes: 84 additions & 0 deletions modules/config/antara/gaming/config/config.loading.hpp
Original file line number Diff line number Diff line change
@@ -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 <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>

namespace antara::gaming::config
{
namespace details
{
template<typename TConfig>
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<typename TConfig>
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<my_game::config>(std::filesystem::current_path() / "assets/config", "my_game.config.json");
* @endcode
*/
template<typename TConfig>
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<TConfig>(config_path, full_path);
}
return details::load_config<TConfig>(full_path);
}
}
Loading

0 comments on commit 708890c

Please sign in to comment.