event bus seems to be working
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -37,8 +37,11 @@ modules.order
|
|||||||
Module.symvers
|
Module.symvers
|
||||||
Mkfile.old
|
Mkfile.old
|
||||||
dkms.conf
|
dkms.conf
|
||||||
|
|
||||||
[.]zig-cache/
|
[.]zig-cache/
|
||||||
zig-out/
|
zig-out/
|
||||||
build/
|
build/
|
||||||
build-*/
|
build-*/
|
||||||
docgen_tmp/
|
docgen_tmp/
|
||||||
|
|
||||||
|
.idea/
|
||||||
|
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 I: *const InstanceProxy = &_ip;
|
||||||
pub const D: *const DeviceProxy = &_dp;
|
pub const D: *const DeviceProxy = &_dp;
|
||||||
pub const W: *const Window = &_window;
|
pub const W: *const Window = &_window;
|
||||||
|
pub const E: *const EventBus = &_events;
|
||||||
pub const Q: *const QueueProxy = &_qp;
|
pub const Q: *const QueueProxy = &_qp;
|
||||||
|
|
||||||
pub const device_config: *const CandidateDeviceInfo = &_dconfig;
|
pub const device_config: *const CandidateDeviceInfo = &_dconfig;
|
||||||
@@ -61,6 +62,7 @@ var _qp: QueueProxy = undefined;
|
|||||||
|
|
||||||
var _instance: vk.Instance = undefined;
|
var _instance: vk.Instance = undefined;
|
||||||
var _window: Window = undefined;
|
var _window: Window = undefined;
|
||||||
|
var _events: EventBus = undefined;
|
||||||
var _device: vk.Device = undefined;
|
var _device: vk.Device = undefined;
|
||||||
var _dconfig: CandidateDeviceInfo = undefined;
|
var _dconfig: CandidateDeviceInfo = undefined;
|
||||||
var _queue: vk.Queue = undefined;
|
var _queue: vk.Queue = undefined;
|
||||||
@@ -75,7 +77,7 @@ pub fn init(alloc: std.mem.Allocator) !void {
|
|||||||
try init_instance(alloc);
|
try init_instance(alloc);
|
||||||
errdefer deinit_instance();
|
errdefer deinit_instance();
|
||||||
|
|
||||||
try init_window();
|
try init_window(alloc);
|
||||||
errdefer deinit_window();
|
errdefer deinit_window();
|
||||||
|
|
||||||
try init_device(alloc);
|
try init_device(alloc);
|
||||||
@@ -178,8 +180,9 @@ fn deinit_instance() void {
|
|||||||
_ip.destroyInstance(null);
|
_ip.destroyInstance(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_window() !void {
|
fn init_window(alloc: std.mem.Allocator) !void {
|
||||||
_window = try Window.init(
|
_window = try Window.init(
|
||||||
|
alloc,
|
||||||
app_info.p_application_name orelse "Au Window",
|
app_info.p_application_name orelse "Au Window",
|
||||||
.{ .height = 720, .width = 1280 },
|
.{ .height = 720, .width = 1280 },
|
||||||
);
|
);
|
||||||
@@ -385,13 +388,13 @@ pub fn debug_callback(
|
|||||||
const Window = struct {
|
const Window = struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
|
alloc: std.mem.Allocator,
|
||||||
handle: *c.GLFWwindow,
|
handle: *c.GLFWwindow,
|
||||||
surface: vk.SurfaceKHR,
|
surface: vk.SurfaceKHR,
|
||||||
events: std.ArrayList(Event),
|
|
||||||
|
|
||||||
pub fn init(alloc: std.mem.Allocator, 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);
|
self.alloc = 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");
|
||||||
@@ -415,7 +418,6 @@ 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);
|
||||||
}
|
}
|
||||||
@@ -423,47 +425,21 @@ const Window = struct {
|
|||||||
pub fn should_close(self: Self) bool {
|
pub fn should_close(self: Self) bool {
|
||||||
return c.glfwWindowShouldClose(self.handle) == c.GLFW_TRUE;
|
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 {
|
pub fn wait_events() void {
|
||||||
const w: *Window =
|
_events.clear();
|
||||||
@ptrCast(c.glfwGetWindowUserPointer(window));
|
c.glfwWaitEvents();
|
||||||
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 {
|
pub fn poll_events() void {
|
||||||
const w: *Window =
|
_events.clear();
|
||||||
@ptrCast(c.glfwGetWindowUserPointer(window));
|
c.glfwPollEvents();
|
||||||
w.events.append(.{
|
}
|
||||||
.mouseButton = .{
|
|
||||||
.btn = btn,
|
pub fn wait_events_timeout(seconds: f64) void {
|
||||||
.action = action == c.GLFW_PRESS,
|
_events.clear();
|
||||||
.mods = mods,
|
c.glfwWaitEventsTimeout(seconds);
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const Event = union(enum) {
|
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 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 GLFWcharfun = ?*const fn (?*GLFWwindow, c_uint) callconv(.C) void;
|
||||||
// pub const GLFWcharmodsfun = ?*const fn (?*GLFWwindow, c_uint, c_int) 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 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 {
|
pub fn onCharMods(handle: ?*c.GLFWwindow, code: u32, mods: i32) callconv(.C) void {
|
||||||
c.glfwSetWindowUserPointer(W.handle, W);
|
const bus: *EventBus =
|
||||||
_ = c.glfwSetCharModsCallback(W.handle, onCharMods);
|
@alignCast(@ptrCast(c.glfwGetWindowUserPointer(handle)));
|
||||||
_ = c.glfwSetMouseButtonCallback(W.handle, onMouseButton);
|
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 {
|
fn deinit_event_bus() void {
|
||||||
_ = c.glfwSetCharModsCallback(W.handle, null);
|
try _events.disconnect(&_window);
|
||||||
_ = c.glfwSetMouseButtonCallback(W.handle, null);
|
_events.deinit();
|
||||||
c.glfwSetWindowUserPointer(W.handle, null);
|
|
||||||
}
|
}
|
||||||
|
@@ -102,8 +102,10 @@ pub fn main() !void {
|
|||||||
// std.debug.print("Initialized!!\n", .{ });
|
// std.debug.print("Initialized!!\n", .{ });
|
||||||
|
|
||||||
while (!au.W.should_close()) {
|
while (!au.W.should_close()) {
|
||||||
au.W.wait_events();
|
au.wait_events();
|
||||||
// std.debug.print("Event!!\n", .{ });
|
// if (au.E.events.items.len > 0) {
|
||||||
|
// std.debug.print("Events: {any}\n", .{au.E.events.items});
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
try au.D.deviceWaitIdle();
|
try au.D.deviceWaitIdle();
|
||||||
|
Reference in New Issue
Block a user