From 634c0d89e494fd50329386670c67ccd24f499650 Mon Sep 17 00:00:00 2001 From: Alexander Shishkin Date: Sun, 27 Oct 2024 22:01:05 +0200 Subject: [PATCH] ui-imgui: Add basic initialization/deinitialization and renderning For now, draw ImGui's demo window on top of the scene to see that it works. Hook up the initialization/deinitialization to the glfw part of the code. The same is done for emscripten, except don't use the glfw emulation, as it's just one more intermediate layer. Instead, skip the platform backend for now. All that's really needed is input, and mostly mouse input. When enough of that is done, it can be made into a proper backend for ImGui. Signed-off-by: Alexander Shishkin --- core/CMakeLists.txt | 2 +- core/clap.c | 1 + core/display-glfw.c | 4 +++ core/display-www.c | 2 ++ core/ui-debug.h | 4 +++ core/ui-imgui.c | 64 ++++++++++++++++++++++++++++++++++++++ demo/ldjam56/onehandclap.c | 1 + 7 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 core/ui-imgui.c diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 3695f47..470786b 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -86,7 +86,7 @@ set(ENGINE_SRC terrain.c ui.c scene.c font.c sound.c networking.c pngloader.c physics.c ui-animations.c input-fuzzer.c character.c settings.c gltf.c input-joystick.c render-gl.c mesh.c pipeline.c input-keyboard.c - game.c camera.c xyarray.c ca2d.c ca3d.c + game.c camera.c xyarray.c ca2d.c ca3d.c ui-imgui.c ${PLATFORM_SRC}) # Mac OS X has deprecated OpenAL and is very vocal about it diff --git a/core/clap.c b/core/clap.c index e94c0b4..b5476d6 100644 --- a/core/clap.c +++ b/core/clap.c @@ -14,6 +14,7 @@ #include "librarian.h" #include "physics.h" #include "settings.h" +#include "ui-debug.h" #include "util.h" #ifdef HAVE_ASAN diff --git a/core/display-glfw.c b/core/display-glfw.c index 9984d52..95dbc10 100644 --- a/core/display-glfw.c +++ b/core/display-glfw.c @@ -7,6 +7,7 @@ //#include #include "display.h" #include +#include "ui-debug.h" #include "common.h" #include "input.h" #include "input-joystick.h" @@ -174,6 +175,8 @@ void gl_init(const char *title, int w, int h, display_update update, void *updat ext = glGetStringi(GL_EXTENSIONS, i); msg("GL extension: '%s'\n", ext); } + + imgui_init(window, width, height); // msg("GL initialized extensions: %s\n", exts); } @@ -191,6 +194,7 @@ void gl_main_loop(void) void gl_done(void) { + imgui_done(); glfwDestroyWindow(window); glfwTerminate(); } diff --git a/core/display-www.c b/core/display-www.c index 663cfce..d3725b0 100644 --- a/core/display-www.c +++ b/core/display-www.c @@ -7,6 +7,7 @@ #include "common.h" #include "display.h" #include "input-joystick.h" +#include "ui-debug.h" static int width, height; @@ -155,6 +156,7 @@ void gl_init(const char *title, int width, int height, display_update update_fn, msg("GL context: %d Extensions: '%s'\n", context, exts); EM_ASM(runtime_ready = true;); gl_get_sizes(NULL, NULL); + imgui_init(NULL, width, height); calc_refresh_rate(update_fn, data); //resize_fn(width, height); } diff --git a/core/ui-debug.h b/core/ui-debug.h index 1554aa5..21e756b 100644 --- a/core/ui-debug.h +++ b/core/ui-debug.h @@ -16,4 +16,8 @@ static inline void ui_show_debug_once(const char *debug_name) ui_show_debug(str_basename(debug_name)); } +void imgui_init(void *data, int width, int height); +void imgui_done(void); +void imgui_render(int width, int height); + #endif /* __CLAP_UI_DEBUG_H__ */ diff --git a/core/ui-imgui.c b/core/ui-imgui.c new file mode 100644 index 0000000..c27a0d3 --- /dev/null +++ b/core/ui-imgui.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: Apache-2.0 +#include "common.h" +#include "ui-debug.h" + +#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS +#include "cimgui.h" + +#ifndef __EMSCRIPTEN__ +#include +#include "imgui_impl_glfw.h" +#endif + +#include "imgui_impl_opengl3.h" + +static struct ImGuiContext *ctx; +static struct ImGuiIO *io; + +void imgui_render(int width, int height) +{ + io->DisplaySize.x = width; + io->DisplaySize.y = height; + + ImGui_ImplOpenGL3_NewFrame(); +#ifndef __EMSCRIPTEN__ + ImGui_ImplGlfw_NewFrame(); +#endif + igNewFrame(); + + // igSetNextWindowPos((struct ImVec2){0,0}, ImGuiCond_FirstUseEver,(struct ImVec2){0,0} ); + // igShowDemoWindow(NULL); + + igRender(); + ImGui_ImplOpenGL3_RenderDrawData(igGetDrawData()); +} + +void imgui_init(void *data, int width, int height) +{ + ctx = igCreateContext(NULL); + io = igGetIO(); + + io->DisplaySize.x = width; + io->DisplaySize.y = height; + +#ifndef __EMSCRIPTEN__ + GLFWwindow *win = data; + ImGui_ImplGlfw_InitForOpenGL(win, true); + const char *glsl_version = "#version 410"; +#else + const char *glsl_version = "#version 300 es"; +#endif + + ImGui_ImplOpenGL3_Init(glsl_version); + + igStyleColorsDark(NULL); +} + +void imgui_done(void) +{ + ImGui_ImplOpenGL3_Shutdown(); +#ifndef __EMSCRIPTEN__ + ImGui_ImplGlfw_Shutdown(); +#endif + igDestroyContext(ctx); +} diff --git a/demo/ldjam56/onehandclap.c b/demo/ldjam56/onehandclap.c index b0af6c7..6852350 100644 --- a/demo/ldjam56/onehandclap.c +++ b/demo/ldjam56/onehandclap.c @@ -163,6 +163,7 @@ EMSCRIPTEN_KEEPALIVE void renderFrame(void *data) s->proj_updated = 0; models_render(&ui.mq, NULL, NULL, NULL, NULL, 0, 0, &count); + imgui_render(s->width, s->height); PROF_STEP(ui, models); s->frames_total += frame_count;