core: refactor
This commit is contained in:
parent
c254337e4b
commit
266e7a548b
38 changed files with 4119 additions and 4836 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 });
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue