Core: use .app local init/deinit/tick events (avoid global events)

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-04-28 11:45:59 -07:00 committed by Stephen Gutekanst
parent fb37f74d41
commit 2c8ba82aa3
9 changed files with 41 additions and 48 deletions

View file

@ -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.

View file

@ -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

View file

@ -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 },

View file

@ -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 {

View file

@ -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(

View file

@ -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(

View file

@ -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(