work on layers

This commit is contained in:
2025-04-09 10:52:23 -05:00
parent 3a797fb19a
commit 42b5a53b89
8 changed files with 102 additions and 63 deletions

View File

@@ -143,6 +143,14 @@ namespace Archimedes {
}
virtual void unload(std::string name) {
//unload modules that depend on the one we are unloading
for(std::string s : runOrder) {
if(modules[s]->deps.find(name) != modules[s]->deps.end()) {
unload(s);
}
}
Module* m = modules[name];
void* h = m->getHandle();

View File

@@ -17,9 +17,11 @@ namespace Archimedes {
}
void push(Layer* l) {
if(l) {
lstack.push_front(l);
l->onAttach();
}
}
void pop() {
Layer* l = lstack.front();

View File

@@ -21,13 +21,13 @@ namespace Archimedes {
return glewInit() == GLEW_OK;
};
void render(std::list<renderCmd*> cmdList, int& w, int& h) {
void render(std::list<std::function<void()>> cmdList, int& w, int& h) {
glViewport(0, 0, w, h);
glClear(GL_COLOR_BUFFER_BIT);
for(auto* f : cmdList)
for(auto f : cmdList)
f();
}
};

View File

@@ -13,17 +13,43 @@ ImguiModule::ImguiModule(void* h, Archimedes::App* a) : Archimedes::GuiModule(h,
ImguiModule::~ImguiModule() {
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();
}
void ImguiModule::onLoad() {
WindowModule* wm = (WindowModule*) depsInstances["WindowModule"];
Archimedes::Module* wm = depsInstances["WindowModule"];
if(!wm) {
std::cout << "No WindowModule for ImguiModule!\n";
std::abort();
}
wm->getLayerstack()->push(&layer);
}
void ImguiModule::run() {
ImGuiIO& io = ImGui::GetIO();
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::End();
}
ImguiModule::IGLayer::~IGLayer() {}
void ImguiModule::IGLayer::onAttach() {
WindowModule* wm = (WindowModule*) imgui->depsInstances["WindowModule"];
if(!wm) {
std::cout << "No WindowModule for ImguiModule!\n";
@@ -44,57 +70,41 @@ void ImguiModule::onLoad() {
//ImGui::StyleColorsLight();
// Setup Platform/Renderer backends
GLFWwindow* w = std::any_cast<GLFWwindow*>(wm->getData("window"));
if(!ImGui_ImplGlfw_InitForOpenGL(w, true))
if(!ImGui_ImplGlfw_InitForOpenGL(wm->getWindow()->getWindowImpl().getWindow(), 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 ImguiModule::run() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
{
ImGuiIO& io = ImGui::GetIO();
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::End();
}
ImGui::Render();
}
void ImguiModule::IGLayer::onRender() {
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
}
bool ImguiModule::IGLayer::onEvent(const Archimedes::Event& e) { return false; }
void ImguiModule::IGLayer::onDetach() {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}

View File

@@ -19,9 +19,29 @@ class ImguiModule : public Archimedes::GuiModule {
private:
std::list<Archimedes::Renderer::renderCmd*>::iterator rcmd;
Archimedes::Window* window;
class IGLayer : public Archimedes::Layer {
public:
IGLayer(ImguiModule* _imgui) : imgui(_imgui) {}
~IGLayer();
void onRender();
void onAttach();
void onDetach();
bool onEvent(const Archimedes::Event&);
private:
ImguiModule* imgui;
} layer = IGLayer(this);
};
#ifdef TESTIMGUI_DYNAMIC

View File

@@ -1,12 +1,16 @@
#include "WindowModule.h"
WindowModule::~WindowModule() {
if(layers)
if(layers) {
delete layers;
if(renderer)
}
if(renderer) {
renderer->getCmdList().clear();
delete renderer;
if(window)
}
if(window) {
delete window;
}
}
void WindowModule::onLoad() {
@@ -27,9 +31,6 @@ void WindowModule::onLoad() {
}
renderer->getCmdList().push_back([this](){ layers->renderAll(); });
data["window"] = window->getWindowImpl().getWindow();
data["renderCmdList"] = &renderer->getCmdList();
}
void WindowModule::run() {

View File

@@ -12,9 +12,7 @@ TestImgui::TestImgui(void* h, Archimedes::App* a) : Archimedes::GuiModule(h, a)
}
TestImgui::~TestImgui() {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
void TestImgui::onLoad() {
@@ -86,9 +84,7 @@ void TestImgui::TILayer::onAttach() {
//ImGui::StyleColorsLight();
// Setup Platform/Renderer backends
GLFWwindow* w = wm->getWindow()->getWindowImpl().getWindow();
if(!ImGui_ImplGlfw_InitForOpenGL(w, true))
if(!ImGui_ImplGlfw_InitForOpenGL(wm->getWindow()->getWindowImpl().getWindow(), true))
std::cout << "GLFWImpl failed\n";
if(!ImGui_ImplOpenGL3_Init("#version 330")) {
std::cout << "ImGui_ImplOpenGL3_Init failed!\n" << std::endl;
@@ -107,6 +103,9 @@ void TestImgui::TILayer::onRender() {
}
void TestImgui::TILayer::onDetach() {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
bool TestImgui::TILayer::onEvent(const Archimedes::Event&) { return false; }

View File

@@ -20,7 +20,6 @@ class TestImgui : public Archimedes::GuiModule {
private:
bool demo = true;
std::list<Archimedes::Renderer::renderCmd*>::iterator rcmd;
Archimedes::Window* window;