Compare commits

...

2 Commits

5 changed files with 63 additions and 11 deletions

View File

@@ -155,6 +155,9 @@ void Application::Run() {
}
SDL_RenderPresent(m_Renderer);
// handle layercommands here after each frame
m_LayerStack.ProcessCommands();
}
}

View File

@@ -1,5 +1,6 @@
#include "Layer.h"
#include "Application.h"
#include "LayerStack.h"
#include <memory>
#include <string>
@@ -8,12 +9,22 @@ Layer::Layer(const std::string &name, bool isActive) : m_LayerName(name), m_isAc
void Layer::QueueTransition(std::unique_ptr<Layer> 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

View File

@@ -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> &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> layer) {
m_LayerStack.emplace(m_LayerStack.begin() + m_LayerIndex, std::move(layer));
m_LayerIndex++;

View File

@@ -6,6 +6,14 @@
#include <vector>
namespace SakuraVNE {
enum class LayerAction { Push, Pop, Transition };
struct LayerCommand {
LayerAction action;
Layer *targetLayer;
std::unique_ptr<Layer> newLayer;
};
class LayerStack {
public:
LayerStack() = default;
@@ -26,11 +34,15 @@ public:
std::vector<std::unique_ptr<Layer>>::const_reverse_iterator rbegin() const { return m_LayerStack.rbegin(); }
std::vector<std::unique_ptr<Layer>>::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<std::unique_ptr<Layer>> &GetLayers() const { return m_LayerStack; }
#endif
private:
std::vector<LayerCommand> m_CommandQueue;
std::vector<std::unique_ptr<Layer>> m_LayerStack;
unsigned int m_LayerIndex = 0;
};

View File

@@ -37,10 +37,6 @@ void Window::Create() {
}
}
// TODO: add the window callbacks here as seperate functions because sdl functions that way
// each of them will get a function which will receive a window event filled in with the window's data (id (i guess))
// and then they will dispatch it to the event system using the same raise event
void Window::ProcessEvent(const SDL_Event &event) {
switch (event.type) {
case SDL_EVENT_WINDOW_CLOSE_REQUESTED: {