From ed9137fe98b102f1509b69d2e406e69199adf1f8 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sun, 28 Apr 2024 11:09:53 -0700 Subject: [PATCH] SpritePipeline: make .init and .deinit explicit Signed-off-by: Stephen Gutekanst --- examples/glyphs/Game.zig | 12 ++++++++++-- examples/glyphs/Glyphs.zig | 5 +---- examples/sprite/Game.zig | 9 +++++++++ src/gfx/SpritePipeline.zig | 6 ++---- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/examples/glyphs/Game.zig b/examples/glyphs/Game.zig index 373a3a31..ac20f307 100644 --- a/examples/glyphs/Game.zig +++ b/examples/glyphs/Game.zig @@ -34,6 +34,7 @@ pub const Mod = mach.Mod(@This()); pub const global_events = .{ .init = .{ .handler = init }, + .deinit = .{ .handler = deinit }, .tick = .{ .handler = tick }, }; @@ -42,9 +43,16 @@ pub const local_events = .{ .end_frame = .{ .handler = endFrame }, }; -fn init(glyphs: *Glyphs.Mod, game: *Mod) !void { - // Prepare which glyphs we will render +fn deinit(sprite_pipeline: *gfx.SpritePipeline.Mod, glyphs: *Glyphs.Mod) !void { + sprite_pipeline.send(.deinit, .{}); + glyphs.send(.deinit, .{}); +} + +fn init(sprite_pipeline: *gfx.SpritePipeline.Mod, glyphs: *Glyphs.Mod, game: *Mod) !void { + sprite_pipeline.send(.init, .{}); glyphs.send(.init, .{}); + + // Prepare which glyphs we will render glyphs.send(.prepare, .{&[_]u21{ '?', '!', 'a', 'b', '#', '@', '%', '$', '&', '^', '*', '+', '=', '<', '>', '/', ':', ';', 'Q', '~' }}); // Run our init code after glyphs module is initialized. diff --git a/examples/glyphs/Glyphs.zig b/examples/glyphs/Glyphs.zig index 9e94d44d..785f8889 100644 --- a/examples/glyphs/Glyphs.zig +++ b/examples/glyphs/Glyphs.zig @@ -8,12 +8,9 @@ const assets = @import("assets"); pub const name = .glyphs; pub const Mod = mach.Mod(@This()); -pub const global_events = .{ - .deinit = .{ .handler = deinit }, -}; - pub const local_events = .{ .init = .{ .handler = init }, + .deinit = .{ .handler = deinit }, .prepare = .{ .handler = prepare }, }; diff --git a/examples/sprite/Game.zig b/examples/sprite/Game.zig index df97ad4d..7f4a0f8c 100644 --- a/examples/sprite/Game.zig +++ b/examples/sprite/Game.zig @@ -38,6 +38,7 @@ pub const Mod = mach.Mod(@This()); pub const global_events = .{ .init = .{ .handler = init }, + .deinit = .{ .handler = deinit }, .tick = .{ .handler = tick }, }; @@ -45,12 +46,20 @@ pub const local_events = .{ .end_frame = .{ .handler = endFrame }, }; +fn deinit( + sprite_pipeline: *gfx.SpritePipeline.Mod, +) !void { + sprite_pipeline.send(.init, .{}); +} + fn init( core: *mach.Core.Mod, sprite: *gfx.Sprite.Mod, sprite_pipeline: *gfx.SpritePipeline.Mod, game: *Mod, ) !void { + sprite_pipeline.send(.init, .{}); + // We can create entities, and set components on them. Note that components live in a module // namespace, e.g. the `.mach_gfx_sprite` module could have a 3D `.location` component with a different // type than the `.physics2d` module's `.location` component if you desire. diff --git a/src/gfx/SpritePipeline.zig b/src/gfx/SpritePipeline.zig index c05fe793..17c10298 100644 --- a/src/gfx/SpritePipeline.zig +++ b/src/gfx/SpritePipeline.zig @@ -59,11 +59,9 @@ pub const components = .{ .built = .{ .type = BuiltPipeline, .description = "internal" }, }; -pub const global_events = .{ - .deinit = .{ .handler = deinit }, -}; - pub const local_events = .{ + .init = .{ .handler = fn () void }, + .deinit = .{ .handler = deinit }, .update = .{ .handler = update }, .pre_render = .{ .handler = preRender }, .render = .{ .handler = render },