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 math = mach.math;
const Renderer = @import("Renderer.zig");
@ -25,7 +24,7 @@ pub const components = .{
pub const systems = .{
.init = .{ .handler = init },
.deinit = .{ .handler = deinit },
.update = .{ .handler = update },
.tick = .{ .handler = tick },
};
// Define the globally unique name of our module. You can use any name here, but keep in mind no
@ -38,8 +37,9 @@ pub const name = .app;
// Note that Mod.state() returns an instance of our module struct.
pub const Mod = mach.Mod(@This());
pub fn deinit(renderer: *Renderer.Mod) void {
pub fn deinit(core: *mach.Core.Mod, renderer: *Renderer.Mod) void {
renderer.schedule(.deinit);
core.schedule(.deinit);
}
fn init(
@ -47,9 +47,11 @@ fn init(
// of the program we can have these types injected here, letting us work with other modules in
// our program seamlessly and with a type-safe API:
entities: *mach.Entities.Mod,
core: *mach.Core.Mod,
renderer: *Renderer.Mod,
game: *Mod,
) !void {
core.schedule(.init);
renderer.schedule(.init);
// Create our player entity.
@ -73,16 +75,16 @@ fn init(
.spawn_timer = try mach.Timer.start(),
.player = player,
});
core.schedule(.start);
}
fn update(
fn tick(
entities: *mach.Entities.Mod,
core: *mach.Core.Mod,
renderer: *Renderer.Mod,
game: *Mod,
) !void {
// TODO(important): event polling should occur in mach.Core module and get fired as ECS event.
// TODO(Core)
var iter = core.state().pollEvents();
var direction = game.state().direction;
var spawning = game.state().spawning;

View file

@ -1,7 +1,3 @@
// TODO(important): docs
// TODO(important): review all code in this file in-depth
const std = @import("std");
const mach = @import("mach");
const gpu = mach.gpu;
const math = mach.math;
@ -110,7 +106,9 @@ fn init(
});
}
fn deinit(renderer: *Mod) !void {
fn deinit(
renderer: *Mod,
) !void {
renderer.state().pipeline.release();
for (renderer.state().bind_groups) |bind_group| bind_group.release();
renderer.state().uniform_buffer.release();
@ -127,7 +125,7 @@ fn renderFrame(
defer back_buffer_view.release();
// Create a command encoder
const label = @tagName(name) ++ ".renderFrame";
const label = @tagName(name) ++ ".tick";
const encoder = core.state().device.createCommandEncoder(&.{ .label = label });
defer encoder.release();
@ -177,5 +175,6 @@ fn renderFrame(
defer command.release();
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
// Present the frame
core.schedule(.present_frame);
}

View file

@ -1,5 +1,3 @@
const std = @import("std");
const mach = @import("mach");
// The global list of Mach modules registered for use in our application.
@ -9,8 +7,9 @@ pub const modules = .{
@import("Renderer.zig"),
};
// TODO: move this to a mach "entrypoint" zig module
pub fn main() !void {
// Initialize mach.Core
// Initialize mach core
try mach.core.initModule();
// Main loop