aquire and release
This commit is contained in:
@@ -95,14 +95,19 @@ namespace Archimedes {
|
|||||||
std::list<std::variant<std::string, Module*>> toOpen;
|
std::list<std::variant<std::string, Module*>> toOpen;
|
||||||
|
|
||||||
void handleEvents() {
|
void handleEvents() {
|
||||||
bool handled = false;
|
static bool handled;
|
||||||
while(!events.empty()) {
|
while(!events.empty()) {
|
||||||
|
|
||||||
|
handled = false;
|
||||||
|
|
||||||
for(auto it = runOrder.rbegin(); it != runOrder.rend(); it++) {
|
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();
|
Event* e = events.front();
|
||||||
events.pop_front();
|
events.pop_front();
|
||||||
delete e;
|
delete e;
|
||||||
handled = true;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,7 +115,6 @@ namespace Archimedes {
|
|||||||
if(!handled) {
|
if(!handled) {
|
||||||
std::cout << "Error: Unhandled Event: " << (std::string) *events.front() << std::endl;
|
std::cout << "Error: Unhandled Event: " << (std::string) *events.front() << std::endl;
|
||||||
}
|
}
|
||||||
handled = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,12 +18,14 @@ namespace Archimedes {
|
|||||||
|
|
||||||
bool shouldClose() { return window.shouldClose(); }
|
bool shouldClose() { return window.shouldClose(); }
|
||||||
|
|
||||||
|
void getSize(int& w, int& h) {
|
||||||
|
window.getSize(w, h);
|
||||||
|
}
|
||||||
|
|
||||||
void doFrame() {
|
void doFrame() {
|
||||||
|
|
||||||
window.pollEvents();
|
window.pollEvents();
|
||||||
|
|
||||||
window.getSize(renderer->w, renderer->h);
|
|
||||||
|
|
||||||
renderer->render();
|
renderer->render();
|
||||||
|
|
||||||
window.doFrame();
|
window.doFrame();
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
#include "ImguiModule.h"
|
#include "ImguiModule.h"
|
||||||
|
|
||||||
#include "modules/WindowModule/src/WindowModule.h"
|
|
||||||
|
|
||||||
#include "backends/imgui_impl_glfw.h"
|
#include "backends/imgui_impl_glfw.h"
|
||||||
#include "backends/imgui_impl_opengl3.h"
|
#include "backends/imgui_impl_opengl3.h"
|
||||||
|
|
||||||
@@ -27,6 +25,8 @@ ImguiModule::~ImguiModule() {
|
|||||||
ImGui_ImplOpenGL3_Shutdown();
|
ImGui_ImplOpenGL3_Shutdown();
|
||||||
ImGui_ImplGlfw_Shutdown();
|
ImGui_ImplGlfw_Shutdown();
|
||||||
ImGui::DestroyContext();
|
ImGui::DestroyContext();
|
||||||
|
|
||||||
|
wm->releaseWindow(window);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,6 +39,8 @@ void ImguiModule::onLoad() {
|
|||||||
std::abort();
|
std::abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window = wm->aquireWindow();
|
||||||
|
|
||||||
IMGUI_CHECKVERSION();
|
IMGUI_CHECKVERSION();
|
||||||
context = ImGui::CreateContext();
|
context = ImGui::CreateContext();
|
||||||
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
||||||
@@ -56,7 +58,7 @@ void ImguiModule::onLoad() {
|
|||||||
//ImGui::StyleColorsLight();
|
//ImGui::StyleColorsLight();
|
||||||
|
|
||||||
// Setup Platform/Renderer backends
|
// 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";
|
std::cout << "GLFWImpl failed\n";
|
||||||
if(!ImGui_ImplOpenGL3_Init("#version 330")) {
|
if(!ImGui_ImplOpenGL3_Init("#version 330")) {
|
||||||
std::cout << "ImGui_ImplOpenGL3_Init failed!\n" << std::endl;
|
std::cout << "ImGui_ImplOpenGL3_Init failed!\n" << std::endl;
|
||||||
|
|||||||
@@ -7,6 +7,8 @@
|
|||||||
#include "imgui.h"
|
#include "imgui.h"
|
||||||
#include "misc/cpp/imgui_stdlib.h"
|
#include "misc/cpp/imgui_stdlib.h"
|
||||||
|
|
||||||
|
#include "modules/WindowModule/src/WindowModule.h"
|
||||||
|
|
||||||
class ImguiModule : public Archimedes::Module {
|
class ImguiModule : public Archimedes::Module {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -20,12 +22,27 @@ class ImguiModule : public Archimedes::Module {
|
|||||||
|
|
||||||
bool onEvent(const Archimedes::Event&) override;
|
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:
|
private:
|
||||||
|
|
||||||
|
int contextRefs = 0;
|
||||||
|
|
||||||
ImGuiContext* context;
|
ImGuiContext* context;
|
||||||
|
|
||||||
|
Archimedes::Window* window;
|
||||||
|
|
||||||
std::list<std::function<void()>>::iterator rcmd_it;
|
std::list<std::function<void()>>::iterator rcmd_it;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,10 @@ MainGUI::MainGUI(Archimedes::App* a, void* h) : Archimedes::Module(a, h) {
|
|||||||
|
|
||||||
MainGUI::~MainGUI() {
|
MainGUI::~MainGUI() {
|
||||||
|
|
||||||
if(app) {}
|
if(app) {
|
||||||
|
ImguiModule* im; { im = (ImguiModule*) moduleInstances[ImguiModule()]; }
|
||||||
|
im->releaseContext(ImGui::GetCurrentContext());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainGUI::onLoad() {
|
void MainGUI::onLoad() {
|
||||||
@@ -24,7 +27,7 @@ void MainGUI::onLoad() {
|
|||||||
std::abort();
|
std::abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui::SetCurrentContext(im->getContext());
|
ImGui::SetCurrentContext(im->aquireContext());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,10 +18,9 @@ WindowModule::~WindowModule() {
|
|||||||
renderer->getCmdList().clear();
|
renderer->getCmdList().clear();
|
||||||
delete renderer;
|
delete renderer;
|
||||||
}
|
}
|
||||||
for(auto* w : windows) {
|
|
||||||
if(w)
|
if(window)
|
||||||
delete w;
|
delete window;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,48 +38,24 @@ void WindowModule::onLoad() {
|
|||||||
app->registerEvent(Archimedes::WindowFocusLostEvent());
|
app->registerEvent(Archimedes::WindowFocusLostEvent());
|
||||||
app->registerEvent(Archimedes::WindowMovedEvent());
|
app->registerEvent(Archimedes::WindowMovedEvent());
|
||||||
|
|
||||||
windows.push_back(new Archimedes::Window([this](Archimedes::Event* e){
|
|
||||||
app->emitEvent(e);
|
|
||||||
}));
|
|
||||||
|
|
||||||
renderer = new Archimedes::Renderer();
|
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() {
|
void WindowModule::run() {
|
||||||
|
|
||||||
for(auto* window : windows) {
|
if(window)
|
||||||
|
|
||||||
if(window->shouldClose()) {
|
|
||||||
app->end();
|
|
||||||
}
|
|
||||||
|
|
||||||
for(auto e : window->getWindowImpl().data.eventList) {
|
|
||||||
app->emitEvent(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
window->getWindowImpl().data.eventList.clear();
|
|
||||||
|
|
||||||
window->doFrame();
|
window->doFrame();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
bool WindowModule::onEvent(const Archimedes::Event& e) {
|
bool WindowModule::onEvent(const Archimedes::Event& e) {
|
||||||
|
|
||||||
unsigned int type = app->getEventType(e);
|
unsigned int type = app->getEventType(e);
|
||||||
|
|
||||||
if(type == app->getEventType(Archimedes::WindowResizeEvent())) {
|
if(type == app->getEventType(Archimedes::WindowResizeEvent())) {
|
||||||
|
window->getSize(renderer->w, renderer->h);
|
||||||
return true;
|
return true;
|
||||||
} else if(type == app->getEventType(Archimedes::WindowCloseEvent())) {
|
} else if(type == app->getEventType(Archimedes::WindowCloseEvent())) {
|
||||||
|
app->stopModule(name);
|
||||||
return true;
|
return true;
|
||||||
} else if(type == app->getEventType(Archimedes::WindowKeyPressedEvent())) {
|
} else if(type == app->getEventType(Archimedes::WindowKeyPressedEvent())) {
|
||||||
|
|
||||||
|
|||||||
@@ -25,12 +25,42 @@ class WindowModule : public Archimedes::Module {
|
|||||||
|
|
||||||
//interface for other modules
|
//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; }
|
Archimedes::Renderer* getRenderer() { return renderer; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
std::vector<Archimedes::Window*> windows;
|
int windowRefs = 0;
|
||||||
|
|
||||||
|
Archimedes::Window* window;
|
||||||
Archimedes::Renderer* renderer;
|
Archimedes::Renderer* renderer;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user