core: refactor

This commit is contained in:
Ali Cheraghi 2024-07-13 01:07:20 +03:30 committed by Stephen Gutekanst
parent c254337e4b
commit 266e7a548b
38 changed files with 4119 additions and 4836 deletions

View file

@ -7,25 +7,18 @@ pub const Mod = mach.Mod(@This());
pub const systems = .{
.init = .{ .handler = init },
.after_init = .{ .handler = afterInit },
.deinit = .{ .handler = deinit },
.tick = .{ .handler = tick },
.update = .{ .handler = update },
};
title_timer: mach.Timer,
pipeline: *gpu.RenderPipeline,
pub fn deinit(core: *mach.Core.Mod, game: *Mod) void {
pub fn deinit(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();
@ -64,14 +57,12 @@ fn afterInit(game: *Mod, core: *mach.Core.Mod) !void {
.pipeline = pipeline,
});
try updateWindowTitle(core);
core.schedule(.start);
}
fn tick(core: *mach.Core.Mod, game: *Mod) !void {
fn update(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 = mach.core.pollEvents();
var iter = core.state().pollEvents();
while (iter.next()) |event| {
switch (event) {
.close => core.schedule(.exit), // Tell mach.Core to exit the app
@ -81,7 +72,7 @@ fn tick(core: *mach.Core.Mod, game: *Mod) !void {
// Grab the back buffer of the swapchain
// TODO(Core)
const back_buffer_view = mach.core.swap_chain.getCurrentTextureView().?;
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();
// Create a command encoder
@ -115,9 +106,6 @@ fn tick(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
if (game.state().title_timer.read() >= 1.0) {
game.state().title_timer.reset();
@ -126,15 +114,13 @@ fn tick(core: *mach.Core.Mod, game: *Mod) !void {
}
fn updateWindowTitle(core: *mach.Core.Mod) !void {
try mach.Core.printTitle(
core,
try core.state().printTitle(
core.state().main_window,
"core-custom-entrypoint [ {d}fps ] [ Input {d}hz ]",
.{
// TODO(Core)
mach.core.frameRate(),
mach.core.inputRate(),
core.state().frameRate(),
core.state().inputRate(),
},
);
core.schedule(.update);
}

View file

@ -1,3 +1,4 @@
const std = @import("std");
const mach = @import("mach");
// The global list of Mach modules registered for use in our application.
@ -7,9 +8,11 @@ pub const modules = .{
};
pub fn main() !void {
// Initialize mach.Core
try mach.core.initModule();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Main loop
while (try mach.core.tick()) {}
var app = try mach.App.init(allocator, .app);
defer app.deinit(allocator);
try app.run(.{ .allocator = allocator });
}

View file

@ -7,25 +7,18 @@ pub const Mod = mach.Mod(@This());
pub const systems = .{
.init = .{ .handler = init },
.after_init = .{ .handler = afterInit },
.deinit = .{ .handler = deinit },
.tick = .{ .handler = tick },
.update = .{ .handler = update },
};
title_timer: mach.Timer,
pipeline: *gpu.RenderPipeline,
pub fn deinit(core: *mach.Core.Mod, game: *Mod) void {
pub fn deinit(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();
@ -63,15 +56,11 @@ fn afterInit(game: *Mod, core: *mach.Core.Mod) !void {
.title_timer = try mach.Timer.start(),
.pipeline = pipeline,
});
try updateWindowTitle(core);
core.schedule(.start);
// try updateWindowTitle(core);
}
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 = mach.core.pollEvents();
fn update(core: *mach.Core.Mod, game: *Mod) !void {
var iter = core.state().pollEvents();
while (iter.next()) |event| {
switch (event) {
.close => core.schedule(.exit), // Tell mach.Core to exit the app
@ -81,11 +70,11 @@ fn tick(core: *mach.Core.Mod, game: *Mod) !void {
// Grab the back buffer of the swapchain
// TODO(Core)
const back_buffer_view = mach.core.swap_chain.getCurrentTextureView().?;
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();
// Create a command encoder
const label = @tagName(name) ++ ".tick";
const label = @tagName(name) ++ ".update";
const encoder = core.state().device.createCommandEncoder(&.{ .label = label });
defer encoder.release();
@ -115,9 +104,6 @@ fn tick(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
if (game.state().title_timer.read() >= 1.0) {
game.state().title_timer.reset();
@ -126,15 +112,13 @@ fn tick(core: *mach.Core.Mod, game: *Mod) !void {
}
fn updateWindowTitle(core: *mach.Core.Mod) !void {
try mach.Core.printTitle(
core,
try core.state().printTitle(
core.state().main_window,
"core-custom-entrypoint [ {d}fps ] [ Input {d}hz ]",
.{
// TODO(Core)
mach.core.frameRate(),
mach.core.inputRate(),
core.state().frameRate(),
core.state().inputRate(),
},
);
core.schedule(.update);
}

View file

@ -1,3 +1,4 @@
const std = @import("std");
const mach = @import("mach");
// The global list of Mach modules registered for use in our application.
@ -7,9 +8,11 @@ pub const modules = .{
};
pub fn main() !void {
// Initialize mach.Core
try mach.core.initModule();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Main loop
while (try mach.core.tick()) {}
var app = try mach.App.init(allocator, .app);
defer app.deinit(allocator);
try app.run(.{ .allocator = allocator });
}

View file

@ -25,7 +25,7 @@ pub const components = .{
pub const systems = .{
.init = .{ .handler = init },
.deinit = .{ .handler = deinit },
.tick = .{ .handler = tick },
.update = .{ .handler = update },
};
// Define the globally unique name of our module. You can use any name here, but keep in mind no
@ -38,9 +38,8 @@ pub const name = .app;
// Note that Mod.state() returns an instance of our module struct.
pub const Mod = mach.Mod(@This());
pub fn deinit(core: *mach.Core.Mod, renderer: *Renderer.Mod) void {
pub fn deinit(renderer: *Renderer.Mod) void {
renderer.schedule(.deinit);
core.schedule(.deinit);
}
fn init(
@ -48,11 +47,9 @@ 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.
@ -76,11 +73,9 @@ fn init(
.spawn_timer = try mach.Timer.start(),
.player = player,
});
core.schedule(.start);
}
fn tick(
fn update(
entities: *mach.Entities.Mod,
core: *mach.Core.Mod,
renderer: *Renderer.Mod,
@ -88,7 +83,7 @@ fn tick(
) !void {
// TODO(important): event polling should occur in mach.Core module and get fired as ECS event.
// TODO(Core)
var iter = mach.core.pollEvents();
var iter = core.state().pollEvents();
var direction = game.state().direction;
var spawning = game.state().spawning;
while (iter.next()) |event| {

View file

@ -125,11 +125,11 @@ fn renderFrame(
) !void {
// Grab the back buffer of the swapchain
// TODO(Core)
const back_buffer_view = mach.core.swap_chain.getCurrentTextureView().?;
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();
// Create a command encoder
const label = @tagName(name) ++ ".tick";
const label = @tagName(name) ++ ".renderFrame";
const encoder = core.state().device.createCommandEncoder(&.{ .label = label });
defer encoder.release();
@ -178,7 +178,4 @@ fn renderFrame(
var command = encoder.finish(&.{ .label = label });
defer command.release();
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
// Present the frame
core.schedule(.present_frame);
}

View file

@ -9,11 +9,12 @@ pub const modules = .{
@import("Renderer.zig"),
};
// TODO: move this to a mach "entrypoint" zig module
pub fn main() !void {
// Initialize mach core
try mach.core.initModule();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Main loop
while (try mach.core.tick()) {}
var app = try mach.App.init(allocator, .app);
defer app.deinit(allocator);
try app.run(.{ .allocator = allocator });
}

View file

@ -35,19 +35,17 @@ pub const Mod = mach.Mod(@This());
pub const systems = .{
.init = .{ .handler = init },
.deinit = .{ .handler = deinit },
.tick = .{ .handler = tick },
.update = .{ .handler = update },
.after_init = .{ .handler = afterInit },
.end_frame = .{ .handler = endFrame },
};
fn deinit(core: *mach.Core.Mod, sprite_pipeline: *gfx.SpritePipeline.Mod, glyphs: *Glyphs.Mod) !void {
fn deinit(sprite_pipeline: *gfx.SpritePipeline.Mod, glyphs: *Glyphs.Mod) !void {
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 {
core.schedule(.init);
fn init(sprite_pipeline: *gfx.SpritePipeline.Mod, glyphs: *Glyphs.Mod, game: *Mod) !void {
sprite_pipeline.schedule(.init);
glyphs.schedule(.init);
@ -64,7 +62,6 @@ fn afterInit(
sprite_pipeline: *gfx.SpritePipeline.Mod,
glyphs: *Glyphs.Mod,
game: *Mod,
core: *mach.Core.Mod,
) !void {
// Create a sprite rendering pipeline
const texture = glyphs.state().texture;
@ -96,11 +93,9 @@ fn afterInit(
.time = 0,
.pipeline = pipeline,
});
core.schedule(.start);
}
fn tick(
fn update(
entities: *mach.Entities.Mod,
core: *mach.Core.Mod,
sprite: *gfx.Sprite.Mod,
@ -110,7 +105,7 @@ fn tick(
) !void {
// TODO(important): event polling should occur in mach.Core module and get fired as ECS events.
// TODO(Core)
var iter = mach.core.pollEvents();
var iter = core.state().pollEvents();
var direction = game.state().direction;
var spawning = game.state().spawning;
while (iter.next()) |event| {
@ -177,7 +172,7 @@ fn tick(
var location = entity_transform.translation();
// TODO: formatting
// TODO(Core)
if (location.x() < -@as(f32, @floatFromInt(mach.core.size().width)) / 1.5 or location.x() > @as(f32, @floatFromInt(mach.core.size().width)) / 1.5 or location.y() < -@as(f32, @floatFromInt(mach.core.size().height)) / 1.5 or location.y() > @as(f32, @floatFromInt(mach.core.size().height)) / 1.5) {
if (location.x() < -@as(f32, @floatFromInt(core.state().size().width)) / 1.5 or location.x() > @as(f32, @floatFromInt(core.state().size().width)) / 1.5 or location.y() < -@as(f32, @floatFromInt(core.state().size().height)) / 1.5 or location.y() > @as(f32, @floatFromInt(core.state().size().height)) / 1.5) {
try entities.remove(id);
game.state().sprites -= 1;
continue;
@ -212,7 +207,7 @@ fn tick(
// Grab the back buffer of the swapchain
// TODO(Core)
const back_buffer_view = mach.core.swap_chain.getCurrentTextureView().?;
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();
// Begin render pass
@ -253,13 +248,11 @@ fn endFrame(game: *Mod, core: *mach.Core.Mod) !void {
// Every second, update the window title with the FPS
if (game.state().fps_timer.read() >= 1.0) {
try mach.Core.printTitle(
core,
try core.state().printTitle(
core.state().main_window,
"glyphs [ FPS: {d} ] [ Sprites: {d} ]",
.{ game.state().frame_count, game.state().sprites },
);
core.schedule(.update);
game.state().fps_timer.reset();
game.state().frame_count = 0;
}

View file

@ -1,3 +1,4 @@
const std = @import("std");
const mach = @import("mach");
// The global list of Mach modules registered for use in our application.
@ -10,9 +11,11 @@ pub const modules = .{
// TODO(important): use standard entrypoint instead
pub fn main() !void {
// Initialize mach.Core
try mach.core.initModule();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Main loop
while (try mach.core.tick()) {}
var app = try mach.App.init(allocator, .app);
defer app.deinit(allocator);
try app.run(.{ .allocator = allocator });
}

View file

@ -44,20 +44,18 @@ pub const systems = .{
.init = .{ .handler = init },
.after_init = .{ .handler = afterInit },
.deinit = .{ .handler = deinit },
.tick = .{ .handler = tick },
.update = .{ .handler = update },
.end_frame = .{ .handler = endFrame },
.audio_state_change = .{ .handler = audioStateChange },
};
fn deinit(
core: *mach.Core.Mod,
text_pipeline: *gfx.TextPipeline.Mod,
sprite_pipeline: *gfx.SpritePipeline.Mod,
audio: *mach.Audio.Mod,
) !void {
text_pipeline.schedule(.deinit);
sprite_pipeline.schedule(.deinit);
core.schedule(.deinit);
audio.schedule(.deinit);
}
@ -66,13 +64,11 @@ fn init(
text_pipeline: *gfx.TextPipeline.Mod,
text: *gfx.Text.Mod,
sprite_pipeline: *gfx.SpritePipeline.Mod,
core: *mach.Core.Mod,
game: *Mod,
) !void {
// If you want to try fullscreen:
// try core.set(core.state().main_window, .fullscreen, true);
core.schedule(.init);
audio.schedule(.init);
text.schedule(.init);
text_pipeline.schedule(.init);
@ -150,7 +146,6 @@ fn afterInit(
.pipeline = pipeline,
.sfx = sfx,
});
core.schedule(.start);
}
fn audioStateChange(entities: *mach.Entities.Mod) !void {
@ -169,7 +164,7 @@ fn audioStateChange(entities: *mach.Entities.Mod) !void {
}
}
fn tick(
fn update(
entities: *mach.Entities.Mod,
core: *mach.Core.Mod,
sprite: *gfx.Sprite.Mod,
@ -181,7 +176,7 @@ fn tick(
) !void {
// TODO(important): event polling should occur in mach.Core module and get fired as ECS events.
// TODO(Core)
var iter = mach.core.pollEvents();
var iter = core.state().pollEvents();
var gotta_go_fast = game.state().gotta_go_fast;
while (iter.next()) |event| {
switch (event) {
@ -286,7 +281,7 @@ fn tick(
// Grab the back buffer of the swapchain
// TODO(Core)
const back_buffer_view = mach.core.swap_chain.getCurrentTextureView().?;
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();
// Begin render pass

View file

@ -1,3 +1,4 @@
const std = @import("std");
const mach = @import("mach");
// The global list of Mach modules registered for use in our application.
@ -11,9 +12,11 @@ pub const modules = .{
// TODO(important): use standard entrypoint instead
pub fn main() !void {
// Initialize mach.Core
try mach.core.initModule();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Main loop
while (try mach.core.tick()) {}
var app = try mach.App.init(allocator, .app);
defer app.deinit(allocator);
try app.run(.{ .allocator = allocator });
}

View file

@ -27,7 +27,7 @@ pub const systems = .{
.init = .{ .handler = init },
.after_init = .{ .handler = afterInit },
.deinit = .{ .handler = deinit },
.tick = .{ .handler = tick },
.update = .{ .handler = update },
.audio_state_change = .{ .handler = audioStateChange },
};
@ -37,8 +37,7 @@ pub const components = .{
ghost_key_mode: bool = false,
fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) void {
core.schedule(.init);
fn init(audio: *mach.Audio.Mod, app: *Mod) void {
audio.schedule(.init);
app.schedule(.after_init);
@ -50,8 +49,6 @@ fn init(core: *mach.Core.Mod, 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("[arrow up] increase volume 10%\n", .{});
std.debug.print("[arrow down] decrease volume 10%\n", .{});
core.schedule(.start);
}
fn afterInit(audio: *mach.Audio.Mod, app: *Mod) void {
@ -60,9 +57,8 @@ fn afterInit(audio: *mach.Audio.Mod, app: *Mod) void {
audio.state().on_state_change = app.system(.audio_state_change);
}
fn deinit(core: *mach.Core.Mod, audio: *mach.Audio.Mod) void {
fn deinit(audio: *mach.Audio.Mod) void {
audio.schedule(.deinit);
core.schedule(.deinit);
}
fn audioStateChange(
@ -94,14 +90,14 @@ fn audioStateChange(
}
}
fn tick(
fn update(
entities: *mach.Entities.Mod,
core: *mach.Core.Mod,
audio: *mach.Audio.Mod,
app: *Mod,
) !void {
// TODO(Core)
var iter = mach.core.pollEvents();
var iter = core.state().pollEvents();
while (iter.next()) |event| {
switch (event) {
.key_press => |ev| {
@ -143,7 +139,7 @@ fn tick(
// Grab the back buffer of the swapchain
// TODO(Core)
const back_buffer_view = mach.core.swap_chain.getCurrentTextureView().?;
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();
// Create a command encoder
@ -208,7 +204,7 @@ fn fillTone(audio: *mach.Audio.Mod, frequency: f32) ![]const f32 {
}
// TODO(Core)
fn keyToFrequency(key: mach.core.Key) f32 {
fn keyToFrequency(key: mach.Core.Key) f32 {
// The frequencies here just come from a piano frequencies chart. You can google for them.
return switch (key) {
// First row of piano keys, the highest.

View file

@ -1,3 +1,4 @@
const std = @import("std");
const mach = @import("mach");
// The global list of Mach modules registered for use in our application.
@ -9,9 +10,11 @@ pub const modules = .{
// TODO(important): use standard entrypoint instead
pub fn main() !void {
// Initialize mach.Core
try mach.core.initModule();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Main loop
while (try mach.core.tick()) {}
var app = try mach.App.init(allocator, .app);
defer app.deinit(allocator);
try app.run(.{ .allocator = allocator });
}

View file

@ -21,7 +21,7 @@ pub const systems = .{
.init = .{ .handler = init },
.after_init = .{ .handler = afterInit },
.deinit = .{ .handler = deinit },
.tick = .{ .handler = tick },
.update = .{ .handler = update },
.audio_state_change = .{ .handler = audioStateChange },
};
@ -33,11 +33,9 @@ sfx: mach.Audio.Opus,
fn init(
entities: *mach.Entities.Mod,
core: *mach.Core.Mod,
audio: *mach.Audio.Mod,
app: *Mod,
) !void {
core.schedule(.init);
audio.schedule(.init);
app.schedule(.after_init);
@ -63,8 +61,6 @@ fn init(
std.debug.print("[typing] Play SFX\n", .{});
std.debug.print("[arrow up] increase volume 10%\n", .{});
std.debug.print("[arrow down] decrease volume 10%\n", .{});
core.schedule(.start);
}
fn afterInit(audio: *mach.Audio.Mod, app: *Mod) void {
@ -104,14 +100,14 @@ fn audioStateChange(
}
}
fn tick(
fn update(
entities: *mach.Entities.Mod,
core: *mach.Core.Mod,
audio: *mach.Audio.Mod,
app: *Mod,
) !void {
// TODO(Core)
var iter = mach.core.pollEvents();
var iter = core.state().pollEvents();
while (iter.next()) |event| {
switch (event) {
.key_press => |ev| switch (ev.key) {
@ -141,7 +137,7 @@ fn tick(
// Grab the back buffer of the swapchain
// TODO(Core)
const back_buffer_view = mach.core.swap_chain.getCurrentTextureView().?;
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();
// Create a command encoder

View file

@ -1,3 +1,4 @@
const std = @import("std");
const mach = @import("mach");
// The global list of Mach modules registered for use in our application.
@ -9,9 +10,11 @@ pub const modules = .{
// TODO(important): use standard entrypoint instead
pub fn main() !void {
// Initialize mach.Core
try mach.core.initModule();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Main loop
while (try mach.core.tick()) {}
var app = try mach.App.init(allocator, .app);
defer app.deinit(allocator);
try app.run(.{ .allocator = allocator });
}

View file

@ -40,7 +40,7 @@ pub const systems = .{
.init = .{ .handler = init },
.deinit = .{ .handler = deinit },
.after_init = .{ .handler = afterInit },
.tick = .{ .handler = tick },
.update = .{ .handler = update },
.end_frame = .{ .handler = endFrame },
};
@ -53,11 +53,9 @@ fn deinit(
}
fn init(
core: *mach.Core.Mod,
sprite_pipeline: *gfx.SpritePipeline.Mod,
game: *Mod,
) !void {
core.schedule(.init);
sprite_pipeline.schedule(.init);
game.schedule(.after_init);
}
@ -99,11 +97,9 @@ fn afterInit(
.allocator = allocator,
.pipeline = pipeline,
});
core.schedule(.start);
}
fn tick(
fn update(
entities: *mach.Entities.Mod,
core: *mach.Core.Mod,
sprite: *gfx.Sprite.Mod,
@ -112,7 +108,7 @@ fn tick(
) !void {
// TODO(important): event polling should occur in mach.Core module and get fired as ECS events.
// TODO(Core)
var iter = mach.core.pollEvents();
var iter = core.state().pollEvents();
var direction = game.state().direction;
var spawning = game.state().spawning;
while (iter.next()) |event| {
@ -201,7 +197,7 @@ fn tick(
// Grab the back buffer of the swapchain
// TODO(Core)
const back_buffer_view = mach.core.swap_chain.getCurrentTextureView().?;
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();
// Begin render pass
@ -242,13 +238,11 @@ fn endFrame(game: *Mod, core: *mach.Core.Mod) !void {
// Every second, update the window title with the FPS
if (game.state().fps_timer.read() >= 1.0) {
try mach.Core.printTitle(
core,
try core.state().printTitle(
core.state().main_window,
"sprite [ FPS: {d} ] [ Sprites: {d} ]",
.{ game.state().frame_count, game.state().sprites },
);
core.schedule(.update);
game.state().fps_timer.reset();
game.state().frame_count = 0;
}

View file

@ -1,3 +1,4 @@
const std = @import("std");
const mach = @import("mach");
// The global list of Mach modules registered for use in our application.
@ -9,9 +10,11 @@ pub const modules = .{
// TODO(important): use standard entrypoint instead
pub fn main() !void {
// Initialize mach.Core
try mach.core.initModule();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Main loop
while (try mach.core.tick()) {}
var app = try mach.App.init(allocator, .app);
defer app.deinit(allocator);
try app.run(.{ .allocator = allocator });
}

View file

@ -38,7 +38,7 @@ pub const systems = .{
.init = .{ .handler = init },
.deinit = .{ .handler = deinit },
.after_init = .{ .handler = afterInit },
.tick = .{ .handler = tick },
.update = .{ .handler = update },
.end_frame = .{ .handler = endFrame },
};
@ -52,21 +52,15 @@ const text1: []const []const u8 = &.{
const text2: []const []const u8 = &.{"$!?"};
fn deinit(
core: *mach.Core.Mod,
text_pipeline: *gfx.TextPipeline.Mod,
) !void {
fn deinit(text_pipeline: *gfx.TextPipeline.Mod) !void {
text_pipeline.schedule(.deinit);
core.schedule(.deinit);
}
fn init(
core: *mach.Core.Mod,
text: *gfx.Text.Mod,
text_pipeline: *gfx.TextPipeline.Mod,
game: *Mod,
) !void {
core.schedule(.init);
text.schedule(.init);
text_pipeline.schedule(.init);
game.schedule(.after_init);
@ -74,7 +68,6 @@ fn init(
fn afterInit(
entities: *mach.Entities.Mod,
core: *mach.Core.Mod,
text: *gfx.Text.Mod,
text_pipeline: *gfx.TextPipeline.Mod,
text_style: *gfx.TextStyle.Mod,
@ -112,11 +105,9 @@ fn afterInit(
.style1 = style1,
.pipeline = pipeline,
});
core.schedule(.start);
}
fn tick(
fn update(
entities: *mach.Entities.Mod,
core: *mach.Core.Mod,
text: *gfx.Text.Mod,
@ -125,7 +116,7 @@ fn tick(
) !void {
// TODO(important): event polling should occur in mach.Core module and get fired as ECS events.
// TODO(Core)
var iter = mach.core.pollEvents();
var iter = core.state().pollEvents();
var direction = game.state().direction;
var spawning = game.state().spawning;
while (iter.next()) |event| {
@ -214,7 +205,7 @@ fn tick(
// Grab the back buffer of the swapchain
// TODO(Core)
const back_buffer_view = mach.core.swap_chain.getCurrentTextureView().?;
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
defer back_buffer_view.release();
// Begin render pass
@ -272,13 +263,11 @@ fn endFrame(
}
}
try mach.Core.printTitle(
core,
try core.state().printTitle(
core.state().main_window,
"text [ FPS: {d} ] [ Texts: {d} ] [ Glyphs: {d} ]",
.{ game.state().frame_count, num_texts, num_glyphs },
);
core.schedule(.update);
game.state().fps_timer.reset();
game.state().frame_count = 0;
}

View file

@ -1,3 +1,4 @@
const std = @import("std");
const mach = @import("mach");
// The global list of Mach modules registered for use in our application.
@ -9,9 +10,11 @@ pub const modules = .{
// TODO(important): use standard entrypoint instead
pub fn main() !void {
// Initialize mach.Core
try mach.core.initModule();
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Main loop
while (try mach.core.tick()) {}
var app = try mach.App.init(allocator, .app);
defer app.deinit(allocator);
try app.run(.{ .allocator = allocator });
}