the rest of the events

This commit is contained in:
David Allemang
2024-06-27 11:34:05 -04:00
parent 1eaf98973b
commit 07c96af5d7
3 changed files with 270 additions and 98 deletions

View File

@@ -4,6 +4,8 @@ const builtin = @import("builtin");
const vk = @import("vk");
const c = @import("c.zig");
const EventBus = @import("au/EventBus.zig");
pub const use_debug_messenger = switch (builtin.mode) {
.Debug, .ReleaseSafe => true,
.ReleaseSmall, .ReleaseFast => false,
@@ -47,7 +49,6 @@ pub const B: *const BaseWrapper = &_bw;
pub const I: *const InstanceProxy = &_ip;
pub const D: *const DeviceProxy = &_dp;
pub const W: *const Window = &_window;
pub const E: *const EventBus = &_events;
pub const Q: *const QueueProxy = &_qp;
pub const device_config: *const CandidateDeviceInfo = &_dconfig;
@@ -385,7 +386,7 @@ pub fn debug_callback(
return vk.FALSE;
}
const Window = struct {
pub const Window = struct {
const Self = @This();
alloc: std.mem.Allocator,
@@ -427,110 +428,24 @@ const Window = struct {
}
};
pub fn wait_events() void {
pub fn wait_events() []const EventBus.Event {
_events.clear();
c.glfwWaitEvents();
return _events.events.items;
}
pub fn poll_events() void {
pub fn poll_events() []const EventBus.Event {
_events.clear();
c.glfwPollEvents();
return _events.events.items;
}
pub fn wait_events_timeout(seconds: f64) void {
pub fn wait_events_timeout(seconds: f64) []const EventBus.Event {
_events.clear();
c.glfwWaitEventsTimeout(seconds);
return _events.events.items;
}
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; // todo lifetime issues
// pub const GLFWmonitorfun = ?*const fn (?*GLFWmonitor, c_int) callconv(.C) void;
// pub const GLFWjoystickfun = ?*const fn (c_int, c_int) callconv(.C) void; // todo skip for now
pub fn onCharMods(handle: ?*c.GLFWwindow, code: u32, mods: i32) callconv(.C) void {
const bus: *EventBus =
@alignCast(@ptrCast(c.glfwGetWindowUserPointer(handle)));
bus.events.append(bus.alloc, .{
.charMods = .{
.codepoint = @intCast(code),
.mods = mods,
},
}) catch unreachable; // todo circular queue; warn.
}
pub fn onMouseButton(handle: ?*c.GLFWwindow, btn: c_int, action: c_int, mods: c_int) callconv(.C) void {
const bus: *EventBus =
@alignCast(@ptrCast(c.glfwGetWindowUserPointer(handle)));
bus.events.append(bus.alloc, .{
.mouseButton = .{
.btn = @intCast(btn),
.action = action == c.GLFW_PRESS,
.mods = @intCast(mods),
},
}) catch unreachable; // todo circular queue; warn.
}
const EventBus = struct {
const Self = @This();
alloc: std.mem.Allocator,
events: std.ArrayListUnmanaged(Event), // todo bounded array?
pub fn init(alloc: std.mem.Allocator) Self {
return .{
.alloc = alloc,
.events = .{},
};
}
pub fn deinit(self: *EventBus) void {
self.events.deinit(self.alloc);
}
pub fn connect(self: *EventBus, window: *Window) !void {
c.glfwSetWindowUserPointer(window.handle, self);
_ = c.glfwSetCharModsCallback(window.handle, onCharMods);
_ = c.glfwSetMouseButtonCallback(window.handle, onMouseButton);
}
pub fn disconnect(_: *EventBus, window: *Window) !void {
// todo somehow prevent double-disconnect?
_ = c.glfwSetCharModsCallback(window.handle, null);
_ = c.glfwSetMouseButtonCallback(window.handle, null);
}
pub fn clear(self: *EventBus) void {
self.events.clearRetainingCapacity();
}
};
fn init_event_bus(alloc: std.mem.Allocator) !void {
_events = EventBus.init(alloc);
errdefer _events.deinit();