dungeon_crawler

view prototype/src/audio/auman.cc @ 48:aa9e28670ae2

added sound playback, more to do
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 17 Sep 2012 08:40:59 +0300
parents
children d57df51f6b50
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 }
13 AudioManager::~AudioManager()
14 {
15 kd_free(sources);
16 }
18 void AudioManager::clear()
19 {
20 kd_clear(sources);
22 for(auto s : active_set) {
23 s->stop();
24 }
25 active_set.clear();
26 }
28 void AudioManager::add_source(AudioSource *s)
29 {
30 Vector3 pos = s->get_position();
31 if(kd_insert3f(sources, pos.x, pos.y, pos.z, s) == -1) {
32 fprintf(stderr, "AudioManager: failed to add source!\n");
33 }
34 }
36 void AudioManager::active_range(const Vector3 &pos, float range)
37 {
38 std::set<AudioSource*> newset;
40 // find all the sources in the given range and construct newset.
41 struct kdres *results = kd_nearest_range3f(sources, pos.x, pos.y, pos.z, range);
42 while(!kd_res_end(results)) {
43 newset.insert((AudioSource*)kd_res_item_data(results));
44 kd_res_next(results);
45 }
46 kd_res_free(results);
48 /* for each of the currently active sources, if they're not in the
49 * new set, stop the playback.
50 */
51 for(auto s : active_set) {
52 if(newset.find(s) == newset.end()) {
53 s->stop();
54 }
55 }
57 /* for each of the new active sources not found in the currently active
58 * set, start the playback.
59 */
60 for(auto s : newset) {
61 if(active_set.find(s) == active_set.end()) {
62 s->play();
63 }
64 }
66 // swap the current with the new, previous current will be destroyed at end of scope
67 std::swap(active_set, newset);
68 }