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

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