dungeon_crawler

view prototype/src/audio/auman.cc @ 51:d57df51f6b50

- fixed audio panning (listener direction) - particles had no fog - sound sources were not destroyed properly
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 18 Sep 2012 09:40:56 +0300
parents aa9e28670ae2
children
line source
1 #include <algorithm>
2 #include <new>
3 #include "auman.h"
5 AudioManager::AudioManager()
6 {
7 if(!(sources = kd_create(3))) {
8 fprintf(stderr, "failed to create kd tree\n");
9 throw std::bad_alloc();
10 }
11 kd_data_destructor(sources, [](void *s) { delete (AudioSource*)s; });
12 }
14 AudioManager::~AudioManager()
15 {
16 kd_free(sources);
17 }
19 void AudioManager::clear()
20 {
21 for(auto s : active_set) {
22 s->stop();
23 }
24 active_set.clear();
26 kd_clear(sources);
27 }
29 void AudioManager::add_source(AudioSource *s)
30 {
31 Vector3 pos = s->get_position();
32 if(kd_insert3f(sources, pos.x, pos.y, pos.z, s) == -1) {
33 fprintf(stderr, "AudioManager: failed to add source!\n");
34 }
35 }
37 void AudioManager::active_range(const Vector3 &pos, float range)
38 {
39 std::set<AudioSource*> newset;
41 // find all the sources in the given range and construct newset.
42 struct kdres *results = kd_nearest_range3f(sources, pos.x, pos.y, pos.z, range);
43 while(!kd_res_end(results)) {
44 newset.insert((AudioSource*)kd_res_item_data(results));
45 kd_res_next(results);
46 }
47 kd_res_free(results);
49 /* for each of the currently active sources, if they're not in the
50 * new set, stop the playback.
51 */
52 for(auto s : active_set) {
53 if(newset.find(s) == newset.end()) {
54 s->stop();
55 }
56 }
58 /* for each of the new active sources not found in the currently active
59 * set, start the playback.
60 */
61 for(auto s : newset) {
62 if(active_set.find(s) == active_set.end()) {
63 s->play();
64 }
65 }
67 // swap the current with the new, previous current will be destroyed at end of scope
68 std::swap(active_set, newset);
69 }