From 5dd3cc1b73a9e65ea14af862141896d25dd46ca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hatvani=20Tam=C3=A1s?= Date: Thu, 9 Apr 2026 22:07:10 +0200 Subject: [PATCH] feat: queuing layer commands such as pop, push and transition at the end of each frame --- SakuraCore/src/Application.cpp | 3 +++ SakuraCore/src/Layer.cpp | 23 +++++++++++++++++------ SakuraCore/src/LayerStack.cpp | 32 +++++++++++++++++++++++++++++++- SakuraCore/src/LayerStack.h | 12 ++++++++++++ 4 files changed, 63 insertions(+), 7 deletions(-) diff --git a/SakuraCore/src/Application.cpp b/SakuraCore/src/Application.cpp index 9ad5f13..bab04ee 100644 --- a/SakuraCore/src/Application.cpp +++ b/SakuraCore/src/Application.cpp @@ -155,6 +155,9 @@ void Application::Run() { } SDL_RenderPresent(m_Renderer); + + // handle layercommands here after each frame + m_LayerStack.ProcessCommands(); } } diff --git a/SakuraCore/src/Layer.cpp b/SakuraCore/src/Layer.cpp index 7f5bf92..91abbdf 100644 --- a/SakuraCore/src/Layer.cpp +++ b/SakuraCore/src/Layer.cpp @@ -1,5 +1,6 @@ #include "Layer.h" #include "Application.h" +#include "LayerStack.h" #include #include @@ -8,12 +9,22 @@ Layer::Layer(const std::string &name, bool isActive) : m_LayerName(name), m_isAc void Layer::QueueTransition(std::unique_ptr toLayer) { // TODO: redo this based on the event video + // basically handle it like the other layer stuff + // put it into a vector and go over the list at the end of the application loop auto &layerStack = Application::Get().m_LayerStack; - for (auto &layer : layerStack) { - if (layer.get() == this) { - layer = std::move(toLayer); - return; - } - } + + LayerCommand command; + command.action = LayerAction::Transition; + command.targetLayer = this; + command.newLayer = std::move(toLayer); + + layerStack.SubmitCommand(std::move(command)); + + // for (auto &layer : layerStack) { + // if (layer.get() == this) { + // layer = std::move(toLayer); + // return; + // } + // } } } // namespace SakuraVNE diff --git a/SakuraCore/src/LayerStack.cpp b/SakuraCore/src/LayerStack.cpp index 7c49ac8..bdf3916 100644 --- a/SakuraCore/src/LayerStack.cpp +++ b/SakuraCore/src/LayerStack.cpp @@ -7,10 +7,40 @@ namespace SakuraVNE { LayerStack::~LayerStack() { for (auto &layer : m_LayerStack) { layer->OnDetach(); - // delete layer; } } +void LayerStack::SubmitCommand(LayerCommand command) { m_CommandQueue.push_back(std::move(command)); } + +void LayerStack::ProcessCommands() { + if (m_CommandQueue.empty()) { + return; + } + + for (auto &command : m_CommandQueue) { + switch (command.action) { + case LayerAction::Pop: + PopLayer(command.targetLayer); + break; + case LayerAction::Push: + PushLayer(std::move(command.newLayer)); + break; + case LayerAction::Transition: + auto it = std::find_if(m_LayerStack.begin(), m_LayerStack.end(), [&](const std::unique_ptr &layer) { return layer.get() == command.targetLayer; }); + + if (it != m_LayerStack.end()) { + (*it)->OnDetach(); + command.newLayer->OnAttach(); + + *it = std::move(command.newLayer); + } + + break; + } + } + m_CommandQueue.clear(); +} + void LayerStack::PushLayer(std::unique_ptr layer) { m_LayerStack.emplace(m_LayerStack.begin() + m_LayerIndex, std::move(layer)); m_LayerIndex++; diff --git a/SakuraCore/src/LayerStack.h b/SakuraCore/src/LayerStack.h index f4bb8aa..ddcfea4 100644 --- a/SakuraCore/src/LayerStack.h +++ b/SakuraCore/src/LayerStack.h @@ -6,6 +6,14 @@ #include namespace SakuraVNE { +enum class LayerAction { Push, Pop, Transition }; + +struct LayerCommand { + LayerAction action; + Layer *targetLayer; + std::unique_ptr newLayer; +}; + class LayerStack { public: LayerStack() = default; @@ -26,11 +34,15 @@ public: std::vector>::const_reverse_iterator rbegin() const { return m_LayerStack.rbegin(); } std::vector>::const_reverse_iterator rend() const { return m_LayerStack.rend(); } + void SubmitCommand(LayerCommand command); + void ProcessCommands(); + #ifdef DEBUG // this is only used for the tests for now, so it will be taken out of the release build inline const std::vector> &GetLayers() const { return m_LayerStack; } #endif private: + std::vector m_CommandQueue; std::vector> m_LayerStack; unsigned int m_LayerIndex = 0; };