Skip to content

Commit

Permalink
ui-imgui: Add basic initialization/deinitialization and renderning
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
virtuoso committed Oct 29, 2024
1 parent d08547c commit 634c0d8
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions core/clap.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "librarian.h"
#include "physics.h"
#include "settings.h"
#include "ui-debug.h"
#include "util.h"

#ifdef HAVE_ASAN
Expand Down
4 changes: 4 additions & 0 deletions core/display-glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//#include <GL/glew.h>
#include "display.h"
#include <GLFW/glfw3.h>
#include "ui-debug.h"
#include "common.h"
#include "input.h"
#include "input-joystick.h"
Expand Down Expand Up @@ -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);
}

Expand All @@ -191,6 +194,7 @@ void gl_main_loop(void)

void gl_done(void)
{
imgui_done();
glfwDestroyWindow(window);
glfwTerminate();
}
Expand Down
2 changes: 2 additions & 0 deletions core/display-www.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "common.h"
#include "display.h"
#include "input-joystick.h"
#include "ui-debug.h"

static int width, height;

Expand Down Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions core/ui-debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */
64 changes: 64 additions & 0 deletions core/ui-imgui.c
Original file line number Diff line number Diff line change
@@ -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 <GLFW/glfw3.h>
#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);
}
1 change: 1 addition & 0 deletions demo/ldjam56/onehandclap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 634c0d8

Please sign in to comment.