make it clear how to use module system without mach.Core (remove mach.App)
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
7ac5bef717
commit
642cc9b7f7
20 changed files with 567 additions and 334 deletions
|
|
@ -25,8 +25,8 @@ pub const name = .app;
|
|||
pub const Mod = mach.Mod(@This());
|
||||
|
||||
pub const systems = .{
|
||||
.start = .{ .handler = start },
|
||||
.init = .{ .handler = init },
|
||||
.after_init = .{ .handler = afterInit },
|
||||
.deinit = .{ .handler = deinit },
|
||||
.tick = .{ .handler = tick },
|
||||
.audio_state_change = .{ .handler = audioStateChange },
|
||||
|
|
@ -38,10 +38,19 @@ pub const components = .{
|
|||
|
||||
ghost_key_mode: bool = false,
|
||||
|
||||
fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) void {
|
||||
fn start(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) void {
|
||||
core.schedule(.init);
|
||||
audio.schedule(.init);
|
||||
app.schedule(.after_init);
|
||||
app.schedule(.init);
|
||||
}
|
||||
|
||||
fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) void {
|
||||
core.state().on_tick = app.system(.tick);
|
||||
core.state().on_exit = app.system(.deinit);
|
||||
|
||||
// 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.system(.audio_state_change);
|
||||
|
||||
// Initialize piano module state
|
||||
app.init(.{});
|
||||
|
|
@ -55,12 +64,6 @@ fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) void {
|
|||
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.system(.audio_state_change);
|
||||
}
|
||||
|
||||
fn deinit(core: *mach.Core.Mod, audio: *mach.Audio.Mod) void {
|
||||
audio.schedule(.deinit);
|
||||
core.schedule(.deinit);
|
||||
|
|
|
|||
|
|
@ -1,17 +1,25 @@
|
|||
const std = @import("std");
|
||||
const mach = @import("mach");
|
||||
|
||||
// The global list of Mach modules registered for use in our application.
|
||||
// The global list of Mach modules our application may use.
|
||||
pub const modules = .{
|
||||
mach.Core,
|
||||
mach.Audio,
|
||||
@import("App.zig"),
|
||||
};
|
||||
|
||||
// TODO(important): use standard entrypoint instead
|
||||
// TODO: move this to a mach "entrypoint" zig module which handles nuances like WASM requires.
|
||||
pub fn main() !void {
|
||||
// Initialize mach.Core
|
||||
try mach.core.initModule();
|
||||
const allocator = std.heap.c_allocator;
|
||||
|
||||
// Main loop
|
||||
while (try mach.core.tick()) {}
|
||||
// Initialize module system
|
||||
try mach.mods.init(allocator);
|
||||
|
||||
// Schedule .app.start to run.
|
||||
mach.mods.schedule(.app, .start);
|
||||
|
||||
// Dispatch systems forever or until there are none left to dispatch. If your app uses mach.Core
|
||||
// then this will block forever and never return.
|
||||
const stack_space = try allocator.alloc(u8, 8 * 1024 * 1024);
|
||||
try mach.mods.dispatch(stack_space, .{});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue