Skip to main content

ImGuiGlfwOpenGLBackend.ixx File

GLFW+OpenGL backend implementation for ImGui rendering. More...

Included Headers

#include <cassert> #include <stdexcept> #include "imgui.h" #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" #include <helios.engine.runtime.world.EngineWorld> #include <helios.engine.platform.window.types.WindowHandle> #include <helios.glfw.components.GLFWWindowHandleComponent> #include <helios.imgui.ImGuiBackend>

Namespaces Index

namespacehelios
namespaceimgui
namespacetypes
namespaceworld
namespacecomponents

Classes Index

classImGuiGlfwOpenGLBackend

ImGui backend for GLFW windowing and OpenGL 4.1 rendering. More...

Description

GLFW+OpenGL backend implementation for ImGui rendering.

File Listing

The file content with the documentation metadata removed is:

1
5module;
6
7#include <cassert>
8#include <stdexcept>
9
10#include "imgui.h"
11#include "imgui_impl_glfw.h"
12#include "imgui_impl_opengl3.h"
13
14export module helios.imgui.ImGuiGlfwOpenGLBackend;
15
16import helios.imgui.ImGuiBackend;
17import helios.engine.platform.window.types.WindowHandle;
18import helios.engine.runtime.world.EngineWorld;
19
20import helios.glfw.components.GLFWWindowHandleComponent;
21
22using namespace helios::engine::platform::window::types;
23using namespace helios::engine::runtime::world;
24using namespace helios::glfw::components;
25export namespace helios::imgui {
26
38
39 private:
40
49 bool initialized_ = false;
50
63 void shutdown() noexcept {
64 if (initialized_) {
65 ImGui_ImplOpenGL3_Shutdown();
66 ImGui_ImplGlfw_Shutdown();
67 ImGui::DestroyContext();
68 initialized_ = false;
69 }
70 }
71
75 WindowHandle windowHandle_;
76
80 PlatformWorld& platformWorld_;
81
90 bool initialize() noexcept {
91
92 if (initialized_) {
93 return true;
94 }
95 auto glfwWindow = platformWorld_.findEntity(windowHandle_);
96
97 if (!glfwWindow) {
98 assert(false && "Expected a valid GLFW window entity");
99 return false;
100 }
101
102 auto glfwComp = glfwWindow->get<GLFWWindowHandleComponent<WindowHandle>>();
103
104 if (!glfwComp) {
105 return false;
106 }
107
108 if (!ImGui_ImplGlfw_InitForOpenGL(glfwComp->handle, true)) {
109 ImGui::DestroyContext();
110 assert(false && "Failed to initialize GLFW backend for ImGui");
111 return false;
112 }
113
114 if (!ImGui_ImplOpenGL3_Init("#version 410")) {
115 ImGui_ImplGlfw_Shutdown();
116 ImGui::DestroyContext();
117 assert(false && "Failed to initialize OpenGL 4.1 backend for ImGui");
118 return false;
119 }
120 initialized_ = true;
121 return true;
122 }
123
124 public:
125
126 // No copy, no move (manages global ImGui context).
131
140 explicit ImGuiGlfwOpenGLBackend(WindowHandle window, PlatformWorld& platformWorld)
141 : windowHandle_(window), platformWorld_(platformWorld) {
142
143 if (ImGui::GetCurrentContext()) {
144 throw std::runtime_error("ImGui context already initialized");
145 }
146
147 IMGUI_CHECKVERSION();
148 ImGui::CreateContext();
149 ImGuiIO& io = ImGui::GetIO();
150 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
151 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
152
153 ImGui::StyleColorsDark();
154 }
155
163 void renderDrawData(ImDrawData* drawData) override {
164 if (initialized_) {
165 ImGui_ImplOpenGL3_RenderDrawData(drawData);
166 }
167 }
168
174 bool newFrame() override {
175
176 if (!initialize()) {
177 return false;
178 }
179
180
181 ImGui_ImplOpenGL3_NewFrame();
182 ImGui_ImplGlfw_NewFrame();
183 ImGui::NewFrame();
184
185 return true;
186 }
187
194 shutdown();
195 }
196
197 };
198
199}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.