Initial commit.
This commit is contained in:
132
engine/engine.odin
Normal file
132
engine/engine.odin
Normal file
@@ -0,0 +1,132 @@
|
||||
package engine
|
||||
|
||||
import "core:container/queue"
|
||||
import "core:log"
|
||||
import "core:math/linalg"
|
||||
|
||||
AUDIO_ENGINE_LJUD :: #config(AUDIO_ENGINE_LJUD, false)
|
||||
AUDIO_ENGINE_FMOD :: #config(AUDIO_ENGINE_FMOD, false)
|
||||
AUDIO_ENGINE_WWISE :: #config(AUDIO_ENGINE_WWISE, false)
|
||||
|
||||
import "ljud"
|
||||
|
||||
when AUDIO_ENGINE_LJUD {
|
||||
Engine_Backend :: ljud.Engine
|
||||
Sound_Backend :: ljud.Sound
|
||||
Sound_Instance_Backend :: ljud.Sound_Instance
|
||||
Bus_Backend :: ljud.Bus
|
||||
}
|
||||
else when AUDIO_ENGINE_FMOD {
|
||||
|
||||
}
|
||||
|
||||
Engine :: struct {
|
||||
backend: Engine_Backend,
|
||||
|
||||
sound_instance_ids: queue.Queue(Sound_Instance_ID),
|
||||
sound_instances: []Sound_Instance,
|
||||
|
||||
listeners: [MAX_LISTENERS]Listener,
|
||||
available_listener_ids: queue.Queue(Listener_ID),
|
||||
}
|
||||
|
||||
init :: proc(engine: ^Engine, max_sound_instances: u32) -> bool {
|
||||
assert(engine != nil)
|
||||
assert(max_sound_instances > 0)
|
||||
|
||||
when AUDIO_ENGINE_LJUD {
|
||||
// engine.§ new(ljud.Engine)
|
||||
// if engine.data != nil {
|
||||
// v := transmute(^ljud.Engine)(engine.data)
|
||||
// if !ljud.init(v) {
|
||||
// free(engine.data)
|
||||
// engine.data = nil
|
||||
|
||||
// log.errorf("Failed to initialize audio-engine LJUD.")
|
||||
// return false
|
||||
// }
|
||||
// }
|
||||
|
||||
if !ljud.init(&engine.backend) {
|
||||
// free(engine.data)
|
||||
// engine.data = nil
|
||||
|
||||
log.errorf("Failed to initialize audio-engine LJUD.")
|
||||
return false
|
||||
}
|
||||
}
|
||||
else {
|
||||
#assert("No audio-engine defined. '-define:AUDIO_ENGINE_X=true' where X is the audio-engine; 'LJUD', 'FMOD', 'WWISE'.")
|
||||
}
|
||||
|
||||
{ // Initialize sound-instances.
|
||||
log.infof("Creating %v sound-instances.", max_sound_instances)
|
||||
engine.sound_instances = make([]Sound_Instance, max_sound_instances)
|
||||
|
||||
queue_init_err := queue.init(&engine.sound_instance_ids, int(max_sound_instances))
|
||||
assert(queue_init_err == .None)
|
||||
|
||||
for i in 0..<len(engine.sound_instances) {
|
||||
queue.append(&engine.sound_instance_ids, Sound_Instance_ID(i))
|
||||
}
|
||||
}
|
||||
|
||||
{ // Initialize the array of listeners and the queue of listener-ids.
|
||||
queue.init(&engine.available_listener_ids, len(engine.listeners))
|
||||
for i in 0..<len(engine.listeners) {
|
||||
engine.listeners[i] = Listener {
|
||||
id = INVALID_LISTENER_ID,
|
||||
enabled = false,
|
||||
}
|
||||
queue.push_back(&engine.available_listener_ids, Listener_ID(i))
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
tick :: proc(engine: ^Engine) {
|
||||
when AUDIO_ENGINE_LJUD {
|
||||
ljud.tick(&engine.backend)
|
||||
}
|
||||
else {
|
||||
#assert("TODO: SS - Implement for Audio Engine backend")
|
||||
}
|
||||
|
||||
// Tick listeners.
|
||||
for listener in engine.listeners {
|
||||
if listener.id == INVALID_LISTENER_ID {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
when AUDIO_ENGINE_LJUD {
|
||||
ljud.tick_listener(
|
||||
&engine.backend,
|
||||
u8(listener.id),
|
||||
listener.enabled,
|
||||
listener.position,
|
||||
listener.velocity,
|
||||
linalg.normalize(listener.direction_forward),
|
||||
linalg.normalize(listener.world_up),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
shutdown :: proc(engine: ^Engine) {
|
||||
assert(engine != nil)
|
||||
|
||||
when AUDIO_ENGINE_LJUD {
|
||||
ljud.shutdown(&engine.backend)
|
||||
}
|
||||
else {
|
||||
#assert("TODO: SS - Implement for Audio Engine backend")
|
||||
}
|
||||
|
||||
delete(engine.sound_instances)
|
||||
engine.sound_instances = nil
|
||||
queue.destroy(&engine.sound_instance_ids)
|
||||
|
||||
queue.destroy(&engine.available_listener_ids)
|
||||
}
|
||||
Reference in New Issue
Block a user