Skip to content

Commit aae775e

Browse files
committed
Add Dear ImGui for ease of debugging
1 parent d866acb commit aae775e

6 files changed

Lines changed: 74 additions & 0 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "libs/Lua-cURLv3"]
55
path = libs/Lua-cURLv3
66
url = https://github.com/Lua-cURL/Lua-cURLv3.git
7+
[submodule "dep/imgui"]
8+
path = dep/imgui
9+
url = https://github.com/ocornut/imgui.git

CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,41 @@ find_package(re2 CONFIG REQUIRED)
104104
find_package(Threads REQUIRED)
105105
find_package(ZLIB REQUIRED)
106106

107+
add_library(imgui STATIC
108+
dep/imgui/imconfig.h
109+
dep/imgui/imgui.cpp
110+
dep/imgui/imgui.h
111+
dep/imgui/imgui_demo.cpp
112+
dep/imgui/imgui_draw.cpp
113+
dep/imgui/imgui_internal.h
114+
dep/imgui/imgui_tables.cpp
115+
dep/imgui/imgui_widgets.cpp
116+
dep/imgui/imstb_rectpack.h
117+
dep/imgui/imstb_textedit.h
118+
dep/imgui/imstb_truetype.h
119+
dep/imgui/backends/imgui_impl_glfw.cpp
120+
dep/imgui/backends/imgui_impl_glfw.h
121+
dep/imgui/backends/imgui_impl_opengl3.cpp
122+
dep/imgui/backends/imgui_impl_opengl3.h
123+
dep/imgui/backends/imgui_impl_opengl3_loader.h
124+
dep/imgui/misc/cpp/imgui_stdlib.cpp
125+
dep/imgui/misc/cpp/imgui_stdlib.h
126+
)
127+
128+
target_compile_definitions(imgui PUBLIC
129+
"IMGUI_IMPL_OPENGL_ES2"
130+
)
131+
132+
target_include_directories(imgui PUBLIC
133+
dep/imgui
134+
dep/imgui/backends
135+
)
136+
137+
target_link_libraries(imgui PUBLIC
138+
unofficial::angle::libGLESv2
139+
glfw
140+
)
141+
107142
target_include_directories(SimpleGraphic
108143
PRIVATE
109144
${CMAKE_CURRENT_SOURCE_DIR}/dep/glad/include
@@ -141,6 +176,7 @@ target_link_libraries(SimpleGraphic
141176
unofficial::angle::libGLESv2
142177
fmt::fmt
143178
glfw
179+
imgui
144180
${LUAJIT_LIBRARIES}
145181
re2::re2
146182
Threads::Threads

dep/imgui

Submodule imgui added at 0962c9f

engine/render/r_main.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#include <sstream>
1515
#include <vector>
1616

17+
#include <imgui_impl_glfw.h>
18+
#include <imgui_impl_opengl3.h>
19+
1720
// =======
1821
// Classes
1922
// =======
@@ -747,6 +750,12 @@ void r_renderer_c::Init()
747750

748751
whiteImage = RegisterShader("@white", 0);
749752

753+
imguiCtx = ImGui::CreateContext();
754+
ImGui::SetCurrentContext(imguiCtx);
755+
756+
ImGui_ImplGlfw_InitForOpenGL((GLFWwindow*)sys->video->GetWindowHandle(), true);
757+
ImGui_ImplOpenGL3_Init("#version 100");
758+
750759
fonts[F_FIXED] = new r_font_c(this, "Bitstream Vera Sans Mono");
751760
fonts[F_VAR] = new r_font_c(this, "Liberation Sans");
752761
fonts[F_VAR_BOLD] = new r_font_c(this, "Liberation Sans Bold");
@@ -760,6 +769,10 @@ void r_renderer_c::Shutdown()
760769

761770
sys->con->Printf("Unloading resources...\n");
762771

772+
ImGui_ImplOpenGL3_Shutdown();
773+
ImGui_ImplGlfw_Shutdown();
774+
ImGui::DestroyContext(imguiCtx);
775+
763776
delete whiteImage;
764777

765778
for (int f = 0; f < F_NUMFONTS; f++) {
@@ -799,6 +812,9 @@ void r_renderer_c::Shutdown()
799812

800813
void r_renderer_c::BeginFrame()
801814
{
815+
ImGui_ImplOpenGL3_NewFrame();
816+
ImGui_ImplGlfw_NewFrame();
817+
ImGui::NewFrame();
802818
{
803819
auto& vid = sys->video->vid;
804820
int wNew = VirtualScreenWidth();
@@ -914,6 +930,9 @@ void r_renderer_c::EndFrame()
914930

915931
glFlush();
916932

933+
ImGui::Render();
934+
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
935+
917936
// Swap output buffers
918937
openGL->Swap();
919938

engine/render/r_main.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#define R_MAXSHADERS 65536
1212

13+
#include <imgui.h>
14+
1315
// =======
1416
// Classes
1517
// =======
@@ -116,6 +118,8 @@ class r_renderer_c: public r_IRenderer, public conCmdHandler_c {
116118

117119
r_shaderHnd_c* whiteImage = nullptr; // White image
118120

121+
ImGuiContext* imguiCtx = nullptr;
122+
119123
r_font_c* fonts[F_NUMFONTS] = {}; // Font objects
120124

121125
col4_t drawColor = {}; // Current draw color

engine/system/win/sys_video.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <optional>
1616
#include <utility>
1717

18+
#include <imgui.h>
19+
1820
// ====================
1921
// sys_IVideo Interface
2022
// ====================
@@ -265,10 +267,16 @@ int sys_video_c::Apply(sys_vidSet_s* set)
265267
});
266268
glfwSetCharCallback(wnd, [](GLFWwindow* wnd, uint32_t codepoint) {
267269
auto sys = (sys_main_c*)glfwGetWindowUserPointer(wnd);
270+
if (ImGui::GetIO().WantCaptureKeyboard) {
271+
return;
272+
}
268273
sys->core->KeyEvent(codepoint, KE_CHAR);
269274
});
270275
glfwSetKeyCallback(wnd, [](GLFWwindow* wnd, int key, int scancode, int action, int mods) {
271276
auto sys = (sys_main_c*)glfwGetWindowUserPointer(wnd);
277+
if (ImGui::GetIO().WantCaptureKeyboard) {
278+
return;
279+
}
272280
if (byte k = sys->GlfwKeyToKey(key)) {
273281
bool is_down = action == GLFW_PRESS || action == GLFW_REPEAT;
274282
sys->heldKeyState[k] = is_down;
@@ -281,6 +289,9 @@ int sys_video_c::Apply(sys_vidSet_s* set)
281289
});
282290
glfwSetMouseButtonCallback(wnd, [](GLFWwindow* wnd, int button, int action, int mods) {
283291
auto sys = (sys_main_c*)glfwGetWindowUserPointer(wnd);
292+
if (ImGui::GetIO().WantCaptureMouse) {
293+
return;
294+
}
284295
auto video = (sys_video_c*)sys->video;
285296
int sg_key;
286297
switch(button) {

0 commit comments

Comments
 (0)