work on main app
This commit is contained in:
13
flake.nix
13
flake.nix
@@ -32,6 +32,7 @@
|
||||
buildPhase = ''
|
||||
g++ \
|
||||
*.cpp \
|
||||
-Wall \
|
||||
-o $name
|
||||
'';
|
||||
|
||||
@@ -54,9 +55,10 @@
|
||||
|
||||
buildPhase = ''
|
||||
g++ \
|
||||
modules/TestMenu/src/*.cpp src/App.cpp \
|
||||
modules/TestMenu/src/**.cpp src/App.cpp \
|
||||
-fpic -shared \
|
||||
-I src \
|
||||
-Wall \
|
||||
-o $name
|
||||
'';
|
||||
|
||||
@@ -79,9 +81,10 @@
|
||||
|
||||
buildPhase = ''
|
||||
g++ \
|
||||
modules/Print/src/*.cpp src/App.cpp \
|
||||
modules/Print/src/**.cpp src/App.cpp \
|
||||
-fpic -shared \
|
||||
-I src -I include \
|
||||
-Wall \
|
||||
-o $name
|
||||
'';
|
||||
|
||||
@@ -109,10 +112,11 @@
|
||||
|
||||
buildPhase = ''
|
||||
g++ \
|
||||
modules/Window/src/*.cpp modules/Window/src/WindowImpl/GLFW/*.cpp src/App.cpp \
|
||||
modules/Window/src/**.cpp src/App.cpp \
|
||||
-fpic -shared \
|
||||
-I src -I include \
|
||||
-lGL -lglfw -lGLEW \
|
||||
-Wall \
|
||||
-o $name
|
||||
'';
|
||||
|
||||
@@ -137,9 +141,10 @@
|
||||
|
||||
buildPhase = ''
|
||||
g++ \
|
||||
modules/MainGUI/src/*.cpp src/App.cpp \
|
||||
modules/MainGUI/src/**.cpp src/App.cpp \
|
||||
-fpic -shared \
|
||||
-I src -I include \
|
||||
-Wall \
|
||||
-o $name
|
||||
'';
|
||||
|
||||
|
||||
@@ -4,13 +4,21 @@ WindowModule::WindowModule(void* h, App& a) : Module(h, a) {
|
||||
name = "Window";
|
||||
}
|
||||
|
||||
WindowModule::~WindowModule() {}
|
||||
WindowModule::~WindowModule() {
|
||||
delete window;
|
||||
delete renderer;
|
||||
}
|
||||
|
||||
void WindowModule::onLoad() {
|
||||
window = new Window();
|
||||
renderer = new Renderer();
|
||||
}
|
||||
|
||||
void WindowModule::run() {
|
||||
|
||||
if(window.shouldClose()) {
|
||||
app.stopModule(self);
|
||||
if(window->shouldClose()) {
|
||||
app.end();
|
||||
}
|
||||
|
||||
window.doFrame();
|
||||
window->doFrame();
|
||||
}
|
||||
|
||||
@@ -11,11 +11,13 @@ class WindowModule : public Module {
|
||||
|
||||
void run();
|
||||
|
||||
void onLoad();
|
||||
|
||||
private:
|
||||
|
||||
Window window;
|
||||
Window* window;
|
||||
|
||||
Renderer renderer;
|
||||
Renderer* renderer;
|
||||
};
|
||||
|
||||
|
||||
|
||||
90
src/App.cpp
90
src/App.cpp
@@ -28,31 +28,15 @@ App::~App() {
|
||||
}
|
||||
}
|
||||
|
||||
void App::run() {
|
||||
std::cout << "\nTesting...\n";
|
||||
|
||||
// Main loop
|
||||
while (!done && !modules.empty()) {
|
||||
bool App::load(std::string lib, std::list<std::string> blacklist = {}) {
|
||||
|
||||
for(auto it = modules.begin(); it != modules.end(); it++) {
|
||||
(*it)->run();
|
||||
}
|
||||
|
||||
for(auto it = toClose.begin(); it != toClose.end(); it++) {
|
||||
unload(it);
|
||||
}
|
||||
toClose.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void App::load(std::string lib) {
|
||||
|
||||
void* h = dlopen(lib.c_str(), RTLD_NOW);
|
||||
|
||||
if(!h) {
|
||||
std::cout << "could not open lib: \"" << lib.c_str() << "\"\nError: " << dlerror() << std::endl;
|
||||
return;
|
||||
std::cout << "could not open lib at: \"" << lib.c_str() << "\"\nError: " << dlerror() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
Module::create_t* create = (Module::create_t*) dlsym(h, "create");
|
||||
@@ -63,33 +47,60 @@ void App::load(std::string lib) {
|
||||
|
||||
Module* m = create(h, App::Get());
|
||||
|
||||
for(auto it = modules.begin(); it != modules.end(); it++) {
|
||||
for(std::string s : blacklist) {
|
||||
if(s == m->getName()) {
|
||||
std::cout << "Module \"" << s << "\" is already loaded!\n";
|
||||
delete m;
|
||||
dlclose(h);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if((*it)->getName() == m->getName()) {
|
||||
std::cout << "Module \"" << m->getName() << "\" is already loaded!\n";
|
||||
return;
|
||||
blacklist.push_back(m->getName());
|
||||
|
||||
bool skip = false;
|
||||
|
||||
for(auto it = m->deps.begin(); it != m->deps.end(); it++) {
|
||||
for(std::string s : blacklist) {
|
||||
if(it->first == s)
|
||||
skip = true;
|
||||
}
|
||||
if(skip) {
|
||||
skip = false;
|
||||
continue;
|
||||
} else {
|
||||
load(it->second, blacklist);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
modules.push_back(m);
|
||||
m->setSelf(--modules.end());
|
||||
modules.end()++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void App::unload(std::list<Module*>::iterator it) {
|
||||
void* h = (*it)->getHandle();
|
||||
|
||||
delete *it;
|
||||
Module* m = *it;
|
||||
void* h = m->getHandle();
|
||||
|
||||
modules.erase(m->self);
|
||||
delete m;
|
||||
|
||||
dlclose(h);
|
||||
|
||||
modules.erase((*it)->self);
|
||||
}
|
||||
|
||||
void App::stopModule(std::list<Module*>::iterator it) {
|
||||
toClose.push_back(*it);
|
||||
}
|
||||
|
||||
void App::startModule(std::string lib) {
|
||||
toOpen.push_back(lib);
|
||||
}
|
||||
|
||||
void App::handleArgs(const int& argc, char* argv[]) {
|
||||
|
||||
int i = 1;
|
||||
@@ -120,3 +131,30 @@ void App::handleArgs(const int& argc, char* argv[]) {
|
||||
void App::printHelp() {
|
||||
std::cout << "archimedes [ -h | --help ] /path/to/some/library.so\n";
|
||||
}
|
||||
|
||||
|
||||
void App::run() {
|
||||
std::cout << "\nTesting...\n";
|
||||
|
||||
for(auto* m : modules)
|
||||
m->onLoad();
|
||||
|
||||
// Main loop
|
||||
while (!done && !modules.empty()) {
|
||||
|
||||
for(auto* m : modules) {
|
||||
m->run();
|
||||
}
|
||||
|
||||
for(auto it = toClose.begin(); it != toClose.end(); it++) {
|
||||
unload(it);
|
||||
}
|
||||
toClose.clear();
|
||||
|
||||
for(std::string s : toOpen) {
|
||||
load(s, getBlacklist());
|
||||
}
|
||||
toOpen.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
12
src/App.h
12
src/App.h
@@ -14,14 +14,22 @@ class App {
|
||||
|
||||
std::list<Module*> modules;
|
||||
std::list<Module*> toClose;
|
||||
std::list<std::string> toOpen;
|
||||
|
||||
void load(std::string);
|
||||
bool load(std::string, std::list<std::string>);
|
||||
void unload(std::list<Module*>::iterator);
|
||||
|
||||
void handleArgs(const int&, char*[]);
|
||||
|
||||
void printHelp();
|
||||
|
||||
std::list<std::string> getBlacklist() {
|
||||
std::list<std::string> l;
|
||||
for(Module* m : modules)
|
||||
l.push_back(m->getName());
|
||||
return l;
|
||||
}
|
||||
|
||||
public:
|
||||
App(const int&, char*[]);
|
||||
~App();
|
||||
@@ -32,6 +40,8 @@ class App {
|
||||
|
||||
void stopModule(std::list<Module*>::iterator);
|
||||
|
||||
void startModule(std::string);
|
||||
|
||||
void end() { done = true; }
|
||||
};
|
||||
|
||||
|
||||
@@ -11,11 +11,12 @@ class Module {
|
||||
|
||||
public:
|
||||
typedef Module* create_t(void*, App&);
|
||||
typedef void* destroy_t(Module*);
|
||||
|
||||
Module(void* h, App& a) : handle(h), app(a) {};
|
||||
virtual ~Module() {}
|
||||
virtual void run() = 0;
|
||||
virtual void onLoad() = 0;
|
||||
|
||||
std::string getName() const { return name; }
|
||||
void* getHandle() { return handle; }
|
||||
|
||||
@@ -28,6 +29,7 @@ class Module {
|
||||
|
||||
App& app;
|
||||
|
||||
std::unordered_map<std::string, std::string> deps;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -4,8 +4,11 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <list>
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
#include <optional>
|
||||
//#include <chrono>
|
||||
//#include <thread>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user