Skip to content

Commit

Permalink
feat(tic-tac-toe): remove all sfml stuffs to use generics one
Browse files Browse the repository at this point in the history
  • Loading branch information
Milerius committed Oct 13, 2019
1 parent 1446891 commit be0a737
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
38 changes: 24 additions & 14 deletions examples/sfml/tic-tac-toe/game.scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
* *
******************************************************************************/

#include <SFML/Graphics/RenderTexture.hpp>
#include <entt/entity/helper.hpp>
#include <antara/gaming/geometry/component.vertex.hpp>
#include <antara/gaming/graphics/component.color.hpp>
#include <antara/gaming/graphics/component.canvas.hpp>
#include <antara/gaming/math/vector.hpp>
#include "tic.tac.toe.constants.hpp"
#include "tic.tac.toe.components.hpp"
#include "tic.tac.toe.factory.hpp"
#include "game.scene.hpp"

using namespace antara::gaming;

namespace tictactoe::example
{
game_scene::game_scene(entt::registry &entity_registry) noexcept : base_scene(entity_registry)
Expand Down Expand Up @@ -89,20 +92,26 @@ namespace tictactoe::example
};
if (player_as_win_functor()) {
this->current_state_ = static_cast<game_state>(this->player_turn_);
this->entity_registry_.view<antara::gaming::geometry::vertex_array, entt::tag<"grid"_hs>>().less(
[this](entt::entity entity, antara::gaming::geometry::vertex_array &array_cmp) {
for (auto&& current_vertex : array_cmp.vertices) {
current_vertex.pixel_color = this->player_turn_ == player::x ? antara::gaming::graphics::magenta : antara::gaming::graphics::cyan;
this->entity_registry_.view<geometry::vertex_array, entt::tag<"grid"_hs>>().less(
[this](entt::entity entity, geometry::vertex_array &array_cmp) {
for (auto &&current_vertex : array_cmp.vertices) {
current_vertex.pixel_color =
this->player_turn_ == player::x ? graphics::magenta
: graphics::cyan;
}
this->entity_registry_.assign_or_replace<antara::gaming::geometry::vertex_array>(entity, array_cmp.vertices, array_cmp.geometry_type);
this->entity_registry_.assign_or_replace<geometry::vertex_array>(entity,
array_cmp.vertices,
array_cmp.geometry_type);
});
} else if (tie_functor()) {
this->entity_registry_.view<antara::gaming::geometry::vertex_array, entt::tag<"grid"_hs>>().less(
[this](entt::entity entity, antara::gaming::geometry::vertex_array &array_cmp) {
for (auto&& current_vertex : array_cmp.vertices) {
current_vertex.pixel_color = antara::gaming::graphics::yellow;
this->entity_registry_.view<geometry::vertex_array, entt::tag<"grid"_hs>>().less(
[this](entt::entity entity, geometry::vertex_array &array_cmp) {
for (auto &&current_vertex : array_cmp.vertices) {
current_vertex.pixel_color = graphics::yellow;
}
this->entity_registry_.assign_or_replace<antara::gaming::geometry::vertex_array>(entity, array_cmp.vertices, array_cmp.geometry_type);
this->entity_registry_.assign_or_replace<geometry::vertex_array>(entity,
array_cmp.vertices,
array_cmp.geometry_type);
});
this->current_state_ = game_state::tie;
}
Expand Down Expand Up @@ -134,7 +143,7 @@ namespace tictactoe::example
}
}

bool game_scene::on_mouse_button_pressed(const antara::gaming::event::mouse_button_pressed &pressed) noexcept
bool game_scene::on_mouse_button_pressed(const event::mouse_button_pressed &pressed) noexcept
{
if (this->current_state_ == game_state::running) {
auto constants = entity_registry_.ctx<tic_tac_toe_constants>();
Expand Down Expand Up @@ -167,7 +176,8 @@ namespace tictactoe::example

void game_scene::prepare_constants() noexcept
{
auto canvas_size = this->entity_registry_.ctx<sf::RenderTexture>().getSize();
this->entity_registry_.set<tic_tac_toe_constants>(3ull, canvas_size.x, canvas_size.y);
auto[canvas_width, canvas_height] = this->entity_registry_.ctx<graphics::canvas_2d>().canvas.size.to<math::vec2u>();

this->entity_registry_.set<tic_tac_toe_constants>(3ull, canvas_width, canvas_height);
}
}
12 changes: 6 additions & 6 deletions examples/sfml/tic-tac-toe/tic.tac.toe.factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
******************************************************************************/

#include <cmath>
#include <SFML/Graphics/RenderTexture.hpp>
#include <entt/entity/helper.hpp>
#include <antara/gaming/graphics/component.layer.hpp>
#include <antara/gaming/transform/component.position.hpp>
#include <antara/gaming/geometry/component.vertex.hpp>
#include <antara/gaming/geometry/component.circle.hpp>
#include <antara/gaming/graphics/component.color.hpp>
#include <antara/gaming/graphics/component.canvas.hpp>
#include "tic.tac.toe.factory.hpp"
#include "tic.tac.toe.constants.hpp"
#include "tic.tac.toe.components.hpp"
Expand All @@ -34,7 +34,7 @@ namespace tictactoe ::example

entt::entity tic_tac_toe_factory::create_grid_entity(entt::registry &entity_registry) noexcept
{
auto window_info = entity_registry.ctx<sf::RenderTexture>().getSize();
auto [canvas_width, canvas_height] = entity_registry.ctx<graphics::canvas_2d>().canvas.size;
auto grid_entity = entity_registry.create();

std::vector<geometry::vertex> lines{8 * 4};
Expand All @@ -58,13 +58,13 @@ namespace tictactoe ::example
// Vertical
lines[counter].pos = {offset_x + i * constants.cell_width - half_thickness, 0.f};
lines[counter + 1].pos = {offset_x + i * constants.cell_width + half_thickness, 0.f};
lines[counter + 2].pos = {offset_x + i * constants.cell_width + half_thickness, static_cast<float>(window_info.y)};
lines[counter + 3].pos = {offset_x + i * constants.cell_width - half_thickness, static_cast<float>(window_info.y)};
lines[counter + 2].pos = {offset_x + i * constants.cell_width + half_thickness, canvas_height};
lines[counter + 3].pos = {offset_x + i * constants.cell_width - half_thickness, canvas_height};

// Horizontal
lines[counter + 4].pos = {offset_x + 0, offset_y + i * constants.cell_height - half_thickness};
lines[counter + 5].pos = {offset_x + window_info.x, offset_y + i * constants.cell_height - half_thickness};
lines[counter + 6].pos = {offset_x + window_info.x, offset_y + i * constants.cell_height + half_thickness};
lines[counter + 5].pos = {offset_x + canvas_width, offset_y + i * constants.cell_height - half_thickness};
lines[counter + 6].pos = {offset_x + canvas_width, offset_y + i * constants.cell_height + half_thickness};
lines[counter + 7].pos = {offset_x + 0, offset_y + i * constants.cell_height + half_thickness};
}

Expand Down

0 comments on commit be0a737

Please sign in to comment.