examples/core: building without ECS
Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
parent
2a13c07d9e
commit
0e12857154
35 changed files with 1365 additions and 4176 deletions
|
|
@ -1,35 +1,36 @@
|
|||
const mach = @import("mach");
|
||||
const gpu = mach.gpu;
|
||||
|
||||
pub const name = .app;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
const App = @This();
|
||||
|
||||
pub const systems = .{
|
||||
.start = .{ .handler = start },
|
||||
.init = .{ .handler = init },
|
||||
.deinit = .{ .handler = deinit },
|
||||
.tick = .{ .handler = tick },
|
||||
};
|
||||
pub const mach_module = .app;
|
||||
|
||||
pub const mach_systems = .{ .main, .init, .deinit, .tick };
|
||||
|
||||
title_timer: mach.time.Timer,
|
||||
pipeline: *gpu.RenderPipeline,
|
||||
|
||||
pub fn deinit(core: *mach.Core.Mod, app: *Mod) void {
|
||||
app.state().pipeline.release();
|
||||
core.schedule(.deinit);
|
||||
pub const main = mach.schedule(.{
|
||||
.{ mach.Core, .init },
|
||||
.{ App, .init },
|
||||
.{ mach.Core, .main },
|
||||
});
|
||||
|
||||
pub fn deinit(app: *App) void {
|
||||
app.pipeline.release();
|
||||
}
|
||||
|
||||
fn start(app: *Mod, core: *mach.Core.Mod) !void {
|
||||
core.schedule(.init);
|
||||
app.schedule(.init);
|
||||
}
|
||||
|
||||
fn init(app: *Mod, core: *mach.Core.Mod) !void {
|
||||
core.state().on_tick = app.system(.tick);
|
||||
core.state().on_exit = app.system(.deinit);
|
||||
pub fn init(
|
||||
app: *App,
|
||||
core: *mach.Core,
|
||||
app_tick: mach.Call(App, .tick),
|
||||
app_deinit: mach.Call(App, .deinit),
|
||||
) !void {
|
||||
core.on_tick = app_tick.id;
|
||||
core.on_exit = app_deinit.id;
|
||||
|
||||
// Create our shader module
|
||||
const shader_module = core.state().device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
|
||||
const shader_module = core.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
|
||||
defer shader_module.release();
|
||||
|
||||
// Blend state describes how rendered colors get blended
|
||||
|
|
@ -37,7 +38,7 @@ fn init(app: *Mod, core: *mach.Core.Mod) !void {
|
|||
|
||||
// Color target describes e.g. the pixel format of the window we are rendering to.
|
||||
const color_target = gpu.ColorTargetState{
|
||||
.format = core.get(core.state().main_window, .framebuffer_format).?,
|
||||
.format = core.windows.get(core.main_window).?.framebuffer_format,
|
||||
.blend = &blend,
|
||||
};
|
||||
|
||||
|
|
@ -49,7 +50,7 @@ fn init(app: *Mod, core: *mach.Core.Mod) !void {
|
|||
});
|
||||
|
||||
// Create our render pipeline that will ultimately get pixels onto the screen.
|
||||
const label = @tagName(name) ++ ".init";
|
||||
const label = @tagName(mach_module) ++ ".init";
|
||||
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
|
||||
.label = label,
|
||||
.fragment = &fragment,
|
||||
|
|
@ -58,34 +59,33 @@ fn init(app: *Mod, core: *mach.Core.Mod) !void {
|
|||
.entry_point = "vertex_main",
|
||||
},
|
||||
};
|
||||
const pipeline = core.state().device.createRenderPipeline(&pipeline_descriptor);
|
||||
const pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
|
||||
|
||||
// Store our render pipeline in our module's state, so we can access it later on.
|
||||
app.init(.{
|
||||
.title_timer = try mach.time.Timer.start(),
|
||||
.pipeline = pipeline,
|
||||
});
|
||||
try updateWindowTitle(core);
|
||||
// TODO(object): module-state-init
|
||||
app.title_timer = try mach.time.Timer.start();
|
||||
app.pipeline = pipeline;
|
||||
|
||||
core.schedule(.start);
|
||||
// TODO(object): window-title
|
||||
// try updateWindowTitle(core);
|
||||
}
|
||||
|
||||
fn tick(core: *mach.Core.Mod, app: *Mod) !void {
|
||||
while (core.state().nextEvent()) |event| {
|
||||
pub fn tick(core: *mach.Core, app: *App) !void {
|
||||
while (core.nextEvent()) |event| {
|
||||
switch (event) {
|
||||
.close => core.schedule(.exit), // Tell mach.Core to exit the app
|
||||
.close => core.exit(),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
// Grab the back buffer of the swapchain
|
||||
// TODO(Core)
|
||||
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
|
||||
const back_buffer_view = core.swap_chain.getCurrentTextureView().?;
|
||||
defer back_buffer_view.release();
|
||||
|
||||
// Create a command encoder
|
||||
const label = @tagName(name) ++ ".tick";
|
||||
const encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
||||
const label = @tagName(mach_module) ++ ".tick";
|
||||
const encoder = core.device.createCommandEncoder(&.{ .label = label });
|
||||
defer encoder.release();
|
||||
|
||||
// Begin render pass
|
||||
|
|
@ -103,7 +103,7 @@ fn tick(core: *mach.Core.Mod, app: *Mod) !void {
|
|||
defer render_pass.release();
|
||||
|
||||
// Draw
|
||||
render_pass.setPipeline(app.state().pipeline);
|
||||
render_pass.setPipeline(app.pipeline);
|
||||
render_pass.draw(3, 1, 0, 0);
|
||||
|
||||
// Finish render pass
|
||||
|
|
@ -112,27 +112,26 @@ fn tick(core: *mach.Core.Mod, app: *Mod) !void {
|
|||
// Submit our commands to the queue
|
||||
var command = encoder.finish(&.{ .label = label });
|
||||
defer command.release();
|
||||
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
|
||||
// Present the frame
|
||||
core.schedule(.present_frame);
|
||||
core.queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
|
||||
// update the window title every second
|
||||
if (app.state().title_timer.read() >= 1.0) {
|
||||
app.state().title_timer.reset();
|
||||
try updateWindowTitle(core);
|
||||
if (app.title_timer.read() >= 1.0) {
|
||||
app.title_timer.reset();
|
||||
// TODO(object): window-title
|
||||
// try updateWindowTitle(core);
|
||||
}
|
||||
}
|
||||
|
||||
fn updateWindowTitle(core: *mach.Core.Mod) !void {
|
||||
try core.state().printTitle(
|
||||
core.state().main_window,
|
||||
"core-custom-entrypoint [ {d}fps ] [ Input {d}hz ]",
|
||||
.{
|
||||
// TODO(Core)
|
||||
core.state().frameRate(),
|
||||
core.state().inputRate(),
|
||||
},
|
||||
);
|
||||
core.schedule(.update);
|
||||
}
|
||||
// TODO(object): window-title
|
||||
// fn updateWindowTitle(core: *mach.Core) !void {
|
||||
// try core.printTitle(
|
||||
// core.main_window,
|
||||
// "core-custom-entrypoint [ {d}fps ] [ Input {d}hz ]",
|
||||
// .{
|
||||
// // TODO(Core)
|
||||
// core.frameRate(),
|
||||
// core.inputRate(),
|
||||
// },
|
||||
// );
|
||||
// core.schedule(.update);
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -1,40 +1,37 @@
|
|||
const std = @import("std");
|
||||
const mach = @import("mach");
|
||||
|
||||
// The global list of Mach modules our application may use.
|
||||
pub const modules = .{
|
||||
// The set of Mach modules our application may use.
|
||||
const Modules = mach.Modules(.{
|
||||
mach.Core,
|
||||
@import("App.zig"),
|
||||
};
|
||||
});
|
||||
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.c_allocator;
|
||||
|
||||
// Initialize module system
|
||||
try mach.mods.init(allocator);
|
||||
// The set of Mach modules our application may use.
|
||||
var mods = Modules.init(allocator);
|
||||
|
||||
// Schedule .app.start to run.
|
||||
mach.mods.schedule(.app, .start);
|
||||
|
||||
// If desired, it is possible to observe when the app has finished starting by dispatching
|
||||
// systems until the app has started:
|
||||
try mach.mods.dispatchUntil(.mach_core, .started);
|
||||
|
||||
// On some platforms, you can drive the mach.Core main loop yourself - but this isn't
|
||||
// possible on all platforms.
|
||||
// On some platforms, you can drive the mach.Core main loop yourself - but this isn't possible
|
||||
// on all platforms. If mach.Core.non_blocking is set to true, and the platform supports
|
||||
// non-blocking mode, then .mach_core.main will return without blocking. Otherwise it will block
|
||||
// forever and app.run(.main) will never return.
|
||||
if (mach.Core.supports_non_blocking) {
|
||||
defer mods.deinit(allocator);
|
||||
|
||||
mach.Core.non_blocking = true;
|
||||
while (mach.mods.mod.mach_core.state().state != .exited) {
|
||||
// Execute systems until a frame has been finished.
|
||||
try mach.mods.dispatchUntil(.mach_core, .frame_finished);
|
||||
|
||||
const app = mods.get(.app);
|
||||
app.run(.main);
|
||||
|
||||
// If you are driving the main loop yourself, you should call tick until exit.
|
||||
const core = mods.get(.mach_core);
|
||||
while (mods.mods.mach_core.state != .exited) {
|
||||
core.run(.tick);
|
||||
}
|
||||
} else {
|
||||
// On platforms where you cannot control the mach.Core main loop, the .mach_core.start
|
||||
// system your app schedules will block forever and the function call below will NEVER
|
||||
// return (std.process.exit will occur first.)
|
||||
//
|
||||
// In this case we can just dispatch systems until there are no more left to execute, which
|
||||
// conviently works even if you aren't using mach.Core in your program.
|
||||
try mach.mods.dispatch(.{});
|
||||
const app = mods.get(.app);
|
||||
app.run(.main);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,35 +1,32 @@
|
|||
const mach = @import("mach");
|
||||
const gpu = mach.gpu;
|
||||
|
||||
pub const name = .app;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
const App = @This();
|
||||
|
||||
pub const systems = .{
|
||||
.start = .{ .handler = start },
|
||||
.init = .{ .handler = init },
|
||||
.deinit = .{ .handler = deinit },
|
||||
.tick = .{ .handler = tick },
|
||||
};
|
||||
pub const mach_module = .app;
|
||||
|
||||
pub const mach_systems = .{ .main, .init, .tick, .deinit };
|
||||
|
||||
pub const main = mach.schedule(.{
|
||||
.{ mach.Core, .init },
|
||||
.{ App, .init },
|
||||
.{ mach.Core, .main },
|
||||
});
|
||||
|
||||
title_timer: mach.time.Timer,
|
||||
pipeline: *gpu.RenderPipeline,
|
||||
|
||||
pub fn deinit(core: *mach.Core.Mod, app: *Mod) void {
|
||||
app.state().pipeline.release();
|
||||
core.schedule(.deinit);
|
||||
}
|
||||
|
||||
fn start(app: *Mod, core: *mach.Core.Mod) !void {
|
||||
core.schedule(.init);
|
||||
app.schedule(.init);
|
||||
}
|
||||
|
||||
fn init(app: *Mod, core: *mach.Core.Mod) !void {
|
||||
core.state().on_tick = app.system(.tick);
|
||||
core.state().on_exit = app.system(.deinit);
|
||||
pub fn init(
|
||||
core: *mach.Core,
|
||||
app: *App,
|
||||
app_tick: mach.Call(App, .tick),
|
||||
app_deinit: mach.Call(App, .deinit),
|
||||
) !void {
|
||||
core.on_tick = app_tick.id;
|
||||
core.on_exit = app_deinit.id;
|
||||
|
||||
// Create our shader module
|
||||
const shader_module = core.state().device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
|
||||
const shader_module = core.device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
|
||||
defer shader_module.release();
|
||||
|
||||
// Blend state describes how rendered colors get blended
|
||||
|
|
@ -37,7 +34,7 @@ fn init(app: *Mod, core: *mach.Core.Mod) !void {
|
|||
|
||||
// Color target describes e.g. the pixel format of the window we are rendering to.
|
||||
const color_target = gpu.ColorTargetState{
|
||||
.format = core.get(core.state().main_window, .framebuffer_format).?,
|
||||
.format = core.windows.get(core.main_window).?.framebuffer_format,
|
||||
.blend = &blend,
|
||||
};
|
||||
|
||||
|
|
@ -49,7 +46,7 @@ fn init(app: *Mod, core: *mach.Core.Mod) !void {
|
|||
});
|
||||
|
||||
// Create our render pipeline that will ultimately get pixels onto the screen.
|
||||
const label = @tagName(name) ++ ".init";
|
||||
const label = @tagName(mach_module) ++ ".init";
|
||||
const pipeline_descriptor = gpu.RenderPipeline.Descriptor{
|
||||
.label = label,
|
||||
.fragment = &fragment,
|
||||
|
|
@ -58,34 +55,33 @@ fn init(app: *Mod, core: *mach.Core.Mod) !void {
|
|||
.entry_point = "vertex_main",
|
||||
},
|
||||
};
|
||||
const pipeline = core.state().device.createRenderPipeline(&pipeline_descriptor);
|
||||
const pipeline = core.device.createRenderPipeline(&pipeline_descriptor);
|
||||
|
||||
// Store our render pipeline in our module's state, so we can access it later on.
|
||||
app.init(.{
|
||||
.title_timer = try mach.time.Timer.start(),
|
||||
.pipeline = pipeline,
|
||||
});
|
||||
try updateWindowTitle(core);
|
||||
// TODO(object): module-state-init
|
||||
app.title_timer = try mach.time.Timer.start();
|
||||
app.pipeline = pipeline;
|
||||
|
||||
core.schedule(.start);
|
||||
// TODO(object): window-title
|
||||
// try updateWindowTitle(core);
|
||||
}
|
||||
|
||||
fn tick(core: *mach.Core.Mod, app: *Mod) !void {
|
||||
while (core.state().nextEvent()) |event| {
|
||||
pub fn tick(app: *App, core: *mach.Core) void {
|
||||
while (core.nextEvent()) |event| {
|
||||
switch (event) {
|
||||
.close => core.schedule(.exit), // Tell mach.Core to exit the app
|
||||
.close => core.exit(),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
// Grab the back buffer of the swapchain
|
||||
// TODO(Core)
|
||||
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
|
||||
const back_buffer_view = core.swap_chain.getCurrentTextureView().?;
|
||||
defer back_buffer_view.release();
|
||||
|
||||
// Create a command encoder
|
||||
const label = @tagName(name) ++ ".tick";
|
||||
const encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
||||
const label = @tagName(mach_module) ++ ".tick";
|
||||
const encoder = core.device.createCommandEncoder(&.{ .label = label });
|
||||
defer encoder.release();
|
||||
|
||||
// Begin render pass
|
||||
|
|
@ -103,7 +99,7 @@ fn tick(core: *mach.Core.Mod, app: *Mod) !void {
|
|||
defer render_pass.release();
|
||||
|
||||
// Draw
|
||||
render_pass.setPipeline(app.state().pipeline);
|
||||
render_pass.setPipeline(app.pipeline);
|
||||
render_pass.draw(3, 1, 0, 0);
|
||||
|
||||
// Finish render pass
|
||||
|
|
@ -112,27 +108,30 @@ fn tick(core: *mach.Core.Mod, app: *Mod) !void {
|
|||
// Submit our commands to the queue
|
||||
var command = encoder.finish(&.{ .label = label });
|
||||
defer command.release();
|
||||
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
|
||||
// Present the frame
|
||||
core.schedule(.present_frame);
|
||||
core.queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
|
||||
// update the window title every second
|
||||
if (app.state().title_timer.read() >= 1.0) {
|
||||
app.state().title_timer.reset();
|
||||
try updateWindowTitle(core);
|
||||
if (app.title_timer.read() >= 1.0) {
|
||||
app.title_timer.reset();
|
||||
// TODO(object): window-title
|
||||
// try updateWindowTitle(core);
|
||||
}
|
||||
}
|
||||
|
||||
fn updateWindowTitle(core: *mach.Core.Mod) !void {
|
||||
try core.state().printTitle(
|
||||
core.state().main_window,
|
||||
"core-custom-entrypoint [ {d}fps ] [ Input {d}hz ]",
|
||||
.{
|
||||
// TODO(Core)
|
||||
core.state().frameRate(),
|
||||
core.state().inputRate(),
|
||||
},
|
||||
);
|
||||
core.schedule(.update);
|
||||
pub fn deinit(app: *App) void {
|
||||
app.pipeline.release();
|
||||
}
|
||||
|
||||
// TODO(object): window-title
|
||||
// fn updateWindowTitle(core: *mach.Core) !void {
|
||||
// try core.printTitle(
|
||||
// core.main_window,
|
||||
// "core-custom-entrypoint [ {d}fps ] [ Input {d}hz ]",
|
||||
// .{
|
||||
// // TODO(Core)
|
||||
// core.frameRate(),
|
||||
// core.inputRate(),
|
||||
// },
|
||||
// );
|
||||
// core.schedule(.update);
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -1,23 +1,21 @@
|
|||
const std = @import("std");
|
||||
const mach = @import("mach");
|
||||
|
||||
// The global list of Mach modules our application may use.
|
||||
pub const modules = .{
|
||||
// The set of Mach modules our application may use.
|
||||
const Modules = mach.Modules(.{
|
||||
mach.Core,
|
||||
@import("App.zig"),
|
||||
};
|
||||
});
|
||||
|
||||
// TODO: move this to a mach "entrypoint" zig module which handles nuances like WASM requires.
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.c_allocator;
|
||||
|
||||
// Initialize module system
|
||||
try mach.mods.init(allocator);
|
||||
// The set of Mach modules our application may use.
|
||||
var mods = Modules.init(allocator);
|
||||
// TODO: enable mods.deinit(allocator); for allocator leak detection
|
||||
// defer mods.deinit(allocator);
|
||||
|
||||
// Schedule .app.start to run.
|
||||
mach.mods.schedule(.app, .start);
|
||||
|
||||
// Dispatch systems forever or until there are none left to dispatch. If your app uses mach.Core
|
||||
// then this will block forever and never return.
|
||||
try mach.mods.dispatch(.{});
|
||||
const app = mods.get(.app);
|
||||
app.run(.main);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,12 @@ const vec2 = math.vec2;
|
|||
const Vec2 = math.Vec2;
|
||||
const Vec3 = math.Vec3;
|
||||
|
||||
const App = @This();
|
||||
|
||||
pub const mach_module = .app;
|
||||
|
||||
pub const mach_systems = .{ .start, .init, .deinit, .tick };
|
||||
|
||||
// Global state for our game module.
|
||||
timer: mach.time.Timer,
|
||||
player: mach.EntityID,
|
||||
|
|
@ -21,32 +27,14 @@ pub const components = .{
|
|||
.follower = .{ .type = void },
|
||||
};
|
||||
|
||||
pub const systems = .{
|
||||
.start = .{ .handler = start },
|
||||
.init = .{ .handler = init },
|
||||
.deinit = .{ .handler = deinit },
|
||||
.tick = .{ .handler = tick },
|
||||
};
|
||||
|
||||
// Define the globally unique name of our module. You can use any name here, but keep in mind no
|
||||
// two modules in the program can have the same name.
|
||||
pub const name = .app;
|
||||
|
||||
// The mach.Mod type corresponding to our module struct (this file.) This provides methods for
|
||||
// working with this module (e.g. sending events, working with its components, etc.)
|
||||
//
|
||||
// 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) void {
|
||||
renderer.schedule(.deinit);
|
||||
core.schedule(.deinit);
|
||||
}
|
||||
|
||||
fn start(
|
||||
core: *mach.Core.Mod,
|
||||
renderer: *Renderer.Mod,
|
||||
app: *Mod,
|
||||
core: *mach.Core,
|
||||
renderer: *Renderer,
|
||||
app: *App,
|
||||
) !void {
|
||||
core.schedule(.init);
|
||||
renderer.schedule(.init);
|
||||
|
|
@ -58,18 +46,20 @@ 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,
|
||||
app: *Mod,
|
||||
core: *mach.Core,
|
||||
renderer: *Renderer,
|
||||
app: *App,
|
||||
app_tick: mach.Call(App, .tick),
|
||||
app_deinit: mach.Call(App, .deinit),
|
||||
) !void {
|
||||
core.state().on_tick = app.system(.tick);
|
||||
core.state().on_exit = app.system(.deinit);
|
||||
core.on_tick = app_tick.id;
|
||||
core.on_exit = app_deinit.id;
|
||||
|
||||
// Create our player entity.
|
||||
const player = try entities.new();
|
||||
|
||||
// Give our player entity a .renderer.position and .renderer.scale component. Note that these
|
||||
// are defined by the Renderer module, so we use `renderer: *Renderer.Mod` to interact with
|
||||
// are defined by the Renderer module, so we use `renderer: *Renderer` to interact with
|
||||
// them.
|
||||
//
|
||||
// Components live in a module's namespace, so e.g. a physics2d module and renderer3d module could
|
||||
|
|
@ -79,26 +69,24 @@ fn init(
|
|||
try renderer.set(player, .scale, 1.0);
|
||||
|
||||
// Initialize our game module's state - these are the struct fields defined at the top of this
|
||||
// file. If this is not done, then app.state() will panic indicating the state was never
|
||||
// file. If this is not done, then app. will panic indicating the state was never
|
||||
// initialized.
|
||||
app.init(.{
|
||||
.timer = try mach.time.Timer.start(),
|
||||
.spawn_timer = try mach.time.Timer.start(),
|
||||
.player = player,
|
||||
});
|
||||
|
||||
core.schedule(.start);
|
||||
}
|
||||
|
||||
fn tick(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
renderer: *Renderer.Mod,
|
||||
app: *Mod,
|
||||
core: *mach.Core,
|
||||
renderer: *Renderer,
|
||||
app: *App,
|
||||
) !void {
|
||||
var direction = app.state().direction;
|
||||
var spawning = app.state().spawning;
|
||||
while (core.state().nextEvent()) |event| {
|
||||
var direction = app.direction;
|
||||
var spawning = app.spawning;
|
||||
while (core.nextEvent()) |event| {
|
||||
switch (event) {
|
||||
.key_press => |ev| {
|
||||
switch (ev.key) {
|
||||
|
|
@ -120,7 +108,7 @@ fn tick(
|
|||
else => {},
|
||||
}
|
||||
},
|
||||
.close => core.schedule(.exit), // Send an event telling mach to exit the app
|
||||
.close => core.exit(),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
|
@ -128,18 +116,18 @@ fn tick(
|
|||
// Keep track of which direction we want the player to move based on input, and whether we want
|
||||
// to be spawning entities.
|
||||
//
|
||||
// Note that app.state() simply returns a pointer to a global singleton of the struct defined
|
||||
// Note that app. simply returns a pointer to a global singleton of the struct defined
|
||||
// by this file, so we can access fields defined at the top of this file.
|
||||
app.state().direction = direction;
|
||||
app.state().spawning = spawning;
|
||||
app.direction = direction;
|
||||
app.spawning = spawning;
|
||||
|
||||
// Get the current player position
|
||||
var player_pos = renderer.get(app.state().player, .position).?;
|
||||
var player_pos = renderer.get(app.player, .position).?;
|
||||
|
||||
// If we want to spawn new entities, then spawn them now. The timer just makes spawning rate
|
||||
// independent of frame rate.
|
||||
if (spawning and app.state().spawn_timer.read() > 1.0 / 60.0) {
|
||||
_ = app.state().spawn_timer.lap(); // Reset the timer
|
||||
if (spawning and app.spawn_timer.read() > 1.0 / 60.0) {
|
||||
_ = app.spawn_timer.lap(); // Reset the timer
|
||||
for (0..5) |_| {
|
||||
// Spawn a new entity at the same position as the player, but smaller in scale.
|
||||
const new_entity = try entities.new();
|
||||
|
|
@ -152,14 +140,14 @@ fn tick(
|
|||
}
|
||||
|
||||
// Multiply by delta_time to ensure that movement is the same speed regardless of the frame rate.
|
||||
const delta_time = app.state().timer.lap();
|
||||
const delta_time = app.timer.lap();
|
||||
|
||||
// Calculate the player position, by moving in the direction the player wants to go
|
||||
// by the speed amount.
|
||||
const speed = 1.0;
|
||||
player_pos.v[0] += direction.x() * speed * delta_time;
|
||||
player_pos.v[1] += direction.y() * speed * delta_time;
|
||||
try renderer.set(app.state().player, .position, player_pos);
|
||||
try renderer.set(app.player, .position, player_pos);
|
||||
|
||||
// Query all the entities that have the .follower tag indicating they should follow the player.
|
||||
// TODO(important): better querying API
|
||||
|
|
@ -168,7 +156,7 @@ fn tick(
|
|||
var q = try entities.query(.{
|
||||
.ids = mach.Entities.Mod.read(.id),
|
||||
.followers = Mod.read(.follower),
|
||||
.positions = Renderer.Mod.write(.position),
|
||||
.positions = Renderer.write(.position),
|
||||
});
|
||||
while (q.next()) |v| {
|
||||
for (v.ids, v.positions) |id, *position| {
|
||||
|
|
@ -182,7 +170,7 @@ fn tick(
|
|||
var q2 = try entities.query(.{
|
||||
.ids = mach.Entities.Mod.read(.id),
|
||||
.followers = Mod.read(.follower),
|
||||
.positions = Renderer.Mod.read(.position),
|
||||
.positions = Renderer.read(.position),
|
||||
});
|
||||
while (q2.next()) |v2| {
|
||||
for (v2.ids, v2.positions) |other_id, other_position| {
|
||||
|
|
|
|||
|
|
@ -13,8 +13,7 @@ pipeline: *gpu.RenderPipeline,
|
|||
bind_groups: [num_bind_groups]*gpu.BindGroup,
|
||||
uniform_buffer: *gpu.Buffer,
|
||||
|
||||
pub const name = .renderer;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
pub const mach_module = .renderer;
|
||||
|
||||
pub const components = .{
|
||||
.position = .{ .type = Vec3 },
|
||||
|
|
@ -22,11 +21,7 @@ pub const components = .{
|
|||
.scale = .{ .type = f32 },
|
||||
};
|
||||
|
||||
pub const systems = .{
|
||||
.init = .{ .handler = init },
|
||||
.deinit = .{ .handler = deinit },
|
||||
.render_frame = .{ .handler = renderFrame },
|
||||
};
|
||||
pub const mach_systems = .{ .init, .deinit, .render_frame };
|
||||
|
||||
const UniformBufferObject = extern struct {
|
||||
offset: Vec3,
|
||||
|
|
@ -34,17 +29,17 @@ const UniformBufferObject = extern struct {
|
|||
};
|
||||
|
||||
fn init(
|
||||
core: *mach.Core.Mod,
|
||||
core: *mach.Core,
|
||||
renderer: *Mod,
|
||||
) !void {
|
||||
const device = core.state().device;
|
||||
const device = core.device;
|
||||
const shader_module = device.createShaderModuleWGSL("shader.wgsl", @embedFile("shader.wgsl"));
|
||||
defer shader_module.release();
|
||||
|
||||
// Fragment state
|
||||
const blend = gpu.BlendState{};
|
||||
const color_target = gpu.ColorTargetState{
|
||||
.format = core.get(core.state().main_window, .framebuffer_format).?,
|
||||
.format = core.windows.get(core.main_window).?.framebuffer_format,
|
||||
.blend = &blend,
|
||||
.write_mask = gpu.ColorWriteMaskFlags.all,
|
||||
};
|
||||
|
|
@ -54,7 +49,7 @@ fn init(
|
|||
.targets = &.{color_target},
|
||||
});
|
||||
|
||||
const label = @tagName(name) ++ ".init";
|
||||
const label = @tagName(mach_module) ++ ".init";
|
||||
const uniform_buffer = device.createBuffer(&.{
|
||||
.label = label ++ " uniform buffer",
|
||||
.usage = .{ .copy_dst = true, .uniform = true },
|
||||
|
|
@ -116,17 +111,17 @@ fn deinit(
|
|||
|
||||
fn renderFrame(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
core: *mach.Core,
|
||||
renderer: *Mod,
|
||||
) !void {
|
||||
// Grab the back buffer of the swapchain
|
||||
// TODO(Core)
|
||||
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
|
||||
const back_buffer_view = core.swap_chain.getCurrentTextureView().?;
|
||||
defer back_buffer_view.release();
|
||||
|
||||
// Create a command encoder
|
||||
const label = @tagName(name) ++ ".tick";
|
||||
const encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
||||
const label = @tagName(mach_module) ++ ".tick";
|
||||
const encoder = core.device.createCommandEncoder(&.{ .label = label });
|
||||
defer encoder.release();
|
||||
|
||||
// Update uniform buffer
|
||||
|
|
@ -173,8 +168,5 @@ fn renderFrame(
|
|||
// Submit our commands to the queue
|
||||
var command = encoder.finish(&.{ .label = label });
|
||||
defer command.release();
|
||||
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
|
||||
// Present the frame
|
||||
core.schedule(.present_frame);
|
||||
core.queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,22 @@
|
|||
const std = @import("std");
|
||||
const mach = @import("mach");
|
||||
|
||||
// The global list of Mach modules registered for use in our application.
|
||||
pub const modules = .{
|
||||
// The set of Mach modules our application may use.
|
||||
const Modules = mach.Modules(.{
|
||||
mach.Core,
|
||||
@import("App.zig"),
|
||||
@import("Renderer.zig"),
|
||||
};
|
||||
});
|
||||
|
||||
// TODO: move this to a mach "entrypoint" zig module which handles nuances like WASM requires.
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.c_allocator;
|
||||
|
||||
// Initialize module system
|
||||
try mach.mods.init(allocator);
|
||||
// The set of Mach modules our application may use.
|
||||
var mods = Modules.init(allocator);
|
||||
// TODO: enable mods.deinit(allocator); for allocator leak detection
|
||||
// defer mods.deinit(allocator);
|
||||
|
||||
// Schedule .app.start to run.
|
||||
mach.mods.schedule(.app, .start);
|
||||
|
||||
// Dispatch systems forever or until there are none left to dispatch. If your app uses mach.Core
|
||||
// then this will block forever and never return.
|
||||
try mach.mods.dispatch(.{});
|
||||
const app = mods.get(.app);
|
||||
app.run(.main);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,12 @@ const Mat4x4 = math.Mat4x4;
|
|||
|
||||
const Glyphs = @import("Glyphs.zig");
|
||||
|
||||
const App = @This();
|
||||
|
||||
pub const mach_module = .app;
|
||||
|
||||
pub const mach_systems = .{ .start, .init, .deinit, .tick, .end_frame };
|
||||
|
||||
timer: mach.time.Timer,
|
||||
player: mach.EntityID,
|
||||
direction: Vec2 = vec2(0, 0),
|
||||
|
|
@ -26,26 +32,12 @@ pipeline: mach.EntityID,
|
|||
frame_encoder: *gpu.CommandEncoder = undefined,
|
||||
frame_render_pass: *gpu.RenderPassEncoder = undefined,
|
||||
|
||||
// Define the globally unique name of our module. You can use any name here, but keep in mind no
|
||||
// two modules in the program can have the same name.
|
||||
pub const name = .app;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
|
||||
pub const systems = .{
|
||||
.start = .{ .handler = start },
|
||||
.init = .{ .handler = init },
|
||||
.deinit = .{ .handler = deinit },
|
||||
.tick = .{ .handler = tick },
|
||||
.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 start(core: *mach.Core.Mod, sprite_pipeline: *gfx.SpritePipeline.Mod, glyphs: *Glyphs.Mod, app: *Mod) !void {
|
||||
fn start(core: *mach.Core, sprite_pipeline: *gfx.SpritePipeline.Mod, glyphs: *Glyphs.Mod, app: *App) !void {
|
||||
core.schedule(.init);
|
||||
sprite_pipeline.schedule(.init);
|
||||
glyphs.schedule(.init);
|
||||
|
|
@ -62,11 +54,13 @@ fn init(
|
|||
sprite: *gfx.Sprite.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
glyphs: *Glyphs.Mod,
|
||||
app: *Mod,
|
||||
core: *mach.Core.Mod,
|
||||
app: *App,
|
||||
core: *mach.Core,
|
||||
app_tick: mach.Call(App, .tick),
|
||||
app_deinit: mach.Call(App, .deinit),
|
||||
) !void {
|
||||
core.state().on_tick = app.system(.tick);
|
||||
core.state().on_exit = app.system(.deinit);
|
||||
core.on_tick = app_tick.id;
|
||||
core.on_exit = app_deinit.id;
|
||||
|
||||
// Create a sprite rendering pipeline
|
||||
const texture = glyphs.state().texture;
|
||||
|
|
@ -98,21 +92,19 @@ fn init(
|
|||
.time = 0,
|
||||
.pipeline = pipeline,
|
||||
});
|
||||
|
||||
core.schedule(.start);
|
||||
}
|
||||
|
||||
fn tick(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
core: *mach.Core,
|
||||
sprite: *gfx.Sprite.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
glyphs: *Glyphs.Mod,
|
||||
app: *Mod,
|
||||
app: *App,
|
||||
) !void {
|
||||
var direction = app.state().direction;
|
||||
var spawning = app.state().spawning;
|
||||
while (core.state().nextEvent()) |event| {
|
||||
var direction = app.direction;
|
||||
var spawning = app.spawning;
|
||||
while (core.nextEvent()) |event| {
|
||||
switch (event) {
|
||||
.key_press => |ev| {
|
||||
switch (ev.key) {
|
||||
|
|
@ -134,37 +126,37 @@ fn tick(
|
|||
else => {},
|
||||
}
|
||||
},
|
||||
.close => core.schedule(.exit),
|
||||
.close => core.exit(),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
app.state().direction = direction;
|
||||
app.state().spawning = spawning;
|
||||
app.direction = direction;
|
||||
app.spawning = spawning;
|
||||
|
||||
var player_transform = sprite.get(app.state().player, .transform).?;
|
||||
var player_transform = sprite.get(app.player, .transform).?;
|
||||
var player_pos = player_transform.translation();
|
||||
if (!spawning and app.state().spawn_timer.read() > 1.0 / 60.0) {
|
||||
if (!spawning and app.spawn_timer.read() > 1.0 / 60.0) {
|
||||
// Spawn new entities
|
||||
_ = app.state().spawn_timer.lap();
|
||||
_ = app.spawn_timer.lap();
|
||||
for (0..50) |_| {
|
||||
var new_pos = player_pos;
|
||||
new_pos.v[0] += app.state().rand.random().floatNorm(f32) * 25;
|
||||
new_pos.v[1] += app.state().rand.random().floatNorm(f32) * 25;
|
||||
new_pos.v[0] += app.rand.random().floatNorm(f32) * 25;
|
||||
new_pos.v[1] += app.rand.random().floatNorm(f32) * 25;
|
||||
|
||||
const rand_index = app.state().rand.random().intRangeAtMost(usize, 0, glyphs.state().regions.count() - 1);
|
||||
const rand_index = app.rand.random().intRangeAtMost(usize, 0, glyphs.state().regions.count() - 1);
|
||||
const r = glyphs.state().regions.entries.get(rand_index).value;
|
||||
|
||||
const new_entity = try entities.new();
|
||||
try sprite.set(new_entity, .transform, Mat4x4.translate(new_pos).mul(&Mat4x4.scaleScalar(0.3)));
|
||||
try sprite.set(new_entity, .size, vec2(@floatFromInt(r.width), @floatFromInt(r.height)));
|
||||
try sprite.set(new_entity, .uv_transform, Mat3x3.translate(vec2(@floatFromInt(r.x), @floatFromInt(r.y))));
|
||||
try sprite.set(new_entity, .pipeline, app.state().pipeline);
|
||||
app.state().sprites += 1;
|
||||
try sprite.set(new_entity, .pipeline, app.pipeline);
|
||||
app.sprites += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Multiply by delta_time to ensure that movement is the same speed regardless of the frame rate.
|
||||
const delta_time = app.state().timer.lap();
|
||||
const delta_time = app.timer.lap();
|
||||
|
||||
// Animate entities
|
||||
var q = try entities.query(.{
|
||||
|
|
@ -176,17 +168,17 @@ fn tick(
|
|||
var location = entity_transform.translation();
|
||||
// TODO: formatting
|
||||
// TODO(Core)
|
||||
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) {
|
||||
if (location.x() < -@as(f32, @floatFromInt(core.size().width)) / 1.5 or location.x() > @as(f32, @floatFromInt(core.size().width)) / 1.5 or location.y() < -@as(f32, @floatFromInt(core.size().height)) / 1.5 or location.y() > @as(f32, @floatFromInt(core.size().height)) / 1.5) {
|
||||
try entities.remove(id);
|
||||
app.state().sprites -= 1;
|
||||
app.sprites -= 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
var transform = Mat4x4.ident;
|
||||
transform = transform.mul(&Mat4x4.scale(Vec3.splat(1.0 + (0.2 * delta_time))));
|
||||
transform = transform.mul(&Mat4x4.translate(location));
|
||||
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * app.state().time));
|
||||
transform = transform.mul(&Mat4x4.scale(Vec3.splat(@max(math.cos(app.state().time / 2.0), 0.2))));
|
||||
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * app.time));
|
||||
transform = transform.mul(&Mat4x4.scale(Vec3.splat(@max(math.cos(app.time / 2.0), 0.2))));
|
||||
entity_transform.* = transform;
|
||||
}
|
||||
}
|
||||
|
|
@ -199,19 +191,19 @@ fn tick(
|
|||
player_transform = Mat4x4.translate(player_pos).mul(
|
||||
&Mat4x4.scale(Vec3.splat(1.0)),
|
||||
);
|
||||
try sprite.set(app.state().player, .transform, player_transform);
|
||||
try sprite.set(app.player, .transform, player_transform);
|
||||
sprite.schedule(.update);
|
||||
|
||||
// Perform pre-render work
|
||||
sprite_pipeline.schedule(.pre_render);
|
||||
|
||||
// Create a command encoder for this frame
|
||||
const label = @tagName(name) ++ ".tick";
|
||||
app.state().frame_encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
||||
const label = @tagName(mach_module) ++ ".tick";
|
||||
app.frame_encoder = core.device.createCommandEncoder(&.{ .label = label });
|
||||
|
||||
// Grab the back buffer of the swapchain
|
||||
// TODO(Core)
|
||||
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
|
||||
const back_buffer_view = core.swap_chain.getCurrentTextureView().?;
|
||||
defer back_buffer_view.release();
|
||||
|
||||
// Begin render pass
|
||||
|
|
@ -222,44 +214,41 @@ fn tick(
|
|||
.load_op = .clear,
|
||||
.store_op = .store,
|
||||
}};
|
||||
app.state().frame_render_pass = app.state().frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||
app.frame_render_pass = app.frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||
.label = label,
|
||||
.color_attachments = &color_attachments,
|
||||
}));
|
||||
|
||||
// Render our sprite batch
|
||||
sprite_pipeline.state().render_pass = app.state().frame_render_pass;
|
||||
sprite_pipeline.state().render_pass = app.frame_render_pass;
|
||||
sprite_pipeline.schedule(.render);
|
||||
|
||||
// Finish the frame once rendering is done.
|
||||
app.schedule(.end_frame);
|
||||
|
||||
app.state().time += delta_time;
|
||||
app.time += delta_time;
|
||||
}
|
||||
|
||||
fn endFrame(app: *Mod, core: *mach.Core.Mod) !void {
|
||||
fn endFrame(app: *App, core: *mach.Core) !void {
|
||||
// Finish render pass
|
||||
app.state().frame_render_pass.end();
|
||||
const label = @tagName(name) ++ ".endFrame";
|
||||
var command = app.state().frame_encoder.finish(&.{ .label = label });
|
||||
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
app.frame_render_pass.end();
|
||||
const label = @tagName(mach_module) ++ ".endFrame";
|
||||
var command = app.frame_encoder.finish(&.{ .label = label });
|
||||
core.queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
command.release();
|
||||
app.state().frame_encoder.release();
|
||||
app.state().frame_render_pass.release();
|
||||
|
||||
// Present the frame
|
||||
core.schedule(.present_frame);
|
||||
app.frame_encoder.release();
|
||||
app.frame_render_pass.release();
|
||||
|
||||
// Every second, update the window title with the FPS
|
||||
if (app.state().fps_timer.read() >= 1.0) {
|
||||
try core.state().printTitle(
|
||||
core.state().main_window,
|
||||
if (app.fps_timer.read() >= 1.0) {
|
||||
try core.printTitle(
|
||||
core.main_window,
|
||||
"glyphs [ FPS: {d} ] [ Sprites: {d} ]",
|
||||
.{ app.state().frame_count, app.state().sprites },
|
||||
.{ app.frame_count, app.sprites },
|
||||
);
|
||||
core.schedule(.update);
|
||||
app.state().fps_timer.reset();
|
||||
app.state().frame_count = 0;
|
||||
app.fps_timer.reset();
|
||||
app.frame_count = 0;
|
||||
}
|
||||
app.state().frame_count += 1;
|
||||
app.frame_count += 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,14 +4,9 @@ const freetype = @import("freetype");
|
|||
const std = @import("std");
|
||||
const assets = @import("assets");
|
||||
|
||||
pub const name = .glyphs;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
pub const mach_module = .glyphs;
|
||||
|
||||
pub const systems = .{
|
||||
.init = .{ .handler = init },
|
||||
.deinit = .{ .handler = deinit },
|
||||
.prepare = .{ .handler = prepare },
|
||||
};
|
||||
pub const mach_systems = .{ .init, .deinit, .prepare };
|
||||
|
||||
const RegionMap = std.AutoArrayHashMapUnmanaged(u21, mach.gfx.Atlas.Region);
|
||||
|
||||
|
|
@ -35,17 +30,17 @@ fn deinit(glyphs: *Mod) !void {
|
|||
}
|
||||
|
||||
fn init(
|
||||
core: *mach.Core.Mod,
|
||||
core: *mach.Core,
|
||||
glyphs: *Mod,
|
||||
) !void {
|
||||
const device = core.state().device;
|
||||
const device = core.device;
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
// rgba32_pixels
|
||||
const img_size = gpu.Extent3D{ .width = 1024, .height = 1024 };
|
||||
|
||||
// Create a GPU texture
|
||||
const label = @tagName(name) ++ ".init";
|
||||
const label = @tagName(mach_module) ++ ".init";
|
||||
const texture = device.createTexture(&.{
|
||||
.label = label,
|
||||
.size = img_size,
|
||||
|
|
@ -75,7 +70,7 @@ fn init(
|
|||
});
|
||||
}
|
||||
|
||||
fn prepare(core: *mach.Core.Mod, glyphs: *Mod) !void {
|
||||
fn prepare(core: *mach.Core, glyphs: *Mod) !void {
|
||||
var s = glyphs.state();
|
||||
|
||||
// Prepare which glyphs we will render
|
||||
|
|
@ -124,5 +119,5 @@ fn prepare(core: *mach.Core.Mod, glyphs: *Mod) !void {
|
|||
.bytes_per_row = @as(u32, @intCast(img_size.width * 4)),
|
||||
.rows_per_image = @as(u32, @intCast(img_size.height)),
|
||||
};
|
||||
core.state().queue.writeTexture(&.{ .texture = s.texture }, &data_layout, &img_size, s.texture_atlas.data);
|
||||
core.queue.writeTexture(&.{ .texture = s.texture }, &data_layout, &img_size, s.texture_atlas.data);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,23 @@
|
|||
const std = @import("std");
|
||||
const mach = @import("mach");
|
||||
|
||||
// The global list of Mach modules our application may use.
|
||||
pub const modules = .{
|
||||
// The set of Mach modules our application may use.
|
||||
const Modules = mach.Modules(.{
|
||||
mach.Core,
|
||||
mach.gfx.sprite_modules,
|
||||
@import("App.zig"),
|
||||
@import("Glyphs.zig"),
|
||||
};
|
||||
});
|
||||
|
||||
// TODO: move this to a mach "entrypoint" zig module which handles nuances like WASM requires.
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.c_allocator;
|
||||
|
||||
// Initialize module system
|
||||
try mach.mods.init(allocator);
|
||||
// The set of Mach modules our application may use.
|
||||
var mods = Modules.init(allocator);
|
||||
// TODO: enable mods.deinit(allocator); for allocator leak detection
|
||||
// defer mods.deinit(allocator);
|
||||
|
||||
// Schedule .app.start to run.
|
||||
mach.mods.schedule(.app, .start);
|
||||
|
||||
// Dispatch systems forever or until there are none left to dispatch. If your app uses mach.Core
|
||||
// then this will block forever and never return.
|
||||
try mach.mods.dispatch(.{});
|
||||
const app = mods.get(.app);
|
||||
app.run(.main);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,12 @@ const Vec3 = math.Vec3;
|
|||
const Mat3x3 = math.Mat3x3;
|
||||
const Mat4x4 = math.Mat4x4;
|
||||
|
||||
const App = @This();
|
||||
|
||||
pub const mach_module = .app;
|
||||
|
||||
pub const mach_systems = .{ .start, .init, .deinit, .tick, .end_frame, .audio_state_change };
|
||||
|
||||
// TODO: banish global allocator
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
|
||||
|
|
@ -36,42 +42,26 @@ frame_encoder: *gpu.CommandEncoder = undefined,
|
|||
frame_render_pass: *gpu.RenderPassEncoder = undefined,
|
||||
sfx: mach.Audio.Opus,
|
||||
|
||||
// Define the globally unique name of our module. You can use any name here, but keep in mind no
|
||||
// two modules in the program can have the same name.
|
||||
pub const name = .app;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
|
||||
pub const systems = .{
|
||||
.start = .{ .handler = start },
|
||||
.init = .{ .handler = init },
|
||||
.deinit = .{ .handler = deinit },
|
||||
.tick = .{ .handler = tick },
|
||||
.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,
|
||||
audio: *mach.Audio,
|
||||
) !void {
|
||||
text_pipeline.schedule(.deinit);
|
||||
sprite_pipeline.schedule(.deinit);
|
||||
core.schedule(.deinit);
|
||||
audio.schedule(.deinit);
|
||||
}
|
||||
|
||||
fn start(
|
||||
audio: *mach.Audio.Mod,
|
||||
audio: *mach.Audio,
|
||||
text_pipeline: *gfx.TextPipeline.Mod,
|
||||
text: *gfx.Text.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
app: *Mod,
|
||||
core: *mach.Core,
|
||||
app: *App,
|
||||
) !void {
|
||||
// If you want to try fullscreen:
|
||||
// try core.set(core.state().main_window, .fullscreen, true);
|
||||
// try core.set(core.main_window, .fullscreen, true);
|
||||
|
||||
core.schedule(.init);
|
||||
audio.schedule(.init);
|
||||
|
|
@ -83,20 +73,23 @@ fn start(
|
|||
|
||||
fn init(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
audio: *mach.Audio.Mod,
|
||||
core: *mach.Core,
|
||||
audio: *mach.Audio,
|
||||
text_pipeline: *gfx.TextPipeline.Mod,
|
||||
text_style: *gfx.TextStyle.Mod,
|
||||
text: *gfx.Text.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
app: *Mod,
|
||||
app: *App,
|
||||
app_tick: mach.Call(App, .tick),
|
||||
app_deinit: mach.Call(App, .deinit),
|
||||
app_audio_state_change: mach.Call(App, .audio_state_change),
|
||||
) !void {
|
||||
core.state().on_tick = app.system(.tick);
|
||||
core.state().on_exit = app.system(.deinit);
|
||||
core.on_tick = app_tick.id;
|
||||
core.on_exit = app_deinit.id;
|
||||
|
||||
// Configure the audio module to run our audio_state_change system when entities audio finishes
|
||||
// playing
|
||||
audio.state().on_state_change = app.system(.audio_state_change);
|
||||
audio.on_state_change = app_audio_state_change.id;
|
||||
|
||||
// Create a sprite rendering pipeline
|
||||
const allocator = gpa.allocator();
|
||||
|
|
@ -127,7 +120,8 @@ fn init(
|
|||
, .{});
|
||||
text.schedule(.update);
|
||||
|
||||
const window_height: f32 = @floatFromInt(core.get(core.state().main_window, .height).?);
|
||||
const win = core.windows.get(core.main_window).?;
|
||||
const window_height: f32 = @floatFromInt(win.height);
|
||||
const info_text = try entities.new();
|
||||
try text.set(info_text, .pipeline, text_rendering_pipeline);
|
||||
try text.set(info_text, .transform, Mat4x4.translate(vec3(0, (window_height / 2.0) - 50.0, 0)));
|
||||
|
|
@ -154,14 +148,13 @@ fn init(
|
|||
.pipeline = pipeline,
|
||||
.sfx = sfx,
|
||||
});
|
||||
core.schedule(.start);
|
||||
}
|
||||
|
||||
fn audioStateChange(entities: *mach.Entities.Mod) !void {
|
||||
// Find audio entities that are no longer playing
|
||||
var q = try entities.query(.{
|
||||
.ids = mach.Entities.Mod.read(.id),
|
||||
.playings = mach.Audio.Mod.read(.playing),
|
||||
.playings = mach.Audio.read(.playing),
|
||||
});
|
||||
while (q.next()) |v| {
|
||||
for (v.ids, v.playings) |id, playing| {
|
||||
|
|
@ -175,18 +168,18 @@ fn audioStateChange(entities: *mach.Entities.Mod) !void {
|
|||
|
||||
fn tick(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
core: *mach.Core,
|
||||
sprite: *gfx.Sprite.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
text: *gfx.Text.Mod,
|
||||
text_pipeline: *gfx.TextPipeline.Mod,
|
||||
app: *Mod,
|
||||
audio: *mach.Audio.Mod,
|
||||
app: *App,
|
||||
audio: *mach.Audio,
|
||||
) !void {
|
||||
// TODO(important): event polling should occur in mach.Core module and get fired as ECS events.
|
||||
// TODO(Core)
|
||||
var gotta_go_fast = app.state().gotta_go_fast;
|
||||
while (core.state().nextEvent()) |event| {
|
||||
var gotta_go_fast = app.gotta_go_fast;
|
||||
while (core.nextEvent()) |event| {
|
||||
switch (event) {
|
||||
.key_press => |ev| {
|
||||
switch (ev.key) {
|
||||
|
|
@ -200,52 +193,53 @@ fn tick(
|
|||
else => {},
|
||||
}
|
||||
},
|
||||
.close => core.schedule(.exit),
|
||||
.close => core.exit(),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
app.state().gotta_go_fast = gotta_go_fast;
|
||||
app.gotta_go_fast = gotta_go_fast;
|
||||
|
||||
// Every second, update the frame rate
|
||||
if (app.state().fps_timer.read() >= 1.0) {
|
||||
app.state().frame_rate = app.state().frame_count;
|
||||
app.state().fps_timer.reset();
|
||||
app.state().frame_count = 0;
|
||||
if (app.fps_timer.read() >= 1.0) {
|
||||
app.frame_rate = app.frame_count;
|
||||
app.fps_timer.reset();
|
||||
app.frame_count = 0;
|
||||
}
|
||||
|
||||
try gfx.Text.allocPrintText(
|
||||
text,
|
||||
app.state().info_text,
|
||||
app.state().info_text_style,
|
||||
app.info_text,
|
||||
app.info_text_style,
|
||||
"[ FPS: {d} ]\n[ Sprites spawned: {d} ]",
|
||||
.{ app.state().frame_rate, app.state().num_sprites_spawned },
|
||||
.{ app.frame_rate, app.num_sprites_spawned },
|
||||
);
|
||||
text.schedule(.update);
|
||||
|
||||
// var player_transform = sprite.get(app.state().player, .transform).?;
|
||||
// var player_transform = sprite.get(app.player, .transform).?;
|
||||
// var player_pos = player_transform.translation();
|
||||
const window_width: f32 = @floatFromInt(core.get(core.state().main_window, .width).?);
|
||||
const win = core.windows.get(core.main_window).?;
|
||||
const window_width: f32 = @floatFromInt(win.width);
|
||||
|
||||
const entities_per_second: f32 = @floatFromInt(
|
||||
app.state().rand.random().intRangeAtMost(usize, 0, if (gotta_go_fast) 50 else 10),
|
||||
app.rand.random().intRangeAtMost(usize, 0, if (gotta_go_fast) 50 else 10),
|
||||
);
|
||||
if (app.state().spawn_timer.read() > 1.0 / entities_per_second) {
|
||||
if (app.spawn_timer.read() > 1.0 / entities_per_second) {
|
||||
// Spawn new entities
|
||||
_ = app.state().spawn_timer.lap();
|
||||
_ = app.spawn_timer.lap();
|
||||
|
||||
var new_pos = vec3(-(window_width / 2), 0, 0);
|
||||
new_pos.v[1] += app.state().rand.random().floatNorm(f32) * 50;
|
||||
new_pos.v[1] += app.rand.random().floatNorm(f32) * 50;
|
||||
|
||||
const new_entity = try entities.new();
|
||||
try sprite.set(new_entity, .transform, Mat4x4.translate(new_pos));
|
||||
try sprite.set(new_entity, .size, vec2(32, 32));
|
||||
try sprite.set(new_entity, .uv_transform, Mat3x3.translate(vec2(0, 0)));
|
||||
try sprite.set(new_entity, .pipeline, app.state().pipeline);
|
||||
app.state().num_sprites_spawned += 1;
|
||||
try sprite.set(new_entity, .pipeline, app.pipeline);
|
||||
app.num_sprites_spawned += 1;
|
||||
}
|
||||
|
||||
// Multiply by delta_time to ensure that movement is the same speed regardless of the frame rate.
|
||||
const delta_time = app.state().timer.lap();
|
||||
const delta_time = app.timer.lap();
|
||||
|
||||
// Move entities to the right, and make them smaller the further they travel
|
||||
var q = try entities.query(.{
|
||||
|
|
@ -263,11 +257,11 @@ fn tick(
|
|||
|
||||
// Play a new SFX
|
||||
const e = try entities.new();
|
||||
try audio.set(e, .samples, app.state().sfx.samples);
|
||||
try audio.set(e, .channels, app.state().sfx.channels);
|
||||
try audio.set(e, .samples, app.sfx.samples);
|
||||
try audio.set(e, .channels, app.sfx.channels);
|
||||
try audio.set(e, .index, 0);
|
||||
try audio.set(e, .playing, true);
|
||||
app.state().score += 1;
|
||||
app.score += 1;
|
||||
} else {
|
||||
var transform = Mat4x4.ident;
|
||||
transform = transform.mul(&Mat4x4.translate(location.add(&vec3(speed * delta_time, (speed / 2.0) * delta_time * progression, 0))));
|
||||
|
|
@ -284,12 +278,12 @@ fn tick(
|
|||
text_pipeline.schedule(.pre_render);
|
||||
|
||||
// Create a command encoder for this frame
|
||||
const label = @tagName(name) ++ ".tick";
|
||||
app.state().frame_encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
||||
const label = @tagName(mach_module) ++ ".tick";
|
||||
app.frame_encoder = core.device.createCommandEncoder(&.{ .label = label });
|
||||
|
||||
// Grab the back buffer of the swapchain
|
||||
// TODO(Core)
|
||||
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
|
||||
const back_buffer_view = core.swap_chain.getCurrentTextureView().?;
|
||||
defer back_buffer_view.release();
|
||||
|
||||
// Begin render pass
|
||||
|
|
@ -300,45 +294,42 @@ fn tick(
|
|||
.load_op = .clear,
|
||||
.store_op = .store,
|
||||
}};
|
||||
app.state().frame_render_pass = app.state().frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||
app.frame_render_pass = app.frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||
.label = label,
|
||||
.color_attachments = &color_attachments,
|
||||
}));
|
||||
|
||||
// Render our sprite batch
|
||||
sprite_pipeline.state().render_pass = app.state().frame_render_pass;
|
||||
sprite_pipeline.state().render_pass = app.frame_render_pass;
|
||||
sprite_pipeline.schedule(.render);
|
||||
|
||||
// Render our text batch
|
||||
text_pipeline.state().render_pass = app.state().frame_render_pass;
|
||||
text_pipeline.state().render_pass = app.frame_render_pass;
|
||||
text_pipeline.schedule(.render);
|
||||
|
||||
// Finish the frame once rendering is done.
|
||||
app.schedule(.end_frame);
|
||||
|
||||
app.state().time += delta_time;
|
||||
app.time += delta_time;
|
||||
}
|
||||
|
||||
fn endFrame(app: *Mod, core: *mach.Core.Mod) !void {
|
||||
fn endFrame(app: *App, core: *mach.Core) !void {
|
||||
// Finish render pass
|
||||
app.state().frame_render_pass.end();
|
||||
const label = @tagName(name) ++ ".endFrame";
|
||||
var command = app.state().frame_encoder.finish(&.{ .label = label });
|
||||
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
app.frame_render_pass.end();
|
||||
const label = @tagName(mach_module) ++ ".endFrame";
|
||||
var command = app.frame_encoder.finish(&.{ .label = label });
|
||||
core.queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
command.release();
|
||||
app.state().frame_encoder.release();
|
||||
app.state().frame_render_pass.release();
|
||||
app.frame_encoder.release();
|
||||
app.frame_render_pass.release();
|
||||
|
||||
// Present the frame
|
||||
core.schedule(.present_frame);
|
||||
|
||||
app.state().frame_count += 1;
|
||||
app.frame_count += 1;
|
||||
}
|
||||
|
||||
// TODO: move this helper into gfx module
|
||||
fn loadTexture(core: *mach.Core.Mod, allocator: std.mem.Allocator) !*gpu.Texture {
|
||||
const device = core.state().device;
|
||||
const queue = core.state().queue;
|
||||
fn loadTexture(core: *mach.Core, allocator: std.mem.Allocator) !*gpu.Texture {
|
||||
const device = core.device;
|
||||
const queue = core.queue;
|
||||
|
||||
// Load the image from memory
|
||||
var img = try zigimg.Image.fromMemory(allocator, assets.sprites_sheet_png);
|
||||
|
|
@ -346,7 +337,7 @@ fn loadTexture(core: *mach.Core.Mod, allocator: std.mem.Allocator) !*gpu.Texture
|
|||
const img_size = gpu.Extent3D{ .width = @as(u32, @intCast(img.width)), .height = @as(u32, @intCast(img.height)) };
|
||||
|
||||
// Create a GPU texture
|
||||
const label = @tagName(name) ++ ".loadTexture";
|
||||
const label = @tagName(mach_module) ++ ".loadTexture";
|
||||
const texture = device.createTexture(&.{
|
||||
.label = label,
|
||||
.size = img_size,
|
||||
|
|
|
|||
|
|
@ -1,26 +1,24 @@
|
|||
const std = @import("std");
|
||||
const mach = @import("mach");
|
||||
|
||||
// The global list of Mach modules our application may use.
|
||||
pub const modules = .{
|
||||
// The set of Mach modules our application may use.
|
||||
const Modules = mach.Modules(.{
|
||||
mach.Core,
|
||||
mach.gfx.sprite_modules,
|
||||
mach.gfx.text_modules,
|
||||
mach.Audio,
|
||||
@import("App.zig"),
|
||||
};
|
||||
});
|
||||
|
||||
// TODO: move this to a mach "entrypoint" zig module which handles nuances like WASM requires.
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.c_allocator;
|
||||
|
||||
// Initialize module system
|
||||
try mach.mods.init(allocator);
|
||||
// The set of Mach modules our application may use.
|
||||
var mods = Modules.init(allocator);
|
||||
// TODO: enable mods.deinit(allocator); for allocator leak detection
|
||||
// defer mods.deinit(allocator);
|
||||
|
||||
// Schedule .app.start to run.
|
||||
mach.mods.schedule(.app, .start);
|
||||
|
||||
// Dispatch systems forever or until there are none left to dispatch. If your app uses mach.Core
|
||||
// then this will block forever and never return.
|
||||
try mach.mods.dispatch(.{});
|
||||
const app = mods.get(.app);
|
||||
app.run(.main);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,41 +16,41 @@ const gpu = mach.gpu;
|
|||
const math = mach.math;
|
||||
const sysaudio = mach.sysaudio;
|
||||
|
||||
pub const App = @This();
|
||||
const App = @This();
|
||||
|
||||
pub const mach_module = .app;
|
||||
|
||||
pub const mach_systems = .{ .start, .init, .deinit, .tick, .audio_state_change };
|
||||
|
||||
// TODO: banish global allocator
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
|
||||
pub const name = .app;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
|
||||
pub const systems = .{
|
||||
.start = .{ .handler = start },
|
||||
.init = .{ .handler = init },
|
||||
.deinit = .{ .handler = deinit },
|
||||
.tick = .{ .handler = tick },
|
||||
.audio_state_change = .{ .handler = audioStateChange },
|
||||
};
|
||||
|
||||
pub const components = .{
|
||||
.play_after = .{ .type = f32 },
|
||||
};
|
||||
|
||||
ghost_key_mode: bool = false,
|
||||
|
||||
fn start(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) void {
|
||||
fn start(core: *mach.Core, audio: *mach.Audio, app: *App) void {
|
||||
core.schedule(.init);
|
||||
audio.schedule(.init);
|
||||
app.schedule(.init);
|
||||
}
|
||||
|
||||
fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) void {
|
||||
core.state().on_tick = app.system(.tick);
|
||||
core.state().on_exit = app.system(.deinit);
|
||||
fn init(
|
||||
core: *mach.Core,
|
||||
audio: *mach.Audio,
|
||||
app: *App,
|
||||
app_tick: mach.Call(App, .tick),
|
||||
app_deinit: mach.Call(App, .deinit),
|
||||
app_audio_state_change: mach.Call(App, .audio_state_change),
|
||||
) !void {
|
||||
core.on_tick = app_tick.id;
|
||||
core.on_exit = app_deinit.id;
|
||||
|
||||
// Configure the audio module to send our app's .audio_state_change event when an entity's sound
|
||||
// finishes playing.
|
||||
audio.state().on_state_change = app.system(.audio_state_change);
|
||||
audio.on_state_change = app_audio_state_change.id;
|
||||
|
||||
// Initialize piano module state
|
||||
app.init(.{});
|
||||
|
|
@ -60,24 +60,21 @@ 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 deinit(core: *mach.Core.Mod, audio: *mach.Audio.Mod) void {
|
||||
fn deinit(audio: *mach.Audio) void {
|
||||
audio.schedule(.deinit);
|
||||
core.schedule(.deinit);
|
||||
}
|
||||
|
||||
fn audioStateChange(
|
||||
entities: *mach.Entities.Mod,
|
||||
audio: *mach.Audio.Mod,
|
||||
app: *Mod,
|
||||
audio: *mach.Audio,
|
||||
app: *App,
|
||||
) !void {
|
||||
// Find audio entities that are no longer playing
|
||||
var q = try entities.query(.{
|
||||
.ids = mach.Entities.Mod.read(.id),
|
||||
.playings = mach.Audio.Mod.read(.playing),
|
||||
.playings = mach.Audio.read(.playing),
|
||||
});
|
||||
while (q.next()) |v| {
|
||||
for (v.ids, v.playings) |id, playing| {
|
||||
|
|
@ -87,7 +84,7 @@ fn audioStateChange(
|
|||
// Play a new sound
|
||||
const e = try entities.new();
|
||||
try audio.set(e, .samples, try fillTone(audio, frequency));
|
||||
try audio.set(e, .channels, @intCast(audio.state().player.channels().len));
|
||||
try audio.set(e, .channels, @intCast(audio.player.channels().len));
|
||||
try audio.set(e, .playing, true);
|
||||
try audio.set(e, .index, 0);
|
||||
}
|
||||
|
|
@ -100,24 +97,24 @@ fn audioStateChange(
|
|||
|
||||
fn tick(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
audio: *mach.Audio.Mod,
|
||||
app: *Mod,
|
||||
core: *mach.Core,
|
||||
audio: *mach.Audio,
|
||||
app: *App,
|
||||
) !void {
|
||||
while (core.state().nextEvent()) |event| {
|
||||
while (core.nextEvent()) |event| {
|
||||
switch (event) {
|
||||
.key_press => |ev| {
|
||||
switch (ev.key) {
|
||||
// Controls
|
||||
.space => app.state().ghost_key_mode = !app.state().ghost_key_mode,
|
||||
.space => app.ghost_key_mode = !app.ghost_key_mode,
|
||||
.down => {
|
||||
const vol = math.clamp(try audio.state().player.volume() - 0.1, 0, 1);
|
||||
try audio.state().player.setVolume(vol);
|
||||
const vol = math.clamp(try audio.player.volume() - 0.1, 0, 1);
|
||||
try audio.player.setVolume(vol);
|
||||
std.debug.print("[volume] {d:.0}%\n", .{vol * 100.0});
|
||||
},
|
||||
.up => {
|
||||
const vol = math.clamp(try audio.state().player.volume() + 0.1, 0, 1);
|
||||
try audio.state().player.setVolume(vol);
|
||||
const vol = math.clamp(try audio.player.volume() + 0.1, 0, 1);
|
||||
try audio.player.setVolume(vol);
|
||||
std.debug.print("[volume] {d:.0}%\n", .{vol * 100.0});
|
||||
},
|
||||
|
||||
|
|
@ -126,11 +123,11 @@ fn tick(
|
|||
// Play a new sound
|
||||
const e = try entities.new();
|
||||
try audio.set(e, .samples, try fillTone(audio, keyToFrequency(ev.key)));
|
||||
try audio.set(e, .channels, @intCast(audio.state().player.channels().len));
|
||||
try audio.set(e, .channels, @intCast(audio.player.channels().len));
|
||||
try audio.set(e, .playing, true);
|
||||
try audio.set(e, .index, 0);
|
||||
|
||||
if (app.state().ghost_key_mode) {
|
||||
if (app.ghost_key_mode) {
|
||||
// After that sound plays, we'll chain on another sound that is one semi-tone higher.
|
||||
const one_semi_tone_higher = keyToFrequency(ev.key) * math.pow(f32, 2.0, (1.0 / 12.0));
|
||||
try app.set(e, .play_after, one_semi_tone_higher);
|
||||
|
|
@ -138,19 +135,19 @@ fn tick(
|
|||
},
|
||||
}
|
||||
},
|
||||
.close => core.schedule(.exit),
|
||||
.close => core.exit(),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
// Grab the back buffer of the swapchain
|
||||
// TODO(Core)
|
||||
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
|
||||
const back_buffer_view = core.swap_chain.getCurrentTextureView().?;
|
||||
defer back_buffer_view.release();
|
||||
|
||||
// Create a command encoder
|
||||
const label = @tagName(name) ++ ".tick";
|
||||
const encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
||||
const label = @tagName(mach_module) ++ ".tick";
|
||||
const encoder = core.device.createCommandEncoder(&.{ .label = label });
|
||||
defer encoder.release();
|
||||
|
||||
// Begin render pass
|
||||
|
|
@ -175,15 +172,12 @@ fn tick(
|
|||
// Submit our commands to the queue
|
||||
var command = encoder.finish(&.{ .label = label });
|
||||
defer command.release();
|
||||
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
|
||||
// Present the frame
|
||||
core.schedule(.present_frame);
|
||||
core.queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
}
|
||||
|
||||
fn fillTone(audio: *mach.Audio.Mod, frequency: f32) ![]const f32 {
|
||||
const channels = audio.state().player.channels().len;
|
||||
const sample_rate: f32 = @floatFromInt(audio.state().player.sampleRate());
|
||||
fn fillTone(audio: *mach.Audio, frequency: f32) ![]const f32 {
|
||||
const channels = audio.player.channels().len;
|
||||
const sample_rate: f32 = @floatFromInt(audio.player.sampleRate());
|
||||
const duration: f32 = 1.5 * @as(f32, @floatFromInt(channels)) * sample_rate; // play the tone for 1.5s
|
||||
const gain = 0.1;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,24 +1,22 @@
|
|||
const std = @import("std");
|
||||
const mach = @import("mach");
|
||||
|
||||
// The global list of Mach modules our application may use.
|
||||
pub const modules = .{
|
||||
// The set of Mach modules our application may use.
|
||||
const Modules = mach.Modules(.{
|
||||
mach.Core,
|
||||
mach.Audio,
|
||||
@import("App.zig"),
|
||||
};
|
||||
});
|
||||
|
||||
// TODO: move this to a mach "entrypoint" zig module which handles nuances like WASM requires.
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.c_allocator;
|
||||
|
||||
// Initialize module system
|
||||
try mach.mods.init(allocator);
|
||||
// The set of Mach modules our application may use.
|
||||
var mods = Modules.init(allocator);
|
||||
// TODO: enable mods.deinit(allocator); for allocator leak detection
|
||||
// defer mods.deinit(allocator);
|
||||
|
||||
// Schedule .app.start to run.
|
||||
mach.mods.schedule(.app, .start);
|
||||
|
||||
// Dispatch systems forever or until there are none left to dispatch. If your app uses mach.Core
|
||||
// then this will block forever and never return.
|
||||
try mach.mods.dispatch(.{});
|
||||
const app = mods.get(.app);
|
||||
app.run(.main);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,16 +15,9 @@ pub const App = @This();
|
|||
// TODO: banish global allocator
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
|
||||
pub const name = .app;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
pub const mach_module = .app;
|
||||
|
||||
pub const systems = .{
|
||||
.start = .{ .handler = start },
|
||||
.init = .{ .handler = init },
|
||||
.deinit = .{ .handler = deinit },
|
||||
.tick = .{ .handler = tick },
|
||||
.audio_state_change = .{ .handler = audioStateChange },
|
||||
};
|
||||
pub const mach_systems = .{ .start, .init, .deinit, .tick, .audio_state_change };
|
||||
|
||||
pub const components = .{
|
||||
.is_bgm = .{ .type = void },
|
||||
|
|
@ -33,9 +26,9 @@ pub const components = .{
|
|||
sfx: mach.Audio.Opus,
|
||||
|
||||
fn start(
|
||||
core: *mach.Core.Mod,
|
||||
audio: *mach.Audio.Mod,
|
||||
app: *Mod,
|
||||
core: *mach.Core,
|
||||
audio: *mach.Audio,
|
||||
app: *App,
|
||||
) !void {
|
||||
core.schedule(.init);
|
||||
audio.schedule(.init);
|
||||
|
|
@ -44,16 +37,19 @@ fn start(
|
|||
|
||||
fn init(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
audio: *mach.Audio.Mod,
|
||||
app: *Mod,
|
||||
core: *mach.Core,
|
||||
audio: *mach.Audio,
|
||||
app: *App,
|
||||
app_tick: mach.Call(App, .tick),
|
||||
app_deinit: mach.Call(App, .deinit),
|
||||
app_audio_state_change: mach.Call(App, .audio_state_change),
|
||||
) !void {
|
||||
core.state().on_tick = app.system(.tick);
|
||||
core.state().on_exit = app.system(.deinit);
|
||||
core.on_tick = app_tick.id;
|
||||
core.on_exit = app_deinit.id;
|
||||
|
||||
// Configure the audio module to send our app's .audio_state_change event when an entity's sound
|
||||
// finishes playing.
|
||||
audio.state().on_state_change = app.system(.audio_state_change);
|
||||
audio.on_state_change = app_audio_state_change.id;
|
||||
|
||||
const bgm_fbs = std.io.fixedBufferStream(assets.bgm.bit_bit_loop);
|
||||
const bgm_sound_stream = std.io.StreamSource{ .const_buffer = bgm_fbs };
|
||||
|
|
@ -77,24 +73,21 @@ 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 deinit(core: *mach.Core.Mod, audio: *mach.Audio.Mod) void {
|
||||
fn deinit(audio: *mach.Audio) void {
|
||||
audio.schedule(.deinit);
|
||||
core.schedule(.deinit);
|
||||
}
|
||||
|
||||
fn audioStateChange(
|
||||
entities: *mach.Entities.Mod,
|
||||
audio: *mach.Audio.Mod,
|
||||
app: *Mod,
|
||||
audio: *mach.Audio,
|
||||
app: *App,
|
||||
) !void {
|
||||
// Find audio entities that are no longer playing
|
||||
var q = try entities.query(.{
|
||||
.ids = mach.Entities.Mod.read(.id),
|
||||
.playings = mach.Audio.Mod.read(.playing),
|
||||
.playings = mach.Audio.read(.playing),
|
||||
});
|
||||
while (q.next()) |v| {
|
||||
for (v.ids, v.playings) |id, playing| {
|
||||
|
|
@ -114,45 +107,45 @@ fn audioStateChange(
|
|||
|
||||
fn tick(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
audio: *mach.Audio.Mod,
|
||||
app: *Mod,
|
||||
core: *mach.Core,
|
||||
audio: *mach.Audio,
|
||||
app: *App,
|
||||
) !void {
|
||||
while (core.state().nextEvent()) |event| {
|
||||
while (core.nextEvent()) |event| {
|
||||
switch (event) {
|
||||
.key_press => |ev| switch (ev.key) {
|
||||
.down => {
|
||||
const vol = math.clamp(try audio.state().player.volume() - 0.1, 0, 1);
|
||||
try audio.state().player.setVolume(vol);
|
||||
const vol = math.clamp(try audio.player.volume() - 0.1, 0, 1);
|
||||
try audio.player.setVolume(vol);
|
||||
std.debug.print("[volume] {d:.0}%\n", .{vol * 100.0});
|
||||
},
|
||||
.up => {
|
||||
const vol = math.clamp(try audio.state().player.volume() + 0.1, 0, 1);
|
||||
try audio.state().player.setVolume(vol);
|
||||
const vol = math.clamp(try audio.player.volume() + 0.1, 0, 1);
|
||||
try audio.player.setVolume(vol);
|
||||
std.debug.print("[volume] {d:.0}%\n", .{vol * 100.0});
|
||||
},
|
||||
else => {
|
||||
// Play a new SFX
|
||||
const e = try entities.new();
|
||||
try audio.set(e, .samples, app.state().sfx.samples);
|
||||
try audio.set(e, .channels, app.state().sfx.channels);
|
||||
try audio.set(e, .samples, app.sfx.samples);
|
||||
try audio.set(e, .channels, app.sfx.channels);
|
||||
try audio.set(e, .index, 0);
|
||||
try audio.set(e, .playing, true);
|
||||
},
|
||||
},
|
||||
.close => core.schedule(.exit),
|
||||
.close => core.exit(),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
// Grab the back buffer of the swapchain
|
||||
// TODO(Core)
|
||||
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
|
||||
const back_buffer_view = core.swap_chain.getCurrentTextureView().?;
|
||||
defer back_buffer_view.release();
|
||||
|
||||
// Create a command encoder
|
||||
const label = @tagName(name) ++ ".tick";
|
||||
const encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
||||
const label = @tagName(mach_module) ++ ".tick";
|
||||
const encoder = core.device.createCommandEncoder(&.{ .label = label });
|
||||
defer encoder.release();
|
||||
|
||||
// Begin render pass
|
||||
|
|
@ -177,8 +170,5 @@ fn tick(
|
|||
// Submit our commands to the queue
|
||||
var command = encoder.finish(&.{ .label = label });
|
||||
defer command.release();
|
||||
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
|
||||
// Present the frame
|
||||
core.schedule(.present_frame);
|
||||
core.queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,22 @@
|
|||
const std = @import("std");
|
||||
const mach = @import("mach");
|
||||
|
||||
// The global list of Mach modules our application may use.
|
||||
pub const modules = .{
|
||||
// The set of Mach modules our application may use.
|
||||
const Modules = mach.Modules(.{
|
||||
mach.Core,
|
||||
mach.Audio,
|
||||
@import("App.zig"),
|
||||
};
|
||||
});
|
||||
|
||||
// TODO: move this to a mach "entrypoint" zig module which handles nuances like WASM requires.
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.c_allocator;
|
||||
|
||||
// Initialize module system
|
||||
try mach.mods.init(allocator);
|
||||
// The set of Mach modules our application may use.
|
||||
var mods = Modules.init(allocator);
|
||||
// TODO: enable mods.deinit(allocator); for allocator leak detection
|
||||
// defer mods.deinit(allocator);
|
||||
|
||||
// Schedule .app.start to run.
|
||||
mach.mods.schedule(.app, .start);
|
||||
|
||||
// Dispatch systems forever or until there are none left to dispatch. If your app uses mach.Core
|
||||
// then this will block forever and never return.
|
||||
try mach.mods.dispatch(.{});
|
||||
const app = mods.get(.app);
|
||||
app.run(.main);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,12 @@ const Vec3 = math.Vec3;
|
|||
const Mat3x3 = math.Mat3x3;
|
||||
const Mat4x4 = math.Mat4x4;
|
||||
|
||||
const App = @This();
|
||||
|
||||
pub const mach_module = .app;
|
||||
|
||||
pub const mach_systems = .{ .start, .init, .deinit, .tick, .end_frame };
|
||||
|
||||
// TODO: banish global allocator
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
|
||||
|
|
@ -32,31 +38,14 @@ pipeline: mach.EntityID,
|
|||
frame_encoder: *gpu.CommandEncoder = undefined,
|
||||
frame_render_pass: *gpu.RenderPassEncoder = undefined,
|
||||
|
||||
// Define the globally unique name of our module. You can use any name here, but keep in mind no
|
||||
// two modules in the program can have the same name.
|
||||
pub const name = .app;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
|
||||
pub const systems = .{
|
||||
.start = .{ .handler = start },
|
||||
.init = .{ .handler = init },
|
||||
.deinit = .{ .handler = deinit },
|
||||
.tick = .{ .handler = tick },
|
||||
.end_frame = .{ .handler = endFrame },
|
||||
};
|
||||
|
||||
fn deinit(
|
||||
core: *mach.Core.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
) !void {
|
||||
fn deinit(sprite_pipeline: *gfx.SpritePipeline.Mod) !void {
|
||||
sprite_pipeline.schedule(.deinit);
|
||||
core.schedule(.deinit);
|
||||
}
|
||||
|
||||
fn start(
|
||||
core: *mach.Core.Mod,
|
||||
core: *mach.Core,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
app: *Mod,
|
||||
app: *App,
|
||||
) !void {
|
||||
core.schedule(.init);
|
||||
sprite_pipeline.schedule(.init);
|
||||
|
|
@ -65,13 +54,15 @@ fn start(
|
|||
|
||||
fn init(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
core: *mach.Core,
|
||||
sprite: *gfx.Sprite.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
app: *Mod,
|
||||
app: *App,
|
||||
app_tick: mach.Call(App, .tick),
|
||||
app_deinit: mach.Call(App, .deinit),
|
||||
) !void {
|
||||
core.state().on_tick = app.system(.tick);
|
||||
core.state().on_exit = app.system(.deinit);
|
||||
core.on_tick = app_tick.id;
|
||||
core.on_exit = app_deinit.id;
|
||||
|
||||
// We can create entities, and set components on them. Note that components live in a module
|
||||
// namespace, e.g. the `.mach_gfx_sprite` module could have a 3D `.location` component with a different
|
||||
|
|
@ -103,20 +94,18 @@ fn init(
|
|||
.allocator = allocator,
|
||||
.pipeline = pipeline,
|
||||
});
|
||||
|
||||
core.schedule(.start);
|
||||
}
|
||||
|
||||
fn tick(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
core: *mach.Core,
|
||||
sprite: *gfx.Sprite.Mod,
|
||||
sprite_pipeline: *gfx.SpritePipeline.Mod,
|
||||
app: *Mod,
|
||||
app: *App,
|
||||
) !void {
|
||||
var direction = app.state().direction;
|
||||
var spawning = app.state().spawning;
|
||||
while (core.state().nextEvent()) |event| {
|
||||
var direction = app.direction;
|
||||
var spawning = app.spawning;
|
||||
while (core.nextEvent()) |event| {
|
||||
switch (event) {
|
||||
.key_press => |ev| {
|
||||
switch (ev.key) {
|
||||
|
|
@ -138,34 +127,34 @@ fn tick(
|
|||
else => {},
|
||||
}
|
||||
},
|
||||
.close => core.schedule(.exit),
|
||||
.close => core.exit(),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
app.state().direction = direction;
|
||||
app.state().spawning = spawning;
|
||||
app.direction = direction;
|
||||
app.spawning = spawning;
|
||||
|
||||
var player_transform = sprite.get(app.state().player, .transform).?;
|
||||
var player_transform = sprite.get(app.player, .transform).?;
|
||||
var player_pos = player_transform.translation();
|
||||
if (spawning and app.state().spawn_timer.read() > 1.0 / 60.0) {
|
||||
if (spawning and app.spawn_timer.read() > 1.0 / 60.0) {
|
||||
// Spawn new entities
|
||||
_ = app.state().spawn_timer.lap();
|
||||
_ = app.spawn_timer.lap();
|
||||
for (0..100) |_| {
|
||||
var new_pos = player_pos;
|
||||
new_pos.v[0] += app.state().rand.random().floatNorm(f32) * 25;
|
||||
new_pos.v[1] += app.state().rand.random().floatNorm(f32) * 25;
|
||||
new_pos.v[0] += app.rand.random().floatNorm(f32) * 25;
|
||||
new_pos.v[1] += app.rand.random().floatNorm(f32) * 25;
|
||||
|
||||
const new_entity = try entities.new();
|
||||
try sprite.set(new_entity, .transform, Mat4x4.translate(new_pos).mul(&Mat4x4.scale(Vec3.splat(0.3))));
|
||||
try sprite.set(new_entity, .size, vec2(32, 32));
|
||||
try sprite.set(new_entity, .uv_transform, Mat3x3.translate(vec2(0, 0)));
|
||||
try sprite.set(new_entity, .pipeline, app.state().pipeline);
|
||||
app.state().sprites += 1;
|
||||
try sprite.set(new_entity, .pipeline, app.pipeline);
|
||||
app.sprites += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Multiply by delta_time to ensure that movement is the same speed regardless of the frame rate.
|
||||
const delta_time = app.state().timer.lap();
|
||||
const delta_time = app.timer.lap();
|
||||
|
||||
// Rotate entities
|
||||
var q = try entities.query(.{
|
||||
|
|
@ -179,8 +168,8 @@ fn tick(
|
|||
// transform = transform.mul(&Mat4x4.translate(location));
|
||||
var transform = Mat4x4.ident;
|
||||
transform = transform.mul(&Mat4x4.translate(location));
|
||||
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * app.state().time));
|
||||
transform = transform.mul(&Mat4x4.scaleScalar(@min(math.cos(app.state().time / 2.0), 0.5)));
|
||||
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * app.time));
|
||||
transform = transform.mul(&Mat4x4.scaleScalar(@min(math.cos(app.time / 2.0), 0.5)));
|
||||
entity_transform.* = transform;
|
||||
}
|
||||
}
|
||||
|
|
@ -190,19 +179,19 @@ fn tick(
|
|||
const speed = 200.0;
|
||||
player_pos.v[0] += direction.x() * speed * delta_time;
|
||||
player_pos.v[1] += direction.y() * speed * delta_time;
|
||||
try sprite.set(app.state().player, .transform, Mat4x4.translate(player_pos));
|
||||
try sprite.set(app.player, .transform, Mat4x4.translate(player_pos));
|
||||
sprite.schedule(.update);
|
||||
|
||||
// Perform pre-render work
|
||||
sprite_pipeline.schedule(.pre_render);
|
||||
|
||||
// Create a command encoder for this frame
|
||||
const label = @tagName(name) ++ ".tick";
|
||||
app.state().frame_encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
||||
const label = @tagName(mach_module) ++ ".tick";
|
||||
app.frame_encoder = core.device.createCommandEncoder(&.{ .label = label });
|
||||
|
||||
// Grab the back buffer of the swapchain
|
||||
// TODO(Core)
|
||||
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
|
||||
const back_buffer_view = core.swap_chain.getCurrentTextureView().?;
|
||||
defer back_buffer_view.release();
|
||||
|
||||
// Begin render pass
|
||||
|
|
@ -213,52 +202,49 @@ fn tick(
|
|||
.load_op = .clear,
|
||||
.store_op = .store,
|
||||
}};
|
||||
app.state().frame_render_pass = app.state().frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||
app.frame_render_pass = app.frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||
.label = label,
|
||||
.color_attachments = &color_attachments,
|
||||
}));
|
||||
|
||||
// Render our sprite batch
|
||||
sprite_pipeline.state().render_pass = app.state().frame_render_pass;
|
||||
sprite_pipeline.state().render_pass = app.frame_render_pass;
|
||||
sprite_pipeline.schedule(.render);
|
||||
|
||||
// Finish the frame once rendering is done.
|
||||
app.schedule(.end_frame);
|
||||
|
||||
app.state().time += delta_time;
|
||||
app.time += delta_time;
|
||||
}
|
||||
|
||||
fn endFrame(app: *Mod, core: *mach.Core.Mod) !void {
|
||||
fn endFrame(app: *App, core: *mach.Core) !void {
|
||||
// Finish render pass
|
||||
app.state().frame_render_pass.end();
|
||||
const label = @tagName(name) ++ ".endFrame";
|
||||
var command = app.state().frame_encoder.finish(&.{ .label = label });
|
||||
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
app.frame_render_pass.end();
|
||||
const label = @tagName(mach_module) ++ ".endFrame";
|
||||
var command = app.frame_encoder.finish(&.{ .label = label });
|
||||
core.queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
command.release();
|
||||
app.state().frame_encoder.release();
|
||||
app.state().frame_render_pass.release();
|
||||
|
||||
// Present the frame
|
||||
core.schedule(.present_frame);
|
||||
app.frame_encoder.release();
|
||||
app.frame_render_pass.release();
|
||||
|
||||
// Every second, update the window title with the FPS
|
||||
if (app.state().fps_timer.read() >= 1.0) {
|
||||
try core.state().printTitle(
|
||||
core.state().main_window,
|
||||
if (app.fps_timer.read() >= 1.0) {
|
||||
try core.printTitle(
|
||||
core.main_window,
|
||||
"sprite [ FPS: {d} ] [ Sprites: {d} ]",
|
||||
.{ app.state().frame_count, app.state().sprites },
|
||||
.{ app.frame_count, app.sprites },
|
||||
);
|
||||
core.schedule(.update);
|
||||
app.state().fps_timer.reset();
|
||||
app.state().frame_count = 0;
|
||||
app.fps_timer.reset();
|
||||
app.frame_count = 0;
|
||||
}
|
||||
app.state().frame_count += 1;
|
||||
app.frame_count += 1;
|
||||
}
|
||||
|
||||
// TODO: move this helper into gfx module
|
||||
fn loadTexture(core: *mach.Core.Mod, allocator: std.mem.Allocator) !*gpu.Texture {
|
||||
const device = core.state().device;
|
||||
const queue = core.state().queue;
|
||||
fn loadTexture(core: *mach.Core, allocator: std.mem.Allocator) !*gpu.Texture {
|
||||
const device = core.device;
|
||||
const queue = core.queue;
|
||||
|
||||
// Load the image from memory
|
||||
var img = try zigimg.Image.fromMemory(allocator, assets.sprites_sheet_png);
|
||||
|
|
@ -266,7 +252,7 @@ fn loadTexture(core: *mach.Core.Mod, allocator: std.mem.Allocator) !*gpu.Texture
|
|||
const img_size = gpu.Extent3D{ .width = @as(u32, @intCast(img.width)), .height = @as(u32, @intCast(img.height)) };
|
||||
|
||||
// Create a GPU texture
|
||||
const label = @tagName(name) ++ ".loadTexture";
|
||||
const label = @tagName(mach_module) ++ ".loadTexture";
|
||||
const texture = device.createTexture(&.{
|
||||
.label = label,
|
||||
.size = img_size,
|
||||
|
|
|
|||
|
|
@ -1,24 +1,22 @@
|
|||
const std = @import("std");
|
||||
const mach = @import("mach");
|
||||
|
||||
// The global list of Mach modules our application may use.
|
||||
pub const modules = .{
|
||||
// The set of Mach modules our application may use.
|
||||
const Modules = mach.Modules(.{
|
||||
mach.Core,
|
||||
mach.gfx.sprite_modules,
|
||||
@import("App.zig"),
|
||||
};
|
||||
});
|
||||
|
||||
// TODO: move this to a mach "entrypoint" zig module which handles nuances like WASM requires.
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.c_allocator;
|
||||
|
||||
// Initialize module system
|
||||
try mach.mods.init(allocator);
|
||||
// The set of Mach modules our application may use.
|
||||
var mods = Modules.init(allocator);
|
||||
// TODO: enable mods.deinit(allocator); for allocator leak detection
|
||||
// defer mods.deinit(allocator);
|
||||
|
||||
// Schedule .app.start to run.
|
||||
mach.mods.schedule(.app, .start);
|
||||
|
||||
// Dispatch systems forever or until there are none left to dispatch. If your app uses mach.Core
|
||||
// then this will block forever and never return.
|
||||
try mach.mods.dispatch(.{});
|
||||
const app = mods.get(.app);
|
||||
app.run(.main);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,12 @@ const Vec3 = math.Vec3;
|
|||
const Mat3x3 = math.Mat3x3;
|
||||
const Mat4x4 = math.Mat4x4;
|
||||
|
||||
const App = @This();
|
||||
|
||||
pub const mach_module = .app;
|
||||
|
||||
pub const mach_systems = .{ .start, .init, .deinit, .tick, .end_frame };
|
||||
|
||||
timer: mach.time.Timer,
|
||||
player: mach.EntityID,
|
||||
direction: Vec2 = vec2(0, 0),
|
||||
|
|
@ -28,19 +34,6 @@ pipeline: mach.EntityID,
|
|||
frame_encoder: *gpu.CommandEncoder = undefined,
|
||||
frame_render_pass: *gpu.RenderPassEncoder = undefined,
|
||||
|
||||
// Define the globally unique name of our module. You can use any name here, but keep in mind no
|
||||
// two modules in the program can have the same name.
|
||||
pub const name = .app;
|
||||
pub const Mod = mach.Mod(@This());
|
||||
|
||||
pub const systems = .{
|
||||
.start = .{ .handler = start },
|
||||
.init = .{ .handler = init },
|
||||
.deinit = .{ .handler = deinit },
|
||||
.tick = .{ .handler = tick },
|
||||
.end_frame = .{ .handler = endFrame },
|
||||
};
|
||||
|
||||
const upscale = 1.0;
|
||||
|
||||
const text1: []const []const u8 = &.{
|
||||
|
|
@ -51,19 +44,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 start(
|
||||
core: *mach.Core.Mod,
|
||||
core: *mach.Core,
|
||||
text: *gfx.Text.Mod,
|
||||
text_pipeline: *gfx.TextPipeline.Mod,
|
||||
app: *Mod,
|
||||
app: *App,
|
||||
) !void {
|
||||
core.schedule(.init);
|
||||
text.schedule(.init);
|
||||
|
|
@ -73,14 +62,16 @@ fn start(
|
|||
|
||||
fn init(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
core: *mach.Core,
|
||||
text: *gfx.Text.Mod,
|
||||
text_pipeline: *gfx.TextPipeline.Mod,
|
||||
text_style: *gfx.TextStyle.Mod,
|
||||
app: *Mod,
|
||||
app: *App,
|
||||
app_tick: mach.Call(App, .tick),
|
||||
app_deinit: mach.Call(App, .deinit),
|
||||
) !void {
|
||||
core.state().on_tick = app.system(.tick);
|
||||
core.state().on_exit = app.system(.deinit);
|
||||
core.on_tick = app_tick.id;
|
||||
core.on_exit = app_deinit.id;
|
||||
|
||||
// TODO: a better way to initialize entities with default values
|
||||
// TODO(text): ability to specify other style options (custom font name, font color, italic/bold, etc.)
|
||||
|
|
@ -114,20 +105,18 @@ fn init(
|
|||
.style1 = style1,
|
||||
.pipeline = pipeline,
|
||||
});
|
||||
|
||||
core.schedule(.start);
|
||||
}
|
||||
|
||||
fn tick(
|
||||
entities: *mach.Entities.Mod,
|
||||
core: *mach.Core.Mod,
|
||||
core: *mach.Core,
|
||||
text: *gfx.Text.Mod,
|
||||
text_pipeline: *gfx.TextPipeline.Mod,
|
||||
app: *Mod,
|
||||
app: *App,
|
||||
) !void {
|
||||
var direction = app.state().direction;
|
||||
var spawning = app.state().spawning;
|
||||
while (core.state().nextEvent()) |event| {
|
||||
var direction = app.direction;
|
||||
var spawning = app.spawning;
|
||||
while (core.nextEvent()) |event| {
|
||||
switch (event) {
|
||||
.key_press => |ev| {
|
||||
switch (ev.key) {
|
||||
|
|
@ -149,33 +138,33 @@ fn tick(
|
|||
else => {},
|
||||
}
|
||||
},
|
||||
.close => core.schedule(.exit),
|
||||
.close => core.exit(),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
app.state().direction = direction;
|
||||
app.state().spawning = spawning;
|
||||
app.direction = direction;
|
||||
app.spawning = spawning;
|
||||
|
||||
var player_transform = text.get(app.state().player, .transform).?;
|
||||
var player_transform = text.get(app.player, .transform).?;
|
||||
var player_pos = player_transform.translation().divScalar(upscale);
|
||||
if (spawning and app.state().spawn_timer.read() > (1.0 / 60.0)) {
|
||||
if (spawning and app.spawn_timer.read() > (1.0 / 60.0)) {
|
||||
// Spawn new entities
|
||||
_ = app.state().spawn_timer.lap();
|
||||
_ = app.spawn_timer.lap();
|
||||
for (0..10) |_| {
|
||||
var new_pos = player_pos;
|
||||
new_pos.v[0] += app.state().rand.random().floatNorm(f32) * 50;
|
||||
new_pos.v[1] += app.state().rand.random().floatNorm(f32) * 50;
|
||||
new_pos.v[0] += app.rand.random().floatNorm(f32) * 50;
|
||||
new_pos.v[1] += app.rand.random().floatNorm(f32) * 50;
|
||||
|
||||
// Create some text
|
||||
const new_entity = try entities.new();
|
||||
try text.set(new_entity, .pipeline, app.state().pipeline);
|
||||
try text.set(new_entity, .pipeline, app.pipeline);
|
||||
try text.set(new_entity, .transform, Mat4x4.scaleScalar(upscale).mul(&Mat4x4.translate(new_pos)));
|
||||
try gfx.Text.allocPrintText(text, new_entity, app.state().style1, "?!$", .{});
|
||||
try gfx.Text.allocPrintText(text, new_entity, app.style1, "?!$", .{});
|
||||
}
|
||||
}
|
||||
|
||||
// Multiply by delta_time to ensure that movement is the same speed regardless of the frame rate.
|
||||
const delta_time = app.state().timer.lap();
|
||||
const delta_time = app.timer.lap();
|
||||
|
||||
// Rotate entities
|
||||
var q = try entities.query(.{
|
||||
|
|
@ -189,8 +178,8 @@ fn tick(
|
|||
// transform = transform.mul(&Mat4x4.translate(location));
|
||||
var transform = Mat4x4.ident;
|
||||
transform = transform.mul(&Mat4x4.translate(location));
|
||||
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * app.state().time));
|
||||
transform = transform.mul(&Mat4x4.scaleScalar(@min(math.cos(app.state().time / 2.0), 0.5)));
|
||||
transform = transform.mul(&Mat4x4.rotateZ(2 * math.pi * app.time));
|
||||
transform = transform.mul(&Mat4x4.scaleScalar(@min(math.cos(app.time / 2.0), 0.5)));
|
||||
entity_transform.* = transform;
|
||||
}
|
||||
}
|
||||
|
|
@ -200,20 +189,20 @@ fn tick(
|
|||
const speed = 200.0 / upscale;
|
||||
player_pos.v[0] += direction.x() * speed * delta_time;
|
||||
player_pos.v[1] += direction.y() * speed * delta_time;
|
||||
try text.set(app.state().player, .transform, Mat4x4.scaleScalar(upscale).mul(&Mat4x4.translate(player_pos)));
|
||||
try text.set(app.state().player, .dirty, true);
|
||||
try text.set(app.player, .transform, Mat4x4.scaleScalar(upscale).mul(&Mat4x4.translate(player_pos)));
|
||||
try text.set(app.player, .dirty, true);
|
||||
text.schedule(.update);
|
||||
|
||||
// Perform pre-render work
|
||||
text_pipeline.schedule(.pre_render);
|
||||
|
||||
// Create a command encoder for this frame
|
||||
const label = @tagName(name) ++ ".tick";
|
||||
app.state().frame_encoder = core.state().device.createCommandEncoder(&.{ .label = label });
|
||||
const label = @tagName(mach_module) ++ ".tick";
|
||||
app.frame_encoder = core.device.createCommandEncoder(&.{ .label = label });
|
||||
|
||||
// Grab the back buffer of the swapchain
|
||||
// TODO(Core)
|
||||
const back_buffer_view = core.state().swap_chain.getCurrentTextureView().?;
|
||||
const back_buffer_view = core.swap_chain.getCurrentTextureView().?;
|
||||
defer back_buffer_view.release();
|
||||
|
||||
// Begin render pass
|
||||
|
|
@ -224,40 +213,37 @@ fn tick(
|
|||
.load_op = .clear,
|
||||
.store_op = .store,
|
||||
}};
|
||||
app.state().frame_render_pass = app.state().frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||
app.frame_render_pass = app.frame_encoder.beginRenderPass(&gpu.RenderPassDescriptor.init(.{
|
||||
.label = label,
|
||||
.color_attachments = &color_attachments,
|
||||
}));
|
||||
|
||||
// Render our text batch
|
||||
text_pipeline.state().render_pass = app.state().frame_render_pass;
|
||||
text_pipeline.state().render_pass = app.frame_render_pass;
|
||||
text_pipeline.schedule(.render);
|
||||
|
||||
// Finish the frame once rendering is done.
|
||||
app.schedule(.end_frame);
|
||||
|
||||
app.state().time += delta_time;
|
||||
app.time += delta_time;
|
||||
}
|
||||
|
||||
fn endFrame(
|
||||
entities: *mach.Entities.Mod,
|
||||
app: *Mod,
|
||||
core: *mach.Core.Mod,
|
||||
app: *App,
|
||||
core: *mach.Core,
|
||||
) !void {
|
||||
// Finish render pass
|
||||
app.state().frame_render_pass.end();
|
||||
const label = @tagName(name) ++ ".endFrame";
|
||||
var command = app.state().frame_encoder.finish(&.{ .label = label });
|
||||
core.state().queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
app.frame_render_pass.end();
|
||||
const label = @tagName(mach_module) ++ ".endFrame";
|
||||
var command = app.frame_encoder.finish(&.{ .label = label });
|
||||
core.queue.submit(&[_]*gpu.CommandBuffer{command});
|
||||
command.release();
|
||||
app.state().frame_encoder.release();
|
||||
app.state().frame_render_pass.release();
|
||||
|
||||
// Present the frame
|
||||
core.schedule(.present_frame);
|
||||
app.frame_encoder.release();
|
||||
app.frame_render_pass.release();
|
||||
|
||||
// Every second, update the window title with the FPS
|
||||
if (app.state().fps_timer.read() >= 1.0) {
|
||||
if (app.fps_timer.read() >= 1.0) {
|
||||
// Gather some text rendering stats
|
||||
var num_texts: u32 = 0;
|
||||
var num_glyphs: usize = 0;
|
||||
|
|
@ -271,14 +257,14 @@ fn endFrame(
|
|||
}
|
||||
}
|
||||
|
||||
try core.state().printTitle(
|
||||
core.state().main_window,
|
||||
try core.printTitle(
|
||||
core.main_window,
|
||||
"text [ FPS: {d} ] [ Texts: {d} ] [ Glyphs: {d} ]",
|
||||
.{ app.state().frame_count, num_texts, num_glyphs },
|
||||
.{ app.frame_count, num_texts, num_glyphs },
|
||||
);
|
||||
core.schedule(.update);
|
||||
app.state().fps_timer.reset();
|
||||
app.state().frame_count = 0;
|
||||
app.fps_timer.reset();
|
||||
app.frame_count = 0;
|
||||
}
|
||||
app.state().frame_count += 1;
|
||||
app.frame_count += 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,22 @@
|
|||
const std = @import("std");
|
||||
const mach = @import("mach");
|
||||
|
||||
// The global list of Mach modules our application may use.
|
||||
pub const modules = .{
|
||||
// The set of Mach modules our application may use.
|
||||
const Modules = mach.Modules(.{
|
||||
mach.Core,
|
||||
mach.gfx.text_modules,
|
||||
@import("App.zig"),
|
||||
};
|
||||
});
|
||||
|
||||
// TODO: move this to a mach "entrypoint" zig module which handles nuances like WASM requires.
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.c_allocator;
|
||||
|
||||
// Initialize module system
|
||||
try mach.mods.init(allocator);
|
||||
// The set of Mach modules our application may use.
|
||||
var mods = Modules.init(allocator);
|
||||
// TODO: enable mods.deinit(allocator); for allocator leak detection
|
||||
// defer mods.deinit(allocator);
|
||||
|
||||
// Schedule .app.start to run.
|
||||
mach.mods.schedule(.app, .start);
|
||||
|
||||
// Dispatch systems forever or until there are none left to dispatch. If your app uses mach.Core
|
||||
// then this will block forever and never return.
|
||||
try mach.mods.dispatch(.{});
|
||||
const app = mods.get(.app);
|
||||
app.run(.main);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue