runtime hooks

This commit is contained in:
David Allemang
2024-07-10 13:06:44 -04:00
parent e297865e93
commit fd1bd9dbf5
6 changed files with 138 additions and 51 deletions

View File

@@ -6,6 +6,7 @@ const vk = @import("vk");
const nu = @import("../nu.zig");
const au = @import("Render/au.zig");
const Hook = @import("hooks.zig").Hook;
const Render = @import("Render.zig");
const Window = @import("Window.zig");
@@ -14,6 +15,11 @@ pub usingnamespace im;
pub const Options = struct {};
pub const Hooks = struct {
pub const Frame = Hook(fn () void);
frame: Frame,
};
pub fn loader_wrapper(procname: [*c]const u8, _: ?*anyopaque) callconv(.C) vk.PfnVoidFunction {
return au.glfwGetInstanceProcAddress(au.I.handle, procname);
}
@@ -21,7 +27,9 @@ pub fn loader_wrapper(procname: [*c]const u8, _: ?*anyopaque) callconv(.C) vk.Pf
var ctx: *im.ImGuiContext = undefined;
var descriptor_pool: vk.DescriptorPool = undefined;
pub fn init() !void {
pub var hooks: Hooks = undefined;
pub fn init(alloc: std.mem.Allocator) !void {
ctx = im.igCreateContext(null) orelse {
return error.igCreateContextFailed;
};
@@ -77,6 +85,24 @@ pub fn init() !void {
if (!im.impl.ImGui_ImplVulkan_CreateFontsTexture()) {
return error.igVulkanFontTextureFailed;
}
hooks = .{
.frame = Hooks.Frame.init(alloc),
};
errdefer hooks.frame.deinit();
}
pub fn deinit() void {
hooks.frame.deinit();
im.impl.ImGui_ImplVulkan_Shutdown();
au.D.destroyDescriptorPool(descriptor_pool, null);
im.impl.ImGui_ImplGlfw_Shutdown();
im.igDestroyContext(ctx);
}
pub fn connect() !void {
try nu.hooks.frame.register(nu_frame);
try Render.hooks.present.register(nu_render_present);
}
pub fn nu_frame() !void {
@@ -84,7 +110,7 @@ pub fn nu_frame() !void {
im.impl.ImGui_ImplVulkan_NewFrame();
im.igNewFrame();
try nu.invoke_hook("nu_imgui_frame", .{});
hooks.frame.invoke(.{});
im.igEndFrame();
im.igRender();
@@ -97,10 +123,3 @@ pub fn nu_render_present(cmd: au.CommandBufferProxy) void {
null,
);
}
pub fn deinit() void {
im.impl.ImGui_ImplVulkan_Shutdown();
au.D.destroyDescriptorPool(descriptor_pool, null);
im.impl.ImGui_ImplGlfw_Shutdown();
im.igDestroyContext(ctx);
}