module: rename events -> systems, remove 'event arguments'

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-05-08 13:18:39 -07:00 committed by Stephen Gutekanst
parent 83d436ffa4
commit 22ac26b57e
19 changed files with 287 additions and 320 deletions

View file

@ -5,7 +5,7 @@ const gpu = mach.gpu;
pub const name = .app;
pub const Mod = mach.Mod(@This());
pub const events = .{
pub const systems = .{
.init = .{ .handler = init },
.deinit = .{ .handler = deinit },
.tick = .{ .handler = tick },
@ -16,7 +16,7 @@ pipeline: *gpu.RenderPipeline,
pub fn deinit(core: *mach.Core.Mod, game: *Mod) void {
game.state().pipeline.release();
core.send(.deinit, .{});
core.schedule(.deinit);
}
fn init(game: *Mod, core: *mach.Core.Mod) !void {
@ -59,7 +59,7 @@ fn init(game: *Mod, core: *mach.Core.Mod) !void {
});
try updateWindowTitle(core);
core.send(.start, .{});
core.schedule(.start);
}
// TODO(important): remove need for returning an error here
@ -69,7 +69,7 @@ fn tick(core: *mach.Core.Mod, game: *Mod) !void {
var iter = mach.core.pollEvents();
while (iter.next()) |event| {
switch (event) {
.close => core.send(.exit, .{}), // Tell mach.Core to exit the app
.close => core.schedule(.exit), // Tell mach.Core to exit the app
else => {},
}
}
@ -111,7 +111,7 @@ fn tick(core: *mach.Core.Mod, game: *Mod) !void {
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
// Present the frame
core.send(.present_frame, .{});
core.schedule(.present_frame);
// update the window title every second
if (game.state().title_timer.read() >= 1.0) {
@ -131,5 +131,5 @@ fn updateWindowTitle(core: *mach.Core.Mod) !void {
mach.core.inputRate(),
},
);
core.send(.update, .{});
core.schedule(.update);
}

View file

@ -22,7 +22,7 @@ pub const components = .{
.follower = .{ .type = void },
};
pub const events = .{
pub const systems = .{
.init = .{ .handler = init },
.deinit = .{ .handler = deinit },
.tick = .{ .handler = tick },
@ -39,8 +39,8 @@ pub const name = .app;
pub const Mod = mach.Mod(@This());
pub fn deinit(core: *mach.Core.Mod, renderer: *Renderer.Mod) void {
renderer.send(.deinit, .{});
core.send(.deinit, .{});
renderer.schedule(.deinit);
core.schedule(.deinit);
}
// TODO(important): remove need for returning an error here
@ -53,7 +53,7 @@ fn init(
renderer: *Renderer.Mod,
game: *Mod,
) !void {
renderer.send(.init, .{});
renderer.schedule(.init);
// Create our player entity.
const player = try entities.new();
@ -77,7 +77,7 @@ fn init(
.player = player,
});
core.send(.start, .{});
core.schedule(.start);
}
// TODO(important): remove need for returning an error here
@ -114,7 +114,7 @@ fn tick(
else => {},
}
},
.close => core.send(.exit, .{}), // Send an event telling mach to exit the app
.close => core.schedule(.exit), // Send an event telling mach to exit the app
else => {},
}
}
@ -208,5 +208,5 @@ fn tick(
}
}
renderer.send(.render_frame, .{});
renderer.schedule(.render_frame);
}

View file

@ -26,7 +26,7 @@ pub const components = .{
.scale = .{ .type = f32 },
};
pub const events = .{
pub const systems = .{
.init = .{ .handler = init },
.deinit = .{ .handler = deinit },
.render_frame = .{ .handler = renderFrame },
@ -185,5 +185,5 @@ fn renderFrame(
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
// Present the frame
core.send(.present_frame, .{});
core.schedule(.present_frame);
}

View file

@ -32,7 +32,7 @@ frame_render_pass: *gpu.RenderPassEncoder = undefined,
pub const name = .app;
pub const Mod = mach.Mod(@This());
pub const events = .{
pub const systems = .{
.init = .{ .handler = init },
.deinit = .{ .handler = deinit },
.tick = .{ .handler = tick },
@ -41,22 +41,22 @@ pub const events = .{
};
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, .{});
sprite_pipeline.schedule(.deinit);
glyphs.schedule(.deinit);
core.schedule(.deinit);
}
fn init(core: *mach.Core.Mod, sprite_pipeline: *gfx.SpritePipeline.Mod, glyphs: *Glyphs.Mod, game: *Mod) !void {
sprite_pipeline.send(.init, .{});
glyphs.send(.init, .{});
sprite_pipeline.schedule(.init);
glyphs.schedule(.init);
// Prepare which glyphs we will render
glyphs.send(.prepare, .{});
glyphs.schedule(.prepare);
// Run our init code after glyphs module is initialized.
game.send(.after_init, .{});
game.schedule(.after_init);
core.send(.start, .{});
core.schedule(.start);
}
fn afterInit(
@ -71,7 +71,7 @@ fn afterInit(
const pipeline = try entities.new();
texture.reference();
try sprite_pipeline.set(pipeline, .texture, texture);
sprite_pipeline.send(.update, .{});
sprite_pipeline.schedule(.update);
// We can create entities, and set components on them. Note that components live in a module
// namespace, e.g. the `Sprite` module could have a 3D `.location` component with a different
@ -83,7 +83,7 @@ fn afterInit(
try sprite.set(player, .pipeline, pipeline);
try sprite.set(player, .size, vec2(@floatFromInt(r.width), @floatFromInt(r.height)));
try sprite.set(player, .uv_transform, Mat3x3.translate(vec2(@floatFromInt(r.x), @floatFromInt(r.y))));
sprite.send(.update, .{});
sprite.schedule(.update);
game.init(.{
.timer = try mach.Timer.start(),
@ -133,7 +133,7 @@ fn tick(
else => {},
}
},
.close => core.send(.exit, .{}),
.close => core.schedule(.exit),
else => {},
}
}
@ -199,10 +199,10 @@ fn tick(
&Mat4x4.scale(Vec3.splat(1.0)),
);
try sprite.set(game.state().player, .transform, player_transform);
sprite.send(.update, .{});
sprite.schedule(.update);
// Perform pre-render work
sprite_pipeline.send(.pre_render, .{});
sprite_pipeline.schedule(.pre_render);
// Create a command encoder for this frame
const label = @tagName(name) ++ ".tick";
@ -228,10 +228,10 @@ fn tick(
// Render our sprite batch
sprite_pipeline.state().render_pass = game.state().frame_render_pass;
sprite_pipeline.send(.render, .{});
sprite_pipeline.schedule(.render);
// Finish the frame once rendering is done.
game.send(.end_frame, .{});
game.schedule(.end_frame);
game.state().time += delta_time;
}
@ -247,7 +247,7 @@ fn endFrame(game: *Mod, core: *mach.Core.Mod) !void {
game.state().frame_render_pass.release();
// Present the frame
core.send(.present_frame, .{});
core.schedule(.present_frame);
// Every second, update the window title with the FPS
if (game.state().fps_timer.read() >= 1.0) {
@ -257,7 +257,7 @@ fn endFrame(game: *Mod, core: *mach.Core.Mod) !void {
"glyphs [ FPS: {d} ] [ Sprites: {d} ]",
.{ game.state().frame_count, game.state().sprites },
);
core.send(.update, .{});
core.schedule(.update);
game.state().fps_timer.reset();
game.state().frame_count = 0;
}

View file

@ -8,7 +8,7 @@ const assets = @import("assets");
pub const name = .glyphs;
pub const Mod = mach.Mod(@This());
pub const events = .{
pub const systems = .{
.init = .{ .handler = init },
.deinit = .{ .handler = deinit },
.prepare = .{ .handler = prepare },

View file

@ -23,7 +23,7 @@ var gpa = std.heap.GeneralPurposeAllocator(.{}){};
pub const name = .app;
pub const Mod = mach.Mod(@This());
pub const events = .{
pub const systems = .{
.init = .{ .handler = init },
.after_init = .{ .handler = afterInit },
.deinit = .{ .handler = deinit },
@ -38,8 +38,8 @@ pub const components = .{
ghost_key_mode: bool = false,
fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) void {
audio.send(.init, .{});
app.send(.after_init, .{});
audio.schedule(.init);
app.schedule(.after_init);
// Initialize piano module state
app.init(.{});
@ -50,18 +50,18 @@ fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) void {
std.debug.print("[arrow up] increase volume 10%\n", .{});
std.debug.print("[arrow down] decrease volume 10%\n", .{});
core.send(.start, .{});
core.schedule(.start);
}
fn afterInit(audio: *mach.Audio.Mod, app: *Mod) void {
// Configure the audio module to send our app's .audio_state_change event when an entity's sound
// finishes playing.
audio.state().on_state_change = app.event(.audio_state_change);
audio.state().on_state_change = app.system(.audio_state_change);
}
fn deinit(core: *mach.Core.Mod, audio: *mach.Audio.Mod) void {
audio.send(.deinit, .{});
core.send(.deinit, .{});
audio.schedule(.deinit);
core.schedule(.deinit);
}
fn audioStateChange(
@ -135,7 +135,7 @@ fn tick(
},
}
},
.close => core.send(.exit, .{}),
.close => core.schedule(.exit),
else => {},
}
}
@ -175,7 +175,7 @@ fn tick(
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
// Present the frame
core.send(.present_frame, .{});
core.schedule(.present_frame);
}
fn fillTone(audio: *mach.Audio.Mod, frequency: f32) ![]const f32 {

View file

@ -18,7 +18,7 @@ var gpa = std.heap.GeneralPurposeAllocator(.{}){};
pub const name = .app;
pub const Mod = mach.Mod(@This());
pub const events = .{
pub const systems = .{
.init = .{ .handler = init },
.after_init = .{ .handler = afterInit },
.deinit = .{ .handler = deinit },
@ -38,8 +38,8 @@ fn init(
audio: *mach.Audio.Mod,
app: *Mod,
) !void {
audio.send(.init, .{});
app.send(.after_init, .{});
audio.schedule(.init);
app.schedule(.after_init);
const bgm_fbs = std.io.fixedBufferStream(assets.bgm.bit_bit_loop);
const sfx_fbs = std.io.fixedBufferStream(assets.sfx.sword1);
@ -65,18 +65,18 @@ fn init(
std.debug.print("[arrow up] increase volume 10%\n", .{});
std.debug.print("[arrow down] decrease volume 10%\n", .{});
core.send(.start, .{});
core.schedule(.start);
}
fn afterInit(audio: *mach.Audio.Mod, app: *Mod) void {
// Configure the audio module to send our app's .audio_state_change event when an entity's sound
// finishes playing.
audio.state().on_state_change = app.event(.audio_state_change);
audio.state().on_state_change = app.system(.audio_state_change);
}
fn deinit(core: *mach.Core.Mod, audio: *mach.Audio.Mod) void {
audio.send(.deinit, .{});
core.send(.deinit, .{});
audio.schedule(.deinit);
core.schedule(.deinit);
}
fn audioStateChange(
@ -135,7 +135,7 @@ fn tick(
try audio.set(e, .playing, true);
},
},
.close => core.send(.exit, .{}),
.close => core.schedule(.exit),
else => {},
}
}
@ -175,5 +175,5 @@ fn tick(
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
// Present the frame
core.send(.present_frame, .{});
core.schedule(.present_frame);
}

View file

@ -36,7 +36,7 @@ frame_render_pass: *gpu.RenderPassEncoder = undefined,
pub const name = .app;
pub const Mod = mach.Mod(@This());
pub const events = .{
pub const systems = .{
.init = .{ .handler = init },
.deinit = .{ .handler = deinit },
.tick = .{ .handler = tick },
@ -47,8 +47,8 @@ fn deinit(
core: *mach.Core.Mod,
sprite_pipeline: *gfx.SpritePipeline.Mod,
) !void {
sprite_pipeline.send(.deinit, .{});
core.send(.deinit, .{});
sprite_pipeline.schedule(.deinit);
core.schedule(.deinit);
}
fn init(
@ -58,7 +58,7 @@ fn init(
sprite_pipeline: *gfx.SpritePipeline.Mod,
game: *Mod,
) !void {
sprite_pipeline.send(.init, .{});
sprite_pipeline.schedule(.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
@ -68,7 +68,7 @@ fn init(
const allocator = gpa.allocator();
const pipeline = try entities.new();
try sprite_pipeline.set(pipeline, .texture, try loadTexture(core, allocator));
sprite_pipeline.send(.update, .{});
sprite_pipeline.schedule(.update);
// Create our player sprite
const player = try entities.new();
@ -76,7 +76,7 @@ fn init(
try sprite.set(player, .size, vec2(32, 32));
try sprite.set(player, .uv_transform, Mat3x3.translate(vec2(0, 0)));
try sprite.set(player, .pipeline, pipeline);
sprite.send(.update, .{});
sprite.schedule(.update);
game.init(.{
.timer = try mach.Timer.start(),
@ -91,7 +91,7 @@ fn init(
.pipeline = pipeline,
});
core.send(.start, .{});
core.schedule(.start);
}
fn tick(
@ -128,7 +128,7 @@ fn tick(
else => {},
}
},
.close => core.send(.exit, .{}),
.close => core.schedule(.exit),
else => {},
}
}
@ -181,10 +181,10 @@ fn tick(
player_pos.v[0] += direction.x() * speed * delta_time;
player_pos.v[1] += direction.y() * speed * delta_time;
try sprite.set(game.state().player, .transform, Mat4x4.translate(player_pos));
sprite.send(.update, .{});
sprite.schedule(.update);
// Perform pre-render work
sprite_pipeline.send(.pre_render, .{});
sprite_pipeline.schedule(.pre_render);
// Create a command encoder for this frame
const label = @tagName(name) ++ ".tick";
@ -210,10 +210,10 @@ fn tick(
// Render our sprite batch
sprite_pipeline.state().render_pass = game.state().frame_render_pass;
sprite_pipeline.send(.render, .{});
sprite_pipeline.schedule(.render);
// Finish the frame once rendering is done.
game.send(.end_frame, .{});
game.schedule(.end_frame);
game.state().time += delta_time;
}
@ -229,7 +229,7 @@ fn endFrame(game: *Mod, core: *mach.Core.Mod) !void {
game.state().frame_render_pass.release();
// Present the frame
core.send(.present_frame, .{});
core.schedule(.present_frame);
// Every second, update the window title with the FPS
if (game.state().fps_timer.read() >= 1.0) {
@ -239,7 +239,7 @@ fn endFrame(game: *Mod, core: *mach.Core.Mod) !void {
"sprite [ FPS: {d} ] [ Sprites: {d} ]",
.{ game.state().frame_count, game.state().sprites },
);
core.send(.update, .{});
core.schedule(.update);
game.state().fps_timer.reset();
game.state().frame_count = 0;
}

View file

@ -37,7 +37,7 @@ frame_render_pass: *gpu.RenderPassEncoder = undefined,
pub const name = .app;
pub const Mod = mach.Mod(@This());
pub const events = .{
pub const systems = .{
.init = .{ .handler = init },
.deinit = .{ .handler = deinit },
.tick = .{ .handler = tick },
@ -58,8 +58,8 @@ fn deinit(
core: *mach.Core.Mod,
text_pipeline: *gfx.TextPipeline.Mod,
) !void {
text_pipeline.send(.deinit, .{});
core.send(.deinit, .{});
text_pipeline.schedule(.deinit);
core.schedule(.deinit);
}
fn init(
@ -70,7 +70,7 @@ fn init(
text_style: *gfx.TextStyle.Mod,
game: *Mod,
) !void {
text_pipeline.send(.init, .{});
text_pipeline.schedule(.init);
// TODO: a better way to initialize entities with default values
// TODO(text): most of these style options are not respected yet.
@ -98,7 +98,7 @@ fn init(
// Create a text rendering pipeline
const pipeline = try entities.new();
try text_pipeline.set(pipeline, .is_pipeline, {});
text_pipeline.send(.update, .{});
text_pipeline.schedule(.update);
// Create some text
const player = try entities.new();
@ -129,7 +129,7 @@ fn init(
.pipeline = pipeline,
});
core.send(.start, .{});
core.schedule(.start);
}
fn tick(
@ -166,7 +166,7 @@ fn tick(
else => {},
}
},
.close => core.send(.exit, .{}),
.close => core.schedule(.exit),
else => {},
}
}
@ -225,10 +225,10 @@ fn tick(
player_pos.v[1] += direction.y() * speed * delta_time;
try text.set(game.state().player, .transform, Mat4x4.scaleScalar(upscale).mul(&Mat4x4.translate(player_pos)));
try text.set(game.state().player, .dirty, true);
text.send(.update, .{});
text.schedule(.update);
// Perform pre-render work
text_pipeline.send(.pre_render, .{});
text_pipeline.schedule(.pre_render);
// Create a command encoder for this frame
const label = @tagName(name) ++ ".tick";
@ -254,10 +254,10 @@ fn tick(
// Render our text batch
text_pipeline.state().render_pass = game.state().frame_render_pass;
text_pipeline.send(.render, .{});
text_pipeline.schedule(.render);
// Finish the frame once rendering is done.
game.send(.end_frame, .{});
game.schedule(.end_frame);
game.state().time += delta_time;
}
@ -277,7 +277,7 @@ fn endFrame(
game.state().frame_render_pass.release();
// Present the frame
core.send(.present_frame, .{});
core.schedule(.present_frame);
// Every second, update the window title with the FPS
if (game.state().fps_timer.read() >= 1.0) {
@ -300,7 +300,7 @@ fn endFrame(
"text [ FPS: {d} ] [ Texts: {d} ] [ Glyphs: {d} ]",
.{ game.state().frame_count, num_texts, num_glyphs },
);
core.send(.update, .{});
core.schedule(.update);
game.state().fps_timer.reset();
game.state().frame_count = 0;
}