event bus seems to be working
This commit is contained in:
134
src/au.zig
134
src/au.zig
@@ -47,6 +47,7 @@ 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;
|
||||
@@ -61,6 +62,7 @@ var _qp: QueueProxy = undefined;
|
||||
|
||||
var _instance: vk.Instance = undefined;
|
||||
var _window: Window = undefined;
|
||||
var _events: EventBus = undefined;
|
||||
var _device: vk.Device = undefined;
|
||||
var _dconfig: CandidateDeviceInfo = undefined;
|
||||
var _queue: vk.Queue = undefined;
|
||||
@@ -75,7 +77,7 @@ pub fn init(alloc: std.mem.Allocator) !void {
|
||||
try init_instance(alloc);
|
||||
errdefer deinit_instance();
|
||||
|
||||
try init_window();
|
||||
try init_window(alloc);
|
||||
errdefer deinit_window();
|
||||
|
||||
try init_device(alloc);
|
||||
@@ -178,8 +180,9 @@ fn deinit_instance() void {
|
||||
_ip.destroyInstance(null);
|
||||
}
|
||||
|
||||
fn init_window() !void {
|
||||
fn init_window(alloc: std.mem.Allocator) !void {
|
||||
_window = try Window.init(
|
||||
alloc,
|
||||
app_info.p_application_name orelse "Au Window",
|
||||
.{ .height = 720, .width = 1280 },
|
||||
);
|
||||
@@ -385,13 +388,13 @@ pub fn debug_callback(
|
||||
const Window = struct {
|
||||
const Self = @This();
|
||||
|
||||
alloc: std.mem.Allocator,
|
||||
handle: *c.GLFWwindow,
|
||||
surface: vk.SurfaceKHR,
|
||||
events: std.ArrayList(Event),
|
||||
|
||||
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);
|
||||
self.alloc = alloc;
|
||||
|
||||
c.glfwWindowHintString(c.GLFW_X11_CLASS_NAME, "floating_window");
|
||||
c.glfwWindowHintString(c.GLFW_X11_INSTANCE_NAME, "floating_window");
|
||||
@@ -415,7 +418,6 @@ const Window = struct {
|
||||
}
|
||||
|
||||
pub fn deinit(self: Self) void {
|
||||
self.events.deinit();
|
||||
I.destroySurfaceKHR(self.surface, null);
|
||||
c.glfwDestroyWindow(self.handle);
|
||||
}
|
||||
@@ -423,47 +425,21 @@ const Window = struct {
|
||||
pub fn should_close(self: Self) bool {
|
||||
return c.glfwWindowShouldClose(self.handle) == c.GLFW_TRUE;
|
||||
}
|
||||
|
||||
pub fn wait_events(self: Self, events: std.ArrayList(Event)) void {
|
||||
c.glfwWaitEvents();
|
||||
events.clearRetainingCapacity();
|
||||
events.appendSlice(self.events.items);
|
||||
self.events.clearRetainingCapacity();
|
||||
}
|
||||
|
||||
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, 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 wait_events() void {
|
||||
_events.clear();
|
||||
c.glfwWaitEvents();
|
||||
}
|
||||
|
||||
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,
|
||||
},
|
||||
});
|
||||
pub fn poll_events() void {
|
||||
_events.clear();
|
||||
c.glfwPollEvents();
|
||||
}
|
||||
|
||||
pub fn wait_events_timeout(seconds: f64) void {
|
||||
_events.clear();
|
||||
c.glfwWaitEventsTimeout(seconds);
|
||||
}
|
||||
|
||||
const Event = union(enum) {
|
||||
@@ -494,18 +470,74 @@ const Event = union(enum) {
|
||||
// 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 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;
|
||||
// pub const GLFWjoystickfun = ?*const fn (c_int, c_int) callconv(.C) void; // todo skip for now
|
||||
|
||||
fn init_event_bus() !void {
|
||||
c.glfwSetWindowUserPointer(W.handle, W);
|
||||
_ = c.glfwSetCharModsCallback(W.handle, onCharMods);
|
||||
_ = c.glfwSetMouseButtonCallback(W.handle, onMouseButton);
|
||||
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();
|
||||
try _events.connect(&_window);
|
||||
}
|
||||
|
||||
fn deinit_event_bus() void {
|
||||
_ = c.glfwSetCharModsCallback(W.handle, null);
|
||||
_ = c.glfwSetMouseButtonCallback(W.handle, null);
|
||||
c.glfwSetWindowUserPointer(W.handle, null);
|
||||
try _events.disconnect(&_window);
|
||||
_events.deinit();
|
||||
}
|
||||
|
@@ -102,8 +102,10 @@ pub fn main() !void {
|
||||
// std.debug.print("Initialized!!\n", .{ });
|
||||
|
||||
while (!au.W.should_close()) {
|
||||
au.W.wait_events();
|
||||
// std.debug.print("Event!!\n", .{ });
|
||||
au.wait_events();
|
||||
// if (au.E.events.items.len > 0) {
|
||||
// std.debug.print("Events: {any}\n", .{au.E.events.items});
|
||||
// }
|
||||
}
|
||||
|
||||
try au.D.deviceWaitIdle();
|
||||
|
Reference in New Issue
Block a user