WIP: event bus
This commit is contained in:
89
src/au.zig
89
src/au.zig
@@ -80,9 +80,13 @@ pub fn init(alloc: std.mem.Allocator) !void {
|
|||||||
|
|
||||||
try init_device(alloc);
|
try init_device(alloc);
|
||||||
errdefer deinit_device();
|
errdefer deinit_device();
|
||||||
|
|
||||||
|
try init_event_bus(alloc);
|
||||||
|
errdefer deinit_event_bus();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit() void {
|
pub fn deinit() void {
|
||||||
|
deinit_event_bus();
|
||||||
deinit_device();
|
deinit_device();
|
||||||
deinit_window();
|
deinit_window();
|
||||||
deinit_instance();
|
deinit_instance();
|
||||||
@@ -383,9 +387,11 @@ const Window = struct {
|
|||||||
|
|
||||||
handle: *c.GLFWwindow,
|
handle: *c.GLFWwindow,
|
||||||
surface: vk.SurfaceKHR,
|
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;
|
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_CLASS_NAME, "floating_window");
|
||||||
c.glfwWindowHintString(c.GLFW_X11_INSTANCE_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 {
|
pub fn deinit(self: Self) void {
|
||||||
|
self.events.deinit();
|
||||||
I.destroySurfaceKHR(self.surface, null);
|
I.destroySurfaceKHR(self.surface, null);
|
||||||
c.glfwDestroyWindow(self.handle);
|
c.glfwDestroyWindow(self.handle);
|
||||||
}
|
}
|
||||||
@@ -417,16 +424,88 @@ const Window = struct {
|
|||||||
return c.glfwWindowShouldClose(self.handle) == c.GLFW_TRUE;
|
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();
|
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();
|
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);
|
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);
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user