dungeon_crawler
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/prototype/src/audio/auman.cc Mon Sep 17 08:40:59 2012 +0300 1.3 @@ -0,0 +1,68 @@ 1.4 +#include <algorithm> 1.5 +#include <new> 1.6 +#include "auman.h" 1.7 + 1.8 +AudioManager::AudioManager() 1.9 +{ 1.10 + if(!(sources = kd_create(3))) { 1.11 + fprintf(stderr, "failed to create kd tree\n"); 1.12 + throw std::bad_alloc(); 1.13 + } 1.14 +} 1.15 + 1.16 +AudioManager::~AudioManager() 1.17 +{ 1.18 + kd_free(sources); 1.19 +} 1.20 + 1.21 +void AudioManager::clear() 1.22 +{ 1.23 + kd_clear(sources); 1.24 + 1.25 + for(auto s : active_set) { 1.26 + s->stop(); 1.27 + } 1.28 + active_set.clear(); 1.29 +} 1.30 + 1.31 +void AudioManager::add_source(AudioSource *s) 1.32 +{ 1.33 + Vector3 pos = s->get_position(); 1.34 + if(kd_insert3f(sources, pos.x, pos.y, pos.z, s) == -1) { 1.35 + fprintf(stderr, "AudioManager: failed to add source!\n"); 1.36 + } 1.37 +} 1.38 + 1.39 +void AudioManager::active_range(const Vector3 &pos, float range) 1.40 +{ 1.41 + std::set<AudioSource*> newset; 1.42 + 1.43 + // find all the sources in the given range and construct newset. 1.44 + struct kdres *results = kd_nearest_range3f(sources, pos.x, pos.y, pos.z, range); 1.45 + while(!kd_res_end(results)) { 1.46 + newset.insert((AudioSource*)kd_res_item_data(results)); 1.47 + kd_res_next(results); 1.48 + } 1.49 + kd_res_free(results); 1.50 + 1.51 + /* for each of the currently active sources, if they're not in the 1.52 + * new set, stop the playback. 1.53 + */ 1.54 + for(auto s : active_set) { 1.55 + if(newset.find(s) == newset.end()) { 1.56 + s->stop(); 1.57 + } 1.58 + } 1.59 + 1.60 + /* for each of the new active sources not found in the currently active 1.61 + * set, start the playback. 1.62 + */ 1.63 + for(auto s : newset) { 1.64 + if(active_set.find(s) == active_set.end()) { 1.65 + s->play(); 1.66 + } 1.67 + } 1.68 + 1.69 + // swap the current with the new, previous current will be destroyed at end of scope 1.70 + std::swap(active_set, newset); 1.71 +}