WIP: event bus

This commit is contained in:
David Allemang
2024-06-27 09:26:53 -04:00
parent 9d99902b42
commit 181f29970e

View File

@@ -80,9 +80,13 @@ pub fn init(alloc: std.mem.Allocator) !void {
try init_device(alloc);
errdefer deinit_device();
try init_event_bus(alloc);
errdefer deinit_event_bus();
}
pub fn deinit() void {
deinit_event_bus();
deinit_device();
deinit_window();
deinit_instance();
@@ -383,9 +387,11 @@ const Window = struct {
handle: *c.GLFWwindow,
surface: vk.SurfaceKHR,
events: std.ArrayList(Event),
pub fn init(title: [*:0]const u8, extent: vk.Extent2D) !Self {
pub fn init(alloc: std.mem.Allocator, title: [*:0]const u8, extent: vk.Extent2D) !Self {
var self: Self = undefined;
self.events = std.ArrayList(Event).init(alloc);
c.glfwWindowHintString(c.GLFW_X11_CLASS_NAME, "floating_window");
c.glfwWindowHintString(c.GLFW_X11_INSTANCE_NAME, "floating_window");
@@ -409,6 +415,7 @@ const Window = struct {
}
pub fn deinit(self: Self) void {
self.events.deinit();
I.destroySurfaceKHR(self.surface, null);
c.glfwDestroyWindow(self.handle);
}
@@ -417,16 +424,88 @@ const Window = struct {
return c.glfwWindowShouldClose(self.handle) == c.GLFW_TRUE;
}
pub fn wait_events(_: Self) void {
pub fn wait_events(self: Self, events: std.ArrayList(Event)) void {
c.glfwWaitEvents();
// todo events as values? push into a buffer and return here?
events.clearRetainingCapacity();
events.appendSlice(self.events.items);
self.events.clearRetainingCapacity();
}
pub fn poll_events(_: Self) void {
pub fn poll_events(self: Self, events: *std.ArrayList(Event)) void {
c.glfwPollEvents();
events.clearRetainingCapacity();
events.appendSlice(self.events.items);
self.events.clearRetainingCapacity();
}
pub fn wait_events_timeout(seconds: f64) void {
pub fn wait_events_timeout(seconds: f64, events: *std.ArrayList(Event)) void {
c.glfwWaitEventsTimeout(seconds);
}
};
pub fn onCharMods(window: ?*c.GLFWwindow, code: u32, mods: i32) callconv(.C) void {
const w: *Window =
@ptrCast(c.glfwGetWindowUserPointer(window));
w.events.append(.{
.charMods = .{
.codepoint = @intCast(code),
.mods = mods,
},
});
}
pub fn onMouseButton(window: ?*c.GLFWwindow, btn: c_int, action: c_int, mods: c_int) callconv(.C) void {
const w: *Window =
@ptrCast(c.glfwGetWindowUserPointer(window));
w.events.append(.{
.mouseButton = .{
.btn = btn,
.action = action == c.GLFW_PRESS,
.mods = mods,
},
});
}
const Event = union(enum) {
charMods: struct {
codepoint: u21,
mods: i32,
},
mouseButton: struct {
btn: u32, // todo enum
action: bool, // todo enum
mods: u32,
},
};
// pub const GLFWwindowposfun = ?*const fn (?*GLFWwindow, c_int, c_int) callconv(.C) void;
// pub const GLFWwindowsizefun = ?*const fn (?*GLFWwindow, c_int, c_int) callconv(.C) void;
// pub const GLFWwindowclosefun = ?*const fn (?*GLFWwindow) callconv(.C) void;
// pub const GLFWwindowrefreshfun = ?*const fn (?*GLFWwindow) callconv(.C) void;
// pub const GLFWwindowfocusfun = ?*const fn (?*GLFWwindow, c_int) callconv(.C) void;
// pub const GLFWwindowiconifyfun = ?*const fn (?*GLFWwindow, c_int) callconv(.C) void;
// pub const GLFWwindowmaximizefun = ?*const fn (?*GLFWwindow, c_int) callconv(.C) void;
// pub const GLFWframebuffersizefun = ?*const fn (?*GLFWwindow, c_int, c_int) callconv(.C) void;
// pub const GLFWwindowcontentscalefun = ?*const fn (?*GLFWwindow, f32, f32) callconv(.C) void;
// pub const GLFWmousebuttonfun = ?*const fn (?*GLFWwindow, c_int, c_int, c_int) callconv(.C) void;
// pub const GLFWcursorposfun = ?*const fn (?*GLFWwindow, f64, f64) callconv(.C) void;
// pub const GLFWcursorenterfun = ?*const fn (?*GLFWwindow, c_int) callconv(.C) void;
// pub const GLFWscrollfun = ?*const fn (?*GLFWwindow, f64, f64) callconv(.C) void;
// pub const GLFWkeyfun = ?*const fn (?*GLFWwindow, c_int, c_int, c_int, c_int) callconv(.C) void;
// pub const GLFWcharfun = ?*const fn (?*GLFWwindow, c_uint) callconv(.C) void;
// pub const GLFWcharmodsfun = ?*const fn (?*GLFWwindow, c_uint, c_int) callconv(.C) void;
// pub const GLFWdropfun = ?*const fn (?*GLFWwindow, c_int, [*c][*c]const u8) callconv(.C) void;
// pub const GLFWmonitorfun = ?*const fn (?*GLFWmonitor, c_int) callconv(.C) void;
// pub const GLFWjoystickfun = ?*const fn (c_int, c_int) callconv(.C) void;
fn init_event_bus() !void {
c.glfwSetWindowUserPointer(W.handle, W);
_ = c.glfwSetCharModsCallback(W.handle, onCharMods);
_ = c.glfwSetMouseButtonCallback(W.handle, onMouseButton);
}
fn deinit_event_bus() void {
_ = c.glfwSetCharModsCallback(W.handle, null);
_ = c.glfwSetMouseButtonCallback(W.handle, null);
c.glfwSetWindowUserPointer(W.handle, null);
}