This commit is contained in:
2025-05-12 00:14:11 -05:00
parent ec4b85587b
commit 700e422270
14 changed files with 70 additions and 71 deletions

View File

@@ -0,0 +1,87 @@
#ifndef WINDOWMODULE_H
#define WINDOWMODULE_H
#include "Archimedes.h"
#include "utils/Renderer/Renderer.h"
#include "utils/Window/Window.h"
static_assert(!(WINDOW == 1 && RENDERER == 2), "Window and Renderer are incompatible!\n");
class WindowModule : public Archimedes::Module {
public:
WindowModule(Archimedes::App* a, void* h) : Archimedes::Module(a, h) {
name = "WindowModule";
}
WindowModule() { name = "WindowModule"; }
~WindowModule() override;
void run() override;
void onLoad() override;
bool onEvent(const Archimedes::Event& e) override;
//interface for other modules
Archimedes::Window* aquireWindow() {
if(!window) {
window = new Archimedes::Window([this](Archimedes::Event* e) {
for(std::function<void(Archimedes::Event*)> f : eventFuncs)
f(e);
app->emitEvent(e);
});
window->setRenderer(renderer);
if(!renderer->init(window->getWindowImpl().getWindow())) {
std::cout << "Renderer init failed!\n";
std::abort();
}
}
windowRefs++;
#if RENDERER == 1
window->getWindowImpl().restoreContext();
#endif
return window;
}
void releaseWindow(Archimedes::Window* w) {
if(w == window && window != nullptr) {
if(--windowRefs == 0) {
delete window;
window = nullptr;
}
}
}
auto addEventFn(const std::function<void(Archimedes::Event*)>& fn) { eventFuncs.push_back(fn); return --eventFuncs.end()++; }
void removeEventFn(std::list<std::function<void(Archimedes::Event*)>>::iterator it) { eventFuncs.erase(it); }
Archimedes::Renderer* getRenderer() { return renderer; }
private:
int windowRefs = 0;
std::list<std::function<void(Archimedes::Event*)>> eventFuncs;
Archimedes::Window* window = nullptr;
Archimedes::Renderer* renderer = nullptr;
};
#ifdef WINDOWMODULE_DYNAMIC
#define MODULE_TYPE WindowModule
#include "endModule.h"
#endif
#endif