From b5eb8fb5daaea466968e09867c65a4cf59229e91 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Mon, 15 Jul 2024 17:14:24 -0400 Subject: [PATCH] misc cleanup --- src/App.zig | 22 ---------- src/main.zig | 108 ++++++++++++++++++++++++++++++---------------- src/nu.zig | 11 ++++- src/nu/ImGui.zig | 3 -- src/nu/Render.zig | 4 +- 5 files changed, 81 insertions(+), 67 deletions(-) delete mode 100644 src/App.zig diff --git a/src/App.zig b/src/App.zig deleted file mode 100644 index a2b0349..0000000 --- a/src/App.zig +++ /dev/null @@ -1,22 +0,0 @@ -const std = @import("std"); -const nu = @import("nu.zig"); - -const Bus = @import("nu/Bus.zig"); -const Render = @import("nu/Render.zig"); -const ImGui = @import("nu/ImGui.zig"); - -pub const depends = .{ ImGui, Render }; - -pub fn setup(_: std.mem.Allocator) !void {} - -pub fn teardown() void {} - -pub fn update() !void {} - -pub fn frame() !void { - ImGui.igShowMetricsWindow(null); -} - -// pub fn gui() void { -// ImGui.igShowMetricsWindow(null); -// } diff --git a/src/main.zig b/src/main.zig index 92139b5..ef9b065 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,7 +1,11 @@ const std = @import("std"); const nu = @import("nu.zig"); -const App = @import("App.zig"); +pub const nu_modules = .{ + App, + // UI, +}; +pub const main = nu.main; pub const nu_options: nu.Options = .{ .window = .{ .title = "Hello World" }, @@ -11,44 +15,72 @@ pub const nu_options: nu.Options = .{ }, }; -// todo declare or infer module dependencies, topological sort for init order. clean up "init" lines in main. -// -// problem: where should gpa go? probably some "Engine" structure in nu.zig -// -// don't necessarily need to declare topological sort - depth-first traversal -// of each module's dependencies without repeats would do. -// -// idea - use a structure like std.Build.Step where the polymorphic part is a -// component of the larger structure. +pub const UI = struct { + const im = nu.ImGui; -pub const nu_driver = nu.Window; -pub const nu_present = nu.Render; -pub const nu_modules = .{ - nu.ImGui, - App, + pub const depends = .{im}; + + var color: @Vector(4, f32) = @splat(1); + + pub fn setup(_: std.mem.Allocator) !void { + const io: *nu.ImGui.ImGuiIO = @ptrCast(nu.ImGui.igGetIO()); + io.ConfigFlags |= nu.ImGui.ImGuiConfigFlags_DockingEnable; + } + + pub fn frame() !void { + nu.ImGui.igShowMetricsWindow(null); + + { + const viewport = im.igGetMainViewport(); + im.igSetNextWindowPos(viewport.*.WorkPos, 0, .{ .x = 0, .y = 0 }); + im.igSetNextWindowSize(viewport.*.WorkSize, 0); + im.igSetNextWindowViewport(viewport.*.ID); + im.igPushStyleVar_Float(im.ImGuiStyleVar_WindowRounding, 0); + im.igPushStyleVar_Float(im.ImGuiStyleVar_WindowBorderSize, 0); + im.igPushStyleVar_Vec2(im.ImGuiStyleVar_WindowPadding, .{ .x = 0, .y = 0 }); + defer im.igPopStyleVar(3); + + const window_flags = + im.ImGuiWindowFlags_MenuBar | + im.ImGuiWindowFlags_NoDocking | + im.ImGuiWindowFlags_NoTitleBar | + im.ImGuiWindowFlags_NoCollapse | + im.ImGuiWindowFlags_NoResize | + im.ImGuiWindowFlags_NoMove | + im.ImGuiWindowFlags_NoBringToFrontOnFocus | + im.ImGuiWindowFlags_NoNavFocus | + im.ImGuiWindowFlags_NoBackground; + + const dock_flags = + im.ImGuiDockNodeFlags_PassthruCentralNode | + im.ImGuiDockNodeFlags_NoDockingOverCentralNode; + + _ = im.igBegin("Main Dockspace", null, window_flags); + const id = im.igGetID_Str("maindockspace"); + _ = im.igDockSpace(id, .{ .x = 0, .y = 0 }, dock_flags, null); + im.igEnd(); + } + + if (nu.ImGui.igBegin("Color", null, nu.ImGui.ImGuiWindowFlags_None)) { + if (nu.ImGui.igColorEdit4("color", @ptrCast(&color), nu.ImGui.ImGuiColorEditFlags_AlphaPreviewHalf)) {} + } + nu.ImGui.igEnd(); + } }; -pub const main = nu.main; +const App = struct { + const vk = @import("vk"); + const au = @import("nu/Render/au.zig"); -// pub fn main() !void { -// var gpa = std.heap.GeneralPurposeAllocator(.{}){}; -// defer _ = gpa.detectLeaks(); -// const alloc = gpa.allocator(); -// -// nu.init(alloc); -// defer nu.deinit(); -// -// try nu.Window.init(alloc); -// defer nu.Window.deinit(); -// -// try nu.Render.init(alloc); -// defer nu.Render.deinit(); -// -// try nu.ImGui.init(alloc); -// defer nu.ImGui.deinit(); -// -// try App.init(alloc); -// defer App.deinit(); -// -// try nu.run(alloc); -// } + pub const depends = .{nu.Render}; + + // todo timeline semaphore + + pub fn setup(_: std.mem.Allocator) !void {} + + pub fn teardown() void {} + + pub fn frame() !void {} + + pub fn present(_: au.CommandBufferProxy) void {} +}; diff --git a/src/nu.zig b/src/nu.zig index d0df829..b94a17a 100644 --- a/src/nu.zig +++ b/src/nu.zig @@ -14,16 +14,23 @@ const Config = struct { }; pub const config: Config = if (@hasDecl(root, "nu_config")) root.nu_config else .{}; +pub const engine = Engine(Window, Render, root.nu_modules); + +// Hooks: setup, teardown, fixed, frame, present pub fn main() void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const alloc = gpa.allocator(); - const engine = Engine(root.nu_driver, root.nu_present, root.nu_modules); + std.debug.print("Engine modules: ", .{}); + inline for (engine.modules) |module| std.debug.print("{s} ", .{@typeName(module)}); + std.debug.print("\n", .{}); engine.try_invoke("setup", .{alloc}) catch @panic("Startup failed"); defer engine.rinvoke("teardown", .{}); + std.debug.print("Setup complete.\n", .{}); + while (engine.next()) { engine.try_invoke("fixed", .{}) catch @panic("fixed failed!"); engine.try_invoke("frame", .{}) catch @panic("frame failed!"); @@ -41,7 +48,7 @@ pub fn Engine(comptime D: type, comptime R: type, comptime M: anytype) type { } pub fn render() !void { - return R.render(@This()); + return R.render(); } pub fn try_invoke(comptime name: []const u8, args: anytype) !void { diff --git a/src/nu/ImGui.zig b/src/nu/ImGui.zig index a65b513..a7d5f72 100644 --- a/src/nu/ImGui.zig +++ b/src/nu/ImGui.zig @@ -26,8 +26,6 @@ var ctx: *im.ImGuiContext = undefined; var descriptor_pool: vk.DescriptorPool = undefined; pub fn setup(_: std.mem.Allocator) !void { - // try Render.present.register(present); - ctx = im.igCreateContext(null) orelse { return error.igCreateContextFailed; }; @@ -87,7 +85,6 @@ pub fn setup(_: std.mem.Allocator) !void { pub fn teardown() void { au.D.deviceWaitIdle() catch |err| std.debug.panic("Device wait failed: {!}", .{err}); - im.impl.ImGui_ImplVulkan_Shutdown(); au.D.destroyDescriptorPool(descriptor_pool, null); im.impl.ImGui_ImplGlfw_Shutdown(); diff --git a/src/nu/Render.zig b/src/nu/Render.zig index 3a6694f..ec49598 100644 --- a/src/nu/Render.zig +++ b/src/nu/Render.zig @@ -56,7 +56,7 @@ pub fn teardown() void { au.deinit(); } -pub fn render(engine: anytype) !void { +pub fn render() !void { const flight: au.Flights.Flight = flights.next(); try flight.wait(); @@ -84,7 +84,7 @@ pub fn render(engine: anytype) !void { // todo manage frame in flight state for each hook; pass the current flight in as context. - engine.invoke("present", .{cmd}); + nu.engine.invoke("present", .{cmd}); target.end_rendering(cmd); try cmd.endCommandBuffer();