From 2c8ba82aa3e85a8ed580fd3e4f7a6c3e4016ec32 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sun, 28 Apr 2024 11:45:59 -0700 Subject: [PATCH] Core: use .app local init/deinit/tick events (avoid global events) Signed-off-by: Stephen Gutekanst --- examples/core-custom-entrypoint/App.zig | 12 ++++++----- examples/custom-renderer/App.zig | 12 +++++++---- examples/custom-renderer/Renderer.zig | 2 +- examples/glyphs/App.zig | 8 +++----- examples/piano/App.zig | 8 +++----- examples/sprite/App.zig | 7 +++---- examples/text/App.zig | 9 ++++----- src/Core.zig | 27 +++++++++---------------- src/core/main.zig | 4 ++-- 9 files changed, 41 insertions(+), 48 deletions(-) diff --git a/examples/core-custom-entrypoint/App.zig b/examples/core-custom-entrypoint/App.zig index ce897cce..52243037 100644 --- a/examples/core-custom-entrypoint/App.zig +++ b/examples/core-custom-entrypoint/App.zig @@ -5,14 +5,20 @@ const gpu = mach.gpu; pub const name = .app; pub const Mod = mach.Mod(@This()); -pub const global_events = .{ +pub const local_events = .{ .init = .{ .handler = init }, + .deinit = .{ .handler = deinit }, .tick = .{ .handler = tick }, }; title_timer: mach.Timer, pipeline: *gpu.RenderPipeline, +pub fn deinit(core: *mach.Core.Mod, game: *Mod) void { + game.state().pipeline.release(); + core.send(.deinit, .{}); +} + fn init(game: *Mod, core: *mach.Core.Mod) !void { // Create our shader module const shader_module = core.state().device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl")); @@ -54,10 +60,6 @@ fn init(game: *Mod, core: *mach.Core.Mod) !void { try updateWindowTitle(core); } -pub fn deinit(game: *Mod) void { - game.state().pipeline.release(); -} - // TODO(important): remove need for returning an error here fn tick(core: *mach.Core.Mod, game: *Mod) !void { // TODO(important): event polling should occur in mach.Core module and get fired as ECS event. diff --git a/examples/custom-renderer/App.zig b/examples/custom-renderer/App.zig index 637d8f45..c735304c 100644 --- a/examples/custom-renderer/App.zig +++ b/examples/custom-renderer/App.zig @@ -22,10 +22,10 @@ pub const components = .{ .follower = .{ .type = void }, }; -// Global events that we will listen for. -pub const global_events = .{ - .init = .{ .handler = init }, // Event sent on app startup - .tick = .{ .handler = tick }, // Event sent on each frame +pub const local_events = .{ + .init = .{ .handler = init }, + .deinit = .{ .handler = deinit }, + .tick = .{ .handler = tick }, }; // Define the globally unique name of our module. You can use any name here, but keep in mind no @@ -38,6 +38,10 @@ pub const name = .app; // Note that Mod.state() returns an instance of our module struct. pub const Mod = mach.Mod(@This()); +pub fn deinit(core: *mach.Core.Mod) void { + core.send(.deinit, .{}); +} + // TODO(important): remove need for returning an error here fn init( // These are injected dependencies - as long as these modules were registered in the top-level diff --git a/examples/custom-renderer/Renderer.zig b/examples/custom-renderer/Renderer.zig index 30d0a40f..65c53d57 100644 --- a/examples/custom-renderer/Renderer.zig +++ b/examples/custom-renderer/Renderer.zig @@ -27,7 +27,7 @@ pub const components = .{ .scale = .{ .type = f32 }, }; -pub const global_events = .{ +pub const local_events = .{ .init = .{ .handler = init }, .deinit = .{ .handler = deinit }, .tick = .{ .handler = tick }, diff --git a/examples/glyphs/App.zig b/examples/glyphs/App.zig index 1bd9e6f0..ff9f8ffb 100644 --- a/examples/glyphs/App.zig +++ b/examples/glyphs/App.zig @@ -32,20 +32,18 @@ frame_render_pass: *gpu.RenderPassEncoder = undefined, pub const name = .app; pub const Mod = mach.Mod(@This()); -pub const global_events = .{ +pub const local_events = .{ .init = .{ .handler = init }, .deinit = .{ .handler = deinit }, .tick = .{ .handler = tick }, -}; - -pub const local_events = .{ .after_init = .{ .handler = afterInit }, .end_frame = .{ .handler = endFrame }, }; -fn deinit(sprite_pipeline: *gfx.SpritePipeline.Mod, glyphs: *Glyphs.Mod) !void { +fn deinit(core: *mach.Core.Mod, sprite_pipeline: *gfx.SpritePipeline.Mod, glyphs: *Glyphs.Mod) !void { sprite_pipeline.send(.deinit, .{}); glyphs.send(.deinit, .{}); + core.send(.deinit, .{}); } fn init(sprite_pipeline: *gfx.SpritePipeline.Mod, glyphs: *Glyphs.Mod, game: *Mod) !void { diff --git a/examples/piano/App.zig b/examples/piano/App.zig index 4795c12b..ed835fc8 100644 --- a/examples/piano/App.zig +++ b/examples/piano/App.zig @@ -24,14 +24,12 @@ pub const name = .app; pub const Mod = mach.Mod(@This()); pub const global_events = .{ - .init = .{ .handler = init }, - .deinit = .{ .handler = deinit }, - .tick = .{ .handler = tick }, .audio_state_change = .{ .handler = audioStateChange }, }; pub const local_events = .{ .init = .{ .handler = init }, + .deinit = .{ .handler = deinit }, .tick = .{ .handler = tick }, }; @@ -55,9 +53,9 @@ fn init(audio: *mach.Audio.Mod, piano: *Mod) void { std.debug.print("[arrow down] decrease volume 10%\n", .{}); } -fn deinit(audio: *mach.Audio.Mod) void { - // Initialize audio module +fn deinit(core: *mach.Core.Mod, audio: *mach.Audio.Mod) void { audio.send(.deinit, .{}); + core.send(.deinit, .{}); } fn audioStateChange( diff --git a/examples/sprite/App.zig b/examples/sprite/App.zig index f3737ff7..c098f88a 100644 --- a/examples/sprite/App.zig +++ b/examples/sprite/App.zig @@ -36,20 +36,19 @@ frame_render_pass: *gpu.RenderPassEncoder = undefined, pub const name = .app; pub const Mod = mach.Mod(@This()); -pub const global_events = .{ +pub const local_events = .{ .init = .{ .handler = init }, .deinit = .{ .handler = deinit }, .tick = .{ .handler = tick }, -}; - -pub const local_events = .{ .end_frame = .{ .handler = endFrame }, }; fn deinit( + core: *mach.Core.Mod, sprite_pipeline: *gfx.SpritePipeline.Mod, ) !void { sprite_pipeline.send(.init, .{}); + core.send(.deinit, .{}); } fn init( diff --git a/examples/text/App.zig b/examples/text/App.zig index 7442d6d4..df953b0a 100644 --- a/examples/text/App.zig +++ b/examples/text/App.zig @@ -34,16 +34,13 @@ frame_render_pass: *gpu.RenderPassEncoder = undefined, // Define the globally unique name of our module. You can use any name here, but keep in mind no // two modules in the program can have the same name. -pub const name = .game; +pub const name = .app; pub const Mod = mach.Mod(@This()); -pub const global_events = .{ +pub const local_events = .{ .init = .{ .handler = init }, .deinit = .{ .handler = deinit }, .tick = .{ .handler = tick }, -}; - -pub const local_events = .{ .end_frame = .{ .handler = endFrame }, }; @@ -58,9 +55,11 @@ const text1: []const []const u8 = &.{ const text2: []const []const u8 = &.{"$!?😊"}; fn deinit( + core: *mach.Core.Mod, text_pipeline: *gfx.TextPipeline.Mod, ) !void { text_pipeline.send(.deinit, .{}); + core.send(.deinit, .{}); } fn init( diff --git a/src/Core.zig b/src/Core.zig index eb5fb7bb..a3013ef1 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -10,12 +10,6 @@ pub const name = .mach_core; pub const Mod = mach.Mod(@This()); -pub const global_events = .{ - .init = .{ .handler = fn () void }, - .deinit = .{ .handler = fn () void }, - .tick = .{ .handler = fn () void }, -}; - pub const local_events = .{ .update = .{ .handler = update, .description = \\ Send this when window entities have been updated and you want the new values respected. @@ -105,7 +99,7 @@ fn init(core: *Mod) !void { .main_window = main_window, }); - core.sendGlobal(.init, .{}); + mach.core.mods.send(.app, .init, .{}); core.send(.init_done, .{}); } @@ -153,15 +147,14 @@ fn deinit(core: *Mod) void { } _ = gpa.deinit(); -} - -fn mainThreadTick(core: *Mod) !void { - _ = try mach.core.update(null); - - // Send .tick to anyone interested - core.sendGlobal(.tick, .{}); -} - -fn exit(core: *Mod) void { core.state().should_exit = true; } + +fn mainThreadTick() !void { + _ = try mach.core.update(null); + mach.core.mods.send(.app, .tick, .{}); +} + +fn exit() void { + mach.core.mods.send(.app, .deinit, .{}); +} diff --git a/src/core/main.zig b/src/core/main.zig index 28f9e171..407eda0c 100644 --- a/src/core/main.zig +++ b/src/core/main.zig @@ -15,7 +15,7 @@ var stack_space: [8 * 1024 * 1024]u8 = undefined; pub fn initModule() !void { // Initialize the global set of Mach modules used in the program. try mods.init(std.heap.c_allocator); - mods.mod.mach_core.send(.init, .{}); + mods.send(.mach_core, .init, .{}); // Dispatch events until this .mach_core.init_done is sent try mods.dispatch(&stack_space, .{ .until = .{ @@ -28,7 +28,7 @@ pub fn initModule() !void { /// /// Returns true if tick() should be called again, false if the application should exit. pub fn tick() !bool { - mods.mod.mach_core.send(.main_thread_tick, .{}); + mods.send(.mach_core, .main_thread_tick, .{}); // Dispatch events until this .mach_core.main_thread_tick_done is sent try mods.dispatch(&stack_space, .{ .until = .{