modules are global

This commit is contained in:
David Allemang
2024-07-09 13:57:08 -04:00
parent 1269018e61
commit 4d3d4e6ee6
6 changed files with 48 additions and 63 deletions

View File

@@ -3,14 +3,9 @@ const nu = @import("nu.zig");
const Self = @This(); 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; _ = alloc;
_ = render;
_ = imgui;
return .{};
} }
pub fn deinit(self: *Self) void { pub fn deinit() void {
_ = self;
} }

View File

@@ -9,21 +9,25 @@ pub fn main() !void {
defer _ = gpa.detectLeaks(); defer _ = gpa.detectLeaks();
const alloc = gpa.allocator(); const alloc = gpa.allocator();
var window = try nu.Window.init(alloc, .{ .title = "Hello World" }); // todo declare or infer module dependencies, topological sort for init order
defer window.deinit(); // 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); try nu.Window.init(alloc, .{ .title = "Hello World" });
defer render.deinit(); defer nu.Window.deinit();
var imgui = try nu.ImGui.init(alloc, &window, &render); try nu.Render.init(alloc);
defer imgui.deinit(); defer nu.Render.deinit();
var app = try App.init(alloc, &render, &imgui); try nu.ImGui.init(alloc);
defer app.deinit(); defer nu.ImGui.deinit();
try nu.run(&window, .{ try App.init(alloc);
&app, defer App.deinit();
&imgui,
&render, try nu.run(nu.Window, .{
App,
nu.ImGui,
nu.Render,
}); });
} }

View File

@@ -13,7 +13,7 @@ pub fn run(
_ = events; _ = events;
inline for (modules) |module| { inline for (modules) |module| {
if (std.meta.hasMethod(@TypeOf(module), "frame")) if (@hasDecl(module, "frame"))
module.frame(); module.frame();
} }

View File

@@ -2,19 +2,11 @@
const std = @import("std"); const std = @import("std");
const Self = @This();
const Window = @import("Window.zig");
const Render = @import("Render.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; _ = alloc;
_ = window;
_ = render;
return .{};
} }
pub fn deinit(self: *Self) void { pub fn deinit() void {
_ = self;
} }

View File

@@ -2,22 +2,22 @@ const std = @import("std");
const Window = @import("Window.zig"); const Window = @import("Window.zig");
const Self = @This();
// isolate all the vulkan code through this path // isolate all the vulkan code through this path
// except for imgui code // except for imgui code
// const au = @import("au.zig"); // const au = @import("au.zig");
pub fn init(alloc: std.mem.Allocator, window: *Window) !Self { pub fn init(alloc: std.mem.Allocator) !void {
_ = alloc; _ = alloc;
_ = window;
std.debug.print("Init Render\n", .{});
// todo check vulkan supported // todo check vulkan supported
// todo create window surface // todo create window surface
return .{};
} }
pub fn deinit(self: *Self) void { pub fn frame() void {
_ = self; std.debug.print("frame\n", .{ });
} }
pub fn deinit() void {}

View File

@@ -1,5 +1,7 @@
//! GLFW Adaptor //! GLFW Adaptor
// todo restructure to handle multiple windows
const std = @import("std"); const std = @import("std");
pub const c = @cImport({ pub const c = @cImport({
@@ -7,8 +9,6 @@ pub const c = @cImport({
@cInclude("GLFW/glfw3.h"); @cInclude("GLFW/glfw3.h");
}); });
const Self = @This();
pub const Bus = @import("Bus.zig"); pub const Bus = @import("Bus.zig");
pub const Options = struct { pub const Options = struct {
@@ -19,25 +19,23 @@ pub const Options = struct {
x11_instance_name: [*:0]const u8 = "floating_window", x11_instance_name: [*:0]const u8 = "floating_window",
}; };
alloc: std.mem.Allocator, var bus: Bus = undefined;
bus: *Bus, var handle: *c.GLFWwindow = undefined;
handle: *c.GLFWwindow, 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) if (c.glfwInit() != c.GLFW_TRUE)
return error.glfwInitFailed; return error.glfwInitFailed;
errdefer c.glfwTerminate(); errdefer c.glfwTerminate();
const bus: *Bus = try alloc.create(Bus); bus = Bus.init(alloc);
errdefer alloc.destroy(bus);
bus.* = Bus.init(alloc);
errdefer bus.deinit(); errdefer bus.deinit();
c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API); 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_CLASS_NAME, options.x11_class_name);
c.glfwWindowHintString(c.GLFW_X11_INSTANCE_NAME, options.x11_instance_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.width),
@intCast(options.height), @intCast(options.height),
options.title, options.title,
@@ -49,29 +47,25 @@ pub fn init(alloc: std.mem.Allocator, options: Options) !Self {
bus.connect(handle); bus.connect(handle);
errdefer bus.disconnect(handle); errdefer bus.disconnect(handle);
return .{
.alloc = alloc,
.bus = bus,
.handle = handle,
};
} }
pub fn deinit(self: *Self) void { pub fn deinit() void {
self.bus.deinit(); bus.deinit();
self.alloc.destroy(self.bus); c.glfwDestroyWindow(handle);
c.glfwDestroyWindow(self.handle);
c.glfwTerminate(); c.glfwTerminate();
} }
pub fn next(self: *Self) ?[]Bus.Event { pub fn next() ?[]Bus.Event {
self.bus.clear(); bus.clear();
if (c.glfwWindowShouldClose(self.handle) == c.GLFW_TRUE) if (c.glfwWindowShouldClose(handle) == c.GLFW_TRUE)
return null; return null;
// c.glfwPollEvents(); if (c.glfwGetWindowAttrib(handle, c.GLFW_FOCUSED) == c.GLFW_TRUE) {
c.glfwWaitEvents(); c.glfwPollEvents();
} else {
c.glfwWaitEventsTimeout(unfocused_rate);
}
return self.bus.events.items; return bus.events.items;
} }