From 9badcddeae23b7a6ea1c00771c390cbb5d7be25c Mon Sep 17 00:00:00 2001 From: light7734 Date: Sat, 20 Sep 2025 15:41:34 +0330 Subject: [PATCH] refactor(surface): adjust to new ecs --- modules/surface/private/linux/system.cpp | 58 ++++++++++-------------- modules/surface/private/system.fuzz.cpp | 2 +- modules/surface/private/system.test.cpp | 23 +++++----- modules/surface/public/system.hpp | 9 ++-- 4 files changed, 40 insertions(+), 52 deletions(-) diff --git a/modules/surface/private/linux/system.cpp b/modules/surface/private/linux/system.cpp index b46b152..93dc265 100644 --- a/modules/surface/private/linux/system.cpp +++ b/modules/surface/private/linux/system.cpp @@ -40,40 +40,32 @@ System::System(Ref registry): m_registry(std::move(registry)) ensure(m_registry, "Failed to initialize surface system: null registry"); ensure( - m_registry->view().size() == 0, + m_registry->view().get_size() == 0, "Failed to initialize surface system: registry has surface component(s)" ); - m_registry->get_entt_registry() - .on_construct() - .connect<&System::on_surface_construct>(this); + m_registry->connect_on_construct( + [this](ecs::Registry ®istry, ecs::Entity entity) { + on_surface_construct(registry, entity); + } + ); - m_registry->get_entt_registry() - .on_update() - .connect<&System::on_surface_update>(this); - - m_registry->get_entt_registry() - .on_destroy() - .connect<&System::on_surface_destroy>(this); + m_registry->connect_on_destruct( + [this](ecs::Registry ®istry, ecs::Entity entity) { + on_surface_destruct(registry, entity); + } + ); } System::~System() { - m_registry->view().each([&](const entt::entity entity, SurfaceComponent &) { - m_registry->get_entt_registry().remove(entity); - }); + for (auto &[entity, surface] : m_registry->view()) + { + m_registry->remove(entity); + } - m_registry->get_entt_registry() - .on_construct() - .disconnect<&System::on_surface_construct>(this); - - m_registry->get_entt_registry() - .on_update() - .connect<&System::on_surface_update>(this); - - m_registry->get_entt_registry() - .on_destroy() - .disconnect<&System::on_surface_destroy>(this); + m_registry->disconnect_on_construct(); + m_registry->disconnect_on_destruct(); } void System::on_register() @@ -84,7 +76,7 @@ void System::on_unregister() { } -void System::on_surface_construct(entt::registry ®istry, entt::entity entity) +void System::on_surface_construct(ecs::Registry ®istry, ecs::Entity entity) { try { @@ -173,12 +165,7 @@ void System::on_surface_construct(entt::registry ®istry, entt::entity entity) } } -void System::on_surface_update(entt::registry ®istry, entt::entity entity) -{ - auto &surface = registry.get(entity); -} - -void System::on_surface_destroy(entt::registry ®istry, entt::entity entity) +void System::on_surface_destruct(ecs::Registry ®istry, ecs::Entity entity) { const auto &[display, window, _] = registry.get(entity).get_native_data(); if (!display) @@ -374,11 +361,12 @@ void System::modify_visiblity(SurfaceComponent &surface, const ModifyVisibilityR auto System::tick() -> bool { - m_registry->view().each([this](SurfaceComponent &surface) { + for (auto &dense : m_registry->view()) + { + auto &surface = dense.second; handle_requests(surface); - handle_events(surface); - }); + } return false; } diff --git a/modules/surface/private/system.fuzz.cpp b/modules/surface/private/system.fuzz.cpp index ad1197a..d31fcbc 100644 --- a/modules/surface/private/system.fuzz.cpp +++ b/modules/surface/private/system.fuzz.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/modules/surface/private/system.test.cpp b/modules/surface/private/system.test.cpp index c5bce3b..a7402e0 100644 --- a/modules/surface/private/system.test.cpp +++ b/modules/surface/private/system.test.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -45,8 +46,8 @@ public: } ) -> SurfaceComponent & { - auto entity = m_registry->create_entity(""); - return entity.add_component(info); + auto entity = m_registry->create_entity(); + return m_registry->add(entity, info); } void check_values(const SurfaceComponent &component) @@ -92,7 +93,7 @@ Suite raii = [] { Case { "post construct has correct state" } = [] { auto fixture = Fixture {}; auto system = System { fixture.registry() }; - expect_eq(fixture.registry()->view()->size(), 0); + expect_eq(fixture.registry()->view().get_size(), 0); }; Case { "post destruct has correct state" } = [] { @@ -100,10 +101,10 @@ Suite raii = [] { auto system = create_scope(fixture.registry()); fixture.add_surface_component(); - expect_eq(fixture.registry()->view()->size(), 1); + expect_eq(fixture.registry()->view().get_size(), 1); system.reset(); - expect_eq(fixture.registry()->view()->size(), 0); + expect_eq(fixture.registry()->view().get_size(), 0); }; }; @@ -113,7 +114,7 @@ Suite system_events = [] { auto system = System { fixture.registry() }; system.on_register(); - expect_eq(fixture.registry()->view().size(), 0); + expect_eq(fixture.registry()->view().get_size(), 0); }; Case { "on_unregister won't throw" } = [] { @@ -122,7 +123,7 @@ Suite system_events = [] { system.on_register(); system.on_unregister(); - expect_eq(fixture.registry()->view().size(), 0); + expect_eq(fixture.registry()->view().get_size(), 0); }; }; @@ -132,7 +133,7 @@ Suite registry_events = [] { auto system = System { fixture.registry() }; const auto &component = fixture.add_surface_component(); - expect_eq(fixture.registry()->view().size(), 1); + expect_eq(fixture.registry()->view().get_size(), 1); fixture.check_values(component); }; @@ -168,7 +169,7 @@ Suite registry_events = [] { auto system = System { fixture.registry() }; expect_throw([&] { fixture.add_surface_component({ .resolution = { width, 0 } }); }); - expect_eq(fixture.registry()->view().size(), 0); + expect_eq(fixture.registry()->view().get_size(), 0); }; Case { "on_destrroy cleans up component" } = [] { @@ -176,11 +177,11 @@ Suite registry_events = [] { auto system = create_scope(fixture.registry()); const auto &component = fixture.add_surface_component(); - expect_eq(fixture.registry()->view().size(), 1); + expect_eq(fixture.registry()->view().get_size(), 1); fixture.check_values(component); system.reset(); - expect_eq(fixture.registry()->view().size(), 0); + expect_eq(fixture.registry()->view().get_size(), 0); }; }; diff --git a/modules/surface/public/system.hpp b/modules/surface/public/system.hpp index 727b119..c55749d 100644 --- a/modules/surface/public/system.hpp +++ b/modules/surface/public/system.hpp @@ -1,7 +1,8 @@ #pragma once #include -#include +#include +#include namespace lt::surface { @@ -27,11 +28,9 @@ public: auto tick() -> bool override; private: - void on_surface_construct(entt::registry ®istry, entt::entity entity); + void on_surface_construct(ecs::Registry ®istry, ecs::Entity entity); - void on_surface_update(entt::registry ®istry, entt::entity entity); - - void on_surface_destroy(entt::registry ®istry, entt::entity entity); + void on_surface_destruct(ecs::Registry ®istry, ecs::Entity entity); void handle_requests(struct SurfaceComponent &surface);