Skip to content

Commit

Permalink
feat(canvas): add ostream utilities + coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Milerius committed Oct 13, 2019
1 parent a51a54c commit 3c1ed95
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 14 deletions.
2 changes: 1 addition & 1 deletion modules/config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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.loading.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace antara::gaming::config::tests
"native_desktop_mode": false,
"canvas_height": 1080.0,
"canvas_width": 1920.0,
"scale_mode": "fit",
"scale_mode": "crop",
"window_width": 1921,
"window_height": 1081,
"window_title": "tic-tac-toe",
Expand All @@ -39,9 +39,21 @@ namespace antara::gaming::config::tests
]
})"_json;
graphics::canvas_2d game_maker_config{};
CHECK_NOTHROW(from_json(json_game_cfg, game_maker_config));
CHECK_EQ(game_maker_config.is_fullscreen, false);
CHECK_NE(game_maker_config, graphics::canvas_2d{});
CHECK_NOTHROW(from_json(json_game_cfg, game_maker_config));
CHECK_EQ(game_maker_config.is_fullscreen, false);
CHECK_NE(game_maker_config, graphics::canvas_2d{});
CHECK_EQ(game_maker_config, graphics::canvas_2d{
.window = {.position = math::vec2f::scalar(0.f), .size = {1921.f, 1081.f}},
.canvas = {.position = math::vec2f::scalar(0.f), .size = {1920.f, 1080.f}},
.canvas_texture = {.position = math::vec2f::scalar(0.f), .size = math::vec2f::scalar(0.f)},
.custom_canvas_height = true,
.custom_canvas_width = true,
.is_fullscreen = false,
.native_desktop_mode = false,
.current_scaling_mode = graphics::canvas_2d::crop,
.canvas_texture_scaling = math::vec2f::scalar(1.f),
.window_title = "tic-tac-toe",
.background_color = graphics::black});
}

TEST_CASE ("game maker config to json")
Expand Down Expand Up @@ -69,7 +81,7 @@ namespace antara::gaming::config::tests
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);
CHECK_NOTHROW(to_json(json_data, game_maker_config));
CHECK_EQ(json_game_cfg, json_data);
}
}
39 changes: 32 additions & 7 deletions modules/graphics/antara/gaming/graphics/component.canvas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <ostream>
#include "antara/gaming/core/safe.refl.hpp"
#include "antara/gaming/math/vector.hpp"
#include "antara/gaming/graphics/component.color.hpp"
Expand All @@ -37,7 +38,13 @@ namespace antara::gaming::graphics
{
return !(rhs == *this);
}

//! pos of the rectangle
friend std::ostream &operator<<(std::ostream &os, const rectangle &rectangle)
{
os << "size: " << rectangle.size << " position: " << rectangle.position;
return os;
}
};

struct canvas_2d
Expand All @@ -50,8 +57,10 @@ namespace antara::gaming::graphics
fit
};

rectangle window{{0.f, 0.f}, {1921.f, 1080.f}};
rectangle canvas{{0.f, 0.f}, {1920.f, 1080.f}};
rectangle window{.size = math::vec2f{1921.f, 1081.f},
.position = math::vec2f::scalar(0.f)};
rectangle canvas{.size = math::vec2f{1921.f, 1081.f},
.position = math::vec2f::scalar(0.f)};
rectangle canvas_texture;
bool custom_canvas_width{true};
bool custom_canvas_height{true};
Expand All @@ -73,6 +82,17 @@ namespace antara::gaming::graphics
background_color == rhs.background_color;
}

friend inline std::ostream &operator<<(std::ostream &os, const canvas_2d &d)
{
os << "window: " << d.window << " canvas: " << d.canvas << " canvas_texture: " << d.canvas_texture
<< " custom_canvas_width: " << d.custom_canvas_width << " custom_canvas_height: "
<< d.custom_canvas_height << " native_desktop_mode: " << d.native_desktop_mode << " is_fullscreen: "
<< d.is_fullscreen << " current_scaling_mode: " << d.current_scaling_mode << " view_port: "
<< d.view_port << " window_title: " << d.window_title << " background_color: " << d.background_color
<< " canvas_texture_scaling: " << d.canvas_texture_scaling;
return os;
}

bool operator!=(const canvas_2d &rhs) const
{
return !(rhs == *this);
Expand Down Expand Up @@ -104,21 +124,26 @@ namespace antara::gaming::graphics
case none:
break;
case stretch:
canvas_texture_scaling = math::vec2f{window_width / canvas_texture_width, window_height / canvas_texture_height};
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));
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));
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;

canvas_2d(const canvas_2d &other) = default;

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

Expand Down
7 changes: 7 additions & 0 deletions modules/graphics/antara/gaming/graphics/component.color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <cstdint>
#include <utility>
#include <ostream>
#include "antara/gaming/core/safe.refl.hpp"

namespace antara::gaming::graphics
Expand Down Expand Up @@ -54,6 +55,12 @@ namespace antara::gaming::graphics
std::uint8_t g{0};
std::uint8_t b{0};
std::uint8_t a{255};

friend std::ostream &operator<<(std::ostream &os, const color &color)
{
os << "r: " << int(color.r) << " g: " << int(color.g) << " b: " << int(color.b) << " a: " << int(color.a);
return os;
}
};

inline constexpr color black{0, 0, 0};
Expand Down
15 changes: 15 additions & 0 deletions modules/math/antara/gaming/math/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <cmath>
#include <array>
#include <ostream>
#include "antara/gaming/core/safe.refl.hpp"

namespace antara::gaming::math
Expand Down Expand Up @@ -213,6 +214,8 @@ namespace antara::gaming::math
return *this;
}

friend std::ostream &operator<<(std::ostream &os, const basic_vector &vector);

constexpr basic_vector &operator*=(Unit b) noexcept
{
update_vec([b](Unit &a) { a *= b; }, sequence_type{});
Expand Down Expand Up @@ -304,8 +307,20 @@ namespace antara::gaming::math
{
return !(rhs < *this);
}

friend std::ostream &operator<<(std::ostream &os, const basic_vector<Unit, Size, Mixins...> &vector)
{
os << " data_: {";
for (auto&& current : vector.data_) {
os << current << " ";
}
os << "}";
return os;
}
};



namespace vector_mixins
{
template<class Derived, class Unit>
Expand Down

0 comments on commit 3c1ed95

Please sign in to comment.