Introducing application layers

This feature is not really used yet but will help in the future simplify the propegation of events
This commit is contained in:
Nigel Barink 2023-05-08 22:01:44 +02:00
parent d8627d0357
commit 8e202f9d59
5 changed files with 92 additions and 2 deletions

View File

@ -38,7 +38,7 @@ namespace YoggieEngine {
void Application::Run() {
std::cout << "No run function implemented!";
}
@ -81,6 +81,12 @@ namespace YoggieEngine {
}
}
void Application::PushLayer(Layer* layer)
{
AppLayerstack.PushLayer(layer);
layer->OnAttach();
}
Application::~Application() {
ImGui_ImplOpenGL3_Shutdown();

View File

@ -1,5 +1,6 @@
#pragma once
#include "YoggieEngine.h"
#include "LayerStack.h"
namespace YoggieEngine {
@ -17,11 +18,16 @@ namespace YoggieEngine {
void GuiBegin();
void GuiEnd();
void PushLayer(Layer* layer);
protected:
std::string m_AppName;
NativeWindow* appWindow;
friend class ApplicationRuntime;
LayerStack AppLayerstack;
};
};

29
YoggieEngine/src/Layer.h Normal file
View File

@ -0,0 +1,29 @@
#pragma once
class Layer {
public:
~Layer() { OnDestroy(); }
Layer() { OnCreate(); }
Layer(const std::string name )
: Name(name) {}
virtual void OnUpdate(){}
virtual void OnUI(){}
virtual void OnStartup(){}
virtual void OnAttach() {}
virtual void OnDetach() {}
virtual void OnCreate() {}
virtual void OnDestroy(){}
private:
std::string Name;
};

View File

@ -0,0 +1,27 @@
#include "YoggieEngine.h"
#include "LayerStack.h"
LayerStack::~LayerStack()
{
for (Layer* layer : layers) {
layer->OnDetach();
delete layer;
}
}
void LayerStack::PushLayer(Layer* layer) {
layers.emplace(layers.begin() + insertion_index, layer);
insertion_index++;
}
void LayerStack::PopLayer(Layer* layer) {
auto it = std::find(layers.begin(), layers.begin() + insertion_index, layer);
if (it != layers.begin() + insertion_index) {
layer->OnDetach();
layers.erase(it);
insertion_index--;
}
}

View File

@ -0,0 +1,22 @@
#pragma once
#include "Layer.h"
#include <vector>
class LayerStack{
public:
LayerStack() = default;
~LayerStack();
void PushLayer(Layer* layer);
void PopLayer(Layer* layer);
std::vector<Layer*>::iterator begin() { return layers.begin(); }
std::vector<Layer*>::iterator end() { return layers.end(); }
std::vector<Layer*>::reverse_iterator rbegin() { return layers.rbegin(); }
std::vector<Layer*>::reverse_iterator rend() { return layers.rend(); }
private:
std::vector<Layer*> layers;
int insertion_index = 0;
};