layers are redundant
This commit is contained in:
@@ -3,8 +3,8 @@
|
|||||||
#ifdef MODULE_TYPE
|
#ifdef MODULE_TYPE
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
Archimedes::Module* create(void* handle, Archimedes::App* app) {
|
Archimedes::Module* create(Archimedes::App* app, void* handle) {
|
||||||
return new MODULE_TYPE(handle, app);
|
return new MODULE_TYPE(app, handle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ namespace Archimedes {
|
|||||||
|
|
||||||
virtual void stopModule(std::string lib) { toClose.push_back(lib); }
|
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; }
|
void end() { done = true; }
|
||||||
|
|
||||||
@@ -57,8 +57,8 @@ namespace Archimedes {
|
|||||||
std::list<std::string> runOrder;
|
std::list<std::string> runOrder;
|
||||||
|
|
||||||
std::list<std::string> toClose;
|
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) {
|
virtual Module* dynamicLoad(std::string lib) {
|
||||||
|
|
||||||
void* h = dlopen(lib.c_str(), RTLD_NOW);
|
void* h = dlopen(lib.c_str(), RTLD_NOW);
|
||||||
@@ -76,7 +76,7 @@ namespace Archimedes {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return create(h, Get());
|
return create(Get(), h);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual Module* load(std::string modulePath) {
|
virtual Module* load(std::string modulePath) {
|
||||||
@@ -144,6 +144,9 @@ namespace Archimedes {
|
|||||||
|
|
||||||
virtual void unload(std::string name) {
|
virtual void unload(std::string name) {
|
||||||
|
|
||||||
|
if(modules.find(name) == modules.end())
|
||||||
|
return;
|
||||||
|
|
||||||
//unload modules that depend on the one we are unloading
|
//unload modules that depend on the one we are unloading
|
||||||
for(std::string s : runOrder) {
|
for(std::string s : runOrder) {
|
||||||
if(modules[s]->deps.find(name) != modules[s]->deps.end()) {
|
if(modules[s]->deps.find(name) != modules[s]->deps.end()) {
|
||||||
@@ -155,10 +158,11 @@ namespace Archimedes {
|
|||||||
void* h = m->getHandle();
|
void* h = m->getHandle();
|
||||||
|
|
||||||
modules[name] = nullptr;
|
modules[name] = nullptr;
|
||||||
delete m;
|
|
||||||
|
|
||||||
if(h)
|
if(h) {
|
||||||
|
delete m;
|
||||||
dlclose(h);
|
dlclose(h);
|
||||||
|
}
|
||||||
|
|
||||||
runOrder.remove(name);
|
runOrder.remove(name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,14 +12,11 @@ namespace Archimedes {
|
|||||||
class GuiModule : public Module {
|
class GuiModule : public Module {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef GuiModule* create_t(void*, App*);
|
GuiModule(App* a, void* h) : Module(a, h) {
|
||||||
|
WindowModule* wm = new WindowModule(a);
|
||||||
GuiModule(void* h, App* a) : Module(h, a) {
|
deps[wm->getName()] = wm;
|
||||||
deps["WindowModule"] = new WindowModule(nullptr, a);
|
|
||||||
}
|
}
|
||||||
virtual ~GuiModule() {}
|
virtual ~GuiModule() {}
|
||||||
virtual void onLoad() = 0;
|
|
||||||
virtual void run() = 0;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -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
|
|
||||||
@@ -7,36 +7,39 @@ namespace Archimedes {
|
|||||||
|
|
||||||
class App;
|
class App;
|
||||||
|
|
||||||
|
class Event;
|
||||||
|
|
||||||
class Module {
|
class Module {
|
||||||
|
|
||||||
friend class App;
|
friend class App;
|
||||||
|
|
||||||
public:
|
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 ~Module() {}
|
||||||
|
|
||||||
virtual void run() = 0;
|
virtual void run() {}
|
||||||
virtual void onLoad() = 0;
|
virtual bool onEvent(const Event& e) { return false; }
|
||||||
|
virtual void onLoad() {};
|
||||||
|
|
||||||
std::string getName() const { return name; }
|
std::string getName() const { return name; }
|
||||||
void* getHandle() { return handle; }
|
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:
|
protected:
|
||||||
std::string name;
|
std::string name;
|
||||||
void* handle;
|
|
||||||
|
|
||||||
App* app;
|
App* app;
|
||||||
|
void* handle;
|
||||||
|
|
||||||
std::unordered_map<std::string, std::variant<std::string, Module*>> deps;
|
std::unordered_map<std::string, std::variant<std::string, Module*>> deps;
|
||||||
std::unordered_map<std::string, Module*> depsInstances;
|
std::unordered_map<std::string, Module*> depsInstances;
|
||||||
|
|
||||||
std::unordered_map<std::string, std::any> data;
|
//std::unordered_map<std::string, std::any> data;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,13 +6,16 @@
|
|||||||
|
|
||||||
#include <GLFW/glfw3.h>
|
#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";
|
name = "ImguiModule";
|
||||||
}
|
}
|
||||||
|
|
||||||
ImguiModule::~ImguiModule() {
|
ImguiModule::~ImguiModule() {
|
||||||
|
|
||||||
|
ImGui_ImplOpenGL3_Shutdown();
|
||||||
|
ImGui_ImplGlfw_Shutdown();
|
||||||
|
ImGui::DestroyContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImguiModule::onLoad() {
|
void ImguiModule::onLoad() {
|
||||||
@@ -22,40 +25,7 @@ void ImguiModule::onLoad() {
|
|||||||
std::cout << "No WindowModule for ImguiModule!\n";
|
std::cout << "No WindowModule for ImguiModule!\n";
|
||||||
std::abort();
|
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_CHECKVERSION();
|
||||||
ImGui::CreateContext();
|
ImGui::CreateContext();
|
||||||
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
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()
|
//Compute first frame ahead of first WindowModule->run()
|
||||||
ImGui_ImplOpenGL3_NewFrame();
|
ImGui_ImplOpenGL3_NewFrame();
|
||||||
ImGui_ImplGlfw_NewFrame();
|
ImGui_ImplGlfw_NewFrame();
|
||||||
ImGui::NewFrame();
|
ImGui::NewFrame();
|
||||||
|
|
||||||
ImGui::Render();
|
|
||||||
|
|
||||||
ImGui_ImplOpenGL3_NewFrame();
|
|
||||||
ImGui_ImplGlfw_NewFrame();
|
|
||||||
ImGui::NewFrame();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ImguiModule::IGLayer::onRender() {
|
void ImguiModule::run() {
|
||||||
ImGui::Render();
|
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::Text("This is some useful text."); // Display some text (you can use a format strings too)
|
||||||
ImGui_ImplGlfw_NewFrame();
|
|
||||||
ImGui::NewFrame();
|
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)
|
||||||
bool ImguiModule::IGLayer::onEvent(const Archimedes::Event& e) { return false; }
|
counter++;
|
||||||
|
ImGui::SameLine();
|
||||||
void ImguiModule::IGLayer::onDetach() {
|
ImGui::Text("counter = %d", counter);
|
||||||
|
|
||||||
ImGui_ImplOpenGL3_Shutdown();
|
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
|
||||||
ImGui_ImplGlfw_Shutdown();
|
ImGui::End();
|
||||||
ImGui::DestroyContext();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,39 +9,20 @@
|
|||||||
class ImguiModule : public Archimedes::GuiModule {
|
class ImguiModule : public Archimedes::GuiModule {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ImguiModule(void*, Archimedes::App*);
|
ImguiModule(Archimedes::App*, void*);
|
||||||
|
|
||||||
~ImguiModule();
|
~ImguiModule();
|
||||||
|
|
||||||
void onLoad();
|
void onLoad();
|
||||||
|
|
||||||
|
bool onEvent(const Archimedes::Event& e) { return false; }
|
||||||
|
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Archimedes::Window* window;
|
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
|
#ifdef TESTIMGUI_DYNAMIC
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
#include "WindowModule.h"
|
#include "WindowModule.h"
|
||||||
|
|
||||||
WindowModule::~WindowModule() {
|
WindowModule::~WindowModule() {
|
||||||
if(layers) {
|
|
||||||
delete layers;
|
|
||||||
}
|
|
||||||
if(renderer) {
|
if(renderer) {
|
||||||
renderer->getCmdList().clear();
|
renderer->getCmdList().clear();
|
||||||
delete renderer;
|
delete renderer;
|
||||||
@@ -23,14 +20,10 @@ void WindowModule::onLoad() {
|
|||||||
|
|
||||||
//renderer = window->getRenderer();
|
//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(); });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowModule::run() {
|
void WindowModule::run() {
|
||||||
|
|||||||
@@ -11,12 +11,11 @@
|
|||||||
|
|
||||||
#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 {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
WindowModule(void* h, Archimedes::App* a) : Archimedes::Module(h, a) {
|
WindowModule(Archimedes::App* a, void* h = nullptr) : Archimedes::Module(a, h) {
|
||||||
name = "WindowModule";
|
name = "WindowModule";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,13 +30,11 @@ class WindowModule : public Archimedes::Module {
|
|||||||
|
|
||||||
Archimedes::Window* getWindow() { return window; }
|
Archimedes::Window* getWindow() { return window; }
|
||||||
Archimedes::Renderer* getRenderer() { return renderer; }
|
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;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -18,8 +18,12 @@ void MinimalApp::run() {
|
|||||||
}
|
}
|
||||||
toClose.clear();
|
toClose.clear();
|
||||||
|
|
||||||
for(std::string m : toOpen) {
|
for(auto m : toOpen) {
|
||||||
load(m);
|
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();
|
toOpen.clear();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user