support drop event

This commit is contained in:
David Allemang
2024-06-27 11:58:18 -04:00
parent 07c96af5d7
commit fbfa8ee8d6
3 changed files with 59 additions and 35 deletions

View File

@@ -4,7 +4,7 @@ const builtin = @import("builtin");
const vk = @import("vk"); const vk = @import("vk");
const c = @import("c.zig"); const c = @import("c.zig");
const EventBus = @import("au/EventBus.zig"); const Bus = @import("au/Bus.zig");
pub const use_debug_messenger = switch (builtin.mode) { pub const use_debug_messenger = switch (builtin.mode) {
.Debug, .ReleaseSafe => true, .Debug, .ReleaseSafe => true,
@@ -63,7 +63,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 _bus: Bus = 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;
@@ -428,31 +428,31 @@ pub const Window = struct {
} }
}; };
pub fn wait_events() []const EventBus.Event { pub fn wait_events() []const Bus.Event {
_events.clear(); _bus.clear();
c.glfwWaitEvents(); c.glfwWaitEvents();
return _events.events.items; return _bus.events.items;
} }
pub fn poll_events() []const EventBus.Event { pub fn poll_events() []const Bus.Event {
_events.clear(); _bus.clear();
c.glfwPollEvents(); c.glfwPollEvents();
return _events.events.items; return _bus.events.items;
} }
pub fn wait_events_timeout(seconds: f64) []const EventBus.Event { pub fn wait_events_timeout(seconds: f64) []const Bus.Event {
_events.clear(); _bus.clear();
c.glfwWaitEventsTimeout(seconds); c.glfwWaitEventsTimeout(seconds);
return _events.events.items; return _bus.events.items;
} }
fn init_event_bus(alloc: std.mem.Allocator) !void { fn init_event_bus(alloc: std.mem.Allocator) !void {
_events = EventBus.init(alloc); _bus = Bus.init(alloc);
errdefer _events.deinit(); errdefer _bus.deinit();
try _events.connect(&_window); try _bus.connect(&_window);
} }
fn deinit_event_bus() void { fn deinit_event_bus() void {
try _events.disconnect(&_window); try _bus.disconnect(&_window);
_events.deinit(); _bus.deinit();
} }

View File

@@ -4,17 +4,20 @@ const Window = @import("../au.zig").Window;
const Self = @This(); const Self = @This();
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
events: std.ArrayListUnmanaged(Event), // todo bounded array? events: std.ArrayListUnmanaged(Event) = .{}, // todo bounded array?
drops: std.ArrayListUnmanaged([][]const u8) = .{}, // todo bounded array?
pub fn init(alloc: std.mem.Allocator) Self { pub fn init(alloc: std.mem.Allocator) Self {
return .{ return .{
.alloc = alloc, .alloc = alloc,
.events = .{},
}; };
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
self.clear();
self.events.deinit(self.alloc); self.events.deinit(self.alloc);
self.drops.deinit(self.alloc);
} }
pub fn connect(self: *Self, window: *Window) !void { pub fn connect(self: *Self, window: *Window) !void {
@@ -35,7 +38,7 @@ pub fn connect(self: *Self, window: *Window) !void {
_ = c.glfwSetScrollCallback(window.handle, onScroll); _ = c.glfwSetScrollCallback(window.handle, onScroll);
_ = c.glfwSetKeyCallback(window.handle, onKey); _ = c.glfwSetKeyCallback(window.handle, onKey);
_ = c.glfwSetCharModsCallback(window.handle, onCharMods); _ = c.glfwSetCharModsCallback(window.handle, onCharMods);
// _ = c.glfwSetDropCallback(window.handle, onDrop); _ = c.glfwSetDropCallback(window.handle, onDrop);
} }
pub fn disconnect(_: *Self, window: *Window) !void { pub fn disconnect(_: *Self, window: *Window) !void {
@@ -56,10 +59,18 @@ pub fn disconnect(_: *Self, window: *Window) !void {
_ = c.glfwSetScrollCallback(window.handle, null); _ = c.glfwSetScrollCallback(window.handle, null);
_ = c.glfwSetKeyCallback(window.handle, null); _ = c.glfwSetKeyCallback(window.handle, null);
_ = c.glfwSetCharModsCallback(window.handle, null); _ = c.glfwSetCharModsCallback(window.handle, null);
// _ = c.glfwSetDropCallback(window.handle, null); _ = c.glfwSetDropCallback(window.handle, null);
} }
pub fn clear(self: *Self) void { pub fn clear(self: *Self) void {
for (self.drops.items) |drop| {
for (drop) |path| {
self.alloc.free(path);
}
self.alloc.free(drop);
}
self.drops.clearAndFree(self.alloc);
self.events.clearRetainingCapacity(); self.events.clearRetainingCapacity();
} }
@@ -99,7 +110,7 @@ pub const Event = union(enum) {
mods: c_int, // todo bitmask mods: c_int, // todo bitmask
}; };
const Drop = struct { const Drop = struct {
paths: []const []const u8, // todo lifetime issues paths: []const []const u8,
}; };
windowPos: WindowPos, windowPos: WindowPos,
@@ -249,9 +260,16 @@ fn onCharMods(handle: ?*c.GLFWwindow, code: c_uint, mods: c_int) callconv(.C) vo
}, },
}) catch unreachable; // todo circular queue; warn }) catch unreachable; // todo circular queue; warn
} }
// fn onDrop(handle: ?*c.GLFWwindow, count: c_int, paths: [*c][*c]const u8) callconv(.C) void { fn onDrop(handle: ?*c.GLFWwindow, count: c_int, paths: [*c][*c]const u8) callconv(.C) void {
// const bus = getBus(handle); const bus = getBus(handle);
// bus.events.append(bus.alloc, .{
// .drop = .{}, const drops = bus.alloc.alloc([]const u8, @intCast(count)) catch unreachable; // todo warn
// }) catch unreachable; // todo circular queue; warn for (drops, paths) |*dst, src| {
// } dst.* = bus.alloc.dupe(u8, std.mem.sliceTo(src, 0)) catch unreachable; // todo warn
}
bus.drops.append(bus.alloc, drops) catch unreachable; // todo warn
bus.events.append(bus.alloc, .{
.drop = .{ .paths = drops },
}) catch unreachable; // todo circular queue; warn
}

View File

@@ -93,18 +93,24 @@ fn render(
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit(); defer _ = gpa.detectLeaks();
const ally = gpa.allocator(); const alloc = gpa.allocator();
try au.init(ally); try au.init(alloc);
defer au.deinit(); defer au.deinit();
// std.debug.print("Initialized!!\n", .{ });
while (!au.W.should_close()) { while (!au.W.should_close()) {
for (au.wait_events()) |event| switch (event) { // todo switch mode depending on if window is focused
.charMods => |e| std.debug.print("{any}\n", .{e}), const events = au.wait_events_timeout(0.10);
.mouseButton => |e| std.debug.print("{any}\n", .{e}),
for (events) |u| switch (u) {
.cursorPos,
.windowPos,
.windowSize,
.framebufferSize,
.windowRefresh,
=> {},
else => |e| std.debug.print("{any}\n", .{e}),
}; };
} }