Skip to content

Commit

Permalink
feat(graphics): add canvas_2d component
Browse files Browse the repository at this point in the history
  • Loading branch information
Milerius committed Oct 12, 2019
1 parent aefc553 commit 5980620
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 140 deletions.
14 changes: 11 additions & 3 deletions examples/sfml/tic-tac-toe/assets/config/game.config.maker.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"native_desktop_mode": false,
"canvas_height": 1080.0,
"canvas_width": 1920.0,
"custom_canvas_height": true,
"custom_canvas_width": true,
"scale_mode": "crop"
"scale_mode": "fit",
"window_width": 1921,
"window_height": 1081,
"window_title": "tic-tac-toe",
"background_color": [
0,
0,
0,
255
]
}
2 changes: 1 addition & 1 deletion modules/config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ target_sources(antara_config_shared_sources PRIVATE
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)
target_link_libraries(antara_config_shared_sources PUBLIC antara::default_settings antara::graphics nlohmann_json::nlohmann_json)
add_library(antara::config ALIAS antara_config_shared_sources)

if (ANTARA_BUILD_UNIT_TESTS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,50 @@ namespace antara::gaming::config::tests
{
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{};
"native_desktop_mode": false,
"canvas_height": 1080.0,
"canvas_width": 1920.0,
"scale_mode": "fit",
"window_width": 1921,
"window_height": 1081,
"window_title": "tic-tac-toe",
"background_color": [
0,
0,
0,
255
]
})"_json;
graphics::canvas_2d 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{});
CHECK_EQ(game_maker_config.is_fullscreen, false);
CHECK_NE(game_maker_config, graphics::canvas_2d{});
}

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};
"native_desktop_mode": false,
"canvas_height": 1080.0,
"canvas_width": 1920.0,
"scale_mode": "crop",
"window_width": 1921.0,
"window_height": 1081.0,
"window_title": "tic-tac-toe",
"background_color": [
0,
0,
0,
255
]
})"_json;
graphics::canvas_2d game_maker_config;
game_maker_config.native_desktop_mode = false;
game_maker_config.canvas.size = math::vec2f{1920.f, 1080.f};
game_maker_config.window.size = math::vec2f{1921.f, 1081.f};
game_maker_config.window_title = "tic-tac-toe";
game_maker_config.background_color = graphics::black;
nlohmann::json json_data;
CHECK_NOTHROW(to_json(json_data, game_maker_config));
CHECK_EQ(json_game_cfg, json_data);
Expand Down
84 changes: 49 additions & 35 deletions modules/config/antara/gaming/config/config.game.maker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,77 @@

#include "antara/gaming/config/config.game.maker.hpp"

namespace antara::gaming::config
namespace antara::gaming::graphics
{
void from_json(const nlohmann::json &json_data, game_maker_cfg &game_maker_cfg)
void from_json(const nlohmann::json &json_data, graphics::canvas_2d &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;
game_maker_cfg.current_scaling_mode = graphics::canvas_2d::scale_mode::none;
} else if (scale_mode_str == "stretch") {
game_maker_cfg.scale_mode = scale_mode::stretch;
game_maker_cfg.current_scaling_mode = graphics::canvas_2d::scale_mode::stretch;
} else if (scale_mode_str == "crop") {
game_maker_cfg.scale_mode = scale_mode::crop;
game_maker_cfg.current_scaling_mode = graphics::canvas_2d::scale_mode::crop;
} else if (scale_mode_str == "fit") {
game_maker_cfg.scale_mode = scale_mode::fit;
game_maker_cfg.current_scaling_mode = graphics::canvas_2d::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);
game_maker_cfg.canvas.size.x_ref() = json_data.at("canvas_width").get<float>();
game_maker_cfg.canvas.size.y_ref() = json_data.at("canvas_height").get<float>();
if (json_data.find("native_desktop_mode") != json_data.end() &&
json_data.at("native_desktop_mode").get<bool>() == true) {
game_maker_cfg.custom_canvas_width = true;
game_maker_cfg.custom_canvas_height = true;
game_maker_cfg.native_desktop_mode = true;
} else {
game_maker_cfg.custom_canvas_width = false;
game_maker_cfg.custom_canvas_height = false;
game_maker_cfg.native_desktop_mode = false;
}

if (not game_maker_cfg.native_desktop_mode) {
game_maker_cfg.window.size.x_ref() = static_cast<float>(json_data.at("window_width").get<int>());
game_maker_cfg.window.size.y_ref() = static_cast<float>(json_data.at("window_height").get<int>());
}

if (game_maker_cfg.window.size.x() > game_maker_cfg.canvas.size.x()) {
game_maker_cfg.custom_canvas_width = true;
}

if (game_maker_cfg.window.size.y() > game_maker_cfg.canvas.size.y()) {
game_maker_cfg.custom_canvas_height = true;
}

json_data.at("window_title").get_to(game_maker_cfg.window_title);
auto vec = json_data.at("background_color").get<std::vector<int>>();
game_maker_cfg.background_color = graphics::fill_color(vec[0], vec[1], vec[2], vec[3]);
}

void to_json(nlohmann::json &json_data, const game_maker_cfg &game_maker_cfg)
void to_json(nlohmann::json &json_data, const graphics::canvas_2d &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["native_desktop_mode"] = game_maker_cfg.native_desktop_mode;
json_data["canvas_width"] = game_maker_cfg.canvas.size.x();
json_data["canvas_height"] = game_maker_cfg.canvas.size.y();
json_data["window_width"] = game_maker_cfg.window.size.x();
json_data["window_height"] = game_maker_cfg.window.size.y();
json_data["window_title"] = game_maker_cfg.window_title;
auto[r, g, b, a] = game_maker_cfg.background_color;
json_data["background_color"] = {r, g, b, a};
switch (game_maker_cfg.current_scaling_mode) {
case graphics::canvas_2d::none:
json_data["scale_mode"] = "none";
break;
case stretch:
case graphics::canvas_2d::stretch:
json_data["scale_mode"] = "stretch";
break;
case crop:
case graphics::canvas_2d::crop:
json_data["scale_mode"] = "crop";
break;
case fit:
case graphics::canvas_2d::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);
}
}
28 changes: 4 additions & 24 deletions modules/config/antara/gaming/config/config.game.maker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,11 @@
#pragma once

#include <nlohmann/json.hpp>
#include "antara/gaming/graphics/component.canvas.hpp"
#include "config.game.hpp"

namespace antara::gaming::config
namespace antara::gaming::graphics
{
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);
void from_json(const nlohmann::json &json_data, canvas_2d &game_maker_cfg);
void to_json(nlohmann::json &json_data, const canvas_2d &game_maker_cfg);
}
2 changes: 1 addition & 1 deletion modules/graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
add_library(antara_graphics_shared_sources INTERFACE)
#target_sources(antara_graphics_shared_sources PRIVATE )
target_include_directories(antara_graphics_shared_sources INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(antara_graphics_shared_sources INTERFACE antara::default_settings antara::event range-v3)
target_link_libraries(antara_graphics_shared_sources INTERFACE antara::default_settings antara::event range-v3 antara::math)
add_library(antara::graphics ALIAS antara_graphics_shared_sources)

if (ANTARA_BUILD_UNIT_TESTS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <meta/sequence/list.hpp>
#include "antara/gaming/graphics/component.layer.hpp"
#include "antara/gaming/graphics/component.color.hpp"
#include "antara/gaming/graphics/component.canvas.hpp"

namespace antara::gaming::graphics
{
Expand All @@ -35,5 +36,7 @@ namespace antara::gaming::graphics
layer_10,
layer_11,
outline_color,
fill_color>;
fill_color,
rectangle,
canvas_2d>;
}
127 changes: 127 additions & 0 deletions modules/graphics/antara/gaming/graphics/component.canvas.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/******************************************************************************
* 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 "antara/gaming/core/safe.refl.hpp"
#include "antara/gaming/math/vector.hpp"
#include "antara/gaming/graphics/component.color.hpp"

namespace antara::gaming::graphics
{
struct rectangle
{
math::vec2f size; //! width/height
math::vec2f position;

bool operator==(const rectangle &rhs) const
{
return size == rhs.size &&
position == rhs.position;
}

bool operator!=(const rectangle &rhs) const
{
return !(rhs == *this);
}
//! pos of the rectangle
};

struct canvas_2d
{
enum scale_mode
{
none,
stretch,
crop,
fit
};

rectangle window{{0.f, 0.f}, {1921.f, 1080.f}};
rectangle canvas{{0.f, 0.f}, {1920.f, 1080.f}};
rectangle canvas_texture;
bool custom_canvas_width{true};
bool custom_canvas_height{true};
bool native_desktop_mode{false};
bool is_fullscreen{false};
scale_mode current_scaling_mode{crop};
rectangle view_port;
std::string window_title{"game title"};
fill_color background_color{graphics::black};
math::vec2f canvas_texture_scaling{1.0f, 1.0f};

bool operator==(const canvas_2d &rhs) const
{
return window == rhs.window &&
canvas == rhs.canvas &&
current_scaling_mode == rhs.current_scaling_mode &&
view_port == rhs.view_port &&
window_title == rhs.window_title &&
background_color == rhs.background_color;
}

bool operator!=(const canvas_2d &rhs) const
{
return !(rhs == *this);
}

void reset_canvas() noexcept
{
auto&&[window_width, window_height] = window.size;
auto&&[canvas_width, canvas_height] = canvas.size;
auto&&[canvas_texture_width, canvas_texture_height] = canvas_texture.size;
if (custom_canvas_width && custom_canvas_height) {
canvas_texture_width = canvas_width;
canvas_texture_height = canvas_height;
} else if (custom_canvas_width) {
canvas_texture_width = canvas_width;
canvas_texture_height = canvas_width * window_height / window_width;
} else if (custom_canvas_height) {
canvas_texture_width = canvas_height * window_width / window_height;
canvas_texture_height = canvas_height;
} else {
canvas_texture_width = window_width;
canvas_texture_height = window_height;
}

view_port.position = math::vec2f::scalar(0.f);
view_port.size = math::vec2f{window_width, window_height};

switch (current_scaling_mode) {
case none:
break;
case stretch:
canvas_texture_scaling = math::vec2f{window_width / canvas_texture_width, window_height / canvas_texture_height};
break;
case crop:
canvas_texture_scaling = math::vec2f::scalar(std::max(window_width / canvas_texture_width, window_height / canvas_texture_height));
break;
case fit:
canvas_texture_scaling = math::vec2f::scalar(std::min(window_width / canvas_texture_width, window_height / canvas_texture_height));
break;
}
canvas_texture.position = math::vec2f{window_width * 0.5f, window_height * 0.5f};
}

canvas_2d() = default;
canvas_2d(const canvas_2d& other) = default;
canvas_2d& operator=(const canvas_2d& other) = default;
};
}

REFL_AUTO(type(antara::gaming::graphics::rectangle), field(size), field(position))
REFL_AUTO(type(antara::gaming::graphics::canvas_2d), field(window), field(canvas), field(current_scaling_mode),
field(view_port), field(window_title), field(background_color))
Loading

0 comments on commit 5980620

Please sign in to comment.