layers are redundant

This commit is contained in:
2025-04-09 14:16:38 -05:00
parent 42b5a53b89
commit 908b00db62
11 changed files with 68 additions and 191 deletions

View File

@@ -3,8 +3,8 @@
#ifdef MODULE_TYPE
extern "C" {
Archimedes::Module* create(void* handle, Archimedes::App* app) {
return new MODULE_TYPE(handle, app);
Archimedes::Module* create(Archimedes::App* app, void* handle) {
return new MODULE_TYPE(app, handle);
}
}

View File

@@ -39,7 +39,7 @@ namespace Archimedes {
virtual void stopModule(std::string lib) { toClose.push_back(lib); }
virtual void startModule(std::string lib) { toOpen.push_back(lib); }
virtual void startModule(std::string lib, std::variant<std::string, Module*> m) { toOpen[lib] = m; }
void end() { done = true; }
@@ -57,7 +57,7 @@ namespace Archimedes {
std::list<std::string> runOrder;
std::list<std::string> toClose;
std::list<std::string> toOpen;
std::unordered_map<std::string, std::variant<std::string, Module*>> toOpen;
virtual Module* dynamicLoad(std::string lib) {
@@ -76,7 +76,7 @@ namespace Archimedes {
return nullptr;
}
return create(h, Get());
return create(Get(), h);
}
virtual Module* load(std::string modulePath) {
@@ -144,6 +144,9 @@ namespace Archimedes {
virtual void unload(std::string name) {
if(modules.find(name) == modules.end())
return;
//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()) {
@@ -155,10 +158,11 @@ namespace Archimedes {
void* h = m->getHandle();
modules[name] = nullptr;
delete m;
if(h)
if(h) {
delete m;
dlclose(h);
}
runOrder.remove(name);
}

View File

@@ -12,14 +12,11 @@ namespace Archimedes {
class GuiModule : public Module {
public:
typedef GuiModule* create_t(void*, App*);
GuiModule(void* h, App* a) : Module(h, a) {
deps["WindowModule"] = new WindowModule(nullptr, a);
GuiModule(App* a, void* h) : Module(a, h) {
WindowModule* wm = new WindowModule(a);
deps[wm->getName()] = wm;
}
virtual ~GuiModule() {}
virtual void onLoad() = 0;
virtual void run() = 0;
};
}

View File

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

View File

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

View File

@@ -7,36 +7,39 @@ namespace Archimedes {
class App;
class Event;
class Module {
friend class App;
public:
typedef Module* create_t(void*, App*);
typedef Module* create_t(App*, void*);
Module(void* h, App* a) : handle(h), app(a) {}
Module(App* a, void* h) : app(a), handle(h) {}
virtual ~Module() {}
virtual void run() = 0;
virtual void onLoad() = 0;
virtual void run() {}
virtual bool onEvent(const Event& e) { return false; }
virtual void onLoad() {};
std::string getName() const { return name; }
void* getHandle() { return handle; }
std::any getData(std::string s) { return data[s.c_str()]; };
//std::any getData(std::string s) { return data[s.c_str()]; };
protected:
std::string name;
void* handle;
App* app;
void* handle;
std::unordered_map<std::string, std::variant<std::string, Module*>> deps;
std::unordered_map<std::string, Module*> depsInstances;
std::unordered_map<std::string, std::any> data;
//std::unordered_map<std::string, std::any> data;
};
}

View File

@@ -6,13 +6,16 @@
#include <GLFW/glfw3.h>
ImguiModule::ImguiModule(void* h, Archimedes::App* a) : Archimedes::GuiModule(h, a) {
ImguiModule::ImguiModule(Archimedes::App* a, void* h = nullptr) : Archimedes::GuiModule(a, h) {
name = "ImguiModule";
}
ImguiModule::~ImguiModule() {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
void ImguiModule::onLoad() {
@@ -23,39 +26,6 @@ void ImguiModule::onLoad() {
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";
std::abort();
}
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
@@ -77,34 +47,38 @@ void ImguiModule::IGLayer::onAttach() {
}
wm->getRenderer()->getCmdList().push_back([](){
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
});
//Compute first frame ahead of first WindowModule->run()
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Render();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
}
void ImguiModule::IGLayer::onRender() {
ImGui::Render();
void ImguiModule::run() {
ImGuiIO& io = ImGui::GetIO();
static float f = 0.0f;
static int counter = 0;
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
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();
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();
}

View File

@@ -9,39 +9,20 @@
class ImguiModule : public Archimedes::GuiModule {
public:
ImguiModule(void*, Archimedes::App*);
ImguiModule(Archimedes::App*, void*);
~ImguiModule();
void onLoad();
bool onEvent(const Archimedes::Event& e) { return false; }
void run();
private:
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,9 +1,6 @@
#include "WindowModule.h"
WindowModule::~WindowModule() {
if(layers) {
delete layers;
}
if(renderer) {
renderer->getCmdList().clear();
delete renderer;
@@ -23,14 +20,10 @@ void WindowModule::onLoad() {
//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(); });
}
void WindowModule::run() {

View File

@@ -11,12 +11,11 @@
#include "utils/Window/Window.h"
#include "utils/Renderer/Renderer.h"
#include "utils/Layers/Layerstack.h"
class WindowModule : public Archimedes::Module {
public:
WindowModule(void* h, Archimedes::App* a) : Archimedes::Module(h, a) {
WindowModule(Archimedes::App* a, void* h = nullptr) : Archimedes::Module(a, h) {
name = "WindowModule";
}
@@ -31,13 +30,11 @@ class WindowModule : public Archimedes::Module {
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

@@ -18,8 +18,12 @@ void MinimalApp::run() {
}
toClose.clear();
for(std::string m : toOpen) {
load(m);
for(auto m : toOpen) {
if(std::holds_alternative<std::string>(m.second)) {
load(std::get<std::string>(m.second));
} else {
load(std::get<Archimedes::Module*>(m.second));
}
}
toOpen.clear();
}