Skip to content

Commit 8872eee

Browse files
committed
- Set extern "C" on IMGUI_IMPL_API so that exported backend functions aren't name-mangled
- Support loading the glfw backend via shared library - Clear the glfw backend's `g_ContextMap` allocation to avoid leak warnings - Fixup ImTextureID -> ImTextureRef - Fixup paddig field naming
1 parent 9c817dc commit 8872eee

File tree

5 files changed

+44
-24
lines changed

5 files changed

+44
-24
lines changed

build.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ pub fn build(b: *std.Build) void {
105105
if (options.shared) {
106106
if (target.result.os.tag == .windows) {
107107
imgui.root_module.addCMacro("IMGUI_API", "__declspec(dllexport)");
108+
imgui.root_module.addCMacro("IMGUI_IMPL_API", "extern \"C\" __declspec(dllexport)");
108109
imgui.root_module.addCMacro("IMPLOT_API", "__declspec(dllexport)");
109110
imgui.root_module.addCMacro("ZGUI_API", "__declspec(dllexport)");
111+
} else {
112+
imgui.root_module.addCMacro("IMGUI_IMPL_API", "extern \"C\"");
110113
}
111114

112115
if (target.result.os.tag == .macos) {

libs/imgui/backends/imgui_impl_glfw.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ void ImGui_ImplGlfw_Shutdown()
807807
io.BackendPlatformUserData = nullptr;
808808
io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport);
809809
ImGui_ImplGlfw_ContextMap_Remove(bd->Window);
810+
g_ContextMap.clear();
810811
IM_DELETE(bd);
811812
}
812813

src/backend_glfw.zig

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
const gui = @import("gui.zig");
2+
const options = @import("zgui_options");
23

34
// This call will install GLFW callbacks to handle GUI interactions.
45
// Those callbacks will chain-call user's previously installed callbacks, if any.
56
// This means that custom user's callbacks need to be installed *before* calling zgpu.gui.init().
67
pub fn init(
78
window: *const anyopaque, // zglfw.Window
89
) void {
10+
const ImGui_ImplGlfw_InitForOther = @extern(*const fn (window: *const anyopaque, install_callbacks: bool) callconv(.c) bool, .{
11+
.name = "ImGui_ImplGlfw_InitForOther",
12+
.is_dll_import = options.shared,
13+
});
14+
915
if (!ImGui_ImplGlfw_InitForOther(window, true)) {
1016
unreachable;
1117
}
@@ -14,6 +20,11 @@ pub fn init(
1420
pub fn initOpenGL(
1521
window: *const anyopaque, // zglfw.Window
1622
) void {
23+
const ImGui_ImplGlfw_InitForOpenGL = @extern(*const fn (window: *const anyopaque, install_callbacks: bool) callconv(.c) bool, .{
24+
.name = "ImGui_ImplGlfw_InitForOpenGL",
25+
.is_dll_import = options.shared,
26+
});
27+
1728
if (!ImGui_ImplGlfw_InitForOpenGL(window, true)) {
1829
unreachable;
1930
}
@@ -22,23 +33,28 @@ pub fn initOpenGL(
2233
pub fn initVulkan(
2334
window: *const anyopaque, // zglfw.Window
2435
) void {
36+
const ImGui_ImplGlfw_InitForVulkan = @extern(*const fn (window: *const anyopaque, install_callbacks: bool) callconv(.c) bool, .{
37+
.name = "ImGui_ImplGlfw_InitForVulkan",
38+
.is_dll_import = options.shared,
39+
});
40+
2541
if (!ImGui_ImplGlfw_InitForVulkan(window, true)) {
2642
unreachable;
2743
}
2844
}
2945

3046
pub fn deinit() void {
47+
const ImGui_ImplGlfw_Shutdown = @extern(*const fn () callconv(.c) void, .{
48+
.name = "ImGui_ImplGlfw_Shutdown",
49+
.is_dll_import = options.shared,
50+
});
3151
ImGui_ImplGlfw_Shutdown();
3252
}
3353

3454
pub fn newFrame() void {
55+
const ImGui_ImplGlfw_NewFrame = @extern(*const fn () callconv(.c) void, .{
56+
.name = "ImGui_ImplGlfw_NewFrame",
57+
.is_dll_import = options.shared,
58+
});
3559
ImGui_ImplGlfw_NewFrame();
3660
}
37-
38-
// Those functions are defined in `imgui_impl_glfw.cpp`
39-
// (they include few custom changes).
40-
extern fn ImGui_ImplGlfw_InitForOther(window: *const anyopaque, install_callbacks: bool) bool;
41-
extern fn ImGui_ImplGlfw_InitForOpenGL(window: *const anyopaque, install_callbacks: bool) bool;
42-
extern fn ImGui_ImplGlfw_InitForVulkan(window: *const anyopaque, install_callbacks: bool) bool;
43-
extern fn ImGui_ImplGlfw_NewFrame() void;
44-
extern fn ImGui_ImplGlfw_Shutdown() void;

src/gui.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub const ConfigFlags = packed struct(c_int) {
160160
user_storage: u4 = 0,
161161
is_srgb: bool = false,
162162
is_touch_screen: bool = false,
163-
_padding: u10 = 0,
163+
_padding2: u10 = 0,
164164
};
165165

166166
pub const BackendFlags = packed struct(c_int) {
@@ -169,11 +169,11 @@ pub const BackendFlags = packed struct(c_int) {
169169
has_set_mouse_pos: bool = false,
170170
renderer_has_vtx_offset: bool = false,
171171
renderer_has_textures: bool = false,
172-
_pading0: u5 = 0,
172+
_padding0: u5 = 0,
173173
platform_has_viewports: bool = false,
174174
has_mouse_hovered_viewports: bool = false,
175175
renderer_has_viewports: bool = false,
176-
_padding: u19 = 0,
176+
_padding1: u19 = 0,
177177
};
178178

179179
pub const FreeTypeLoaderFlags = packed struct(c_uint) {
@@ -3946,7 +3946,7 @@ pub fn beginDragDropSource(flags: DragDropFlags) bool {
39463946

39473947
/// Note: `payload_type` can be at most 32 characters long
39483948
pub fn setDragDropPayload(payload_type: [*:0]const u8, data: []const u8, cond: Condition) bool {
3949-
return zguiSetDragDropPayload(payload_type, @alignCast(@ptrCast(data.ptr)), data.len, cond);
3949+
return zguiSetDragDropPayload(payload_type, @ptrCast(@alignCast(data.ptr)), data.len, cond);
39503950
}
39513951
pub fn endDragDropSource() void {
39523952
zguiEndDragDropSource();

src/zgui.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,21 +1090,21 @@ extern "C"
10901090
}
10911091

10921092
ZGUI_API void zguiImage(
1093-
ImTextureID user_texture_id,
1093+
ImTextureRef user_texture_ref,
10941094
float w,
10951095
float h,
10961096
const float uv0[2],
10971097
const float uv1[2])
10981098
{
10991099
ImGui::Image(
1100-
user_texture_id,
1100+
user_texture_ref,
11011101
{w, h},
11021102
{uv0[0], uv0[1]},
11031103
{uv1[0], uv1[1]});
11041104
}
11051105

11061106
ZGUI_API void zguiImageWithBg(
1107-
ImTextureID user_texture_id,
1107+
ImTextureRef user_texture_ref,
11081108
float w,
11091109
float h,
11101110
const float uv0[2],
@@ -1113,7 +1113,7 @@ extern "C"
11131113
const float tint_col[4])
11141114
{
11151115
ImGui::ImageWithBg(
1116-
user_texture_id,
1116+
user_texture_ref,
11171117
{w, h},
11181118
{uv0[0], uv0[1]},
11191119
{uv1[0], uv1[1]},
@@ -1123,7 +1123,7 @@ extern "C"
11231123

11241124
ZGUI_API bool zguiImageButton(
11251125
const char *str_id,
1126-
ImTextureID user_texture_id,
1126+
ImTextureRef user_texture_ref,
11271127
float w,
11281128
float h,
11291129
const float uv0[2],
@@ -1133,7 +1133,7 @@ extern "C"
11331133
{
11341134
return ImGui::ImageButton(
11351135
str_id,
1136-
user_texture_id,
1136+
user_texture_ref,
11371137
{w, h},
11381138
{uv0[0], uv0[1]},
11391139
{uv1[0], uv1[1]},
@@ -2484,15 +2484,15 @@ extern "C"
24842484

24852485
ZGUI_API void zguiDrawList_AddImage(
24862486
ImDrawList *draw_list,
2487-
ImTextureID user_texture_id,
2487+
ImTextureRef user_texture_ref,
24882488
const float pmin[2],
24892489
const float pmax[2],
24902490
const float uvmin[2],
24912491
const float uvmax[2],
24922492
ImU32 col)
24932493
{
24942494
draw_list->AddImage(
2495-
user_texture_id,
2495+
user_texture_ref,
24962496
{pmin[0], pmin[1]},
24972497
{pmax[0], pmax[1]},
24982498
{uvmin[0], uvmin[1]},
@@ -2502,7 +2502,7 @@ extern "C"
25022502

25032503
ZGUI_API void zguiDrawList_AddImageQuad(
25042504
ImDrawList *draw_list,
2505-
ImTextureID user_texture_id,
2505+
ImTextureRef user_texture_ref,
25062506
const float p1[2],
25072507
const float p2[2],
25082508
const float p3[2],
@@ -2514,7 +2514,7 @@ extern "C"
25142514
ImU32 col)
25152515
{
25162516
draw_list->AddImageQuad(
2517-
user_texture_id,
2517+
user_texture_ref,
25182518
{p1[0], p1[1]},
25192519
{p2[0], p2[1]},
25202520
{p3[0], p3[1]},
@@ -2528,7 +2528,7 @@ extern "C"
25282528

25292529
ZGUI_API void zguiDrawList_AddImageRounded(
25302530
ImDrawList *draw_list,
2531-
ImTextureID user_texture_id,
2531+
ImTextureRef user_texture_ref,
25322532
const float pmin[2],
25332533
const float pmax[2],
25342534
const float uvmin[2],
@@ -2538,7 +2538,7 @@ extern "C"
25382538
ImDrawFlags flags)
25392539
{
25402540
draw_list->AddImageRounded(
2541-
user_texture_id,
2541+
user_texture_ref,
25422542
{pmin[0], pmin[1]},
25432543
{pmax[0], pmax[1]},
25442544
{uvmin[0], uvmin[1]},

0 commit comments

Comments
 (0)