wip polymorphic modules

This commit is contained in:
David Allemang
2024-07-10 17:27:40 -04:00
parent 53cbe35a97
commit 0efc931006
8 changed files with 208 additions and 219 deletions

View File

@@ -13,61 +13,62 @@ pub const c = @cImport({
pub const Bus = @import("Bus.zig");
pub const Options = struct {
pub const Config = struct {
title: [*:0]const u8 = "Hello World",
width: u32 = 1280,
height: u32 = 720,
x11_class_name: [*:0]const u8 = "floating_window",
x11_instance_name: [*:0]const u8 = "floating_window",
unfocused_wait: f32 = 1.0 / 20.0,
};
const config: Config = nu.config.window;
var bus: Bus = undefined; // todo bus should probably move to engine.
pub var handle: *c.GLFWwindow = undefined;
var unfocused_rate: f32 = 1.0 / 20.0;
pub fn init(alloc: std.mem.Allocator) !void {
if (c.glfwInit() != c.GLFW_TRUE)
return error.glfwInitFailed;
errdefer c.glfwTerminate();
bus = Bus.init(alloc);
errdefer bus.deinit();
c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API);
c.glfwWindowHintString(c.GLFW_X11_CLASS_NAME, nu.options.window.x11_class_name);
c.glfwWindowHintString(c.GLFW_X11_INSTANCE_NAME, nu.options.window.x11_instance_name);
handle = c.glfwCreateWindow(
@intCast(nu.options.window.width),
@intCast(nu.options.window.height),
nu.options.window.title,
null,
null,
) orelse
return error.glfWCreateWindowFailed;
errdefer c.glfwDestroyWindow(handle);
bus.connect(handle);
errdefer bus.disconnect(handle);
pub fn driver() nu.Driver {
return nu.Driver{
.module = .{
.name = "Window",
.dependencies = &.{}, // todo bus
.setup = setup,
.teardown = teardown,
},
.next = next,
};
}
pub fn deinit() void {
bus.deinit();
pub var handle: *c.GLFWwindow = undefined;
pub fn setup(_: std.mem.Allocator) !void {
if (c.glfwInit() != c.GLFW_TRUE) std.debug.panic("GLFW Init Failed", .{});
c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API);
c.glfwWindowHintString(c.GLFW_X11_CLASS_NAME, config.x11_class_name);
c.glfwWindowHintString(c.GLFW_X11_INSTANCE_NAME, config.x11_instance_name);
handle = c.glfwCreateWindow(
@intCast(config.width),
@intCast(config.height),
config.title,
null,
null,
) orelse std.debug.panic("GLFW Create Window Failed", .{});
// bus.connect(handle);
// errdefer bus.disconnect(handle);
}
pub fn teardown() void {
c.glfwDestroyWindow(handle);
c.glfwTerminate();
}
pub fn next() ?[]Bus.Event {
bus.clear();
pub fn next() bool {
if (c.glfwWindowShouldClose(handle) == c.GLFW_TRUE)
return null;
return false;
if (c.glfwGetWindowAttrib(handle, c.GLFW_FOCUSED) == c.GLFW_TRUE) {
c.glfwPollEvents();
} else {
c.glfwWaitEventsTimeout(unfocused_rate);
c.glfwWaitEventsTimeout(config.unfocused_wait);
}
return bus.events.items;
return true;
}