examples: update play-opus to latest Audio API

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-05-06 13:56:36 -07:00
parent 9a96820926
commit 7ccfcefa96

View file

@ -20,6 +20,7 @@ pub const Mod = mach.Mod(@This());
pub const events = .{ pub const events = .{
.init = .{ .handler = init }, .init = .{ .handler = init },
.after_init = .{ .handler = afterInit },
.deinit = .{ .handler = deinit }, .deinit = .{ .handler = deinit },
.tick = .{ .handler = tick }, .tick = .{ .handler = tick },
.audio_state_change = .{ .handler = audioStateChange }, .audio_state_change = .{ .handler = audioStateChange },
@ -32,9 +33,8 @@ pub const components = .{
sfx: Opus, sfx: Opus,
fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) !void { fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) !void {
// Initialize audio module, telling it to send our module's .audio_state_change event when an audio.send(.init, .{});
// entity's sound stops playing app.send(.after_init, .{});
audio.send(.init, .{app.event(.audio_state_change)});
const bgm_fbs = std.io.fixedBufferStream(assets.bgm.bit_bit_loop); const bgm_fbs = std.io.fixedBufferStream(assets.bgm.bit_bit_loop);
const sfx_fbs = std.io.fixedBufferStream(assets.sfx.death); const sfx_fbs = std.io.fixedBufferStream(assets.sfx.death);
@ -63,6 +63,12 @@ fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) !void {
core.send(.start, .{}); core.send(.start, .{});
} }
fn afterInit(audio: *mach.Audio.Mod, app: *Mod) void {
// 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.event(.audio_state_change);
}
fn deinit(core: *mach.Core.Mod, audio: *mach.Audio.Mod) void { fn deinit(core: *mach.Core.Mod, audio: *mach.Audio.Mod) void {
audio.send(.deinit, .{}); audio.send(.deinit, .{});
core.send(.deinit, .{}); core.send(.deinit, .{});