#include "sound.hpp" #include "dbc.hpp" #include #include "config.hpp" namespace sound { static SoundManager SMGR; static bool initialized = false; using namespace fmt; using std::make_shared; namespace fs = std::filesystem; void init() { if(!initialized) { Config assets("assets/config.json"); for(auto& el : assets["sounds"].items()) { load(el.key(), el.value()); } initialized = true; } } void load(const std::string name, const std::string sound_path) { dbc::check(fs::exists(sound_path), fmt::format("sound file {} does not exist", sound_path)); // create the buffer and keep in the buffer map auto buffer = make_shared(sound_path); // set it on the sound and keep in the sound map auto sound = make_shared(*buffer); sound->setRelativeToListener(false); sound->setPosition({0.0f, 0.0f, 1.0f}); SMGR.sounds.try_emplace(name, buffer, sound); } void play(const std::string name) { dbc::check(initialized, "You need to call sound::init() first"); if(SMGR.sounds.contains(name)) { // get the sound from the sound map auto pair = SMGR.sounds.at(name); // play it pair.sound->play(); } else { dbc::log(fmt::format("Attempted to play {} sound but not available.", name)); } } void play_at(const std::string name, float x, float y, float z) { dbc::check(initialized, "You need to call sound::init() first"); if(SMGR.sounds.contains(name)) { auto pair = SMGR.sounds.at(name); pair.sound->setPosition({x, y, z}); pair.sound->play(); } else { dbc::log(fmt::format("Attempted to play_at {} sound but not available.", name)); } } }