work on layers

This commit is contained in:
2025-04-08 11:47:24 -05:00
parent fbfa13a742
commit 3a797fb19a
10 changed files with 183 additions and 87 deletions

View File

@@ -1,3 +1,30 @@
#ifndef EVENT_H
#define EVENT_H
#include "pch.hpp" #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

View File

@@ -5,6 +5,8 @@
#include "modules/WindowModule/src/WindowModule.h" #include "modules/WindowModule/src/WindowModule.h"
#include "utils/Layers/Layer.h"
namespace Archimedes { namespace Archimedes {
class GuiModule : public Module { class GuiModule : public Module {

View File

@@ -1,15 +1,25 @@
#ifndef LAYER_H
#define LAYER_H
#include "pch.hpp" #include "pch.hpp"
#include "utils/Events/Event.h" #include "utils/Events/Event.h"
class Layer { namespace Archimedes {
virtual ~Layer() {} class Layer {
virtual void onRender() = 0; public:
virtual void onAttach() = 0; virtual ~Layer() {}
virtual void onDetach() = 0; virtual void onRender() = 0;
virtual bool onEvent(const Event&) = 0; virtual void onAttach() = 0;
};
virtual void onDetach() = 0;
virtual bool onEvent(const Event&) = 0;
};
}
#endif

View File

@@ -1,31 +1,49 @@
#ifndef LAYERSTACK_H
#define LAYERSTACK_H
#include "pch.hpp" #include "pch.hpp"
#include "Layer.h" #include "Layer.h"
class Layerstack { namespace Archimedes {
class Layerstack {
public: public:
Layerstack() {} Layerstack() {}
~Layerstack() { ~Layerstack() {
while(!lstack.empty()) { while(!lstack.empty()) {
pop(); pop();
}
} }
}
void push(Layer* l) { lstack.push_front(l); } void push(Layer* l) {
lstack.push_front(l);
l->onAttach();
}
void pop() { void pop() {
Layer* l = lstack.front(); Layer* l = lstack.front();
lstack.pop_front(); lstack.pop_front();
delete l; l->onDetach();
} }
void renderAll() { void renderAll() {
for(Layer* l : lstack) for(Layer* l : lstack)
l->onRender(); l->onRender();
} }
private: void sendEvent(const Event& e) {
for(Layer* l : lstack) {
if(l->onEvent(e)) {
break;
}
}
}
std::list<Layer*> lstack; private:
};
std::list<Layer*> lstack;
};
}
#endif

View File

@@ -9,7 +9,6 @@ namespace Archimedes {
public: public:
int w, h; int w, h;
typedef void renderCmd();
~Renderer() {} ~Renderer() {}
@@ -19,12 +18,12 @@ namespace Archimedes {
r.render(rc, w, h); r.render(rc, w, h);
} }
std::list<renderCmd*>& getCmdList() { std::list<std::function<void()>>& getCmdList() {
return rc; return rc;
} }
private: private:
std::list<renderCmd*> rc; std::list<std::function<void()>> rc;
RendererImpl r; RendererImpl r;
}; };
} }

View File

@@ -11,9 +11,9 @@ namespace Archimedes {
public: public:
Window() { renderer = new Renderer(); } Window() {}
~Window() { delete renderer; } ~Window() {}
bool shouldClose() { return window.shouldClose(); } bool shouldClose() { return window.shouldClose(); }
@@ -29,7 +29,7 @@ namespace Archimedes {
} }
Renderer* getRenderer() { return renderer; } Renderer* getRenderer() { return renderer; }
//void setRenderer(Renderer* r) { renderer = r; } void setRenderer(Renderer* r) { renderer = r; }
WindowImpl& getWindowImpl() { return window; } WindowImpl& getWindowImpl() { return window; }

View File

@@ -1,26 +1,33 @@
#include "WindowModule.h" #include "WindowModule.h"
WindowModule::~WindowModule() { WindowModule::~WindowModule() {
if(window) if(layers)
delete window; delete layers;
if(renderer) if(renderer)
delete renderer; delete renderer;
if(window)
delete window;
} }
void WindowModule::onLoad() { void WindowModule::onLoad() {
window = new Archimedes::Window(); window = new Archimedes::Window();
//renderer = new Archimedes::Renderer();
//window->setRenderer(renderer); renderer = new Archimedes::Renderer();
renderer = window->getRenderer(); window->setRenderer(renderer);
//renderer = window->getRenderer();
layers = new Archimedes::Layerstack();
if(!renderer->init()) { if(!renderer->init()) {
std::cout << "Renderer init failed!\n"; std::cout << "Renderer init failed!\n";
std::abort(); std::abort();
} }
renderer->getCmdList().push_back([this](){ layers->renderAll(); });
data["window"] = window->getWindowImpl().getWindow(); data["window"] = window->getWindowImpl().getWindow();
data["renderCmdList"] = &renderer->getCmdList(); data["renderCmdList"] = &renderer->getCmdList();
} }

View File

@@ -11,6 +11,7 @@
#include "utils/Window/Window.h" #include "utils/Window/Window.h"
#include "utils/Renderer/Renderer.h" #include "utils/Renderer/Renderer.h"
#include "utils/Layers/Layerstack.h"
class WindowModule : public Archimedes::Module { class WindowModule : public Archimedes::Module {
@@ -27,12 +28,16 @@ class WindowModule : public Archimedes::Module {
void onLoad(); void onLoad();
//interface for other modules //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: private:
Archimedes::Window* window; Archimedes::Window* window;
Archimedes::Renderer* renderer; Archimedes::Renderer* renderer;
Archimedes::Layerstack* layers;
}; };

View File

@@ -12,10 +12,6 @@ TestImgui::TestImgui(void* h, Archimedes::App* a) : Archimedes::GuiModule(h, a)
} }
TestImgui::~TestImgui() { TestImgui::~TestImgui() {
std::list<Archimedes::Renderer::renderCmd*>* cmdList =
std::any_cast<std::list<Archimedes::Renderer::renderCmd*>*>(depsInstances["WindowModule"]->getData("renderCmdList"));
cmdList->erase(rcmd);
ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown(); ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext(); ImGui::DestroyContext();
@@ -24,52 +20,13 @@ TestImgui::~TestImgui() {
void TestImgui::onLoad() { void TestImgui::onLoad() {
WindowModule* wm = (WindowModule*) depsInstances["WindowModule"]; WindowModule* wm = (WindowModule*) depsInstances["WindowModule"];
std::cout << "Say hello: " << wm->sayHello() << std::endl;
if(!wm) { if(!wm) {
std::cout << "No WindowModule for TestImgui!\n"; std::cout << "No WindowModule for TestImgui!\n";
std::abort(); std::abort();
} }
IMGUI_CHECKVERSION(); wm->getLayerstack()->push(&layer);
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<GLFWwindow*>(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<Archimedes::Renderer::renderCmd*>* cmdList =
std::any_cast<std::list<Archimedes::Renderer::renderCmd*>*>(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();
} }
void TestImgui::run() { void TestImgui::run() {
@@ -103,3 +60,53 @@ void TestImgui::run() {
} }
ImGui::Render(); 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; }

View File

@@ -23,6 +23,27 @@ class TestImgui : public Archimedes::GuiModule {
std::list<Archimedes::Renderer::renderCmd*>::iterator rcmd; std::list<Archimedes::Renderer::renderCmd*>::iterator rcmd;
Archimedes::Window* window; 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 #ifdef TESTIMGUI_DYNAMIC