Working sound system and most enemies have a sound effect. This will make it easier to add sounds now.
This commit is contained in:
parent
83df9ff03b
commit
20cbc3a21c
12 changed files with 126 additions and 5 deletions
54
sound.cpp
Normal file
54
sound.cpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
#include "sound.hpp"
|
||||
#include "dbc.hpp"
|
||||
#include <fmt/core.h>
|
||||
#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<sf::SoundBuffer>(sound_path);
|
||||
|
||||
// set it on the sound and keep in the sound map
|
||||
auto sound = make_shared<sf::Sound>(*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");
|
||||
dbc::check(SMGR.sounds.contains(name), fmt::format("sound {} is not loaded in map", name));
|
||||
// get the sound from the sound map
|
||||
auto pair = SMGR.sounds.at(name);
|
||||
// play it
|
||||
pair.sound->play();
|
||||
}
|
||||
|
||||
void play_at(const std::string name, float x, float y, float z) {
|
||||
dbc::check(initialized, "You need to call sound::init() first");
|
||||
auto pair = SMGR.sounds.at(name);
|
||||
pair.sound->setPosition({x, y, z});
|
||||
pair.sound->play();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue