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 mach = @import("mach");
const gpu = mach.gpu; const gpu = mach.gpu;
@ -7,18 +6,25 @@ pub const Mod = mach.Mod(@This());
pub const systems = .{ pub const systems = .{
.init = .{ .handler = init }, .init = .{ .handler = init },
.after_init = .{ .handler = afterInit },
.deinit = .{ .handler = deinit }, .deinit = .{ .handler = deinit },
.update = .{ .handler = update }, .tick = .{ .handler = tick },
}; };
title_timer: mach.Timer, title_timer: mach.Timer,
pipeline: *gpu.RenderPipeline, pipeline: *gpu.RenderPipeline,
pub fn deinit(game: *Mod) void { pub fn deinit(core: *mach.Core.Mod, game: *Mod) void {
game.state().pipeline.release(); game.state().pipeline.release();
core.schedule(.deinit);
} }
fn init(game: *Mod, core: *mach.Core.Mod) !void { 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 // Create our shader module
const shader_module = core.state().device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl")); const shader_module = core.state().device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
defer shader_module.release(); defer shader_module.release();
@ -57,11 +63,11 @@ fn init(game: *Mod, core: *mach.Core.Mod) !void {
.pipeline = pipeline, .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 {
// TODO(important): event polling should occur in mach.Core module and get fired as ECS event.
// TODO(Core)
var iter = core.state().pollEvents(); var iter = core.state().pollEvents();
while (iter.next()) |event| { while (iter.next()) |event| {
switch (event) { switch (event) {
@ -106,6 +112,7 @@ fn update(core: *mach.Core.Mod, game: *Mod) !void {
defer command.release(); defer command.release();
core.state().queue.submit(&[_]*gpu.CommandBuffer{command}); core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
// Present the frame
core.schedule(.present_frame); core.schedule(.present_frame);
// update the window title every second // update the window title every second
@ -125,4 +132,5 @@ fn updateWindowTitle(core: *mach.Core.Mod) !void {
core.state().inputRate(), core.state().inputRate(),
}, },
); );
core.schedule(.update);
} }

View file

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

View file

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

View file

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

View file

@ -1,4 +1,3 @@
const std = @import("std");
const mach = @import("mach"); const mach = @import("mach");
const math = mach.math; const math = mach.math;
const Renderer = @import("Renderer.zig"); const Renderer = @import("Renderer.zig");
@ -25,7 +24,7 @@ pub const components = .{
pub const systems = .{ pub const systems = .{
.init = .{ .handler = init }, .init = .{ .handler = init },
.deinit = .{ .handler = deinit }, .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 // 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. // Note that Mod.state() returns an instance of our module struct.
pub const Mod = mach.Mod(@This()); 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); renderer.schedule(.deinit);
core.schedule(.deinit);
} }
fn init( 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 // 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: // our program seamlessly and with a type-safe API:
entities: *mach.Entities.Mod, entities: *mach.Entities.Mod,
core: *mach.Core.Mod,
renderer: *Renderer.Mod, renderer: *Renderer.Mod,
game: *Mod, game: *Mod,
) !void { ) !void {
core.schedule(.init);
renderer.schedule(.init); renderer.schedule(.init);
// Create our player entity. // Create our player entity.
@ -73,16 +75,16 @@ fn init(
.spawn_timer = try mach.Timer.start(), .spawn_timer = try mach.Timer.start(),
.player = player, .player = player,
}); });
core.schedule(.start);
} }
fn update( fn tick(
entities: *mach.Entities.Mod, entities: *mach.Entities.Mod,
core: *mach.Core.Mod, core: *mach.Core.Mod,
renderer: *Renderer.Mod, renderer: *Renderer.Mod,
game: *Mod, game: *Mod,
) !void { ) !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 iter = core.state().pollEvents();
var direction = game.state().direction; var direction = game.state().direction;
var spawning = game.state().spawning; 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 mach = @import("mach");
const gpu = mach.gpu; const gpu = mach.gpu;
const math = mach.math; const math = mach.math;
@ -110,7 +106,9 @@ fn init(
}); });
} }
fn deinit(renderer: *Mod) !void { fn deinit(
renderer: *Mod,
) !void {
renderer.state().pipeline.release(); renderer.state().pipeline.release();
for (renderer.state().bind_groups) |bind_group| bind_group.release(); for (renderer.state().bind_groups) |bind_group| bind_group.release();
renderer.state().uniform_buffer.release(); renderer.state().uniform_buffer.release();
@ -127,7 +125,7 @@ fn renderFrame(
defer back_buffer_view.release(); defer back_buffer_view.release();
// Create a command encoder // Create a command encoder
const label = @tagName(name) ++ ".renderFrame"; const label = @tagName(name) ++ ".tick";
const encoder = core.state().device.createCommandEncoder(&.{ .label = label }); const encoder = core.state().device.createCommandEncoder(&.{ .label = label });
defer encoder.release(); defer encoder.release();
@ -177,5 +175,6 @@ fn renderFrame(
defer command.release(); defer command.release();
core.state().queue.submit(&[_]*gpu.CommandBuffer{command}); core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
// Present the frame
core.schedule(.present_frame); core.schedule(.present_frame);
} }

View file

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

View file

@ -1,4 +1,3 @@
// TODO(important): review all code in this file in-depth
const std = @import("std"); const std = @import("std");
const mach = @import("mach"); const mach = @import("mach");
const gpu = mach.gpu; const gpu = mach.gpu;
@ -35,17 +34,19 @@ pub const Mod = mach.Mod(@This());
pub const systems = .{ pub const systems = .{
.init = .{ .handler = init }, .init = .{ .handler = init },
.deinit = .{ .handler = deinit }, .deinit = .{ .handler = deinit },
.update = .{ .handler = update }, .tick = .{ .handler = tick },
.after_init = .{ .handler = afterInit }, .after_init = .{ .handler = afterInit },
.end_frame = .{ .handler = endFrame }, .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.schedule(.deinit); sprite_pipeline.schedule(.deinit);
glyphs.schedule(.deinit); glyphs.schedule(.deinit);
core.schedule(.deinit);
} }
fn init(sprite_pipeline: *gfx.SpritePipeline.Mod, glyphs: *Glyphs.Mod, game: *Mod) !void { fn init(core: *mach.Core.Mod, sprite_pipeline: *gfx.SpritePipeline.Mod, glyphs: *Glyphs.Mod, game: *Mod) !void {
core.schedule(.init);
sprite_pipeline.schedule(.init); sprite_pipeline.schedule(.init);
glyphs.schedule(.init); glyphs.schedule(.init);
@ -62,6 +63,7 @@ fn afterInit(
sprite_pipeline: *gfx.SpritePipeline.Mod, sprite_pipeline: *gfx.SpritePipeline.Mod,
glyphs: *Glyphs.Mod, glyphs: *Glyphs.Mod,
game: *Mod, game: *Mod,
core: *mach.Core.Mod,
) !void { ) !void {
// Create a sprite rendering pipeline // Create a sprite rendering pipeline
const texture = glyphs.state().texture; const texture = glyphs.state().texture;
@ -93,9 +95,11 @@ fn afterInit(
.time = 0, .time = 0,
.pipeline = pipeline, .pipeline = pipeline,
}); });
core.schedule(.start);
} }
fn update( fn tick(
entities: *mach.Entities.Mod, entities: *mach.Entities.Mod,
core: *mach.Core.Mod, core: *mach.Core.Mod,
sprite: *gfx.Sprite.Mod, sprite: *gfx.Sprite.Mod,
@ -253,6 +257,7 @@ fn endFrame(game: *Mod, core: *mach.Core.Mod) !void {
"glyphs [ FPS: {d} ] [ Sprites: {d} ]", "glyphs [ FPS: {d} ] [ Sprites: {d} ]",
.{ game.state().frame_count, game.state().sprites }, .{ game.state().frame_count, game.state().sprites },
); );
core.schedule(.update);
game.state().fps_timer.reset(); game.state().fps_timer.reset();
game.state().frame_count = 0; game.state().frame_count = 0;
} }

View file

@ -1,4 +1,3 @@
// TODO(important): review all code in this file in-depth
const mach = @import("mach"); const mach = @import("mach");
const gpu = mach.gpu; const gpu = mach.gpu;
const ft = @import("freetype"); const ft = @import("freetype");
@ -16,6 +15,7 @@ pub const systems = .{
const RegionMap = std.AutoArrayHashMapUnmanaged(u21, mach.gfx.Atlas.Region); const RegionMap = std.AutoArrayHashMapUnmanaged(u21, mach.gfx.Atlas.Region);
// TODO: banish global allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
texture_atlas: mach.gfx.Atlas, texture_atlas: mach.gfx.Atlas,

View file

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

View file

@ -15,6 +15,7 @@ const Vec3 = math.Vec3;
const Mat3x3 = math.Mat3x3; const Mat3x3 = math.Mat3x3;
const Mat4x4 = math.Mat4x4; const Mat4x4 = math.Mat4x4;
// TODO: banish global allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
info_text: mach.EntityID, info_text: mach.EntityID,
@ -44,18 +45,20 @@ pub const systems = .{
.init = .{ .handler = init }, .init = .{ .handler = init },
.after_init = .{ .handler = afterInit }, .after_init = .{ .handler = afterInit },
.deinit = .{ .handler = deinit }, .deinit = .{ .handler = deinit },
.update = .{ .handler = update }, .tick = .{ .handler = tick },
.end_frame = .{ .handler = endFrame }, .end_frame = .{ .handler = endFrame },
.audio_state_change = .{ .handler = audioStateChange }, .audio_state_change = .{ .handler = audioStateChange },
}; };
fn deinit( fn deinit(
core: *mach.Core.Mod,
text_pipeline: *gfx.TextPipeline.Mod, text_pipeline: *gfx.TextPipeline.Mod,
sprite_pipeline: *gfx.SpritePipeline.Mod, sprite_pipeline: *gfx.SpritePipeline.Mod,
audio: *mach.Audio.Mod, audio: *mach.Audio.Mod,
) !void { ) !void {
text_pipeline.schedule(.deinit); text_pipeline.schedule(.deinit);
sprite_pipeline.schedule(.deinit); sprite_pipeline.schedule(.deinit);
core.schedule(.deinit);
audio.schedule(.deinit); audio.schedule(.deinit);
} }
@ -64,11 +67,13 @@ fn init(
text_pipeline: *gfx.TextPipeline.Mod, text_pipeline: *gfx.TextPipeline.Mod,
text: *gfx.Text.Mod, text: *gfx.Text.Mod,
sprite_pipeline: *gfx.SpritePipeline.Mod, sprite_pipeline: *gfx.SpritePipeline.Mod,
core: *mach.Core.Mod,
game: *Mod, game: *Mod,
) !void { ) !void {
// If you want to try fullscreen: // If you want to try fullscreen:
// try core.set(core.state().main_window, .fullscreen, true); // try core.set(core.state().main_window, .fullscreen, true);
core.schedule(.init);
audio.schedule(.init); audio.schedule(.init);
text.schedule(.init); text.schedule(.init);
text_pipeline.schedule(.init); text_pipeline.schedule(.init);
@ -146,6 +151,7 @@ fn afterInit(
.pipeline = pipeline, .pipeline = pipeline,
.sfx = sfx, .sfx = sfx,
}); });
core.schedule(.start);
} }
fn audioStateChange(entities: *mach.Entities.Mod) !void { fn audioStateChange(entities: *mach.Entities.Mod) !void {
@ -164,7 +170,7 @@ fn audioStateChange(entities: *mach.Entities.Mod) !void {
} }
} }
fn update( fn tick(
entities: *mach.Entities.Mod, entities: *mach.Entities.Mod,
core: *mach.Core.Mod, core: *mach.Core.Mod,
sprite: *gfx.Sprite.Mod, sprite: *gfx.Sprite.Mod,

View file

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

View file

@ -18,6 +18,7 @@ const sysaudio = mach.sysaudio;
pub const App = @This(); pub const App = @This();
// TODO: banish global allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
pub const name = .app; pub const name = .app;
@ -27,7 +28,7 @@ pub const systems = .{
.init = .{ .handler = init }, .init = .{ .handler = init },
.after_init = .{ .handler = afterInit }, .after_init = .{ .handler = afterInit },
.deinit = .{ .handler = deinit }, .deinit = .{ .handler = deinit },
.update = .{ .handler = update }, .tick = .{ .handler = tick },
.audio_state_change = .{ .handler = audioStateChange }, .audio_state_change = .{ .handler = audioStateChange },
}; };
@ -37,7 +38,8 @@ pub const components = .{
ghost_key_mode: bool = false, ghost_key_mode: bool = false,
fn init(audio: *mach.Audio.Mod, app: *Mod) void { fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) void {
core.schedule(.init);
audio.schedule(.init); audio.schedule(.init);
app.schedule(.after_init); app.schedule(.after_init);
@ -49,6 +51,8 @@ fn init(audio: *mach.Audio.Mod, app: *Mod) void {
std.debug.print("[spacebar] enable ghost-key mode (demonstrate seamless back-to-back sound playback)\n", .{}); std.debug.print("[spacebar] enable ghost-key mode (demonstrate seamless back-to-back sound playback)\n", .{});
std.debug.print("[arrow up] increase volume 10%\n", .{}); std.debug.print("[arrow up] increase volume 10%\n", .{});
std.debug.print("[arrow down] decrease volume 10%\n", .{}); std.debug.print("[arrow down] decrease volume 10%\n", .{});
core.schedule(.start);
} }
fn afterInit(audio: *mach.Audio.Mod, app: *Mod) void { fn afterInit(audio: *mach.Audio.Mod, app: *Mod) void {
@ -57,8 +61,9 @@ fn afterInit(audio: *mach.Audio.Mod, app: *Mod) void {
audio.state().on_state_change = app.system(.audio_state_change); audio.state().on_state_change = app.system(.audio_state_change);
} }
fn deinit(audio: *mach.Audio.Mod) void { fn deinit(core: *mach.Core.Mod, audio: *mach.Audio.Mod) void {
audio.schedule(.deinit); audio.schedule(.deinit);
core.schedule(.deinit);
} }
fn audioStateChange( fn audioStateChange(
@ -90,7 +95,7 @@ fn audioStateChange(
} }
} }
fn update( fn tick(
entities: *mach.Entities.Mod, entities: *mach.Entities.Mod,
core: *mach.Core.Mod, core: *mach.Core.Mod,
audio: *mach.Audio.Mod, audio: *mach.Audio.Mod,

View file

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

View file

@ -12,6 +12,7 @@ const sysaudio = mach.sysaudio;
pub const App = @This(); pub const App = @This();
// TODO: banish global allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
pub const name = .app; pub const name = .app;
@ -21,7 +22,7 @@ pub const systems = .{
.init = .{ .handler = init }, .init = .{ .handler = init },
.after_init = .{ .handler = afterInit }, .after_init = .{ .handler = afterInit },
.deinit = .{ .handler = deinit }, .deinit = .{ .handler = deinit },
.update = .{ .handler = update }, .tick = .{ .handler = tick },
.audio_state_change = .{ .handler = audioStateChange }, .audio_state_change = .{ .handler = audioStateChange },
}; };
@ -33,9 +34,11 @@ sfx: mach.Audio.Opus,
fn init( fn init(
entities: *mach.Entities.Mod, entities: *mach.Entities.Mod,
core: *mach.Core.Mod,
audio: *mach.Audio.Mod, audio: *mach.Audio.Mod,
app: *Mod, app: *Mod,
) !void { ) !void {
core.schedule(.init);
audio.schedule(.init); audio.schedule(.init);
app.schedule(.after_init); app.schedule(.after_init);
@ -61,6 +64,8 @@ fn init(
std.debug.print("[typing] Play SFX\n", .{}); std.debug.print("[typing] Play SFX\n", .{});
std.debug.print("[arrow up] increase volume 10%\n", .{}); std.debug.print("[arrow up] increase volume 10%\n", .{});
std.debug.print("[arrow down] decrease volume 10%\n", .{}); std.debug.print("[arrow down] decrease volume 10%\n", .{});
core.schedule(.start);
} }
fn afterInit(audio: *mach.Audio.Mod, app: *Mod) void { fn afterInit(audio: *mach.Audio.Mod, app: *Mod) void {
@ -69,8 +74,9 @@ fn afterInit(audio: *mach.Audio.Mod, app: *Mod) void {
audio.state().on_state_change = app.system(.audio_state_change); audio.state().on_state_change = app.system(.audio_state_change);
} }
fn deinit(audio: *mach.Audio.Mod) void { fn deinit(core: *mach.Core.Mod, audio: *mach.Audio.Mod) void {
audio.schedule(.deinit); audio.schedule(.deinit);
core.schedule(.deinit);
} }
fn audioStateChange( fn audioStateChange(
@ -99,7 +105,7 @@ fn audioStateChange(
} }
} }
fn update( fn tick(
entities: *mach.Entities.Mod, entities: *mach.Entities.Mod,
core: *mach.Core.Mod, core: *mach.Core.Mod,
audio: *mach.Audio.Mod, audio: *mach.Audio.Mod,

View file

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

View file

@ -14,6 +14,7 @@ const Vec3 = math.Vec3;
const Mat3x3 = math.Mat3x3; const Mat3x3 = math.Mat3x3;
const Mat4x4 = math.Mat4x4; const Mat4x4 = math.Mat4x4;
// TODO: banish global allocator
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
timer: mach.Timer, timer: mach.Timer,
@ -40,20 +41,24 @@ pub const systems = .{
.init = .{ .handler = init }, .init = .{ .handler = init },
.deinit = .{ .handler = deinit }, .deinit = .{ .handler = deinit },
.after_init = .{ .handler = afterInit }, .after_init = .{ .handler = afterInit },
.update = .{ .handler = update }, .tick = .{ .handler = tick },
.end_frame = .{ .handler = endFrame }, .end_frame = .{ .handler = endFrame },
}; };
fn deinit( fn deinit(
core: *mach.Core.Mod,
sprite_pipeline: *gfx.SpritePipeline.Mod, sprite_pipeline: *gfx.SpritePipeline.Mod,
) !void { ) !void {
sprite_pipeline.schedule(.deinit); sprite_pipeline.schedule(.deinit);
core.schedule(.deinit);
} }
fn init( fn init(
core: *mach.Core.Mod,
sprite_pipeline: *gfx.SpritePipeline.Mod, sprite_pipeline: *gfx.SpritePipeline.Mod,
game: *Mod, game: *Mod,
) !void { ) !void {
core.schedule(.init);
sprite_pipeline.schedule(.init); sprite_pipeline.schedule(.init);
game.schedule(.after_init); game.schedule(.after_init);
} }
@ -95,9 +100,11 @@ fn afterInit(
.allocator = allocator, .allocator = allocator,
.pipeline = pipeline, .pipeline = pipeline,
}); });
core.schedule(.start);
} }
fn update( fn tick(
entities: *mach.Entities.Mod, entities: *mach.Entities.Mod,
core: *mach.Core.Mod, core: *mach.Core.Mod,
sprite: *gfx.Sprite.Mod, sprite: *gfx.Sprite.Mod,
@ -241,6 +248,7 @@ fn endFrame(game: *Mod, core: *mach.Core.Mod) !void {
"sprite [ FPS: {d} ] [ Sprites: {d} ]", "sprite [ FPS: {d} ] [ Sprites: {d} ]",
.{ game.state().frame_count, game.state().sprites }, .{ game.state().frame_count, game.state().sprites },
); );
core.schedule(.update);
game.state().fps_timer.reset(); game.state().fps_timer.reset();
game.state().frame_count = 0; game.state().frame_count = 0;
} }

View file

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

View file

@ -1,4 +1,3 @@
// TODO(important): review all code in this file in-depth
const std = @import("std"); const std = @import("std");
const zigimg = @import("zigimg"); const zigimg = @import("zigimg");
const assets = @import("assets"); const assets = @import("assets");
@ -38,7 +37,7 @@ pub const systems = .{
.init = .{ .handler = init }, .init = .{ .handler = init },
.deinit = .{ .handler = deinit }, .deinit = .{ .handler = deinit },
.after_init = .{ .handler = afterInit }, .after_init = .{ .handler = afterInit },
.update = .{ .handler = update }, .tick = .{ .handler = tick },
.end_frame = .{ .handler = endFrame }, .end_frame = .{ .handler = endFrame },
}; };
@ -52,15 +51,21 @@ const text1: []const []const u8 = &.{
const text2: []const []const u8 = &.{"$!?"}; const text2: []const []const u8 = &.{"$!?"};
fn deinit(text_pipeline: *gfx.TextPipeline.Mod) !void { fn deinit(
core: *mach.Core.Mod,
text_pipeline: *gfx.TextPipeline.Mod,
) !void {
text_pipeline.schedule(.deinit); text_pipeline.schedule(.deinit);
core.schedule(.deinit);
} }
fn init( fn init(
core: *mach.Core.Mod,
text: *gfx.Text.Mod, text: *gfx.Text.Mod,
text_pipeline: *gfx.TextPipeline.Mod, text_pipeline: *gfx.TextPipeline.Mod,
game: *Mod, game: *Mod,
) !void { ) !void {
core.schedule(.init);
text.schedule(.init); text.schedule(.init);
text_pipeline.schedule(.init); text_pipeline.schedule(.init);
game.schedule(.after_init); game.schedule(.after_init);
@ -68,6 +73,7 @@ fn init(
fn afterInit( fn afterInit(
entities: *mach.Entities.Mod, entities: *mach.Entities.Mod,
core: *mach.Core.Mod,
text: *gfx.Text.Mod, text: *gfx.Text.Mod,
text_pipeline: *gfx.TextPipeline.Mod, text_pipeline: *gfx.TextPipeline.Mod,
text_style: *gfx.TextStyle.Mod, text_style: *gfx.TextStyle.Mod,
@ -105,9 +111,11 @@ fn afterInit(
.style1 = style1, .style1 = style1,
.pipeline = pipeline, .pipeline = pipeline,
}); });
core.schedule(.start);
} }
fn update( fn tick(
entities: *mach.Entities.Mod, entities: *mach.Entities.Mod,
core: *mach.Core.Mod, core: *mach.Core.Mod,
text: *gfx.Text.Mod, text: *gfx.Text.Mod,
@ -268,6 +276,7 @@ fn endFrame(
"text [ FPS: {d} ] [ Texts: {d} ] [ Glyphs: {d} ]", "text [ FPS: {d} ] [ Texts: {d} ] [ Glyphs: {d} ]",
.{ game.state().frame_count, num_texts, num_glyphs }, .{ game.state().frame_count, num_texts, num_glyphs },
); );
core.schedule(.update);
game.state().fps_timer.reset(); game.state().fps_timer.reset();
game.state().frame_count = 0; game.state().frame_count = 0;
} }

View file

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