misc cleanup

This commit is contained in:
David Allemang
2024-07-15 17:14:24 -04:00
parent 8c3e65fced
commit b5eb8fb5da
5 changed files with 81 additions and 67 deletions

View File

@@ -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);
// }

View File

@@ -1,7 +1,11 @@
const std = @import("std"); const std = @import("std");
const nu = @import("nu.zig"); 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 = .{ pub const nu_options: nu.Options = .{
.window = .{ .title = "Hello World" }, .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. pub const UI = struct {
// const im = nu.ImGui;
// 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 nu_driver = nu.Window; pub const depends = .{im};
pub const nu_present = nu.Render;
pub const nu_modules = .{ var color: @Vector(4, f32) = @splat(1);
nu.ImGui,
App, 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 { pub const depends = .{nu.Render};
// var gpa = std.heap.GeneralPurposeAllocator(.{}){};
// defer _ = gpa.detectLeaks(); // todo timeline semaphore
// const alloc = gpa.allocator();
// pub fn setup(_: std.mem.Allocator) !void {}
// nu.init(alloc);
// defer nu.deinit(); pub fn teardown() void {}
//
// try nu.Window.init(alloc); pub fn frame() !void {}
// defer nu.Window.deinit();
// pub fn present(_: au.CommandBufferProxy) void {}
// 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);
// }

View File

@@ -14,16 +14,23 @@ const Config = struct {
}; };
pub const config: Config = if (@hasDecl(root, "nu_config")) root.nu_config else .{}; 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 { pub fn main() void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator(); 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"); engine.try_invoke("setup", .{alloc}) catch @panic("Startup failed");
defer engine.rinvoke("teardown", .{}); defer engine.rinvoke("teardown", .{});
std.debug.print("Setup complete.\n", .{});
while (engine.next()) { while (engine.next()) {
engine.try_invoke("fixed", .{}) catch @panic("fixed failed!"); engine.try_invoke("fixed", .{}) catch @panic("fixed failed!");
engine.try_invoke("frame", .{}) catch @panic("frame 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 { pub fn render() !void {
return R.render(@This()); return R.render();
} }
pub fn try_invoke(comptime name: []const u8, args: anytype) !void { pub fn try_invoke(comptime name: []const u8, args: anytype) !void {

View File

@@ -26,8 +26,6 @@ var ctx: *im.ImGuiContext = undefined;
var descriptor_pool: vk.DescriptorPool = undefined; var descriptor_pool: vk.DescriptorPool = undefined;
pub fn setup(_: std.mem.Allocator) !void { pub fn setup(_: std.mem.Allocator) !void {
// try Render.present.register(present);
ctx = im.igCreateContext(null) orelse { ctx = im.igCreateContext(null) orelse {
return error.igCreateContextFailed; return error.igCreateContextFailed;
}; };
@@ -87,7 +85,6 @@ pub fn setup(_: std.mem.Allocator) !void {
pub fn teardown() void { pub fn teardown() void {
au.D.deviceWaitIdle() catch |err| std.debug.panic("Device wait failed: {!}", .{err}); au.D.deviceWaitIdle() catch |err| std.debug.panic("Device wait failed: {!}", .{err});
im.impl.ImGui_ImplVulkan_Shutdown(); im.impl.ImGui_ImplVulkan_Shutdown();
au.D.destroyDescriptorPool(descriptor_pool, null); au.D.destroyDescriptorPool(descriptor_pool, null);
im.impl.ImGui_ImplGlfw_Shutdown(); im.impl.ImGui_ImplGlfw_Shutdown();

View File

@@ -56,7 +56,7 @@ pub fn teardown() void {
au.deinit(); au.deinit();
} }
pub fn render(engine: anytype) !void { pub fn render() !void {
const flight: au.Flights.Flight = flights.next(); const flight: au.Flights.Flight = flights.next();
try flight.wait(); 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. // 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); target.end_rendering(cmd);
try cmd.endCommandBuffer(); try cmd.endCommandBuffer();