diff --git a/modules/surface/private/linux/system.cpp b/modules/surface/private/linux/system.cpp index 419c609..c172b66 100644 --- a/modules/surface/private/linux/system.cpp +++ b/modules/surface/private/linux/system.cpp @@ -44,13 +44,13 @@ System::System(Ref registry): m_registry(std::move(registry)) ); m_registry->connect_on_construct( - [this](ecs::Registry ®istry, ecs::Entity entity) { + [this](ecs::Registry ®istry, ecs::EntityId entity) { on_surface_construct(registry, entity); } ); m_registry->connect_on_destruct( - [this](ecs::Registry ®istry, ecs::Entity entity) { + [this](ecs::Registry ®istry, ecs::EntityId entity) { on_surface_destruct(registry, entity); } ); @@ -58,10 +58,15 @@ System::System(Ref registry): m_registry(std::move(registry)) System::~System() { + if (!m_registry) + { + return; + } + try { // TODO(Light): make registry.remove not validate iterators - auto entities_to_remove = std::vector {}; + auto entities_to_remove = std::vector {}; for (auto &[entity, surface] : m_registry->view()) { entities_to_remove.emplace_back(entity); @@ -90,7 +95,7 @@ void System::on_unregister() { } -void System::on_surface_construct(ecs::Registry ®istry, ecs::Entity entity) +void System::on_surface_construct(ecs::Registry ®istry, ecs::EntityId entity) { try { @@ -182,7 +187,7 @@ void System::on_surface_construct(ecs::Registry ®istry, ecs::Entity entity) } } -void System::on_surface_destruct(ecs::Registry ®istry, ecs::Entity entity) +void System::on_surface_destruct(ecs::Registry ®istry, ecs::EntityId entity) { const auto &[display, window, _] = registry.get(entity).get_native_data(); if (!display) @@ -355,7 +360,7 @@ void System::modify_visiblity(SurfaceComponent &surface, const ModifyVisibilityR } } -auto System::tick() -> bool +void System::tick(app::TickInfo tick) { for (auto &dense : m_registry->view()) { @@ -364,7 +369,12 @@ auto System::tick() -> bool handle_events(surface); } - return false; + const auto now = std::chrono::steady_clock::now(); + m_last_tick_result = app::TickResult { + .info = tick, + .duration = now - tick.start_time, + .end_time = now, + }; } void ensure_component_sanity(const SurfaceComponent &component) diff --git a/modules/surface/private/system.test.cpp b/modules/surface/private/system.test.cpp index a7402e0..b278c58 100644 --- a/modules/surface/private/system.test.cpp +++ b/modules/surface/private/system.test.cpp @@ -16,6 +16,15 @@ using test::expect_not_nullptr; using test::expect_throw; using test::Suite; +[[nodiscard]] auto tick_info() -> app::TickInfo +{ + return { + .delta_time = std::chrono::milliseconds { 16 }, + .budget = std::chrono::milliseconds { 10 }, + .start_time = std::chrono::steady_clock::now(), + }; +} + constexpr auto title = "TestWindow"; constexpr auto width = 800u; constexpr auto height = 600u; @@ -188,7 +197,7 @@ Suite registry_events = [] { Suite tick = [] { Case { "ticking on empty registry won't throw" } = [] { auto fixture = Fixture {}; - System { fixture.registry() }.tick(); + System { fixture.registry() }.tick(tick_info()); }; Case { "ticking on non-empty registry won't throw" } = [] { @@ -196,7 +205,7 @@ Suite tick = [] { auto system = System { fixture.registry() }; fixture.add_surface_component(); - system.tick(); + system.tick(tick_info()); }; }; @@ -207,7 +216,7 @@ Suite tick_handles_events = [] { auto &surface = fixture.add_surface_component(); // flush window-creation events - system.tick(); + system.tick(tick_info()); expect_eq(surface.peek_events().size(), 0); surface.push_event(surface::MovedEvent({}, {})); @@ -216,7 +225,7 @@ Suite tick_handles_events = [] { surface.push_event(surface::ButtonPressedEvent({})); expect_eq(surface.peek_events().size(), 2); - system.tick(); + system.tick(tick_info()); expect_eq(surface.peek_events().size(), 0); }; }; @@ -235,7 +244,7 @@ Suite tick_handles_requests = [] { surface.push_request(surface::ModifyVisibilityRequest(true)); expect_eq(surface.peek_requests().size(), 1); - system.tick(); + system.tick(tick_info()); expect_eq(surface.peek_requests().size(), 0); surface.push_request(surface::ModifyTitleRequest(title)); @@ -250,7 +259,7 @@ Suite tick_handles_requests = [] { surface.push_request(surface::ModifyVisibilityRequest(false)); expect_eq(surface.peek_requests().size(), 1 + 2 + 3); - system.tick(); + system.tick(tick_info()); expect_eq(surface.peek_requests().size(), 0); expect_eq(surface.get_title(), title); diff --git a/modules/surface/public/system.hpp b/modules/surface/public/system.hpp index c55749d..0d8f08d 100644 --- a/modules/surface/public/system.hpp +++ b/modules/surface/public/system.hpp @@ -25,12 +25,17 @@ public: void on_unregister() override; - auto tick() -> bool override; + void tick(app::TickInfo tick) override; + + [[nodiscard]] auto get_last_tick_result() const -> const app::TickResult & override + { + return m_last_tick_result; + } private: - void on_surface_construct(ecs::Registry ®istry, ecs::Entity entity); + void on_surface_construct(ecs::Registry ®istry, ecs::EntityId entity); - void on_surface_destruct(ecs::Registry ®istry, ecs::Entity entity); + void on_surface_destruct(ecs::Registry ®istry, ecs::EntityId entity); void handle_requests(struct SurfaceComponent &surface); @@ -53,13 +58,15 @@ private: const struct ModifyVisibilityRequest &request ); - void modify_position(ecs::Entity surface_entity, const math::ivec2 &new_size); + void modify_position(ecs::EntityId surface_entity, const math::ivec2 &new_size); - void modify_position(ecs::Entity surface_entity, const math::uvec2 &new_size); + void modify_position(ecs::EntityId surface_entity, const math::uvec2 &new_size); - void set_visibility(ecs::Entity surface_entity, bool visible); + void set_visibility(ecs::EntityId surface_entity, bool visible); Ref m_registry; + + app::TickResult m_last_tick_result; }; } // namespace lt::surface