examples: revert to 0.4 entrypoint / control API design

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-08-24 17:05:21 -07:00 committed by Stephen Gutekanst
parent a54d20daa2
commit 80be6b7bca
20 changed files with 105 additions and 56 deletions

View file

@ -1,4 +1,3 @@
const std = @import("std");
const mach = @import("mach");
const gpu = mach.gpu;
@ -7,18 +6,25 @@ pub const Mod = mach.Mod(@This());
pub const systems = .{
.init = .{ .handler = init },
.after_init = .{ .handler = afterInit },
.deinit = .{ .handler = deinit },
.update = .{ .handler = update },
.tick = .{ .handler = tick },
};
title_timer: mach.Timer,
pipeline: *gpu.RenderPipeline,
pub fn deinit(game: *Mod) void {
pub fn deinit(core: *mach.Core.Mod, game: *Mod) void {
game.state().pipeline.release();
core.schedule(.deinit);
}
fn init(game: *Mod, core: *mach.Core.Mod) !void {
core.schedule(.init);
game.schedule(.after_init);
}
fn afterInit(game: *Mod, core: *mach.Core.Mod) !void {
// Create our shader module
const shader_module = core.state().device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
defer shader_module.release();
@ -56,10 +62,12 @@ fn init(game: *Mod, core: *mach.Core.Mod) !void {
.title_timer = try mach.Timer.start(),
.pipeline = pipeline,
});
// try updateWindowTitle(core);
try updateWindowTitle(core);
core.schedule(.start);
}
fn update(core: *mach.Core.Mod, game: *Mod) !void {
fn tick(core: *mach.Core.Mod, game: *Mod) !void {
var iter = core.state().pollEvents();
while (iter.next()) |event| {
switch (event) {
@ -74,7 +82,7 @@ fn update(core: *mach.Core.Mod, game: *Mod) !void {
defer back_buffer_view.release();
// Create a command encoder
const label = @tagName(name) ++ ".update";
const label = @tagName(name) ++ ".tick";
const encoder = core.state().device.createCommandEncoder(&.{ .label = label });
defer encoder.release();
@ -104,6 +112,7 @@ fn update(core: *mach.Core.Mod, game: *Mod) !void {
defer command.release();
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
// Present the frame
core.schedule(.present_frame);
// update the window title every second
@ -123,4 +132,5 @@ fn updateWindowTitle(core: *mach.Core.Mod) !void {
core.state().inputRate(),
},
);
core.schedule(.update);
}

View file

@ -1,4 +1,3 @@
const std = @import("std");
const mach = @import("mach");
// The global list of Mach modules registered for use in our application.