Skip to content

Commit

Permalink
feat: using geometry::circle in tictactoe example
Browse files Browse the repository at this point in the history
  • Loading branch information
Milerius committed Oct 11, 2019
1 parent 51caaae commit 99a9b8a
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 43 deletions.
16 changes: 7 additions & 9 deletions examples/sfml/tic-tac-toe/tic.tac.toe.factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <entt/entity/helper.hpp>
#include <antara/gaming/graphics/component.layer.hpp>
#include <antara/gaming/transform/component.position.hpp>
#include <antara/gaming/geometry/component.circle.hpp>
#include <antara/gaming/graphics/component.color.hpp>
#include <antara/gaming/sfml/component.drawable.hpp>
#include "tic.tac.toe.factory.hpp"
#include "tic.tac.toe.constants.hpp"
Expand Down Expand Up @@ -98,16 +100,12 @@ namespace tictactoe ::example
const auto center_y = static_cast<float>(constants.cell_height * 0.5 + row * constants.cell_height);

auto o_entity = entity_registry.create();
auto &circle_cmp = entity_registry.assign<sfml::circle>(o_entity,
sf::CircleShape(half_box_side));
sf::CircleShape &circle = circle_cmp.drawable;
entity_registry.assign<graphics::fill_color>(o_entity, graphics::transparent);
entity_registry.assign<graphics::outline_color>(o_entity, 1.f, graphics::blue);
entity_registry.assign<geometry::circle>(o_entity, half_box_side);
entity_registry.assign<transform::position>(o_entity,
center_x - circle.getGlobalBounds().width / 2,
center_y -
circle.getGlobalBounds().height / 2);
circle.setFillColor(sf::Color::Transparent);
circle.setOutlineThickness(1.f);
circle.setOutlineColor(sf::Color::Blue);
center_x,
center_y);

entity_registry.assign<entt::tag<"game_scene"_hs>>(o_entity);
entity_registry.assign<graphics::layer<1>>(o_entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <meta/sequence/list.hpp>
#include "antara/gaming/graphics/component.layer.hpp"
#include "antara/gaming/graphics/component.color.hpp"

namespace antara::gaming::graphics
{
Expand All @@ -32,5 +33,7 @@ namespace antara::gaming::graphics
layer_8,
layer_9,
layer_10,
layer_11>;
layer_11,
outline_color,
fill_color>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace antara::gaming::graphics::tests

graphics::outline_color c_out_color;
CHECK_EQ(c_out_color, graphics::black);
CHECK_EQ(1.0f, c_out_color.thickness);

graphics::fill_color c_fill_color;
CHECK_EQ(c_fill_color, graphics::black);
Expand All @@ -41,8 +42,9 @@ namespace antara::gaming::graphics::tests
c_color = graphics::red;
CHECK_EQ(c_color, graphics::red);

graphics::outline_color c_outline_color(graphics::green);
graphics::outline_color c_outline_color(42.f, graphics::green);
CHECK_EQ(c_outline_color, graphics::green);
CHECK_EQ(c_outline_color.thickness, 42.f);
c_outline_color = graphics::blue;
CHECK_EQ(c_outline_color, graphics::blue);

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

}

template<typename ... TArgs>
constexpr
outline_color(float thickness, TArgs &&...args) noexcept : graphics::color(std::forward<TArgs>(args)...),
thickness(thickness)
{

}

float thickness{1.f};
};

struct fill_color : color
Expand Down
9 changes: 8 additions & 1 deletion modules/sfml/antara/gaming/sfml/component.drawable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,28 @@ namespace antara::gaming::sfml
struct sprite
{
sprite() = default;

sf::Sprite drawable;
};

struct rectangle
{
rectangle() = default;

sf::RectangleShape drawable;
};

struct circle
{
circle(sf::CircleShape drawable_) : drawable(std::move(drawable_))
{
drawable.setOrigin(drawable.getRadius(), drawable.getRadius());
}

circle()
{
drawable.setOrigin(drawable.getRadius(), drawable.getRadius());
}
circle() = default;

sf::CircleShape drawable;
};
Expand All @@ -56,6 +62,7 @@ namespace antara::gaming::sfml
struct vertex_array
{
vertex_array() = default;

sf::VertexArray drawable;
};
// LCOV_EXCL_STOP
Expand Down
62 changes: 34 additions & 28 deletions modules/sfml/antara/gaming/sfml/graphic.system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
#include "antara/gaming/config/config.game.maker.hpp"
#include "antara/gaming/event/canvas.resized.hpp"
#include "antara/gaming/transform/component.position.hpp"
#include "antara/gaming/graphics/component.color.hpp"
#include "antara/gaming/graphics/component.layer.hpp"
#include "antara/gaming/geometry/component.circle.hpp"
#include "antara/gaming/sfml/graphic.system.hpp"
#include "antara/gaming/sfml/component.drawable.hpp"

Expand All @@ -26,34 +28,32 @@ namespace antara::gaming::sfml
graphic_system::graphic_system(entt::registry &registry) noexcept : system(registry)
{
this->dispatcher_.sink<event::window_resized>().connect<&graphic_system::on_window_resized_event>(*this);
this->entity_registry_.on_construct<geometry::circle>().connect<&graphic_system::on_geometry_circle_construct>(*this);
refresh_render_texture();
}

void graphic_system::refresh_render_texture() noexcept
{
// User config
auto &&[custom_canvas_width,
custom_canvas_height,
canvas_width,
canvas_height,
scale_mode] = this->entity_registry_.ctx<config::game_maker_cfg>();
custom_canvas_height,
canvas_width,
canvas_height,
scale_mode] = this->entity_registry_.ctx<config::game_maker_cfg>();


// Set the Render Texture size
sf::Vector2f rt_size;
if(custom_canvas_width && custom_canvas_height) {
if (custom_canvas_width && custom_canvas_height) {
rt_size.x = canvas_width;
rt_size.y = canvas_height;
}
else if(custom_canvas_width) {
} else if (custom_canvas_width) {
rt_size.x = canvas_width;
rt_size.y = canvas_width * window_.getSize().y / window_.getSize().x;
}
else if(custom_canvas_height) {
rt_size.y = canvas_width * window_.getSize().y / window_.getSize().x;
} else if (custom_canvas_height) {
rt_size.x = canvas_height * window_.getSize().x / window_.getSize().y;
rt_size.y = canvas_height;
}
else {
} else {
rt_size.x = window_.getSize().x;
rt_size.y = window_.getSize().y;
}
Expand All @@ -69,23 +69,24 @@ namespace antara::gaming::sfml
sf::Vector2f rt_scale(1.0f, 1.0f);

// Stretch
if(scale_mode == 1) {
if (scale_mode == 1) {
rt_scale.x = window_.getSize().x / rt_size.x;
rt_scale.y = window_.getSize().y / rt_size.y;
}
// Crop
else if(scale_mode == 2) {
// Crop
else if (scale_mode == 2) {
rt_scale.x = rt_scale.y = std::max(window_.getSize().x / rt_size.x, window_.getSize().y / rt_size.y);
}
// Fit
else if(scale_mode == 3) {
// Fit
else if (scale_mode == 3) {
rt_scale.x = rt_scale.y = std::min(window_.getSize().x / rt_size.x, window_.getSize().y / rt_size.y);
}

render_texture_sprite_.setScale(rt_scale);

// Center canvas to the window
render_texture_sprite_.setOrigin(render_texture_sprite_.getLocalBounds().width * 0.5f, render_texture_sprite_.getLocalBounds().height * 0.5f);
render_texture_sprite_.setOrigin(render_texture_sprite_.getLocalBounds().width * 0.5f,
render_texture_sprite_.getLocalBounds().height * 0.5f);
render_texture_sprite_.setPosition(window_.getSize().x * 0.5f, window_.getSize().y * 0.5f);
}

Expand All @@ -104,16 +105,6 @@ namespace antara::gaming::sfml
return window_;
}

sf::RenderTexture &graphic_system::get_render_texture() noexcept
{
return render_texture_;
}

sf::Sprite &graphic_system::get_render_texture_sprite() noexcept
{
return render_texture_sprite_;
}

template<size_t Layer, typename DrawableType>
void graphic_system::draw() noexcept
{
Expand Down Expand Up @@ -153,4 +144,19 @@ namespace antara::gaming::sfml
refresh_render_texture();
this->dispatcher_.trigger<event::canvas_resized>();
}

void graphic_system::on_geometry_circle_construct(entt::entity entity,
entt::registry &registry,
geometry::circle &circle) noexcept
{
auto& sfml_circle = registry.assign<sfml::circle>(entity, sf::CircleShape(circle.radius));
if (auto out_color = registry.try_get<graphics::outline_color>(entity); out_color != nullptr) {
sfml_circle.drawable.setOutlineColor(sf::Color(out_color->r, out_color->g, out_color->b, out_color->a));
sfml_circle.drawable.setOutlineThickness(out_color->thickness);
}

if (auto fill_color = registry.try_get<graphics::fill_color>(entity); fill_color != nullptr) {
sfml_circle.drawable.setFillColor(sf::Color(fill_color->r, fill_color->g, fill_color->b, fill_color->a));
}
}
}
6 changes: 3 additions & 3 deletions modules/sfml/antara/gaming/sfml/graphic.system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <SFML/Graphics/Sprite.hpp>
#include "meta/sequence/list.hpp"
#include "antara/gaming/event/window.resized.hpp"
#include "antara/gaming/geometry/component.circle.hpp"
#include "antara/gaming/core/safe.refl.hpp"
#include "antara/gaming/config/config.game.hpp"
#include "antara/gaming/ecs/system.hpp"
Expand Down Expand Up @@ -50,11 +51,10 @@ namespace antara::gaming::sfml

//! Public getter
sf::RenderWindow &get_window() noexcept;
sf::RenderTexture &get_render_texture() noexcept;
sf::Sprite &get_render_texture_sprite() noexcept;

//! Callback
void on_window_resized_event(const event::window_resized &evt) noexcept;

void on_geometry_circle_construct(entt::entity entity, entt::registry &registry, geometry::circle &circle) noexcept;
private:
config::game_cfg &game_cfg_{entity_registry_.ctx<config::game_cfg>()};
config::window_cfg &window_cfg_{game_cfg_.win_cfg};
Expand Down

0 comments on commit 99a9b8a

Please sign in to comment.