# HG changeset patch # User John Tsiombikas # Date 1347917669 -10800 # Node ID 303743485aba9fc6f99e020aaa057664e7b19249 # Parent aa9e28670ae2b34421c67506b390850b6debbb86 pretty much implemented the positional torch sound sources diff -r aa9e28670ae2 -r 303743485aba prototype/src/level.cc --- a/prototype/src/level.cc Mon Sep 17 08:40:59 2012 +0300 +++ b/prototype/src/level.cc Tue Sep 18 00:34:29 2012 +0300 @@ -5,6 +5,7 @@ #include "level.h" #include "tile.h" #include "tileset.h" +#include "cfg.h" Level::Level() { @@ -72,6 +73,15 @@ GridCell *cell = new GridCell(deftile); *grid_row = cell; cell_list.push_back(*grid_row); + + // add any audio sources in this grid-cell to the level static audio manager + std::list asrc = cell->get_audio_sources(); + for(auto sdesc : asrc) { + AudioSource *s = new AudioSource; + s->set_sample(sdesc.sample); + s->set_position(sdesc.pos + get_cell_pos(i, y)); + austatic.add_source(s); + } } grid_row++; } @@ -133,11 +143,21 @@ return dmask; } +void Level::set_player_position(const Vector3 &ppos) +{ + player_pos = ppos; +} + void Level::update(unsigned long msec, float dt) { for(auto cell : cell_list) { cell->update(msec, dt); } + + // activate the closest audio sources + if(cfg.sound) { + austatic.active_range(player_pos, 2.0); + } } void Level::draw() const @@ -263,12 +283,12 @@ tiles.push_back(tile); /* instanciate any particle systems */ - const struct psys_attributes * const *psattr = tile->get_unique_psys(); int num_psattr = tile->get_unique_psys_count(); for(int i=0; iget_unique_psys(i); struct psys_emitter *emitter = psys_create(); - emitter->attr = *psattr[i]; + emitter->attr = *psattr; psys.push_back(emitter); } } @@ -314,3 +334,16 @@ } return 0; } + +std::list GridCell::get_audio_sources() const +{ + std::list srclist; + + for(auto tile : tiles) { + int num_src = tile->get_audio_source_count(); + for(int i=0; iget_audio_source(i)); + } + } + return srclist; +} diff -r aa9e28670ae2 -r 303743485aba prototype/src/level.h --- a/prototype/src/level.h Mon Sep 17 08:40:59 2012 +0300 +++ b/prototype/src/level.h Tue Sep 18 00:34:29 2012 +0300 @@ -2,9 +2,11 @@ #define LEVEL_H_ #include +#include #include "vmath/vmath.h" #include "psys/psys.h" #include "tile.h" +#include "audio/auman.h" class GridCell; @@ -19,6 +21,10 @@ // secondary data structure, simple list of all populated cells std::vector cell_list; + // audio manager for static sources in the level + AudioManager austatic; + Vector3 player_pos; + void draw_grid() const; public: @@ -33,6 +39,7 @@ void get_cell_coords_at(const Vector3 &pos, int *xptr, int *yptr) const; unsigned int get_cell_dirmask(int x, int y) const; + void set_player_position(const Vector3 &ppos); void update(unsigned long msec, float dt); void draw() const; @@ -62,6 +69,8 @@ void draw_post(unsigned int draw_mask) const; AudioSample *get_sample(int which) const; + + std::list get_audio_sources() const; }; #endif // LEVEL_H_ diff -r aa9e28670ae2 -r 303743485aba prototype/src/main.cc --- a/prototype/src/main.cc Mon Sep 17 08:40:59 2012 +0300 +++ b/prototype/src/main.cc Tue Sep 18 00:34:29 2012 +0300 @@ -267,23 +267,27 @@ cam.input_move(dx, 0, dy); tileset->update_tiles(msec); + + level->set_player_position(cam.get_position()); level->update(msec, dt); // play the walking sound if we're walking - int cellx, celly; - level->get_cell_coords_at(cam.get_position(), &cellx, &celly); + if(cfg.sound) { + int cellx, celly; + level->get_cell_coords_at(cam.get_position(), &cellx, &celly); - const AudioSample *move_sample; - if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) { - if(move_sample != move_sound->get_sample()) { - move_sound->stop(); - move_sound->set_sample(move_sample); - move_sound->play(); - } - } else { - if(move_sound->get_sample()) { - move_sound->stop(); - move_sound->set_sample(0); + const AudioSample *move_sample; + if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) { + if(move_sample != move_sound->get_sample()) { + move_sound->stop(); + move_sound->set_sample(move_sample); + move_sound->play(); + } + } else { + if(move_sound->get_sample()) { + move_sound->stop(); + move_sound->set_sample(0); + } } } diff -r aa9e28670ae2 -r 303743485aba prototype/src/tile.cc --- a/prototype/src/tile.cc Mon Sep 17 08:40:59 2012 +0300 +++ b/prototype/src/tile.cc Tue Sep 18 00:34:29 2012 +0300 @@ -9,6 +9,7 @@ #include "tileset.h" #include "renderer.h" #include "datapath.h" +#include "cfg.h" using std::map; @@ -49,9 +50,18 @@ return 0; } -const struct psys_attributes * const *Tile::get_unique_psys() const +int Tile::get_audio_source_count() const { - return &psattr[0]; + return (int)ausrc.size(); +} + +const Tile::AudioSourceDesc &Tile::get_audio_source(int idx) const +{ + if(idx < 0 || idx >= (int)ausrc.size()) { + static AudioSourceDesc d = { Vector3(), 0 }; + return d; + } + return ausrc[idx]; } int Tile::get_unique_psys_count() const @@ -59,6 +69,22 @@ return (int)psattr.size(); } +struct psys_attributes *Tile::get_unique_psys(int idx) +{ + if(idx < 0 || idx >= (int)psattr.size()) { + return 0; + } + return psattr[idx]; +} + +const struct psys_attributes *Tile::get_unique_psys(int idx) const +{ + if(idx < 0 || idx >= (int)psattr.size()) { + return 0; + } + return psattr[idx]; +} + bool Tile::load(const char *fname) { if(!fname) { @@ -97,9 +123,11 @@ aiReleaseImport(scn); // XXX get the default audio samples for now - SampleSet *sampleset = tset->get_samples(); - samples[TILE_SAMPLE_WALK] = sampleset->get("walk_stone.ogg"); - samples[TILE_SAMPLE_RUN] = sampleset->get("run_stone.ogg"); + if(cfg.sound) { + SampleSet *sampleset = tset->get_samples(); + samples[TILE_SAMPLE_WALK] = sampleset->get("walk_stone.ogg"); + samples[TILE_SAMPLE_RUN] = sampleset->get("run_stone.ogg"); + } return true; } @@ -258,6 +286,16 @@ fprintf(stderr, "failed to create global particle system\n"); } + // ... AND make an audio source out of each light source + if(cfg.sound) { + SampleSet *sampleset = tset->get_samples(); + AudioSample *sample = sampleset->get("fire.ogg"); + if(sample) { + AudioSourceDesc adesc = {lt->get_position(), sample}; + ausrc.push_back(adesc); + } + } + } else { meshes.push_back(mesh); mesh_side.push_back(side); diff -r aa9e28670ae2 -r 303743485aba prototype/src/tile.h --- a/prototype/src/tile.h Mon Sep 17 08:40:59 2012 +0300 +++ b/prototype/src/tile.h Tue Sep 18 00:34:29 2012 +0300 @@ -8,6 +8,7 @@ #include "mesh.h" #include "light.h" #include "audio/sample.h" +#include "audio/source.h" enum { TILE_NORTH = 1, @@ -27,6 +28,12 @@ class TileSet; class Tile { +public: + struct AudioSourceDesc { + Vector3 pos; + AudioSample *sample; + }; + private: TileSet *tset; @@ -41,6 +48,9 @@ std::vector psys_global; long last_upd; + // audio sources + std::vector ausrc; + int load_lights(const aiScene *scn); int load_meshes(const aiScene *scn, const std::map &nmap); @@ -50,8 +60,12 @@ AudioSample *get_sample(int sidx) const; - const struct psys_attributes * const *get_unique_psys() const; + int get_audio_source_count() const; + const AudioSourceDesc &get_audio_source(int idx) const; + int get_unique_psys_count() const; + struct psys_attributes *get_unique_psys(int idx); + const struct psys_attributes *get_unique_psys(int idx) const; bool load(const char *fname);