Skip to content

Commit 068c563

Browse files
authored
Merge pull request #62 from mjakeman/issue/expose-context-api
2 parents d6c7792 + d132f6c commit 068c563

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/gui.zig

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ pub fn init(allocator: std.mem.Allocator) void {
5959
}
6060
}
6161
}
62+
/// Allows sharing a context across static/DLL boundaries. This is useful for
63+
/// hot-reloading mechanisms which rely on shared libraries.
64+
/// See "CONTEXT AND MEMORY ALLOCATORS" section of ImGui docs.
65+
pub fn initWithExistingContext(allocator: std.mem.Allocator, ctx: Context) void {
66+
mem_allocator = allocator;
67+
mem_allocations = std.AutoHashMap(usize, usize).init(allocator);
68+
mem_allocations.?.ensureTotalCapacity(32) catch @panic("zgui: out of memory");
69+
zguiSetAllocatorFunctions(zguiMemAlloc, zguiMemFree);
70+
71+
zguiSetCurrentContext(ctx);
72+
73+
temp_buffer = std.ArrayList(u8).init(allocator);
74+
temp_buffer.?.resize(3 * 1024 + 1) catch unreachable;
75+
76+
if (te_enabled) {
77+
te.init();
78+
}
79+
}
80+
pub fn getCurrentContext() ?Context {
81+
return zguiGetCurrentContext();
82+
}
6283
pub fn deinit() void {
6384
if (zguiGetCurrentContext() != null) {
6485
temp_buffer.?.deinit();
@@ -104,6 +125,7 @@ pub fn deinitNoContext() void {
104125
extern fn zguiCreateContext(shared_font_atlas: ?*const anyopaque) Context;
105126
extern fn zguiDestroyContext(ctx: ?Context) void;
106127
extern fn zguiGetCurrentContext() ?Context;
128+
extern fn zguiSetCurrentContext(ctx: ?Context) void;
107129
//--------------------------------------------------------------------------------------------------
108130
var mem_allocator: ?std.mem.Allocator = null;
109131
var mem_allocations: ?std.AutoHashMap(usize, usize) = null;
@@ -405,7 +427,7 @@ pub fn getClipboardText() [:0]const u8 {
405427
extern fn zguiSetClipboardText(text: [*:0]const u8) void;
406428
extern fn zguiGetClipboardText() [*:0]const u8;
407429
//--------------------------------------------------------------------------------------------------
408-
const Context = *opaque {};
430+
pub const Context = *opaque {};
409431
pub const DrawData = *extern struct {
410432
valid: bool,
411433
cmd_lists_count: c_int,

0 commit comments

Comments
 (0)