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"
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 "utils/Layers/Layer.h"
namespace Archimedes {
class GuiModule : public Module {

View File

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

View File

@@ -1,7 +1,11 @@
#ifndef LAYERSTACK_H
#define LAYERSTACK_H
#include "pch.hpp"
#include "Layer.h"
class Layerstack {
namespace Archimedes {
class Layerstack {
public:
Layerstack() {}
@@ -12,12 +16,15 @@ class Layerstack {
}
}
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;
l->onDetach();
}
void renderAll() {
@@ -25,7 +32,18 @@ class Layerstack {
l->onRender();
}
void sendEvent(const Event& e) {
for(Layer* l : lstack) {
if(l->onEvent(e)) {
break;
}
}
}
private:
std::list<Layer*> lstack;
};
};
}
#endif

View File

@@ -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<renderCmd*>& getCmdList() {
std::list<std::function<void()>>& getCmdList() {
return rc;
}
private:
std::list<renderCmd*> rc;
std::list<std::function<void()>> rc;
RendererImpl r;
};
}

View File

@@ -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; }

View File

@@ -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 = new Archimedes::Renderer();
renderer = window->getRenderer();
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();
}

View File

@@ -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;
};

View File

@@ -12,10 +12,6 @@ TestImgui::TestImgui(void* h, Archimedes::App* a) : Archimedes::GuiModule(h, a)
}
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_ImplGlfw_Shutdown();
ImGui::DestroyContext();
@@ -24,52 +20,13 @@ TestImgui::~TestImgui() {
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<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();
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; }

View File

@@ -23,6 +23,27 @@ class TestImgui : public Archimedes::GuiModule {
std::list<Archimedes::Renderer::renderCmd*>::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