From 6381ed15148d0c97398fe07129725bfb6a2f9e47 Mon Sep 17 00:00:00 2001 From: light7734 Date: Fri, 11 Jul 2025 00:56:57 +0330 Subject: [PATCH] refactor: layer stack --- .../include/engine/core/application.hpp | 7 ++++ .../include/engine/layer/layer_stack.hpp | 37 +++++-------------- modules/engine/src/core/application.cpp | 13 ++++--- modules/engine/src/layer/layer_stack.cpp | 12 ++---- modules/mirror/src/mirror.cpp | 16 +++----- 5 files changed, 34 insertions(+), 51 deletions(-) diff --git a/modules/engine/include/engine/core/application.hpp b/modules/engine/include/engine/core/application.hpp index 2426053..81af177 100644 --- a/modules/engine/include/engine/core/application.hpp +++ b/modules/engine/include/engine/core/application.hpp @@ -36,6 +36,11 @@ public: return *m_window; } + [[nodiscard]] auto get_layer_stack() -> LayerStack & + { + return *m_layer_stack; + } + static void quit(); protected: @@ -64,6 +69,8 @@ private: Scope m_renderer; + Scope m_layer_stack; + static Application *s_instance; }; diff --git a/modules/engine/include/engine/layer/layer_stack.hpp b/modules/engine/include/engine/layer/layer_stack.hpp index 37b2f7f..eb369d5 100644 --- a/modules/engine/include/engine/layer/layer_stack.hpp +++ b/modules/engine/include/engine/layer/layer_stack.hpp @@ -1,6 +1,5 @@ #pragma once - namespace lt { class Layer; @@ -9,61 +8,43 @@ class Event; class LayerStack { public: - static auto instance() -> LayerStack & - { - static auto instance = LayerStack {}; - return instance; - } - template - static void emplace_layer(Args &&...args) + void emplace_layer(Args &&...args) { - instance().attach_layer_impl(create_ref(std::forward(args)...)); + attach_layer(create_ref(std::forward(args)...)); } - static void attach_layer(Ref layer) - { - instance().attach_layer_impl(std::move(layer)); - } + void attach_layer(Ref layer); - static void detach_layer(const Ref &layer) - { - instance().detach_layer_impl(layer); - } + void detach_layer(const Ref &layer); - auto is_empty() -> bool + [[nodiscard]] auto is_empty() const -> bool { return m_layers.empty(); } - auto begin() -> std::vector>::iterator + [[nodiscard]] auto begin() -> std::vector>::iterator { return m_layers.begin(); } - auto end() -> std::vector>::iterator + [[nodiscard]] auto end() -> std::vector>::iterator { return m_layers.end(); } - auto rbegin() -> std::vector>::reverse_iterator + [[nodiscard]] auto rbegin() -> std::vector>::reverse_iterator { return m_layers.rbegin(); } - auto rend() -> std::vector>::reverse_iterator + [[nodiscard]] auto rend() -> std::vector>::reverse_iterator { return m_layers.rend(); } private: std::vector> m_layers; - - LayerStack() = default; - - void attach_layer_impl(Ref layer); - - void detach_layer_impl(const Ref &layer); }; } // namespace lt diff --git a/modules/engine/src/core/application.cpp b/modules/engine/src/core/application.cpp index 452c579..d3474c0 100644 --- a/modules/engine/src/core/application.cpp +++ b/modules/engine/src/core/application.cpp @@ -66,6 +66,8 @@ Application::Application(): m_window(nullptr) (GLFWwindow *)m_window->get_handle(), m_graphics_context->get_shared_context() ); + + m_layer_stack = create_scope(); } void Application::game_loop() @@ -88,7 +90,7 @@ void Application::quit() void Application::update_layers() { - for (auto &it : LayerStack::instance()) + for (auto &it : *m_layer_stack) { it->on_update(m_timer.get_elapsed_time()); } @@ -101,7 +103,7 @@ void Application::render_layers() { m_renderer->begin_frame(); - for (auto &it : LayerStack::instance()) + for (auto &it : *m_layer_stack) { it->on_render(); } @@ -113,7 +115,7 @@ void Application::render_user_interface() { m_user_interface->begin(); - for (auto &it : LayerStack::instance()) + for (auto &it : *m_layer_stack) { it->on_user_interface_update(); } @@ -150,7 +152,7 @@ void Application::on_event(const Event &event) } } - for (auto &it : std::ranges::reverse_view(LayerStack::instance())) + for (auto &it : std::ranges::reverse_view(*m_layer_stack)) { if (it->on_event(event)) { @@ -167,7 +169,8 @@ void Application::on_event(const Event &event) lt_assert(m_user_interface, "User interface is not initialized"); lt_assert(m_graphics_context, "Graphics context is not initialized"); lt_assert(m_renderer, "Renderer is not initialized"); - lt_assert(!LayerStack::instance().is_empty(), "Layer_stack is empty"); + lt_assert(m_layer_stack, "Layer_stack is not initialized"); + lt_assert(!m_layer_stack->is_empty(), "Layer_stack is empty"); log_inf("Logging application state..."); this->log_debug_data(); diff --git a/modules/engine/src/layer/layer_stack.cpp b/modules/engine/src/layer/layer_stack.cpp index 03b73ed..3834a36 100644 --- a/modules/engine/src/layer/layer_stack.cpp +++ b/modules/engine/src/layer/layer_stack.cpp @@ -1,21 +1,17 @@ #include #include -#include -#include -#include -#include namespace lt { -void LayerStack::attach_layer_impl(Ref layer) +void LayerStack::attach_layer(Ref layer) { - log_trc("Attaching [{}]", layer->get_name()); + log_trc("Attaching layer [{}]", layer->get_name()); m_layers.emplace_back(std::move(layer)); } -void LayerStack::detach_layer_impl(const Ref &layer) +void LayerStack::detach_layer(const Ref &layer) { - log_trc("Detaching [{}]", layer->get_name()); + log_trc("Detaching layer [{}]", layer->get_name()); m_layers.erase(std::find(m_layers.begin(), m_layers.end(), layer)); } diff --git a/modules/mirror/src/mirror.cpp b/modules/mirror/src/mirror.cpp index 2fad1cc..49fc46e 100644 --- a/modules/mirror/src/mirror.cpp +++ b/modules/mirror/src/mirror.cpp @@ -13,17 +13,13 @@ class Mirror: public Application public: Mirror() { - // Set window properties - auto properties = WindowProperties { - .title = "Mirror", - .size = glm::uvec2(1280u, 720u), - .vsync = true, - }; + get_window().set_properties(WindowProperties { + .title = "Mirror", + .size = glm::uvec2(1280u, 720u), + .vsync = true, + }); - get_window().set_properties(properties); - - // Attach the sandbox layer - LayerStack::emplace_layer("MirrorLayer"); + get_layer_stack().emplace_layer("MirrorLayer"); } };