diff --git a/modules/sfml/antara/gaming/sfml/component.drawable.hpp b/modules/sfml/antara/gaming/sfml/component.drawable.hpp index 25d271f6..59257aef 100644 --- a/modules/sfml/antara/gaming/sfml/component.drawable.hpp +++ b/modules/sfml/antara/gaming/sfml/component.drawable.hpp @@ -68,4 +68,5 @@ namespace antara::gaming::sfml // LCOV_EXCL_STOP using drawable_list = doom::meta::list; + using drawable_list_transformable = doom::meta::list; } \ No newline at end of file diff --git a/modules/sfml/antara/gaming/sfml/graphic.system.cpp b/modules/sfml/antara/gaming/sfml/graphic.system.cpp index 042aa1dc..51d3367b 100644 --- a/modules/sfml/antara/gaming/sfml/graphic.system.cpp +++ b/modules/sfml/antara/gaming/sfml/graphic.system.cpp @@ -31,30 +31,37 @@ namespace antara::gaming::sfml graphic_system::graphic_system(entt::registry ®istry) noexcept : system(registry) { this->dispatcher_.sink().connect<&graphic_system::on_window_resized_event>(*this); - this->entity_registry_.on_construct().connect<&graphic_system::on_geometry_vertex_array_construct>(*this); - this->entity_registry_.on_replace().connect<&graphic_system::on_geometry_vertex_array_construct>(*this); - this->entity_registry_.on_construct().connect<&graphic_system::on_geometry_circle_construct>(*this); + this->entity_registry_.on_construct().connect<&graphic_system::on_position_2d_construct>( + *this); + this->entity_registry_.on_replace().connect<&graphic_system::on_position_2d_construct>( + *this); + this->entity_registry_.on_construct().connect<&graphic_system::on_geometry_vertex_array_construct>( + *this); + this->entity_registry_.on_replace().connect<&graphic_system::on_geometry_vertex_array_construct>( + *this); + this->entity_registry_.on_construct().connect<&graphic_system::on_geometry_circle_construct>( + *this); refresh_render_texture(); } void graphic_system::refresh_render_texture() noexcept { - auto& canvas_2d = this->entity_registry_.ctx(); + auto &canvas_2d = this->entity_registry_.ctx(); - auto [view_pos_x, view_pos_y] = canvas_2d.view_port.position; - auto [view_width, view_height] = canvas_2d.view_port.size; + auto[view_pos_x, view_pos_y] = canvas_2d.view_port.position; + auto[view_width, view_height] = canvas_2d.view_port.size; window_.setView(sf::View(sf::FloatRect(view_pos_x, view_pos_y, view_width, view_height))); - auto [rt_size_x, rt_size_y] = canvas_2d.canvas_texture.size; + auto[rt_size_x, rt_size_y] = canvas_2d.canvas_texture.size; render_texture_.create(rt_size_x, rt_size_y); render_texture_sprite_.setTexture(render_texture_.getTexture(), true); render_texture_.setSmooth(true); - auto [scale_x, scale_y] = canvas_2d.canvas_texture_scaling; + auto[scale_x, scale_y] = canvas_2d.canvas_texture_scaling; render_texture_sprite_.setScale(sf::Vector2f(scale_x, scale_y)); render_texture_sprite_.setOrigin(render_texture_sprite_.getLocalBounds().width * 0.5f, render_texture_sprite_.getLocalBounds().height * 0.5f); - auto [rt_texture_pos_x, rt_texture_pos_y] = canvas_2d.canvas_texture.position; + auto[rt_texture_pos_x, rt_texture_pos_y] = canvas_2d.canvas_texture.position; render_texture_sprite_.setPosition(rt_texture_pos_x, rt_texture_pos_y); } @@ -80,12 +87,6 @@ namespace antara::gaming::sfml [this](auto entity, auto &&drawable, [[maybe_unused]] auto &&) { - if constexpr (std::is_base_of_v) { - if (auto cmp_position = this->entity_registry_.try_get(entity); - cmp_position != nullptr) { - drawable.drawable.setPosition(cmp_position->x(), cmp_position->y()); - } - } this->render_texture_.draw(drawable.drawable); }); } @@ -137,11 +138,35 @@ namespace antara::gaming::sfml using ranges::views::zip; using ranges::views::ints; - for(auto &&[current_vertex, current_idx]: zip(cmp_vertex_array.vertices, ints(0u, ranges::unreachable))) { + for (auto &&[current_vertex, current_idx]: zip(cmp_vertex_array.vertices, ints(0u, ranges::unreachable))) { sf_vertex_array[current_idx].position = sf::Vector2f{current_vertex.pos.x(), current_vertex.pos.y()}; - sf_vertex_array[current_idx].texCoords = sf::Vector2f{current_vertex.texture_pos.x(), current_vertex.texture_pos.y()}; - auto [r,g,b,a] = current_vertex.pixel_color; + sf_vertex_array[current_idx].texCoords = sf::Vector2f{current_vertex.texture_pos.x(), + current_vertex.texture_pos.y()}; + auto[r, g, b, a] = current_vertex.pixel_color; sf_vertex_array[current_idx].color = sf::Color(r, g, b, a); } } + + void graphic_system::on_position_2d_construct(entt::entity entity, entt::registry &, + transform::position_2d &pos) noexcept + { + set_position(entity, pos, drawable_list_transformable{}); + } + + template + void graphic_system::set_position(entt::entity entity, transform::position_2d &pos, + doom::meta::list) noexcept + { + (... || set_position(entity, pos)); + } + + template + bool graphic_system::set_position(entt::entity entity, transform::position_2d &pos) noexcept + { + if (auto cmp = this->entity_registry_.try_get(entity); cmp != nullptr) { + cmp->drawable.setPosition(pos.x(), pos.y()); + return true; + } + return false; + } } diff --git a/modules/sfml/antara/gaming/sfml/graphic.system.hpp b/modules/sfml/antara/gaming/sfml/graphic.system.hpp index 556b6ee4..1a478964 100644 --- a/modules/sfml/antara/gaming/sfml/graphic.system.hpp +++ b/modules/sfml/antara/gaming/sfml/graphic.system.hpp @@ -39,6 +39,12 @@ namespace antara::gaming::sfml void update() noexcept final; + template + bool set_position(entt::entity entity, transform::position_2d &pos) noexcept; + + template + void set_position(entt::entity entity, transform::position_2d &pos, doom::meta::list) noexcept; + template void draw() noexcept; @@ -56,6 +62,7 @@ namespace antara::gaming::sfml //! Callback void on_window_resized_event(const event::window_resized &evt) noexcept; void on_geometry_circle_construct(entt::entity entity, entt::registry ®istry, geometry::circle &circle) noexcept; + void on_position_2d_construct(entt::entity entity, entt::registry ®istry, transform::position_2d &pos) noexcept; void on_geometry_vertex_array_construct(entt::entity entity, entt::registry ®istry, geometry::vertex_array &cmp_vertex_array) noexcept; private: