dungeon_crawler
changeset 49:303743485aba
pretty much implemented the positional torch sound sources
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 18 Sep 2012 00:34:29 +0300 |
parents | aa9e28670ae2 |
children | c40efa9cf844 |
files | prototype/src/level.cc prototype/src/level.h prototype/src/main.cc prototype/src/tile.cc prototype/src/tile.h |
diffstat | 5 files changed, 119 insertions(+), 21 deletions(-) [+] |
line diff
1.1 --- a/prototype/src/level.cc Mon Sep 17 08:40:59 2012 +0300 1.2 +++ b/prototype/src/level.cc Tue Sep 18 00:34:29 2012 +0300 1.3 @@ -5,6 +5,7 @@ 1.4 #include "level.h" 1.5 #include "tile.h" 1.6 #include "tileset.h" 1.7 +#include "cfg.h" 1.8 1.9 Level::Level() 1.10 { 1.11 @@ -72,6 +73,15 @@ 1.12 GridCell *cell = new GridCell(deftile); 1.13 *grid_row = cell; 1.14 cell_list.push_back(*grid_row); 1.15 + 1.16 + // add any audio sources in this grid-cell to the level static audio manager 1.17 + std::list<Tile::AudioSourceDesc> asrc = cell->get_audio_sources(); 1.18 + for(auto sdesc : asrc) { 1.19 + AudioSource *s = new AudioSource; 1.20 + s->set_sample(sdesc.sample); 1.21 + s->set_position(sdesc.pos + get_cell_pos(i, y)); 1.22 + austatic.add_source(s); 1.23 + } 1.24 } 1.25 grid_row++; 1.26 } 1.27 @@ -133,11 +143,21 @@ 1.28 return dmask; 1.29 } 1.30 1.31 +void Level::set_player_position(const Vector3 &ppos) 1.32 +{ 1.33 + player_pos = ppos; 1.34 +} 1.35 + 1.36 void Level::update(unsigned long msec, float dt) 1.37 { 1.38 for(auto cell : cell_list) { 1.39 cell->update(msec, dt); 1.40 } 1.41 + 1.42 + // activate the closest audio sources 1.43 + if(cfg.sound) { 1.44 + austatic.active_range(player_pos, 2.0); 1.45 + } 1.46 } 1.47 1.48 void Level::draw() const 1.49 @@ -263,12 +283,12 @@ 1.50 tiles.push_back(tile); 1.51 1.52 /* instanciate any particle systems */ 1.53 - const struct psys_attributes * const *psattr = tile->get_unique_psys(); 1.54 int num_psattr = tile->get_unique_psys_count(); 1.55 1.56 for(int i=0; i<num_psattr; i++) { 1.57 + const struct psys_attributes *psattr = tile->get_unique_psys(i); 1.58 struct psys_emitter *emitter = psys_create(); 1.59 - emitter->attr = *psattr[i]; 1.60 + emitter->attr = *psattr; 1.61 psys.push_back(emitter); 1.62 } 1.63 } 1.64 @@ -314,3 +334,16 @@ 1.65 } 1.66 return 0; 1.67 } 1.68 + 1.69 +std::list<Tile::AudioSourceDesc> GridCell::get_audio_sources() const 1.70 +{ 1.71 + std::list<Tile::AudioSourceDesc> srclist; 1.72 + 1.73 + for(auto tile : tiles) { 1.74 + int num_src = tile->get_audio_source_count(); 1.75 + for(int i=0; i<num_src; i++) { 1.76 + srclist.push_back(tile->get_audio_source(i)); 1.77 + } 1.78 + } 1.79 + return srclist; 1.80 +}
2.1 --- a/prototype/src/level.h Mon Sep 17 08:40:59 2012 +0300 2.2 +++ b/prototype/src/level.h Tue Sep 18 00:34:29 2012 +0300 2.3 @@ -2,9 +2,11 @@ 2.4 #define LEVEL_H_ 2.5 2.6 #include <vector> 2.7 +#include <list> 2.8 #include "vmath/vmath.h" 2.9 #include "psys/psys.h" 2.10 #include "tile.h" 2.11 +#include "audio/auman.h" 2.12 2.13 class GridCell; 2.14 2.15 @@ -19,6 +21,10 @@ 2.16 // secondary data structure, simple list of all populated cells 2.17 std::vector<GridCell*> cell_list; 2.18 2.19 + // audio manager for static sources in the level 2.20 + AudioManager austatic; 2.21 + Vector3 player_pos; 2.22 + 2.23 void draw_grid() const; 2.24 2.25 public: 2.26 @@ -33,6 +39,7 @@ 2.27 void get_cell_coords_at(const Vector3 &pos, int *xptr, int *yptr) const; 2.28 unsigned int get_cell_dirmask(int x, int y) const; 2.29 2.30 + void set_player_position(const Vector3 &ppos); 2.31 void update(unsigned long msec, float dt); 2.32 2.33 void draw() const; 2.34 @@ -62,6 +69,8 @@ 2.35 void draw_post(unsigned int draw_mask) const; 2.36 2.37 AudioSample *get_sample(int which) const; 2.38 + 2.39 + std::list<Tile::AudioSourceDesc> get_audio_sources() const; 2.40 }; 2.41 2.42 #endif // LEVEL_H_
3.1 --- a/prototype/src/main.cc Mon Sep 17 08:40:59 2012 +0300 3.2 +++ b/prototype/src/main.cc Tue Sep 18 00:34:29 2012 +0300 3.3 @@ -267,23 +267,27 @@ 3.4 cam.input_move(dx, 0, dy); 3.5 3.6 tileset->update_tiles(msec); 3.7 + 3.8 + level->set_player_position(cam.get_position()); 3.9 level->update(msec, dt); 3.10 3.11 // play the walking sound if we're walking 3.12 - int cellx, celly; 3.13 - level->get_cell_coords_at(cam.get_position(), &cellx, &celly); 3.14 + if(cfg.sound) { 3.15 + int cellx, celly; 3.16 + level->get_cell_coords_at(cam.get_position(), &cellx, &celly); 3.17 3.18 - const AudioSample *move_sample; 3.19 - if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) { 3.20 - if(move_sample != move_sound->get_sample()) { 3.21 - move_sound->stop(); 3.22 - move_sound->set_sample(move_sample); 3.23 - move_sound->play(); 3.24 - } 3.25 - } else { 3.26 - if(move_sound->get_sample()) { 3.27 - move_sound->stop(); 3.28 - move_sound->set_sample(0); 3.29 + const AudioSample *move_sample; 3.30 + if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) { 3.31 + if(move_sample != move_sound->get_sample()) { 3.32 + move_sound->stop(); 3.33 + move_sound->set_sample(move_sample); 3.34 + move_sound->play(); 3.35 + } 3.36 + } else { 3.37 + if(move_sound->get_sample()) { 3.38 + move_sound->stop(); 3.39 + move_sound->set_sample(0); 3.40 + } 3.41 } 3.42 } 3.43
4.1 --- a/prototype/src/tile.cc Mon Sep 17 08:40:59 2012 +0300 4.2 +++ b/prototype/src/tile.cc Tue Sep 18 00:34:29 2012 +0300 4.3 @@ -9,6 +9,7 @@ 4.4 #include "tileset.h" 4.5 #include "renderer.h" 4.6 #include "datapath.h" 4.7 +#include "cfg.h" 4.8 4.9 using std::map; 4.10 4.11 @@ -49,9 +50,18 @@ 4.12 return 0; 4.13 } 4.14 4.15 -const struct psys_attributes * const *Tile::get_unique_psys() const 4.16 +int Tile::get_audio_source_count() const 4.17 { 4.18 - return &psattr[0]; 4.19 + return (int)ausrc.size(); 4.20 +} 4.21 + 4.22 +const Tile::AudioSourceDesc &Tile::get_audio_source(int idx) const 4.23 +{ 4.24 + if(idx < 0 || idx >= (int)ausrc.size()) { 4.25 + static AudioSourceDesc d = { Vector3(), 0 }; 4.26 + return d; 4.27 + } 4.28 + return ausrc[idx]; 4.29 } 4.30 4.31 int Tile::get_unique_psys_count() const 4.32 @@ -59,6 +69,22 @@ 4.33 return (int)psattr.size(); 4.34 } 4.35 4.36 +struct psys_attributes *Tile::get_unique_psys(int idx) 4.37 +{ 4.38 + if(idx < 0 || idx >= (int)psattr.size()) { 4.39 + return 0; 4.40 + } 4.41 + return psattr[idx]; 4.42 +} 4.43 + 4.44 +const struct psys_attributes *Tile::get_unique_psys(int idx) const 4.45 +{ 4.46 + if(idx < 0 || idx >= (int)psattr.size()) { 4.47 + return 0; 4.48 + } 4.49 + return psattr[idx]; 4.50 +} 4.51 + 4.52 bool Tile::load(const char *fname) 4.53 { 4.54 if(!fname) { 4.55 @@ -97,9 +123,11 @@ 4.56 aiReleaseImport(scn); 4.57 4.58 // XXX get the default audio samples for now 4.59 - SampleSet *sampleset = tset->get_samples(); 4.60 - samples[TILE_SAMPLE_WALK] = sampleset->get("walk_stone.ogg"); 4.61 - samples[TILE_SAMPLE_RUN] = sampleset->get("run_stone.ogg"); 4.62 + if(cfg.sound) { 4.63 + SampleSet *sampleset = tset->get_samples(); 4.64 + samples[TILE_SAMPLE_WALK] = sampleset->get("walk_stone.ogg"); 4.65 + samples[TILE_SAMPLE_RUN] = sampleset->get("run_stone.ogg"); 4.66 + } 4.67 return true; 4.68 } 4.69 4.70 @@ -258,6 +286,16 @@ 4.71 fprintf(stderr, "failed to create global particle system\n"); 4.72 } 4.73 4.74 + // ... AND make an audio source out of each light source 4.75 + if(cfg.sound) { 4.76 + SampleSet *sampleset = tset->get_samples(); 4.77 + AudioSample *sample = sampleset->get("fire.ogg"); 4.78 + if(sample) { 4.79 + AudioSourceDesc adesc = {lt->get_position(), sample}; 4.80 + ausrc.push_back(adesc); 4.81 + } 4.82 + } 4.83 + 4.84 } else { 4.85 meshes.push_back(mesh); 4.86 mesh_side.push_back(side);
5.1 --- a/prototype/src/tile.h Mon Sep 17 08:40:59 2012 +0300 5.2 +++ b/prototype/src/tile.h Tue Sep 18 00:34:29 2012 +0300 5.3 @@ -8,6 +8,7 @@ 5.4 #include "mesh.h" 5.5 #include "light.h" 5.6 #include "audio/sample.h" 5.7 +#include "audio/source.h" 5.8 5.9 enum { 5.10 TILE_NORTH = 1, 5.11 @@ -27,6 +28,12 @@ 5.12 class TileSet; 5.13 5.14 class Tile { 5.15 +public: 5.16 + struct AudioSourceDesc { 5.17 + Vector3 pos; 5.18 + AudioSample *sample; 5.19 + }; 5.20 + 5.21 private: 5.22 TileSet *tset; 5.23 5.24 @@ -41,6 +48,9 @@ 5.25 std::vector<struct psys_emitter*> psys_global; 5.26 long last_upd; 5.27 5.28 + // audio sources 5.29 + std::vector<AudioSourceDesc> ausrc; 5.30 + 5.31 int load_lights(const aiScene *scn); 5.32 int load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap); 5.33 5.34 @@ -50,8 +60,12 @@ 5.35 5.36 AudioSample *get_sample(int sidx) const; 5.37 5.38 - const struct psys_attributes * const *get_unique_psys() const; 5.39 + int get_audio_source_count() const; 5.40 + const AudioSourceDesc &get_audio_source(int idx) const; 5.41 + 5.42 int get_unique_psys_count() const; 5.43 + struct psys_attributes *get_unique_psys(int idx); 5.44 + const struct psys_attributes *get_unique_psys(int idx) const; 5.45 5.46 bool load(const char *fname); 5.47