From 2eac60caf1a76e1a349b0fbbdebd622a723e6504 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sun, 25 Aug 2024 16:04:25 -0700 Subject: [PATCH] core: centralize EventQueue and EventIterator declaration Signed-off-by: Stephen Gutekanst --- src/Core.zig | 22 +++++++++++----------- src/core/Darwin.zig | 15 +++------------ src/core/Null.zig | 15 +++------------ src/core/Windows.zig | 8 -------- 4 files changed, 17 insertions(+), 43 deletions(-) diff --git a/src/Core.zig b/src/Core.zig index a2b8a661..89d5db2b 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -7,9 +7,7 @@ const gpu = mach.gpu; const log = std.log.scoped(.mach); const gamemode_log = std.log.scoped(.gamemode); -pub const sysgpu = @import("../main.zig").sysgpu; - -pub const Platform = switch (build_options.core_platform) { +const Platform = switch (build_options.core_platform) { .wasm => @panic("TODO: support mach.Core WASM platform"), .windows => @import("core/Windows.zig"), .darwin => @import("core/Darwin.zig"), @@ -25,6 +23,16 @@ pub const supports_non_blocking = switch (build_options.core_platform) { .null => true, }; +pub const EventQueue = std.fifo.LinearFifo(Event, .Dynamic); + +pub const EventIterator = struct { + queue: *EventQueue, + + pub fn next(self: *EventIterator) ?Event { + return self.queue.readItem(); + } +}; + /// Set this to true if you intend to drive the main loop yourself. /// /// A panic will occur if `supports_non_blocking == false` for the platform. @@ -148,14 +156,6 @@ surface: *gpu.Surface, swap_chain: *gpu.SwapChain, descriptor: gpu.SwapChain.Descriptor, -pub const EventIterator = struct { - platform: Platform.EventIterator, - - pub inline fn next(iter: *EventIterator) ?Event { - return iter.platform.next(); - } -}; - // TODO: this needs to be removed. pub const InitOptions = struct { allocator: std.mem.Allocator, diff --git a/src/core/Darwin.zig b/src/core/Darwin.zig index 2f80d733..fe612e2e 100644 --- a/src/core/Darwin.zig +++ b/src/core/Darwin.zig @@ -19,21 +19,12 @@ const objc = @import("objc"); const log = std.log.scoped(.mach); -const EventQueue = std.fifo.LinearFifo(Event, .Dynamic); -pub const EventIterator = struct { - queue: *EventQueue, - - pub inline fn next(self: *EventIterator) ?Event { - return self.queue.readItem(); - } -}; - pub const Darwin = @This(); allocator: std.mem.Allocator, core: *Core, -events: EventQueue, +events: Core.EventQueue, input_state: Core.InputState, // modifiers: KeyMods, @@ -148,8 +139,8 @@ pub fn update(_: *Darwin) !void { } // May be called from any thread. -pub inline fn pollEvents(n: *Darwin) EventIterator { - return EventIterator{ .queue = &n.events }; +pub inline fn pollEvents(n: *Darwin) Core.EventIterator { + return .{ .queue = &n.events }; } // May be called from any thread. diff --git a/src/core/Null.zig b/src/core/Null.zig index 801d6393..a738ddba 100644 --- a/src/core/Null.zig +++ b/src/core/Null.zig @@ -21,21 +21,12 @@ const KeyMods = Core.KeyMods; const log = std.log.scoped(.mach); -const EventQueue = std.fifo.LinearFifo(Event, .Dynamic); -pub const EventIterator = struct { - queue: *EventQueue, - - pub inline fn next(self: *EventIterator) ?Event { - return self.queue.readItem(); - } -}; - pub const Null = @This(); allocator: std.mem.Allocator, core: *Core, -events: EventQueue, +events: Core.EventQueue, input_state: Core.InputState, modifiers: KeyMods, @@ -65,8 +56,8 @@ pub fn update(_: *Null) !void { } // May be called from any thread. -pub inline fn pollEvents(n: *Null) EventIterator { - return EventIterator{ .queue = &n.events }; +pub inline fn pollEvents(n: *Null) Core.EventIterator { + return .{ .queue = &n.events }; } // May be called from any thread. diff --git a/src/core/Windows.zig b/src/core/Windows.zig index f6c34190..d877656a 100644 --- a/src/core/Windows.zig +++ b/src/core/Windows.zig @@ -151,14 +151,6 @@ pub fn update(self: *Win32) !void { } } -pub const EventIterator = struct { - queue: *EventQueue, - - pub fn next(self: *EventIterator) ?Event { - return self.queue.readItem(); - } -}; - pub fn pollEvents(self: *Win32) EventIterator { return .{ .queue = &self.events }; }