aquire and release

This commit is contained in:
2025-04-20 14:58:39 -05:00
parent c6fa731df0
commit d253ebb197
7 changed files with 78 additions and 45 deletions

View File

@@ -95,14 +95,19 @@ namespace Archimedes {
std::list<std::variant<std::string, Module*>> toOpen;
void handleEvents() {
bool handled = false;
static bool handled;
while(!events.empty()) {
handled = false;
for(auto it = runOrder.rbegin(); it != runOrder.rend(); it++) {
if(modules[*it]->onEvent(*events.front())) {
handled = modules[*it]->onEvent(*events.front());
if(handled) {
Event* e = events.front();
events.pop_front();
delete e;
handled = true;
break;
}
}
@@ -110,7 +115,6 @@ namespace Archimedes {
if(!handled) {
std::cout << "Error: Unhandled Event: " << (std::string) *events.front() << std::endl;
}
handled = false;
}
}

View File

@@ -18,12 +18,14 @@ namespace Archimedes {
bool shouldClose() { return window.shouldClose(); }
void getSize(int& w, int& h) {
window.getSize(w, h);
}
void doFrame() {
window.pollEvents();
window.getSize(renderer->w, renderer->h);
renderer->render();
window.doFrame();

View File

@@ -1,7 +1,5 @@
#include "ImguiModule.h"
#include "modules/WindowModule/src/WindowModule.h"
#include "backends/imgui_impl_glfw.h"
#include "backends/imgui_impl_opengl3.h"
@@ -27,6 +25,8 @@ ImguiModule::~ImguiModule() {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
wm->releaseWindow(window);
}
}
@@ -38,6 +38,8 @@ void ImguiModule::onLoad() {
std::cout << "No WindowModule for ImguiModule!\n";
std::abort();
}
window = wm->aquireWindow();
IMGUI_CHECKVERSION();
context = ImGui::CreateContext();
@@ -56,7 +58,7 @@ void ImguiModule::onLoad() {
//ImGui::StyleColorsLight();
// Setup Platform/Renderer backends
if(!ImGui_ImplGlfw_InitForOpenGL(wm->getWindow()->getWindowImpl().getWindow(), true))
if(!ImGui_ImplGlfw_InitForOpenGL(window->getWindowImpl().getWindow(), true))
std::cout << "GLFWImpl failed\n";
if(!ImGui_ImplOpenGL3_Init("#version 330")) {
std::cout << "ImGui_ImplOpenGL3_Init failed!\n" << std::endl;

View File

@@ -7,6 +7,8 @@
#include "imgui.h"
#include "misc/cpp/imgui_stdlib.h"
#include "modules/WindowModule/src/WindowModule.h"
class ImguiModule : public Archimedes::Module {
public:
@@ -20,12 +22,27 @@ class ImguiModule : public Archimedes::Module {
bool onEvent(const Archimedes::Event&) override;
ImGuiContext* getContext() { return context; }
ImGuiContext* aquireContext() {
contextRefs++;
return context;
}
void releaseContext(ImGuiContext* ctxt) {
if(ctxt == context && context != nullptr) {
if(--contextRefs == 0) {
app->stopModule(name);
}
}
}
private:
int contextRefs = 0;
ImGuiContext* context;
Archimedes::Window* window;
std::list<std::function<void()>>::iterator rcmd_it;
};

View File

@@ -12,7 +12,10 @@ MainGUI::MainGUI(Archimedes::App* a, void* h) : Archimedes::Module(a, h) {
MainGUI::~MainGUI() {
if(app) {}
if(app) {
ImguiModule* im; { im = (ImguiModule*) moduleInstances[ImguiModule()]; }
im->releaseContext(ImGui::GetCurrentContext());
}
}
void MainGUI::onLoad() {
@@ -24,7 +27,7 @@ void MainGUI::onLoad() {
std::abort();
}
ImGui::SetCurrentContext(im->getContext());
ImGui::SetCurrentContext(im->aquireContext());
}

View File

@@ -18,10 +18,9 @@ WindowModule::~WindowModule() {
renderer->getCmdList().clear();
delete renderer;
}
for(auto* w : windows) {
if(w)
delete w;
}
if(window)
delete window;
}
}
@@ -39,37 +38,13 @@ void WindowModule::onLoad() {
app->registerEvent(Archimedes::WindowFocusLostEvent());
app->registerEvent(Archimedes::WindowMovedEvent());
windows.push_back(new Archimedes::Window([this](Archimedes::Event* e){
app->emitEvent(e);
}));
renderer = new Archimedes::Renderer();
for(auto* window : windows)
window->setRenderer(renderer);
if(!renderer->init()) {
std::cout << "Renderer init failed!\n";
std::abort();
}
}
void WindowModule::run() {
for(auto* window : windows) {
if(window->shouldClose()) {
app->end();
}
for(auto e : window->getWindowImpl().data.eventList) {
app->emitEvent(e);
}
window->getWindowImpl().data.eventList.clear();
if(window)
window->doFrame();
}
}
bool WindowModule::onEvent(const Archimedes::Event& e) {
@@ -77,10 +52,10 @@ bool WindowModule::onEvent(const Archimedes::Event& e) {
unsigned int type = app->getEventType(e);
if(type == app->getEventType(Archimedes::WindowResizeEvent())) {
window->getSize(renderer->w, renderer->h);
return true;
} else if(type == app->getEventType(Archimedes::WindowCloseEvent())) {
app->stopModule(name);
return true;
} else if(type == app->getEventType(Archimedes::WindowKeyPressedEvent())) {

View File

@@ -25,12 +25,42 @@ class WindowModule : public Archimedes::Module {
//interface for other modules
std::vector<Archimedes::Window*>& getWindow() { return windows; }
Archimedes::Window* aquireWindow() {
if(!window) {
window = new Archimedes::Window([this](Archimedes::Event* e){
app->emitEvent(e);
});
window->setRenderer(renderer);
if(!renderer->init()) {
std::cout << "Renderer init failed!\n";
std::abort();
}
}
windowRefs++;
return window;
}
void releaseWindow(Archimedes::Window* w) {
if(w == window && window != nullptr) {
if(--windowRefs == 0) {
delete window;
window = nullptr;
}
}
}
Archimedes::Renderer* getRenderer() { return renderer; }
private:
std::vector<Archimedes::Window*> windows;
int windowRefs = 0;
Archimedes::Window* window;
Archimedes::Renderer* renderer;
};