diff --git a/include/utils/Events/Event.h b/include/utils/Events/Event.h index 26a7524..6d146c0 100644 --- a/include/utils/Events/Event.h +++ b/include/utils/Events/Event.h @@ -1,3 +1,30 @@ +#ifndef EVENT_H +#define EVENT_H + #include "pch.hpp" -class Event {}; +namespace Archimedes { + + class Event { + + public: + + enum class Type : unsigned int { + None = 0, + WindowEvent = 1 << 0, + KeyEvent = 1 << 1, + MouseMoveEvent = 1 << 2, + MouseScrollEvent = 1 << 3, + MouseButtonEvent = 1 << 4, + PressedEvent = 1 << 5, + ReleasedEvent = 1 << 6, + WindowCloseEvent = 1 << 7, + WindowResizeEvent = 1 << 8, + }; + + unsigned int type; + + }; +} + +#endif diff --git a/include/utils/GuiModule/GuiModule.h b/include/utils/GuiModule/GuiModule.h index a44cf9b..9cd2995 100644 --- a/include/utils/GuiModule/GuiModule.h +++ b/include/utils/GuiModule/GuiModule.h @@ -5,6 +5,8 @@ #include "modules/WindowModule/src/WindowModule.h" +#include "utils/Layers/Layer.h" + namespace Archimedes { class GuiModule : public Module { diff --git a/include/utils/Layers/Layer.h b/include/utils/Layers/Layer.h index b334f4b..7ff5e1b 100644 --- a/include/utils/Layers/Layer.h +++ b/include/utils/Layers/Layer.h @@ -1,15 +1,25 @@ +#ifndef LAYER_H +#define LAYER_H + #include "pch.hpp" #include "utils/Events/Event.h" -class Layer { +namespace Archimedes { + + class Layer { - virtual ~Layer() {} + public: - virtual void onRender() = 0; + virtual ~Layer() {} - virtual void onAttach() = 0; + virtual void onRender() = 0; - virtual void onDetach() = 0; + virtual void onAttach() = 0; - virtual bool onEvent(const Event&) = 0; -}; + virtual void onDetach() = 0; + + virtual bool onEvent(const Event&) = 0; + }; +} + +#endif diff --git a/include/utils/Layers/Layerstack.h b/include/utils/Layers/Layerstack.h index 3a829e2..365f350 100644 --- a/include/utils/Layers/Layerstack.h +++ b/include/utils/Layers/Layerstack.h @@ -1,31 +1,49 @@ +#ifndef LAYERSTACK_H +#define LAYERSTACK_H + #include "pch.hpp" #include "Layer.h" -class Layerstack { +namespace Archimedes { + class Layerstack { - public: - Layerstack() {} + public: + Layerstack() {} - ~Layerstack() { - while(!lstack.empty()) { - pop(); + ~Layerstack() { + while(!lstack.empty()) { + pop(); + } } - } - void push(Layer* l) { lstack.push_front(l); } + void push(Layer* l) { + lstack.push_front(l); + l->onAttach(); + } - void pop() { - Layer* l = lstack.front(); - lstack.pop_front(); - delete l; - } + void pop() { + Layer* l = lstack.front(); + lstack.pop_front(); + l->onDetach(); + } - void renderAll() { - for(Layer* l : lstack) - l->onRender(); - } + void renderAll() { + for(Layer* l : lstack) + l->onRender(); + } - private: + void sendEvent(const Event& e) { + for(Layer* l : lstack) { + if(l->onEvent(e)) { + break; + } + } + } - std::list lstack; -}; + private: + + std::list lstack; + }; +} + +#endif diff --git a/include/utils/Renderer/Renderer.h b/include/utils/Renderer/Renderer.h index 610915e..e99a5b7 100644 --- a/include/utils/Renderer/Renderer.h +++ b/include/utils/Renderer/Renderer.h @@ -9,7 +9,6 @@ namespace Archimedes { public: int w, h; - typedef void renderCmd(); ~Renderer() {} @@ -19,12 +18,12 @@ namespace Archimedes { r.render(rc, w, h); } - std::list& getCmdList() { + std::list>& getCmdList() { return rc; } private: - std::list rc; + std::list> rc; RendererImpl r; }; } diff --git a/include/utils/Window/Window.h b/include/utils/Window/Window.h index 1e51a18..32dd04c 100644 --- a/include/utils/Window/Window.h +++ b/include/utils/Window/Window.h @@ -11,9 +11,9 @@ namespace Archimedes { public: - Window() { renderer = new Renderer(); } + Window() {} - ~Window() { delete renderer; } + ~Window() {} bool shouldClose() { return window.shouldClose(); } @@ -29,7 +29,7 @@ namespace Archimedes { } Renderer* getRenderer() { return renderer; } - //void setRenderer(Renderer* r) { renderer = r; } + void setRenderer(Renderer* r) { renderer = r; } WindowImpl& getWindowImpl() { return window; } diff --git a/modules/WindowModule/src/WindowModule.cpp b/modules/WindowModule/src/WindowModule.cpp index 55b108d..6721883 100644 --- a/modules/WindowModule/src/WindowModule.cpp +++ b/modules/WindowModule/src/WindowModule.cpp @@ -1,26 +1,33 @@ #include "WindowModule.h" WindowModule::~WindowModule() { - if(window) - delete window; + if(layers) + delete layers; if(renderer) delete renderer; + if(window) + delete window; } void WindowModule::onLoad() { window = new Archimedes::Window(); - //renderer = new Archimedes::Renderer(); - - //window->setRenderer(renderer); - renderer = window->getRenderer(); + renderer = new Archimedes::Renderer(); + + window->setRenderer(renderer); + + //renderer = window->getRenderer(); + + layers = new Archimedes::Layerstack(); if(!renderer->init()) { std::cout << "Renderer init failed!\n"; std::abort(); } + renderer->getCmdList().push_back([this](){ layers->renderAll(); }); + data["window"] = window->getWindowImpl().getWindow(); data["renderCmdList"] = &renderer->getCmdList(); } diff --git a/modules/WindowModule/src/WindowModule.h b/modules/WindowModule/src/WindowModule.h index d682257..0f0c10e 100644 --- a/modules/WindowModule/src/WindowModule.h +++ b/modules/WindowModule/src/WindowModule.h @@ -11,6 +11,7 @@ #include "utils/Window/Window.h" #include "utils/Renderer/Renderer.h" +#include "utils/Layers/Layerstack.h" class WindowModule : public Archimedes::Module { @@ -27,12 +28,16 @@ class WindowModule : public Archimedes::Module { void onLoad(); //interface for other modules - std::string sayHello() { return "Call from TestImgui!"; } + + Archimedes::Window* getWindow() { return window; } + Archimedes::Renderer* getRenderer() { return renderer; } + Archimedes::Layerstack* getLayerstack() { return layers; } private: Archimedes::Window* window; Archimedes::Renderer* renderer; + Archimedes::Layerstack* layers; }; diff --git a/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp b/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp index 1dcc0e5..1a6a62f 100644 --- a/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp +++ b/modules/examples/GuiModules/TestImgui/src/TestImgui.cpp @@ -12,64 +12,21 @@ TestImgui::TestImgui(void* h, Archimedes::App* a) : Archimedes::GuiModule(h, a) } TestImgui::~TestImgui() { - - std::list* cmdList = - std::any_cast*>(depsInstances["WindowModule"]->getData("renderCmdList")); - cmdList->erase(rcmd); ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); } void TestImgui::onLoad() { - + WindowModule* wm = (WindowModule*) depsInstances["WindowModule"]; - std::cout << "Say hello: " << wm->sayHello() << std::endl; - + if(!wm) { std::cout << "No WindowModule for TestImgui!\n"; std::abort(); } - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - ImGuiIO& io = ImGui::GetIO(); (void)io; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls - io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls - - io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking - //io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows - - // Setup Dear ImGui style - ImGui::StyleColorsDark(); - //ImGui::StyleColorsLight(); - - // Setup Platform/Renderer backends - GLFWwindow* w = std::any_cast(wm->getData("window")); - - if(!ImGui_ImplGlfw_InitForOpenGL(w, true)) - std::cout << "GLFWImpl failed\n"; - if(!ImGui_ImplOpenGL3_Init("#version 330")) { - std::cout << "ImGui_ImplOpenGL3_Init failed!\n" << std::endl; - } - - - std::list* cmdList = - std::any_cast*>(wm->getData("renderCmdList")); - - cmdList->push_back([](){ - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - }); - - rcmd = --cmdList->end(); - cmdList->end()++; - - //Compute first frame ahead of first WindowModule->run() - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGlfw_NewFrame(); - ImGui::NewFrame(); - - ImGui::Render(); + wm->getLayerstack()->push(&layer); } void TestImgui::run() { @@ -103,3 +60,53 @@ void TestImgui::run() { } ImGui::Render(); } + + +TestImgui::TILayer::~TILayer() {} + +void TestImgui::TILayer::onAttach() { + WindowModule* wm = (WindowModule*) ti->depsInstances["WindowModule"]; + + if(!wm) { + std::cout << "No WindowModule for TestImgui!\n"; + std::abort(); + } + + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void)io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls + + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking + //io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows + + // Setup Dear ImGui style + ImGui::StyleColorsDark(); + //ImGui::StyleColorsLight(); + + // Setup Platform/Renderer backends + GLFWwindow* w = wm->getWindow()->getWindowImpl().getWindow(); + + if(!ImGui_ImplGlfw_InitForOpenGL(w, true)) + std::cout << "GLFWImpl failed\n"; + if(!ImGui_ImplOpenGL3_Init("#version 330")) { + std::cout << "ImGui_ImplOpenGL3_Init failed!\n" << std::endl; + } + + //Compute first frame ahead of first WindowModule->run() + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + + ImGui::Render(); +} + +void TestImgui::TILayer::onRender() { + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); +} + +void TestImgui::TILayer::onDetach() { +} + +bool TestImgui::TILayer::onEvent(const Archimedes::Event&) { return false; } diff --git a/modules/examples/GuiModules/TestImgui/src/TestImgui.h b/modules/examples/GuiModules/TestImgui/src/TestImgui.h index 871e63c..9250b69 100644 --- a/modules/examples/GuiModules/TestImgui/src/TestImgui.h +++ b/modules/examples/GuiModules/TestImgui/src/TestImgui.h @@ -23,6 +23,27 @@ class TestImgui : public Archimedes::GuiModule { std::list::iterator rcmd; Archimedes::Window* window; + + class TILayer : public Archimedes::Layer { + public: + + TILayer(TestImgui* _ti) : ti(_ti) {} + + ~TILayer(); + + void onRender(); + + void onAttach(); + + void onDetach(); + + bool onEvent(const Archimedes::Event&); + + private: + + TestImgui* ti; + + } layer = TILayer(this); }; #ifdef TESTIMGUI_DYNAMIC