Skip to content

Commit

Permalink
Merge pull request #1166 from floooh/sokol-imgui-prefix
Browse files Browse the repository at this point in the history
sokol_imgui.h: allow to override the prefix for Dear ImGui C bindings
  • Loading branch information
floooh authored Dec 8, 2024
2 parents ce91d77 + de212eb commit a94f66a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 42 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
## Updates

### 08-Dec-2924

- sokol_imgui.h: when compiling the implementation in C mode, it is now possible
to define an alternative function prefix via the define SOKOL_IMGUI_CPREFIX
(with `ig` being the default prefix), this allows sokol_imgui.h to be
used with the default dear_bindings prefix (which is `ImGui_`), the
traditional cimgui.h prefix (`ig`) and it is also possible to not use
a prefix at all, which might be useful for language bindings.

See PR https://github.com/floooh/sokol/pull/1166 for details.

### 07-Dec-2024

- Changes in sokol_imgui.h and sokol_gfx_imgui.h for a switch of the Dear ImGui
Expand Down
81 changes: 39 additions & 42 deletions util/sokol_imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
If the implementation is compiled as C:
cimgui.h
When compiling as C, you can override the Dear ImGui C bindings prefix
via the define SOKOL_IMGUI_CPREFIX before including the sokol_imgui.h
implementation:
#define SOKOL_IMGUI_IMPL
#define SOKOL_IMGUI_CPREFIX ImGui_
#include "sokol_imgui.h"
Note that the default prefix is 'ig'.
FEATURE OVERVIEW:
=================
Expand Down Expand Up @@ -565,6 +575,13 @@ inline void simgui_create_fonts_texture(const simgui_font_tex_desc_t& desc) { re
#ifdef SOKOL_IMGUI_IMPL
#define SOKOL_IMGUI_IMPL_INCLUDED (1)

#ifndef SOKOL_IMGUI_CPREFIX
#define SOKOL_IMGUI_CPREFIX ig
#endif
#define _SIMGUI_CONCAT2(prefix, name) prefix ## name
#define _SIMGUI_CONCAT(prefix, name) _SIMGUI_CONCAT2(prefix, name)
#define _SIMGUI_CFUNC(name) _SIMGUI_CONCAT(SOKOL_IMGUI_CPREFIX, name)

#if defined(SOKOL_MALLOC) || defined(SOKOL_CALLOC) || defined(SOKOL_FREE)
#error "SOKOL_MALLOC/CALLOC/FREE macros are no longer supported, please use simgui_desc_t.allocator to override memory allocation functions"
#endif
Expand Down Expand Up @@ -2650,9 +2667,9 @@ SOKOL_API_IMPL void simgui_setup(const simgui_desc_t* desc) {
io->Fonts->AddFontDefault();
}
#else
igCreateContext(NULL);
igStyleColorsDark(igGetStyle());
ImGuiIO* io = igGetIO();
_SIMGUI_CFUNC(CreateContext)(NULL);
_SIMGUI_CFUNC(StyleColorsDark)(_SIMGUI_CFUNC(GetStyle)());
ImGuiIO* io = _SIMGUI_CFUNC(GetIO)();
if (!_simgui.desc.no_default_font) {
ImFontAtlas_AddFontDefault(io->Fonts, NULL);
}
Expand All @@ -2667,7 +2684,7 @@ SOKOL_API_IMPL void simgui_setup(const simgui_desc_t* desc) {
#if defined(__cplusplus)
ImGuiPlatformIO* pio = &ImGui::GetPlatformIO();
#else
ImGuiPlatformIO* pio = igGetPlatformIO();
ImGuiPlatformIO* pio = _SIMGUI_CFUNC(GetPlatformIO)();
#endif
pio->Platform_SetClipboardTextFn = _simgui_set_clipboard;
pio->Platform_GetClipboardTextFn = _simgui_get_clipboard;
Expand Down Expand Up @@ -2850,7 +2867,7 @@ SOKOL_API_IMPL void simgui_create_fonts_texture(const simgui_font_tex_desc_t* de
#if defined(__cplusplus)
ImGuiIO* io = &ImGui::GetIO();
#else
ImGuiIO* io = igGetIO();
ImGuiIO* io = _SIMGUI_CFUNC(GetIO)();
#endif

// a default font sampler
Expand Down Expand Up @@ -2904,7 +2921,7 @@ SOKOL_API_IMPL void simgui_shutdown(void) {
#if defined(__cplusplus)
ImGui::DestroyContext();
#else
igDestroyContext(0);
_SIMGUI_CFUNC(DestroyContext)(0);
#endif
// NOTE: it's valid to call the destroy funcs with SG_INVALID_ID
sg_destroy_pipeline(_simgui.pip_unfilterable);
Expand Down Expand Up @@ -2980,7 +2997,7 @@ SOKOL_API_IMPL void simgui_new_frame(const simgui_frame_desc_t* desc) {
#if defined(__cplusplus)
ImGuiIO* io = &ImGui::GetIO();
#else
ImGuiIO* io = igGetIO();
ImGuiIO* io = _SIMGUI_CFUNC(GetIO)();
#endif
if (!io->Fonts->TexReady) {
simgui_destroy_fonts_texture();
Expand All @@ -3002,7 +3019,7 @@ SOKOL_API_IMPL void simgui_new_frame(const simgui_frame_desc_t* desc) {
#if defined(__cplusplus)
ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
#else
ImGuiMouseCursor imgui_cursor = igGetMouseCursor();
ImGuiMouseCursor imgui_cursor = _SIMGUI_CFUNC(GetMouseCursor)();
#endif
sapp_mouse_cursor cursor = sapp_get_mouse_cursor();
switch (imgui_cursor) {
Expand All @@ -3023,7 +3040,7 @@ SOKOL_API_IMPL void simgui_new_frame(const simgui_frame_desc_t* desc) {
#if defined(__cplusplus)
ImGui::NewFrame();
#else
igNewFrame();
_SIMGUI_CFUNC(NewFrame)();
#endif
}

Expand Down Expand Up @@ -3054,9 +3071,9 @@ SOKOL_API_IMPL void simgui_render(void) {
ImDrawData* draw_data = ImGui::GetDrawData();
ImGuiIO* io = &ImGui::GetIO();
#else
igRender();
ImDrawData* draw_data = igGetDrawData();
ImGuiIO* io = igGetIO();
_SIMGUI_CFUNC(Render)();
ImDrawData* draw_data = _SIMGUI_CFUNC(GetDrawData)();
ImGuiIO* io = _SIMGUI_CFUNC(GetIO)();
#endif
if (0 == draw_data) {
return;
Expand Down Expand Up @@ -3207,7 +3224,7 @@ SOKOL_API_IMPL void simgui_add_focus_event(bool focus) {
ImGuiIO* io = &ImGui::GetIO();
io->AddFocusEvent(focus);
#else
ImGuiIO* io = igGetIO();
ImGuiIO* io = _SIMGUI_CFUNC(GetIO)();
ImGuiIO_AddFocusEvent(io, focus);
#endif
}
Expand All @@ -3216,15 +3233,11 @@ SOKOL_API_IMPL void simgui_add_mouse_pos_event(float x, float y) {
SOKOL_ASSERT(_SIMGUI_INIT_COOKIE == _simgui.init_cookie);
#if defined(__cplusplus)
ImGuiIO* io = &ImGui::GetIO();
#if (IMGUI_VERSION_NUM >= 18950)
io->AddMouseSourceEvent(ImGuiMouseSource_Mouse);
#endif
io->AddMousePosEvent(x, y);
#else
ImGuiIO* io = igGetIO();
#if (IMGUI_VERSION_NUM >= 18950)
ImGuiIO* io = _SIMGUI_CFUNC(GetIO)();
ImGuiIO_AddMouseSourceEvent(io, ImGuiMouseSource_Mouse);
#endif
ImGuiIO_AddMousePosEvent(io, x, y);
#endif
}
Expand All @@ -3233,15 +3246,11 @@ SOKOL_API_IMPL void simgui_add_touch_pos_event(float x, float y) {
SOKOL_ASSERT(_SIMGUI_INIT_COOKIE == _simgui.init_cookie);
#if defined(__cplusplus)
ImGuiIO* io = &ImGui::GetIO();
#if (IMGUI_VERSION_NUM >= 18950)
io->AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);
#endif
io->AddMousePosEvent(x, y);
#else
ImGuiIO* io = igGetIO();
#if (IMGUI_VERSION_NUM >= 18950)
ImGuiIO* io = _SIMGUI_CFUNC(GetIO)();
ImGuiIO_AddMouseSourceEvent(io, ImGuiMouseSource_TouchScreen);
#endif
ImGuiIO_AddMousePosEvent(io, x, y);
#endif
}
Expand All @@ -3250,15 +3259,11 @@ SOKOL_API_IMPL void simgui_add_mouse_button_event(int mouse_button, bool down) {
SOKOL_ASSERT(_SIMGUI_INIT_COOKIE == _simgui.init_cookie);
#if defined(__cplusplus)
ImGuiIO* io = &ImGui::GetIO();
#if (IMGUI_VERSION_NUM >= 18950)
io->AddMouseSourceEvent(ImGuiMouseSource_Mouse);
#endif
io->AddMouseButtonEvent(mouse_button, down);
#else
ImGuiIO* io = igGetIO();
#if (IMGUI_VERSION_NUM >= 18950)
ImGuiIO* io = _SIMGUI_CFUNC(GetIO)();
ImGuiIO_AddMouseSourceEvent(io, ImGuiMouseSource_Mouse);
#endif
ImGuiIO_AddMouseButtonEvent(io, mouse_button, down);
#endif
}
Expand All @@ -3267,15 +3272,11 @@ SOKOL_API_IMPL void simgui_add_touch_button_event(int mouse_button, bool down) {
SOKOL_ASSERT(_SIMGUI_INIT_COOKIE == _simgui.init_cookie);
#if defined(__cplusplus)
ImGuiIO* io = &ImGui::GetIO();
#if (IMGUI_VERSION_NUM >= 18950)
io->AddMouseSourceEvent(ImGuiMouseSource_TouchScreen);
#endif
io->AddMouseButtonEvent(mouse_button, down);
#else
ImGuiIO* io = igGetIO();
#if (IMGUI_VERSION_NUM >= 18950)
ImGuiIO* io = _SIMGUI_CFUNC(GetIO)();
ImGuiIO_AddMouseSourceEvent(io, ImGuiMouseSource_TouchScreen);
#endif
ImGuiIO_AddMouseButtonEvent(io, mouse_button, down);
#endif
}
Expand All @@ -3284,15 +3285,11 @@ SOKOL_API_IMPL void simgui_add_mouse_wheel_event(float wheel_x, float wheel_y) {
SOKOL_ASSERT(_SIMGUI_INIT_COOKIE == _simgui.init_cookie);
#if defined(__cplusplus)
ImGuiIO* io = &ImGui::GetIO();
#if (IMGUI_VERSION_NUM >= 18950)
io->AddMouseSourceEvent(ImGuiMouseSource_Mouse);
#endif
io->AddMouseWheelEvent(wheel_x, wheel_y);
#else
ImGuiIO* io = igGetIO();
#if (IMGUI_VERSION_NUM >= 18950)
ImGuiIO* io = _SIMGUI_CFUNC(GetIO)();
ImGuiIO_AddMouseSourceEvent(io, ImGuiMouseSource_Mouse);
#endif
ImGuiIO_AddMouseWheelEvent(io, wheel_x, wheel_y);
#endif
}
Expand All @@ -3303,7 +3300,7 @@ SOKOL_API_IMPL void simgui_add_key_event(int imgui_key, bool down) {
ImGuiIO* io = &ImGui::GetIO();
io->AddKeyEvent((ImGuiKey)imgui_key, down);
#else
ImGuiIO* io = igGetIO();
ImGuiIO* io = _SIMGUI_CFUNC(GetIO)();
ImGuiIO_AddKeyEvent(io, (ImGuiKey)imgui_key, down);
#endif
}
Expand All @@ -3314,7 +3311,7 @@ SOKOL_API_IMPL void simgui_add_input_character(uint32_t c) {
ImGuiIO* io = &ImGui::GetIO();
io->AddInputCharacter(c);
#else
ImGuiIO* io = igGetIO();
ImGuiIO* io = _SIMGUI_CFUNC(GetIO)();
ImGuiIO_AddInputCharacter(io, c);
#endif
}
Expand All @@ -3325,7 +3322,7 @@ SOKOL_API_IMPL void simgui_add_input_characters_utf8(const char* c) {
ImGuiIO* io = &ImGui::GetIO();
io->AddInputCharactersUTF8(c);
#else
ImGuiIO* io = igGetIO();
ImGuiIO* io = _SIMGUI_CFUNC(GetIO)();
ImGuiIO_AddInputCharactersUTF8(io, c);
#endif
}
Expand Down Expand Up @@ -3490,7 +3487,7 @@ SOKOL_API_IMPL bool simgui_handle_event(const sapp_event* ev) {
#if defined(__cplusplus)
ImGuiIO* io = &ImGui::GetIO();
#else
ImGuiIO* io = igGetIO();
ImGuiIO* io = _SIMGUI_CFUNC(GetIO)();
#endif
switch (ev->type) {
case SAPP_EVENTTYPE_FOCUSED:
Expand Down

0 comments on commit a94f66a

Please sign in to comment.