Skip to main content

ImGuiGlfwOpenGLBackend.ixx File

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

Included Headers

#include <stdexcept> #include "imgui.h" #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" #include <helios.ext.imgui.ImGuiBackend>

Namespaces Index

namespacehelios
namespaceext

Platform-specific extensions and backend implementations. More...

namespaceimgui

Classes Index

classImGuiGlfwOpenGLBackend

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

Description

GLFW+OpenGL backend implementation for ImGui rendering.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file ImGuiGlfwOpenGLBackend.ixx
3 * @brief GLFW+OpenGL backend implementation for ImGui rendering.
4 */
5module;
6
7#include <stdexcept>
8
9#include "imgui.h"
10#include "imgui_impl_glfw.h"
11#include "imgui_impl_opengl3.h"
12
13export module helios.ext.imgui.ImGuiGlfwOpenGLBackend;
14
15import helios.ext.imgui.ImGuiBackend;
16
17export namespace helios::ext::imgui {
18
19 /**
20 * @class ImGuiGlfwOpenGLBackend
21 * @brief ImGui backend for GLFW windowing and OpenGL 4.6 rendering.
22 *
23 * Initializes ImGui context, GLFW platform layer, and OpenGL renderer.
24 * This backend is non-copyable and non-movable due to resource ownership semantics.
25 *
26 * @note Only one instance should exist per application. Creating multiple instances
27 * will throw a `std::runtime_error`.
28 */
30
31 private:
32
33 /**
34 * @brief Indicates whether the backend has been successfully initialized.
35 *
36 * Tracks the initialization state of the ImGui backend to prevent multiple
37 * redundant initialization attempts and manage proper shutdown procedures.
38 *
39 * @note Modified during initialization and shutdown processes.
40 */
41 bool initialized_ = false;
42
43 /**
44 * @brief Shuts down the ImGui GLFW and OpenGL backend.
45 *
46 * Ensures proper cleanup of the ImGui context, OpenGL, and GLFW resources.
47 * After calling this method, the backend will no longer be initialized, and
48 * associated resources will be released.
49 *
50 * This function is safe to call multiple times but has no effect if the backend
51 * is not currently initialized.
52 *
53 * @note This method is noexcept and will not throw exceptions.
54 */
55 void shutdown() noexcept {
56 if (initialized_) {
57 ImGui_ImplOpenGL3_Shutdown();
58 ImGui_ImplGlfw_Shutdown();
59 ImGui::DestroyContext();
60 initialized_ = false;
61 }
62 }
63
64 public:
65
66 // No copy, no move (manages global ImGui context).
71
72 /**
73 * @brief Constructs and initializes the ImGui backend for GLFW+OpenGL.
74 *
75 * @param window GLFW window handle. Must be valid for the lifetime of this backend.
76 *
77 * @throws std::runtime_error if ImGui context already exists or initialization fails.
78 */
79 explicit ImGuiGlfwOpenGLBackend(GLFWwindow* window) {
80
81 if (ImGui::GetCurrentContext()) {
82 throw std::runtime_error("ImGui context already initialized");
83 }
84
85 IMGUI_CHECKVERSION();
86 ImGui::CreateContext();
87 ImGuiIO& io = ImGui::GetIO();
88 io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
89 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
90
91 ImGui::StyleColorsDark();
92
93 if (!ImGui_ImplGlfw_InitForOpenGL(window, true)) {
94 ImGui::DestroyContext();
95 throw std::runtime_error("Failed to initialize GLFW backend for ImGui");
96 }
97
98 if (!ImGui_ImplOpenGL3_Init("#version 460")) {
99 ImGui_ImplGlfw_Shutdown();
100 ImGui::DestroyContext();
101 throw std::runtime_error("Failed to initialize OpenGL 4.6 backend for ImGui");
102 }
103 initialized_ = true;
104 }
105
106 /**
107 * @brief Renders ImGui draw data using OpenGL.
108 *
109 * @param drawData Pointer to ImGui draw data.
110 */
111 void renderDrawData(ImDrawData* drawData) override {
112 ImGui_ImplOpenGL3_RenderDrawData(drawData);
113 }
114
115 /**
116 * @brief Starts a new ImGui frame.
117 */
118 void newFrame() override {
119 ImGui_ImplOpenGL3_NewFrame();
120 ImGui_ImplGlfw_NewFrame();
121 ImGui::NewFrame();
122 }
123
124 /**
125 * @brief Destructor; shuts down ImGui backend and releases resources.
126 *
127 * @see shutdown()
128 */
130 shutdown();
131 }
132
133 };
134
135}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.