Skip to content

Commit 872b36a

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 4f47a64 commit 872b36a

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
@@ -107,8 +107,11 @@ pub fn build(b: *std.Build) void {
107107
if (options.shared) {
108108
if (target.result.os.tag == .windows) {
109109
imgui.root_module.addCMacro("IMGUI_API", "__declspec(dllexport)");
110+
imgui.root_module.addCMacro("IMGUI_IMPL_API", "extern \"C\" __declspec(dllexport)");
110111
imgui.root_module.addCMacro("IMPLOT_API", "__declspec(dllexport)");
111112
imgui.root_module.addCMacro("ZGUI_API", "__declspec(dllexport)");
113+
} else {
114+
imgui.root_module.addCMacro("IMGUI_IMPL_API", "extern \"C\"");
112115
}
113116

114117
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
@@ -184,7 +184,7 @@ pub const ConfigFlags = packed struct(c_int) {
184184
user_storage: u4 = 0,
185185
is_srgb: bool = false,
186186
is_touch_screen: bool = false,
187-
_padding: u10 = 0,
187+
_padding2: u10 = 0,
188188
};
189189

190190
pub const BackendFlags = packed struct(c_int) {
@@ -193,11 +193,11 @@ pub const BackendFlags = packed struct(c_int) {
193193
has_set_mouse_pos: bool = false,
194194
renderer_has_vtx_offset: bool = false,
195195
renderer_has_textures: bool = false,
196-
_pading0: u5 = 0,
196+
_padding0: u5 = 0,
197197
platform_has_viewports: bool = false,
198198
has_mouse_hovered_viewports: bool = false,
199199
renderer_has_viewports: bool = false,
200-
_padding: u19 = 0,
200+
_padding1: u19 = 0,
201201
};
202202

203203
pub const FreeTypeLoaderFlags = packed struct(c_uint) {
@@ -4030,7 +4030,7 @@ pub fn beginDragDropSource(flags: DragDropFlags) bool {
40304030

40314031
/// Note: `payload_type` can be at most 32 characters long
40324032
pub fn setDragDropPayload(payload_type: [*:0]const u8, data: []const u8, cond: Condition) bool {
4033-
return zguiSetDragDropPayload(payload_type, @alignCast(@ptrCast(data.ptr)), data.len, cond);
4033+
return zguiSetDragDropPayload(payload_type, @ptrCast(@alignCast(data.ptr)), data.len, cond);
40344034
}
40354035
pub fn endDragDropSource() void {
40364036
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]},
@@ -2529,15 +2529,15 @@ extern "C"
25292529

25302530
ZGUI_API void zguiDrawList_AddImage(
25312531
ImDrawList *draw_list,
2532-
ImTextureID user_texture_id,
2532+
ImTextureRef user_texture_ref,
25332533
const float pmin[2],
25342534
const float pmax[2],
25352535
const float uvmin[2],
25362536
const float uvmax[2],
25372537
ImU32 col)
25382538
{
25392539
draw_list->AddImage(
2540-
user_texture_id,
2540+
user_texture_ref,
25412541
{pmin[0], pmin[1]},
25422542
{pmax[0], pmax[1]},
25432543
{uvmin[0], uvmin[1]},
@@ -2547,7 +2547,7 @@ extern "C"
25472547

25482548
ZGUI_API void zguiDrawList_AddImageQuad(
25492549
ImDrawList *draw_list,
2550-
ImTextureID user_texture_id,
2550+
ImTextureRef user_texture_ref,
25512551
const float p1[2],
25522552
const float p2[2],
25532553
const float p3[2],
@@ -2559,7 +2559,7 @@ extern "C"
25592559
ImU32 col)
25602560
{
25612561
draw_list->AddImageQuad(
2562-
user_texture_id,
2562+
user_texture_ref,
25632563
{p1[0], p1[1]},
25642564
{p2[0], p2[1]},
25652565
{p3[0], p3[1]},
@@ -2573,7 +2573,7 @@ extern "C"
25732573

25742574
ZGUI_API void zguiDrawList_AddImageRounded(
25752575
ImDrawList *draw_list,
2576-
ImTextureID user_texture_id,
2576+
ImTextureRef user_texture_ref,
25772577
const float pmin[2],
25782578
const float pmax[2],
25792579
const float uvmin[2],
@@ -2583,7 +2583,7 @@ extern "C"
25832583
ImDrawFlags flags)
25842584
{
25852585
draw_list->AddImageRounded(
2586-
user_texture_id,
2586+
user_texture_ref,
25872587
{pmin[0], pmin[1]},
25882588
{pmax[0], pmax[1]},
25892589
{uvmin[0], uvmin[1]},

0 commit comments

Comments
 (0)