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 b5bfb76..1182ebe 100644 --- a/core/ui-debug.h +++ b/core/ui-debug.h @@ -4,5 +4,8 @@ void __ui_debug_printf(const char *mod, const char *fmt, ...); #define ui_debug_printf(args...) __ui_debug_printf(MODNAME, ## args) +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..987c9e8 --- /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;