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();
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 {
}

View File

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

View File

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

View File

@@ -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 {
}

View File

@@ -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 {}

View File

@@ -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();
return self.bus.events.items;
if (c.glfwGetWindowAttrib(handle, c.GLFW_FOCUSED) == c.GLFW_TRUE) {
c.glfwPollEvents();
} else {
c.glfwWaitEventsTimeout(unfocused_rate);
}
return bus.events.items;
}