diff --git a/SakuraCore/src/Application.cpp b/SakuraCore/src/Application.cpp index b98e89c..76689bf 100644 --- a/SakuraCore/src/Application.cpp +++ b/SakuraCore/src/Application.cpp @@ -8,6 +8,7 @@ #include "SDL3/SDL_timer.h" #include "imgui.h" #include "imgui_impl_sdl3.h" +#include "imguilayer.h" #define SDL_MAIN_HANDLED 1 @@ -125,7 +126,8 @@ void Application::Run() { } // Rendering - m_ImGuiLayer->Begin(); + // m_ImGuiLayer->Begin(); + this->GetLayer()->Begin(); SDL_SetRenderScale(m_Renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y); SDL_SetRenderDrawColor(m_Renderer, (Uint8)111, (Uint8)232, (Uint8)168, (Uint8)0); @@ -135,7 +137,8 @@ void Application::Run() { layer->OnImGuiRender(); } - m_ImGuiLayer->End(); + // m_ImGuiLayer->End(); + this->GetLayer()->End(); SDL_RenderPresent(m_Renderer); } diff --git a/SakuraCore/src/Application.h b/SakuraCore/src/Application.h index c5bb136..1e08212 100644 --- a/SakuraCore/src/Application.h +++ b/SakuraCore/src/Application.h @@ -4,7 +4,7 @@ #include "LayerStack.h" #include "SDL3/SDL_render.h" #include "SDL3/SDL_video.h" -#include "imguilayer.h" +// #include "imguilayer.h" #include #include @@ -52,6 +52,17 @@ public: return nullptr; } + template + requires(std::is_base_of_v) + TLayer *GetLayer(const std::string &layerName) { + for (const auto &layer : m_LayerStack) { + if (auto casted = dynamic_cast(layer.get()) && layer->GetName() == layerName) { + return casted; + } + } + return nullptr; + } + template requires(std::is_base_of_v) void PushOverlay() { @@ -85,7 +96,7 @@ private: AppData m_AppData; SakuraVNE::LayerStack m_LayerStack; - SakuraVNE::ImGuiLayer *m_ImGuiLayer; + // SakuraVNE::ImGuiLayer *m_ImGuiLayer; static Application *s_Instance; }; diff --git a/SakuraCore/src/Layer.cpp b/SakuraCore/src/Layer.cpp index 285a987..35c9890 100644 --- a/SakuraCore/src/Layer.cpp +++ b/SakuraCore/src/Layer.cpp @@ -2,5 +2,5 @@ #include namespace SakuraVNE { -Layer::Layer(const std::string &name) : m_LayerName(name), isActive(true) {} +Layer::Layer(const std::string &name, bool isActive) : m_LayerName(name), m_isActive(isActive) {} } // namespace SakuraVNE diff --git a/SakuraCore/src/Layer.h b/SakuraCore/src/Layer.h index 8dd616e..72e8320 100644 --- a/SakuraCore/src/Layer.h +++ b/SakuraCore/src/Layer.h @@ -6,7 +6,7 @@ namespace SakuraVNE { class Layer { public: - Layer(const std::string &name = "Layer"); + Layer(const std::string &name = "Layer", bool isActive = true); virtual ~Layer() = default; virtual void OnFrame(Uint64 timestamp) {} @@ -22,6 +22,6 @@ public: protected: std::string m_LayerName; - bool isActive; + bool m_isActive = true; }; } // namespace SakuraVNE diff --git a/SakuraCore/src/LayerStack.cpp b/SakuraCore/src/LayerStack.cpp index ba099b2..7c49ac8 100644 --- a/SakuraCore/src/LayerStack.cpp +++ b/SakuraCore/src/LayerStack.cpp @@ -1,5 +1,6 @@ #include "LayerStack.h" #include +#include #include namespace SakuraVNE { @@ -10,9 +11,8 @@ LayerStack::~LayerStack() { } } -// should these be fixed with std::move and take layer param as std::unique_ptr? -void LayerStack::PushLayer(Layer *layer) { - m_LayerStack.emplace(m_LayerStack.begin() + m_LayerIndex, layer); +void LayerStack::PushLayer(std::unique_ptr layer) { + m_LayerStack.emplace(m_LayerStack.begin() + m_LayerIndex, std::move(layer)); m_LayerIndex++; } @@ -25,7 +25,7 @@ void LayerStack::PopLayer(Layer *layer) { } } -void LayerStack::PushOverLay(Layer *layer) { m_LayerStack.emplace_back(layer); } +void LayerStack::PushOverLay(std::unique_ptr layer) { m_LayerStack.emplace_back(std::move(layer)); } void LayerStack::PopOverlay(Layer *layer) { auto match = std::find_if(m_LayerStack.begin() + m_LayerIndex, m_LayerStack.end(), [layer](const std::unique_ptr &ptr) { return ptr.get() == layer; }); diff --git a/SakuraCore/src/LayerStack.h b/SakuraCore/src/LayerStack.h index 93e065d..f4bb8aa 100644 --- a/SakuraCore/src/LayerStack.h +++ b/SakuraCore/src/LayerStack.h @@ -11,9 +11,9 @@ public: LayerStack() = default; ~LayerStack(); - void PushLayer(Layer *layer); + void PushLayer(std::unique_ptr layer); void PopLayer(Layer *layer); - void PushOverLay(Layer *layer); + void PushOverLay(std::unique_ptr layer); void PopOverlay(Layer *layer); std::vector>::iterator begin() { return m_LayerStack.begin(); } diff --git a/SakuraCore/test/test.cpp b/SakuraCore/test/test.cpp index 0578bc5..bc8ff82 100644 --- a/SakuraCore/test/test.cpp +++ b/SakuraCore/test/test.cpp @@ -3,19 +3,21 @@ #include "Layer.h" #include "LayerStack.h" -#include +#include #include TEST_CASE("Layer operations", "[Layer]") { SakuraVNE::LayerStack lstack; const auto &layers = lstack.GetLayers(); - SakuraVNE::Layer *layer1 = new SakuraVNE::Layer("layer1"); - lstack.PushLayer(layer1); + // Create the unique_ptr, keep a raw pointer for testing Pop operations later, then move it. + auto u_layer1 = std::make_unique("layer1"); + SakuraVNE::Layer *layer1 = u_layer1.get(); + lstack.PushLayer(std::move(u_layer1)); SECTION("PushLayer should make the size 2") { - SakuraVNE::Layer *layer2 = new SakuraVNE::Layer("layer2"); - lstack.PushLayer(layer2); + auto u_layer2 = std::make_unique("layer2"); + lstack.PushLayer(std::move(u_layer2)); REQUIRE(layers.size() == 2); REQUIRE(layers[1]->GetName() == "layer2"); } @@ -24,19 +26,20 @@ TEST_CASE("Layer operations", "[Layer]") { REQUIRE(layers.size() == 0); } SECTION("PushOverlay should always put at the end of the list and should always be after the last fence index") { - SakuraVNE::Layer *layer2 = new SakuraVNE::Layer("layer2"); - lstack.PushOverLay(layer2); + auto u_layer2 = std::make_unique("layer2"); + lstack.PushOverLay(std::move(u_layer2)); REQUIRE(layers[1]->GetName() == "layer2"); - SakuraVNE::Layer *layer3 = new SakuraVNE::Layer("layer3"); - lstack.PushLayer(layer3); + auto u_layer3 = std::make_unique("layer3"); + lstack.PushLayer(std::move(u_layer3)); REQUIRE(layers[1]->GetName() == "layer3"); REQUIRE(layers[2]->GetName() == "layer2"); } SECTION("Pop overlay") { - SakuraVNE::Layer *layer6 = new SakuraVNE::Layer("layer6"); - lstack.PushOverLay(layer6); + auto u_layer6 = std::make_unique("layer6"); + SakuraVNE::Layer *layer6 = u_layer6.get(); + lstack.PushOverLay(std::move(u_layer6)); REQUIRE(layers[1]->GetName() == "layer6"); lstack.PopOverlay(layer6);