adding audio work
This commit is contained in:
parent
ab184d5a33
commit
89fbe381e8
|
|
@ -4,13 +4,13 @@ const std = @import("std");
|
||||||
const memory = core.MemoryTracker;
|
const memory = core.MemoryTracker;
|
||||||
|
|
||||||
const soundEngine = @import("sound_engine.zig");
|
const soundEngine = @import("sound_engine.zig");
|
||||||
pub const NeonSoundEngine = soundEngine.NeonSoundEngine;
|
pub const SoundEngine = soundEngine.SoundEngine;
|
||||||
pub const sound_err = soundEngine.sound_err;
|
pub const sound_err = soundEngine.sound_err;
|
||||||
pub const sound_errs = soundEngine.sound_errs;
|
pub const sound_errs = soundEngine.sound_errs;
|
||||||
pub const sound_log = soundEngine.sound_log;
|
pub const sound_log = soundEngine.sound_log;
|
||||||
pub const sound_logs = soundEngine.sound_logs;
|
pub const sound_logs = soundEngine.sound_logs;
|
||||||
|
|
||||||
pub var gSoundEngine: *NeonSoundEngine = undefined;
|
pub var gSoundEngine: *SoundEngine = undefined;
|
||||||
pub var gSoundLoader: *soundEngine.SoundLoader = undefined;
|
pub var gSoundLoader: *soundEngine.SoundLoader = undefined;
|
||||||
|
|
||||||
pub fn start_module(spec: *core.SpecVariantMap, args: anytype, allocator: std.mem.Allocator) !void {
|
pub fn start_module(spec: *core.SpecVariantMap, args: anytype, allocator: std.mem.Allocator) !void {
|
||||||
|
|
@ -20,12 +20,13 @@ pub fn start_module(spec: *core.SpecVariantMap, args: anytype, allocator: std.me
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
gSoundEngine = core.createObject(NeonSoundEngine, .{ .can_tick = true }) catch unreachable;
|
gSoundEngine = core.createObject(SoundEngine, .{ .can_tick = true }) catch unreachable;
|
||||||
|
|
||||||
gSoundLoader = allocator.create(soundEngine.SoundLoader) catch unreachable;
|
gSoundLoader = allocator.create(soundEngine.SoundLoader) catch unreachable;
|
||||||
gSoundLoader.* = soundEngine.SoundLoader.init(gSoundEngine);
|
gSoundLoader.* = soundEngine.SoundLoader.init(gSoundEngine);
|
||||||
|
|
||||||
assets.getAssets().registerLoader(gSoundLoader) catch unreachable;
|
assets.getAssets().registerLoader(gSoundLoader) catch unreachable;
|
||||||
|
_ = try core.fs().installFileBytesMount("embedded:engineTick.wav", @constCast(&engineTickWav), true);
|
||||||
// var name = core.MakeName("s_test");
|
// var name = core.MakeName("s_test");
|
||||||
// gSoundEngine.loadSound(&name, "content/sounds/engineTick.wav", .{}) catch unreachable;
|
// gSoundEngine.loadSound(&name, "content/sounds/engineTick.wav", .{}) catch unreachable;
|
||||||
|
|
||||||
|
|
@ -37,7 +38,16 @@ pub fn shutdown_module(allocator: std.mem.Allocator) void {
|
||||||
gSoundEngine.shutdown();
|
gSoundEngine.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const context = core.EngineObject(SoundEngine).get;
|
||||||
|
|
||||||
|
pub fn loadBuiltinSounds() !void {
|
||||||
|
var name = core.MakeName("engine/s_tick");
|
||||||
|
try context().loadSound(&name, "embedded:engineTick.wav", .{});
|
||||||
|
}
|
||||||
|
|
||||||
pub const Module = core.ModuleDescription{
|
pub const Module = core.ModuleDescription{
|
||||||
.name = "audio",
|
.name = "audio",
|
||||||
.enabledByDefault = false,
|
.enabledByDefault = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const engineTickWav align(8) = @embedFile("embedded/engineTick.wav").*;
|
||||||
|
|
|
||||||
|
|
@ -29,9 +29,9 @@ pub fn sound_errs(comptime fmt: []const u8) void {
|
||||||
pub const SoundLoader = struct {
|
pub const SoundLoader = struct {
|
||||||
pub var LoaderInterfaceVTable: assets.AssetLoaderInterface = assets.AssetLoaderInterface.from("Sound", @This());
|
pub var LoaderInterfaceVTable: assets.AssetLoaderInterface = assets.AssetLoaderInterface.from("Sound", @This());
|
||||||
|
|
||||||
engine: *NeonSoundEngine,
|
engine: *SoundEngine,
|
||||||
|
|
||||||
pub fn init(engine: *NeonSoundEngine) @This() {
|
pub fn init(engine: *SoundEngine) @This() {
|
||||||
return @This(){
|
return @This(){
|
||||||
.engine = engine,
|
.engine = engine,
|
||||||
};
|
};
|
||||||
|
|
@ -64,11 +64,12 @@ fn ma_res(value: anytype) !void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// On init, SoundEngine will spawn a
|
// On init, SoundEngine will spawn a
|
||||||
pub const NeonSoundEngine = struct {
|
pub const SoundEngine = struct {
|
||||||
pub var NeonObjectTable: core.EngineObjectVTable = core.EngineObjectVTable.from(@This(), "audio.SoundEngine");
|
pub var NeonObjectTable: core.EngineObjectVTable = core.EngineObjectVTable.from(@This(), "audio.SoundEngine");
|
||||||
|
|
||||||
engine: *ma.ma_engine,
|
engine: *ma.ma_engine,
|
||||||
sounds: AutoHashMapUnmanaged(u32, *ma.ma_sound),
|
sounds: AutoHashMapUnmanaged(u32, *ma.ma_sound),
|
||||||
|
decoders: AutoHashMapUnmanaged(u32, *ma.ma_decoder),
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
volume: f32 = 1.0,
|
volume: f32 = 1.0,
|
||||||
|
|
||||||
|
|
@ -77,6 +78,7 @@ pub const NeonSoundEngine = struct {
|
||||||
self.* = @This(){
|
self.* = @This(){
|
||||||
.engine = allocator.create(ma.ma_engine) catch unreachable,
|
.engine = allocator.create(ma.ma_engine) catch unreachable,
|
||||||
.sounds = .{},
|
.sounds = .{},
|
||||||
|
.decoders = .{},
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -101,17 +103,31 @@ pub const NeonSoundEngine = struct {
|
||||||
const sound = try self.allocator.create(ma.ma_sound);
|
const sound = try self.allocator.create(ma.ma_sound);
|
||||||
errdefer self.allocator.destroy(sound);
|
errdefer self.allocator.destroy(sound);
|
||||||
|
|
||||||
const res = ma.ma_sound_init_from_file(self.engine, fileName.ptr, 0, null, null, sound);
|
const engineSoundBytes = try core.fs().loadFile(fileName);
|
||||||
|
const bytes = engineSoundBytes.bytes;
|
||||||
|
|
||||||
|
// Create a decoder from the loaded bytes
|
||||||
|
const decoder = try self.allocator.create(ma.ma_decoder);
|
||||||
|
errdefer self.allocator.destroy(decoder);
|
||||||
|
|
||||||
|
const res_decoder = ma.ma_decoder_init_memory(bytes.ptr, bytes.len, null, decoder);
|
||||||
|
if (res_decoder != ma.MA_SUCCESS) {
|
||||||
|
sound_err("failed to initialize decoder for sound: {s}", .{soundName.utf8()});
|
||||||
|
return error.MiniAudioError;
|
||||||
|
}
|
||||||
|
|
||||||
|
const res = ma.ma_sound_init_from_data_source(self.engine, decoder, 0, null, sound);
|
||||||
if (res != ma.MA_SUCCESS) {
|
if (res != ma.MA_SUCCESS) {
|
||||||
sound_err("tried loading sound: {s} failed", .{soundName.utf8()});
|
sound_err("tried loading sound: {s} failed", .{soundName.utf8()});
|
||||||
return error.MiniAudioError;
|
return error.MiniAudioError;
|
||||||
}
|
}
|
||||||
|
|
||||||
try self.sounds.put(self.allocator, soundName.handle(), sound);
|
try self.sounds.put(self.allocator, soundName.handle(), sound);
|
||||||
|
try self.decoders.put(self.allocator, soundName.handle(), decoder);
|
||||||
ma.ma_sound_set_volume(sound, soundParams.volume);
|
ma.ma_sound_set_volume(sound, soundParams.volume);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn playSound(self: *@This(), soundName: core.Name) !void {
|
pub fn playSound(self: *@This(), soundName: *core.Name) !void {
|
||||||
const maybeSound = self.sounds.get(soundName.handle());
|
const maybeSound = self.sounds.get(soundName.handle());
|
||||||
|
|
||||||
if (maybeSound == null)
|
if (maybeSound == null)
|
||||||
|
|
@ -142,6 +158,14 @@ pub const NeonSoundEngine = struct {
|
||||||
self.allocator.destroy(sound.value_ptr.*);
|
self.allocator.destroy(sound.value_ptr.*);
|
||||||
}
|
}
|
||||||
self.sounds.deinit(self.allocator);
|
self.sounds.deinit(self.allocator);
|
||||||
|
|
||||||
|
var decoder_iter = self.decoders.iterator();
|
||||||
|
while (decoder_iter.next()) |decoder| {
|
||||||
|
_ = ma.ma_decoder_uninit(decoder.value_ptr.*);
|
||||||
|
self.allocator.destroy(decoder.value_ptr.*);
|
||||||
|
}
|
||||||
|
self.decoders.deinit(self.allocator);
|
||||||
|
|
||||||
ma.ma_engine_uninit(self.engine);
|
ma.ma_engine_uninit(self.engine);
|
||||||
self.allocator.destroy(self.engine);
|
self.allocator.destroy(self.engine);
|
||||||
self.allocator.destroy(self);
|
self.allocator.destroy(self);
|
||||||
|
|
|
||||||
|
|
@ -422,6 +422,7 @@ pub const Context = struct {
|
||||||
debugText: std.ArrayList([]u8),
|
debugText: std.ArrayList([]u8),
|
||||||
debugTextCount: u32 = 0,
|
debugTextCount: u32 = 0,
|
||||||
drawDebug: bool = false,
|
drawDebug: bool = false,
|
||||||
|
debugMousePick: bool = false,
|
||||||
rootNodes: std.ArrayList(NodeHandle),
|
rootNodes: std.ArrayList(NodeHandle),
|
||||||
|
|
||||||
const debugTextMax = 32;
|
const debugTextMax = 32;
|
||||||
|
|
@ -438,19 +439,21 @@ pub const Context = struct {
|
||||||
pub fn tickDebug(self: *@This(), deltaTime: f64) !void {
|
pub fn tickDebug(self: *@This(), deltaTime: f64) !void {
|
||||||
_ = deltaTime;
|
_ = deltaTime;
|
||||||
try self.pushDebugText("mouse Position: {d}, {d}", .{ self.currentCursorPosition.x, self.currentCursorPosition.y });
|
try self.pushDebugText("mouse Position: {d}, {d}", .{ self.currentCursorPosition.x, self.currentCursorPosition.y });
|
||||||
if (self.mousePick.selected_node) |node| {
|
if (self.debugMousePick) {
|
||||||
const n = self.getRead(node);
|
if (self.mousePick.selected_node) |node| {
|
||||||
const layout = self._displayLayout.items[node.index];
|
const n = self.getRead(node);
|
||||||
try self.pushDebugText("found node: {d},{d} size={d}x{d} layoutpos={d},{d} layoutsize={d},{d}", .{
|
const layout = self._displayLayout.items[node.index];
|
||||||
node.index,
|
try self.pushDebugText("found node: {d},{d} size={d}x{d} layoutpos={d},{d} layoutsize={d},{d}", .{
|
||||||
node.generation,
|
node.index,
|
||||||
n.size.x,
|
node.generation,
|
||||||
n.size.y,
|
n.size.x,
|
||||||
layout.pos.x,
|
n.size.y,
|
||||||
layout.pos.y,
|
layout.pos.x,
|
||||||
layout.size.x,
|
layout.pos.y,
|
||||||
layout.size.y,
|
layout.size.x,
|
||||||
});
|
layout.size.y,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1192,6 +1195,10 @@ pub const Context = struct {
|
||||||
try self.addDebugInfo(drawList);
|
try self.addDebugInfo(drawList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (self.debugMousePick) {
|
||||||
|
try self.mousePick.addMousePickInfo(self, drawList);
|
||||||
|
}
|
||||||
|
|
||||||
for (0..drawList.items.len) |i| {
|
for (0..drawList.items.len) |i| {
|
||||||
switch (drawList.items[i].primitive) {
|
switch (drawList.items[i].primitive) {
|
||||||
.Text => |*text| {
|
.Text => |*text| {
|
||||||
|
|
@ -1211,8 +1218,6 @@ pub const Context = struct {
|
||||||
|
|
||||||
const fontHandle = self.fontCache.defaultMonoFont.atlas.fontHandle;
|
const fontHandle = self.fontCache.defaultMonoFont.atlas.fontHandle;
|
||||||
|
|
||||||
try self.mousePick.addMousePickInfo(&self, drawList);
|
|
||||||
|
|
||||||
try drawList.append(.{
|
try drawList.append(.{
|
||||||
.node = .{},
|
.node = .{},
|
||||||
.primitive = .{
|
.primitive = .{
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,7 @@ pub const ExternGameObject = struct {
|
||||||
fireTraceFilter: physics.IgnoreFixedBodiesFilter = .{},
|
fireTraceFilter: physics.IgnoreFixedBodiesFilter = .{},
|
||||||
particleDebugger: extras.ParticleDebugger.ParticleDebugger = undefined,
|
particleDebugger: extras.ParticleDebugger.ParticleDebugger = undefined,
|
||||||
|
|
||||||
|
volume: f32 = 100,
|
||||||
//lmao: bool = false,
|
//lmao: bool = false,
|
||||||
//lmao2: bool = true,
|
//lmao2: bool = true,
|
||||||
// lib: bool = false,
|
// lib: bool = false,
|
||||||
|
|
@ -342,6 +343,15 @@ pub const ExternGameObject = struct {
|
||||||
|
|
||||||
if (ig.checkbox("vectors test", &self.displayVectorsTest)) {}
|
if (ig.checkbox("vectors test", &self.displayVectorsTest)) {}
|
||||||
if (ig.checkbox("showdebug", &ui.context().screenContext.drawDebug)) {}
|
if (ig.checkbox("showdebug", &ui.context().screenContext.drawDebug)) {}
|
||||||
|
|
||||||
|
if (ig.smallButton("click for fun")) {
|
||||||
|
// var soundname = core.MakeName("s_macintosh");
|
||||||
|
// audio.context().playSound(&soundname) catch {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ig.sliderFloat("sliderFloat", &self.volume, 0, 100, null, .{})) {
|
||||||
|
audio.context().setVolume(self.volume / 100);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ig.end();
|
ig.end();
|
||||||
|
|
||||||
|
|
@ -379,6 +389,7 @@ const rend = backlog.rend;
|
||||||
const ig = imgui.api;
|
const ig = imgui.api;
|
||||||
const sys = backlog.sys;
|
const sys = backlog.sys;
|
||||||
const ui = backlog.ui;
|
const ui = backlog.ui;
|
||||||
|
const audio = backlog.audio;
|
||||||
const platform = backlog.platform;
|
const platform = backlog.platform;
|
||||||
const extras = @import("gameExtras");
|
const extras = @import("gameExtras");
|
||||||
const bsp = @import("bsp");
|
const bsp = @import("bsp");
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,11 @@ const assetReferences = [_]assets.AssetImportReference{
|
||||||
"a_fox_survey",
|
"a_fox_survey",
|
||||||
.{ .path = "gltf-samples/Fox/glTF/Survey.ozz" },
|
.{ .path = "gltf-samples/Fox/glTF/Survey.ozz" },
|
||||||
),
|
),
|
||||||
|
// assets.MakeImportRefOptions(
|
||||||
|
// "Sound",
|
||||||
|
// "s_macintosh",
|
||||||
|
// .{ .path = "sounds/MACINTOSHPLUS.wav" },
|
||||||
|
// ),
|
||||||
assets.MakeImportRefOptions(
|
assets.MakeImportRefOptions(
|
||||||
"Mesh",
|
"Mesh",
|
||||||
"m_fox",
|
"m_fox",
|
||||||
|
|
@ -233,6 +238,8 @@ pub fn prepare(self: *@This()) !void {
|
||||||
|
|
||||||
try extras.setup();
|
try extras.setup();
|
||||||
|
|
||||||
|
try audio.loadBuiltinSounds();
|
||||||
|
|
||||||
// try core.loadModule("externGame", true);
|
// try core.loadModule("externGame", true);
|
||||||
|
|
||||||
try backlog.loadDynamicModules();
|
try backlog.loadDynamicModules();
|
||||||
|
|
@ -537,6 +544,7 @@ const assets = backlog.assets;
|
||||||
const platform = backlog.platform;
|
const platform = backlog.platform;
|
||||||
const core = backlog.core;
|
const core = backlog.core;
|
||||||
const ig = backlog.imgui.api;
|
const ig = backlog.imgui.api;
|
||||||
|
const audio = backlog.audio;
|
||||||
const rend = backlog.rend;
|
const rend = backlog.rend;
|
||||||
const script = core.script;
|
const script = core.script;
|
||||||
const tracy = core.tracy;
|
const tracy = core.tracy;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue