Need the hit.wav to be mono, but now we have a sound we can move around, just not sure how to place it based on the visuals.
parent
9102bdc8ad
commit
4ed06b10b1
Binary file not shown.
@ -0,0 +1,42 @@ |
||||
#include "sound.hpp" |
||||
#include "dbc.hpp" |
||||
#include <fmt/core.h> |
||||
|
||||
using namespace fmt; |
||||
namespace fs = std::filesystem; |
||||
|
||||
SoundManager::SoundManager(std::string base_path) : $base_path(base_path) { |
||||
dbc::check(fs::exists($base_path), "sound asset path is missing"); |
||||
} |
||||
|
||||
void SoundManager::load(const std::string name, const std::string sound_path) { |
||||
// get the sound file with base_path
|
||||
fs::path full_path = $base_path / sound_path; |
||||
// confirm it's there
|
||||
dbc::check(fs::exists(full_path), format("sound file {} does not exist", sound_path)); |
||||
|
||||
// create the buffer and keep in the buffer map
|
||||
SoundPair *pair = new SoundPair(); |
||||
$sounds[name] = pair; |
||||
|
||||
bool good = pair->buffer.loadFromFile(full_path.string()); |
||||
dbc::check(good, format("failed to load sound {}", sound_path)); |
||||
|
||||
// set it on the sound and keep in the sound map
|
||||
pair->sound.setBuffer(pair->buffer); |
||||
pair->sound.setRelativeToListener(true); |
||||
} |
||||
|
||||
void SoundManager::play(const std::string name) { |
||||
dbc::check($sounds.contains(name), format("sound {} is not loaded in map", name)); |
||||
// get the sound from the sound map
|
||||
SoundPair *pair = $sounds.at(name); |
||||
// play it
|
||||
pair->sound.play(); |
||||
} |
||||
|
||||
void SoundManager::playAt(const std::string name, float x, float y, float z) { |
||||
SoundPair *pair = $sounds.at(name); |
||||
pair->sound.setPosition(x, y, z); |
||||
pair->sound.play(); |
||||
} |
@ -0,0 +1,21 @@ |
||||
#pragma once |
||||
#include <string> |
||||
#include <filesystem> |
||||
#include <unordered_map> |
||||
#include <SFML/Audio.hpp> |
||||
|
||||
struct SoundPair { |
||||
sf::SoundBuffer buffer; |
||||
sf::Sound sound; |
||||
}; |
||||
|
||||
struct SoundManager { |
||||
std::filesystem::path $base_path; |
||||
std::unordered_map<std::string, SoundPair*> $sounds; |
||||
|
||||
SoundManager(std::string base_path); |
||||
|
||||
void load(const std::string name, const std::string path); |
||||
void play(const std::string name); |
||||
void playAt(const std::string name, float x, float y, float z); |
||||
}; |
@ -0,0 +1,21 @@ |
||||
#include <catch2/catch_test_macros.hpp> |
||||
#include <fmt/core.h> |
||||
#include <string> |
||||
#include "sound.hpp" |
||||
#include "dinkyecs.hpp" |
||||
|
||||
using DinkyECS::Entity; |
||||
using namespace fmt; |
||||
|
||||
TEST_CASE("confirm basic functionality", "[sounds]") { |
||||
REQUIRE_THROWS([&](){SoundManager sounds("./BADassets");}()); |
||||
|
||||
SoundManager sounds("./assets"); |
||||
|
||||
REQUIRE_THROWS(sounds.load("hit", "badfileDOESNOTEXIST.wav")); |
||||
REQUIRE_THROWS(sounds.play("hit")); |
||||
|
||||
sounds.load("hit", "hit.wav"); |
||||
sounds.play("hit"); |
||||
sounds.playAt("hit", 1, 1, 1); |
||||
} |
Loading…
Reference in new issue