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
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue