diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2018-03-31 16:04:04 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2018-03-31 16:04:04 +0200 |
commit | d72e8d61de2f7efba3685dda2dc52b31f64f8a6e (patch) | |
tree | 2cda01dcc5b0e17fcf109469ac99296c2212346b /src/audio.cpp | |
parent | d87217dffc1582b8dbed10da62b3b4d3f7b511de (diff) |
3D audio.
Diffstat (limited to 'src/audio.cpp')
-rw-r--r-- | src/audio.cpp | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/src/audio.cpp b/src/audio.cpp index ff4fcd3..b806959 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -22,13 +22,6 @@ namespace audio { static std::list<sf::Sound> playing_sounds; static std::list<ambient_t*> ambient_sounds; -void clean_sounds(void) -{ - playing_sounds.remove_if( [](sf::Sound &sound) { - return sound.getStatus() == sf::Sound::Stopped; - }); -} - void sound_t::load(const char *path) { printf("load %s\n", path); @@ -45,6 +38,19 @@ void sound_t::play(void) auto sound = playing_sounds.emplace(playing_sounds.end()); sound->setBuffer(sounds[rand() % sounds.size()]); sound->setVolume(volume * 100.0f); + sound->setRelativeToListener(true); // disable spatialization + sound->play(); +} + +void sound_t::play_3d(v2f_t x) +{ + if (!sounds.size()) + return; + + auto sound = playing_sounds.emplace(playing_sounds.end()); + sound->setBuffer(sounds[rand() % sounds.size()]); + sound->setVolume(volume * 100.0f); + sound->setPosition(x[0], x[1], 0.0f); sound->play(); } @@ -53,14 +59,28 @@ void ambient_t::load(const char *path) printf("load %s\n", path); sound.openFromFile(path); sound.setLoop(true); - sound.setRelativeToListener(true); ambient_sounds.push_back(this); } -void update_ambience(bool playing) +void update(v3f_t camera, bool paused) { + // update 3D settings + // the listener is looking down at the XY plane with +X pointing right + // and +Y pointing down + sf::Listener::setPosition(camera); + sf::Listener::setDirection(0.0f, 0.0f, -1.0f); + sf::Listener::setUpVector(0.0f, 1.0f, 0.0f); + + // remove sounds that finished playing from playing_sounds + playing_sounds.remove_if( [](sf::Sound &sound) { + return sound.getStatus() == sf::Sound::Stopped; + }); + + // update ambient sounds (weights set by game) for (ambient_t *ambient : ambient_sounds) { - if (!playing) { + v3f_t origin; + + if (paused) { if (ambient->playing) { ambient->sound.stop(); ambient->playing = false; @@ -73,7 +93,11 @@ void update_ambience(bool playing) ambient->playing = true; } - ambient->sound.setVolume(ambient->weight * 100.0f); + origin[0] = ambient->origin[0]; + origin[1] = ambient->origin[1]; + origin[2] = 0.0f; + ambient->sound.setPosition(origin); + ambient->sound.setVolume(ambient->volume * ambient->weight * 100.0f); } } |