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

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