Working sound system and most enemies have a sound effect. This will make it easier to add sounds now.
parent
83df9ff03b
commit
20cbc3a21c
Binary file not shown.
Binary file not shown.
@ -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(); |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
#pragma once |
||||
#include <string> |
||||
#include <filesystem> |
||||
#include <memory> |
||||
#include <unordered_map> |
||||
#include <SFML/Audio.hpp> |
||||
|
||||
namespace sound { |
||||
struct SoundPair { |
||||
std::shared_ptr<sf::SoundBuffer> buffer; |
||||
std::shared_ptr<sf::Sound> sound; |
||||
}; |
||||
|
||||
struct SoundManager { |
||||
std::unordered_map<std::string, SoundPair> sounds; |
||||
}; |
||||
|
||||
void init(); |
||||
void load(const std::string name, const std::string path); |
||||
void play(const std::string name); |
||||
void play_at(const std::string name, float x, float y, float z); |
||||
} |
@ -0,0 +1,15 @@ |
||||
#include <catch2/catch_test_macros.hpp> |
||||
#include <fmt/core.h> |
||||
#include <string> |
||||
#include "sound.hpp" |
||||
#include "levelmanager.hpp" |
||||
|
||||
using namespace fmt; |
||||
|
||||
TEST_CASE("test sound manager", "[sound]") { |
||||
sound::init(); |
||||
|
||||
sound::play("sword_1"); |
||||
|
||||
sound::play_at("sword_1", 0.1, 0.1, 0.1); |
||||
} |
Loading…
Reference in new issue