refactor: layer stack

This commit is contained in:
light7734 2025-07-11 00:56:57 +03:30
parent 284ef9be53
commit 6381ed1514
Signed by: light7734
GPG key ID: 8C30176798F1A6BA
5 changed files with 34 additions and 51 deletions

View file

@ -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<Renderer> m_renderer;
Scope<LayerStack> m_layer_stack;
static Application *s_instance;
};

View file

@ -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<typename Layer_T, typename... Args>
static void emplace_layer(Args &&...args)
void emplace_layer(Args &&...args)
{
instance().attach_layer_impl(create_ref<Layer_T>(std::forward<Args>(args)...));
attach_layer(create_ref<Layer_T>(std::forward<Args>(args)...));
}
static void attach_layer(Ref<Layer> layer)
{
instance().attach_layer_impl(std::move(layer));
}
void attach_layer(Ref<Layer> layer);
static void detach_layer(const Ref<Layer> &layer)
{
instance().detach_layer_impl(layer);
}
void detach_layer(const Ref<Layer> &layer);
auto is_empty() -> bool
[[nodiscard]] auto is_empty() const -> bool
{
return m_layers.empty();
}
auto begin() -> std::vector<Ref<Layer>>::iterator
[[nodiscard]] auto begin() -> std::vector<Ref<Layer>>::iterator
{
return m_layers.begin();
}
auto end() -> std::vector<Ref<Layer>>::iterator
[[nodiscard]] auto end() -> std::vector<Ref<Layer>>::iterator
{
return m_layers.end();
}
auto rbegin() -> std::vector<Ref<Layer>>::reverse_iterator
[[nodiscard]] auto rbegin() -> std::vector<Ref<Layer>>::reverse_iterator
{
return m_layers.rbegin();
}
auto rend() -> std::vector<Ref<Layer>>::reverse_iterator
[[nodiscard]] auto rend() -> std::vector<Ref<Layer>>::reverse_iterator
{
return m_layers.rend();
}
private:
std::vector<Ref<Layer>> m_layers;
LayerStack() = default;
void attach_layer_impl(Ref<Layer> layer);
void detach_layer_impl(const Ref<Layer> &layer);
};
} // namespace lt

View file

@ -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<LayerStack>();
}
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();

View file

@ -1,21 +1,17 @@
#include <engine/layer/layer.hpp>
#include <engine/layer/layer_stack.hpp>
#include <input/events/event.hpp>
#include <input/events/keyboard.hpp>
#include <input/events/mouse.hpp>
#include <input/events/window.hpp>
namespace lt {
void LayerStack::attach_layer_impl(Ref<Layer> layer)
void LayerStack::attach_layer(Ref<Layer> 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> &layer)
void LayerStack::detach_layer(const Ref<Layer> &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));
}

View file

@ -13,17 +13,13 @@ class Mirror: public Application
public:
Mirror()
{
// Set window properties
auto properties = WindowProperties {
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<EditorLayer>("MirrorLayer");
get_layer_stack().emplace_layer<EditorLayer>("MirrorLayer");
}
};