refactor(surface): adjust to new ecs
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
light7734 2025-09-20 15:41:34 +03:30
parent 120b6c24d9
commit 9badcddeae
Signed by: light7734
GPG key ID: 8C30176798F1A6BA
4 changed files with 40 additions and 52 deletions

View file

@ -40,40 +40,32 @@ System::System(Ref<ecs::Registry> registry): m_registry(std::move(registry))
ensure(m_registry, "Failed to initialize surface system: null registry"); ensure(m_registry, "Failed to initialize surface system: null registry");
ensure( ensure(
m_registry->view<SurfaceComponent>().size() == 0, m_registry->view<SurfaceComponent>().get_size() == 0,
"Failed to initialize surface system: registry has surface component(s)" "Failed to initialize surface system: registry has surface component(s)"
); );
m_registry->get_entt_registry() m_registry->connect_on_construct<SurfaceComponent>(
.on_construct<SurfaceComponent>() [this](ecs::Registry &registry, ecs::Entity entity) {
.connect<&System::on_surface_construct>(this); on_surface_construct(registry, entity);
}
);
m_registry->get_entt_registry() m_registry->connect_on_destruct<SurfaceComponent>(
.on_update<SurfaceComponent>() [this](ecs::Registry &registry, ecs::Entity entity) {
.connect<&System::on_surface_update>(this); on_surface_destruct(registry, entity);
}
m_registry->get_entt_registry() );
.on_destroy<SurfaceComponent>()
.connect<&System::on_surface_destroy>(this);
} }
System::~System() System::~System()
{ {
m_registry->view<SurfaceComponent>().each([&](const entt::entity entity, SurfaceComponent &) { for (auto &[entity, surface] : m_registry->view<SurfaceComponent>())
m_registry->get_entt_registry().remove<SurfaceComponent>(entity); {
}); m_registry->remove<SurfaceComponent>(entity);
}
m_registry->get_entt_registry() m_registry->disconnect_on_construct<SurfaceComponent>();
.on_construct<SurfaceComponent>() m_registry->disconnect_on_destruct<SurfaceComponent>();
.disconnect<&System::on_surface_construct>(this);
m_registry->get_entt_registry()
.on_update<SurfaceComponent>()
.connect<&System::on_surface_update>(this);
m_registry->get_entt_registry()
.on_destroy<SurfaceComponent>()
.disconnect<&System::on_surface_destroy>(this);
} }
void System::on_register() void System::on_register()
@ -84,7 +76,7 @@ void System::on_unregister()
{ {
} }
void System::on_surface_construct(entt::registry &registry, entt::entity entity) void System::on_surface_construct(ecs::Registry &registry, ecs::Entity entity)
{ {
try try
{ {
@ -173,12 +165,7 @@ void System::on_surface_construct(entt::registry &registry, entt::entity entity)
} }
} }
void System::on_surface_update(entt::registry &registry, entt::entity entity) void System::on_surface_destruct(ecs::Registry &registry, ecs::Entity entity)
{
auto &surface = registry.get<SurfaceComponent>(entity);
}
void System::on_surface_destroy(entt::registry &registry, entt::entity entity)
{ {
const auto &[display, window, _] = registry.get<SurfaceComponent>(entity).get_native_data(); const auto &[display, window, _] = registry.get<SurfaceComponent>(entity).get_native_data();
if (!display) if (!display)
@ -374,11 +361,12 @@ void System::modify_visiblity(SurfaceComponent &surface, const ModifyVisibilityR
auto System::tick() -> bool auto System::tick() -> bool
{ {
m_registry->view<SurfaceComponent>().each([this](SurfaceComponent &surface) { for (auto &dense : m_registry->view<SurfaceComponent>())
{
auto &surface = dense.second;
handle_requests(surface); handle_requests(surface);
handle_events(surface); handle_events(surface);
}); }
return false; return false;
} }

View file

@ -1,5 +1,5 @@
#include <ecs/entity.hpp> #include <ecs/entity.hpp>
#include <ecs/scene.hpp> #include <ecs/registry.hpp>
#include <surface/components.hpp> #include <surface/components.hpp>
#include <surface/system.hpp> #include <surface/system.hpp>
#include <test/fuzz.hpp> #include <test/fuzz.hpp>

View file

@ -1,4 +1,5 @@
#include <ecs/entity.hpp> #include <ecs/entity.hpp>
#include <ranges>
#include <surface/components.hpp> #include <surface/components.hpp>
#include <surface/requests/surface.hpp> #include <surface/requests/surface.hpp>
#include <surface/system.hpp> #include <surface/system.hpp>
@ -45,8 +46,8 @@ public:
} }
) -> SurfaceComponent & ) -> SurfaceComponent &
{ {
auto entity = m_registry->create_entity(""); auto entity = m_registry->create_entity();
return entity.add_component<SurfaceComponent>(info); return m_registry->add<SurfaceComponent>(entity, info);
} }
void check_values(const SurfaceComponent &component) void check_values(const SurfaceComponent &component)
@ -92,7 +93,7 @@ Suite raii = [] {
Case { "post construct has correct state" } = [] { Case { "post construct has correct state" } = [] {
auto fixture = Fixture {}; auto fixture = Fixture {};
auto system = System { fixture.registry() }; auto system = System { fixture.registry() };
expect_eq(fixture.registry()->view<SurfaceComponent>()->size(), 0); expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
}; };
Case { "post destruct has correct state" } = [] { Case { "post destruct has correct state" } = [] {
@ -100,10 +101,10 @@ Suite raii = [] {
auto system = create_scope<System>(fixture.registry()); auto system = create_scope<System>(fixture.registry());
fixture.add_surface_component(); fixture.add_surface_component();
expect_eq(fixture.registry()->view<SurfaceComponent>()->size(), 1); expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 1);
system.reset(); system.reset();
expect_eq(fixture.registry()->view<SurfaceComponent>()->size(), 0); expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
}; };
}; };
@ -113,7 +114,7 @@ Suite system_events = [] {
auto system = System { fixture.registry() }; auto system = System { fixture.registry() };
system.on_register(); system.on_register();
expect_eq(fixture.registry()->view<SurfaceComponent>().size(), 0); expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
}; };
Case { "on_unregister won't throw" } = [] { Case { "on_unregister won't throw" } = [] {
@ -122,7 +123,7 @@ Suite system_events = [] {
system.on_register(); system.on_register();
system.on_unregister(); system.on_unregister();
expect_eq(fixture.registry()->view<SurfaceComponent>().size(), 0); expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
}; };
}; };
@ -132,7 +133,7 @@ Suite registry_events = [] {
auto system = System { fixture.registry() }; auto system = System { fixture.registry() };
const auto &component = fixture.add_surface_component(); const auto &component = fixture.add_surface_component();
expect_eq(fixture.registry()->view<SurfaceComponent>().size(), 1); expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 1);
fixture.check_values(component); fixture.check_values(component);
}; };
@ -168,7 +169,7 @@ Suite registry_events = [] {
auto system = System { fixture.registry() }; auto system = System { fixture.registry() };
expect_throw([&] { fixture.add_surface_component({ .resolution = { width, 0 } }); }); expect_throw([&] { fixture.add_surface_component({ .resolution = { width, 0 } }); });
expect_eq(fixture.registry()->view<SurfaceComponent>().size(), 0); expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
}; };
Case { "on_destrroy<SurfaceComponent> cleans up component" } = [] { Case { "on_destrroy<SurfaceComponent> cleans up component" } = [] {
@ -176,11 +177,11 @@ Suite registry_events = [] {
auto system = create_scope<System>(fixture.registry()); auto system = create_scope<System>(fixture.registry());
const auto &component = fixture.add_surface_component(); const auto &component = fixture.add_surface_component();
expect_eq(fixture.registry()->view<SurfaceComponent>().size(), 1); expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 1);
fixture.check_values(component); fixture.check_values(component);
system.reset(); system.reset();
expect_eq(fixture.registry()->view<SurfaceComponent>().size(), 0); expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
}; };
}; };

View file

@ -1,7 +1,8 @@
#pragma once #pragma once
#include <app/system.hpp> #include <app/system.hpp>
#include <ecs/scene.hpp> #include <ecs/registry.hpp>
#include <math/vec2.hpp>
namespace lt::surface { namespace lt::surface {
@ -27,11 +28,9 @@ public:
auto tick() -> bool override; auto tick() -> bool override;
private: private:
void on_surface_construct(entt::registry &registry, entt::entity entity); void on_surface_construct(ecs::Registry &registry, ecs::Entity entity);
void on_surface_update(entt::registry &registry, entt::entity entity); void on_surface_destruct(ecs::Registry &registry, ecs::Entity entity);
void on_surface_destroy(entt::registry &registry, entt::entity entity);
void handle_requests(struct SurfaceComponent &surface); void handle_requests(struct SurfaceComponent &surface);