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);
}