-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): add game config maker + unit tests
- Loading branch information
Showing
11 changed files
with
288 additions
and
72 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
examples/sfml/tic-tac-toe/assets/config/game.config.maker.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
modules/config/antara/gaming/config/antara.config.game.maker.tests.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.