From 4d3d4e6ee6c50d35717615bf39acc1cf0a422c6d Mon Sep 17 00:00:00 2001 From: David Allemang Date: Tue, 9 Jul 2024 13:57:08 -0400 Subject: [PATCH] modules are global --- src/App.zig | 9 ++------- src/main.zig | 28 ++++++++++++++++------------ src/nu.zig | 2 +- src/nu/ImGui.zig | 12 ++---------- src/nu/Render.zig | 14 +++++++------- src/nu/Window.zig | 46 ++++++++++++++++++++-------------------------- 6 files changed, 48 insertions(+), 63 deletions(-) diff --git a/src/App.zig b/src/App.zig index 69ab7ca..c0bd5ce 100644 --- a/src/App.zig +++ b/src/App.zig @@ -3,14 +3,9 @@ const nu = @import("nu.zig"); const Self = @This(); -pub fn init(alloc: std.mem.Allocator, render: *nu.Render, imgui: *nu.ImGui) !Self { +pub fn init(alloc: std.mem.Allocator) !void { _ = alloc; - _ = render; - _ = imgui; - - return .{}; } -pub fn deinit(self: *Self) void { - _ = self; +pub fn deinit() void { } diff --git a/src/main.zig b/src/main.zig index 59eae20..a075d3e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -9,21 +9,25 @@ pub fn main() !void { defer _ = gpa.detectLeaks(); const alloc = gpa.allocator(); - var window = try nu.Window.init(alloc, .{ .title = "Hello World" }); - defer window.deinit(); + // todo declare or infer module dependencies, topological sort for init order + // problem: how to specify runtime options, like Window title? + // problem: where should gpa go? probably some "Engine" structure in nu.zig - var render = try nu.Render.init(alloc, &window); - defer render.deinit(); + try nu.Window.init(alloc, .{ .title = "Hello World" }); + defer nu.Window.deinit(); - var imgui = try nu.ImGui.init(alloc, &window, &render); - defer imgui.deinit(); + try nu.Render.init(alloc); + defer nu.Render.deinit(); - var app = try App.init(alloc, &render, &imgui); - defer app.deinit(); + try nu.ImGui.init(alloc); + defer nu.ImGui.deinit(); - try nu.run(&window, .{ - &app, - &imgui, - &render, + try App.init(alloc); + defer App.deinit(); + + try nu.run(nu.Window, .{ + App, + nu.ImGui, + nu.Render, }); } diff --git a/src/nu.zig b/src/nu.zig index 3bafb54..f3e7dbc 100644 --- a/src/nu.zig +++ b/src/nu.zig @@ -13,7 +13,7 @@ pub fn run( _ = events; inline for (modules) |module| { - if (std.meta.hasMethod(@TypeOf(module), "frame")) + if (@hasDecl(module, "frame")) module.frame(); } diff --git a/src/nu/ImGui.zig b/src/nu/ImGui.zig index 8dbdf6a..e5f5c61 100644 --- a/src/nu/ImGui.zig +++ b/src/nu/ImGui.zig @@ -2,19 +2,11 @@ const std = @import("std"); -const Self = @This(); - -const Window = @import("Window.zig"); const Render = @import("Render.zig"); -pub fn init(alloc: std.mem.Allocator, window: *Window, render: *Render) !Self { +pub fn init(alloc: std.mem.Allocator) !void { _ = alloc; - _ = window; - _ = render; - - return .{}; } -pub fn deinit(self: *Self) void { - _ = self; +pub fn deinit() void { } diff --git a/src/nu/Render.zig b/src/nu/Render.zig index 54dc41b..fa48ff7 100644 --- a/src/nu/Render.zig +++ b/src/nu/Render.zig @@ -2,22 +2,22 @@ const std = @import("std"); const Window = @import("Window.zig"); -const Self = @This(); - // isolate all the vulkan code through this path // except for imgui code // const au = @import("au.zig"); -pub fn init(alloc: std.mem.Allocator, window: *Window) !Self { +pub fn init(alloc: std.mem.Allocator) !void { _ = alloc; - _ = window; + + std.debug.print("Init Render\n", .{}); // todo check vulkan supported // todo create window surface - return .{}; } -pub fn deinit(self: *Self) void { - _ = self; +pub fn frame() void { + std.debug.print("frame\n", .{ }); } + +pub fn deinit() void {} diff --git a/src/nu/Window.zig b/src/nu/Window.zig index 52234ca..dd03900 100644 --- a/src/nu/Window.zig +++ b/src/nu/Window.zig @@ -1,5 +1,7 @@ //! GLFW Adaptor +// todo restructure to handle multiple windows + const std = @import("std"); pub const c = @cImport({ @@ -7,8 +9,6 @@ pub const c = @cImport({ @cInclude("GLFW/glfw3.h"); }); -const Self = @This(); - pub const Bus = @import("Bus.zig"); pub const Options = struct { @@ -19,25 +19,23 @@ pub const Options = struct { x11_instance_name: [*:0]const u8 = "floating_window", }; -alloc: std.mem.Allocator, -bus: *Bus, -handle: *c.GLFWwindow, +var bus: Bus = undefined; +var handle: *c.GLFWwindow = undefined; +var unfocused_rate: f32 = 1.0 / 20.0; -pub fn init(alloc: std.mem.Allocator, options: Options) !Self { +pub fn init(alloc: std.mem.Allocator, options: Options) !void { if (c.glfwInit() != c.GLFW_TRUE) return error.glfwInitFailed; errdefer c.glfwTerminate(); - const bus: *Bus = try alloc.create(Bus); - errdefer alloc.destroy(bus); - bus.* = Bus.init(alloc); + bus = Bus.init(alloc); errdefer bus.deinit(); c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API); c.glfwWindowHintString(c.GLFW_X11_CLASS_NAME, options.x11_class_name); c.glfwWindowHintString(c.GLFW_X11_INSTANCE_NAME, options.x11_instance_name); - const handle: *c.GLFWwindow = c.glfwCreateWindow( + handle = c.glfwCreateWindow( @intCast(options.width), @intCast(options.height), options.title, @@ -49,29 +47,25 @@ pub fn init(alloc: std.mem.Allocator, options: Options) !Self { bus.connect(handle); errdefer bus.disconnect(handle); - - return .{ - .alloc = alloc, - .bus = bus, - .handle = handle, - }; } -pub fn deinit(self: *Self) void { - self.bus.deinit(); - self.alloc.destroy(self.bus); - c.glfwDestroyWindow(self.handle); +pub fn deinit() void { + bus.deinit(); + c.glfwDestroyWindow(handle); c.glfwTerminate(); } -pub fn next(self: *Self) ?[]Bus.Event { - self.bus.clear(); +pub fn next() ?[]Bus.Event { + bus.clear(); - if (c.glfwWindowShouldClose(self.handle) == c.GLFW_TRUE) + if (c.glfwWindowShouldClose(handle) == c.GLFW_TRUE) return null; - // c.glfwPollEvents(); - c.glfwWaitEvents(); + if (c.glfwGetWindowAttrib(handle, c.GLFW_FOCUSED) == c.GLFW_TRUE) { + c.glfwPollEvents(); + } else { + c.glfwWaitEventsTimeout(unfocused_rate); + } - return self.bus.events.items; + return bus.events.items; }