dungeon_crawler

changeset 50:c40efa9cf844

torches sound
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 18 Sep 2012 04:45:46 +0300
parents 303743485aba
children d57df51f6b50
files prototype/data/test.level prototype/doc/notes prototype/src/audio/audio.cc prototype/src/audio/audio.h prototype/src/level.cc prototype/src/level.h prototype/src/main.cc prototype/src/tile.cc prototype/src/tile.h
diffstat 9 files changed, 86 insertions(+), 14 deletions(-) [+]
line diff
     1.1 --- a/prototype/data/test.level	Tue Sep 18 00:34:29 2012 +0300
     1.2 +++ b/prototype/data/test.level	Tue Sep 18 04:45:46 2012 +0300
     1.3 @@ -29,7 +29,7 @@
     1.4  ################################################################
     1.5  ################################################################
     1.6  ################################################################
     1.7 -################################################################
     1.8 +################################ ###############################
     1.9  ################################ ############# #################
    1.10  ################################################################
    1.11  ################################################################
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/prototype/doc/notes	Tue Sep 18 04:45:46 2012 +0300
     2.3 @@ -0,0 +1,3 @@
     2.4 +- The approach of preallocating OpenAL sources for each sound source is plainly
     2.5 +  wrong. There's a limit to the number of OpenAL source objects, and that limit
     2.6 +  is probably 255 on the implementation I'm using now.
     3.1 --- a/prototype/src/audio/audio.cc	Tue Sep 18 00:34:29 2012 +0300
     3.2 +++ b/prototype/src/audio/audio.cc	Tue Sep 18 04:45:46 2012 +0300
     3.3 @@ -33,3 +33,23 @@
     3.4  		alcCloseDevice(dev);
     3.5  	}
     3.6  }
     3.7 +
     3.8 +void set_audio_listener(const Matrix4x4 &xform)
     3.9 +{
    3.10 +	float pos[3], orient[6];
    3.11 +
    3.12 +	pos[0] = xform[0][3];
    3.13 +	pos[1] = xform[1][3];
    3.14 +	pos[2] = xform[2][3];
    3.15 +
    3.16 +	orient[0] = xform[0][2];
    3.17 +	orient[1] = xform[1][2];
    3.18 +	orient[2] = xform[2][2];
    3.19 +
    3.20 +	orient[3] = xform[0][1];
    3.21 +	orient[4] = xform[1][1];
    3.22 +	orient[5] = xform[2][1];
    3.23 +
    3.24 +	alListenerfv(AL_POSITION, pos);
    3.25 +	alListenerfv(AL_ORIENTATION, orient);
    3.26 +}
     4.1 --- a/prototype/src/audio/audio.h	Tue Sep 18 00:34:29 2012 +0300
     4.2 +++ b/prototype/src/audio/audio.h	Tue Sep 18 04:45:46 2012 +0300
     4.3 @@ -1,7 +1,11 @@
     4.4  #ifndef AUDIO_H_
     4.5  #define AUDIO_H_
     4.6  
     4.7 +#include "vmath/vmath.h"
     4.8 +
     4.9  bool init_audio();
    4.10  void destroy_audio();
    4.11  
    4.12 -#endif	/* AUDIO_H_ */
    4.13 +void set_audio_listener(const Matrix4x4 &xform);
    4.14 +
    4.15 +#endif	// AUDIO_H_
     5.1 --- a/prototype/src/level.cc	Tue Sep 18 00:34:29 2012 +0300
     5.2 +++ b/prototype/src/level.cc	Tue Sep 18 04:45:46 2012 +0300
     5.3 @@ -73,15 +73,6 @@
     5.4  				GridCell *cell = new GridCell(deftile);
     5.5  				*grid_row = cell;
     5.6  				cell_list.push_back(*grid_row);
     5.7 -
     5.8 -				// add any audio sources in this grid-cell to the level static audio manager
     5.9 -				std::list<Tile::AudioSourceDesc> asrc = cell->get_audio_sources();
    5.10 -				for(auto sdesc : asrc) {
    5.11 -					AudioSource *s = new AudioSource;
    5.12 -					s->set_sample(sdesc.sample);
    5.13 -					s->set_position(sdesc.pos + get_cell_pos(i, y));
    5.14 -					austatic.add_source(s);
    5.15 -				}
    5.16  			}
    5.17  			grid_row++;
    5.18  		}
    5.19 @@ -92,6 +83,30 @@
    5.20  	}
    5.21  	fclose(fp);
    5.22  
    5.23 +	for(int i=0; i<ysz; i++) {
    5.24 +		for(int j=0; j<xsz; j++) {
    5.25 +			GridCell *cell = get_cell(j, i);
    5.26 +			if(!cell) {
    5.27 +				continue;
    5.28 +			}
    5.29 +
    5.30 +			// find the adjacency mask of this cell and store it
    5.31 +			unsigned int adjmask = get_cell_dirmask(j, i);
    5.32 +			cell->set_adj_mask(adjmask);
    5.33 +
    5.34 +			// add any audio sources in this grid-cell to the level static audio manager
    5.35 +			std::list<Tile::AudioSourceDesc> asrc = cell->get_audio_sources();
    5.36 +			for(auto sdesc : asrc) {
    5.37 +				if(sdesc.dirmask & adjmask) {
    5.38 +					AudioSource *s = new AudioSource;
    5.39 +					s->set_sample(sdesc.sample);
    5.40 +					s->set_position(sdesc.pos + get_cell_pos(i, y));
    5.41 +					austatic.add_source(s);
    5.42 +				}
    5.43 +			}
    5.44 +		}
    5.45 +	}
    5.46 +
    5.47  	return true;
    5.48  }
    5.49  
    5.50 @@ -100,6 +115,14 @@
    5.51  	return false;
    5.52  }
    5.53  
    5.54 +GridCell *Level::get_cell(int x, int y)
    5.55 +{
    5.56 +	if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
    5.57 +		return 0;
    5.58 +	}
    5.59 +	return cells[y * xsz + x];
    5.60 +}
    5.61 +
    5.62  const GridCell *Level::get_cell(int x, int y) const
    5.63  {
    5.64  	if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
    5.65 @@ -272,6 +295,18 @@
    5.66  	if(tile) {
    5.67  		add_tile(tile);
    5.68  	}
    5.69 +
    5.70 +	adjmask = TILE_ALL;
    5.71 +}
    5.72 +
    5.73 +void GridCell::set_adj_mask(unsigned int mask)
    5.74 +{
    5.75 +	adjmask = mask;
    5.76 +}
    5.77 +
    5.78 +unsigned int GridCell::get_adj_mask() const
    5.79 +{
    5.80 +	return adjmask;
    5.81  }
    5.82  
    5.83  void GridCell::add_tile(Tile *tile)
     6.1 --- a/prototype/src/level.h	Tue Sep 18 00:34:29 2012 +0300
     6.2 +++ b/prototype/src/level.h	Tue Sep 18 04:45:46 2012 +0300
     6.3 @@ -34,6 +34,7 @@
     6.4  	bool load(const char *fname);
     6.5  	bool save(const char *fname) const;
     6.6  
     6.7 +	GridCell *get_cell(int x, int y);
     6.8  	const GridCell *get_cell(int x, int y) const;
     6.9  	Vector3 get_cell_pos(int x, int y) const;
    6.10  	void get_cell_coords_at(const Vector3 &pos, int *xptr, int *yptr) const;
    6.11 @@ -57,9 +58,14 @@
    6.12  	// particle systems
    6.13  	std::vector<struct psys_emitter*> psys;
    6.14  
    6.15 +	unsigned int adjmask;
    6.16 +
    6.17  public:
    6.18  	GridCell(Tile *tile = 0);
    6.19  
    6.20 +	void set_adj_mask(unsigned int mask);
    6.21 +	unsigned int get_adj_mask() const;
    6.22 +
    6.23  	void add_tile(Tile *tile);
    6.24  
    6.25  	void update(unsigned long msec, float dt);
     7.1 --- a/prototype/src/main.cc	Tue Sep 18 00:34:29 2012 +0300
     7.2 +++ b/prototype/src/main.cc	Tue Sep 18 04:45:46 2012 +0300
     7.3 @@ -271,8 +271,11 @@
     7.4  	level->set_player_position(cam.get_position());
     7.5  	level->update(msec, dt);
     7.6  
     7.7 -	// play the walking sound if we're walking
     7.8  	if(cfg.sound) {
     7.9 +		// set the listener matrix
    7.10 +		set_audio_listener(cam.matrix());
    7.11 +
    7.12 +		// play the walking sound if we're walking
    7.13  		int cellx, celly;
    7.14  		level->get_cell_coords_at(cam.get_position(), &cellx, &celly);
    7.15  
     8.1 --- a/prototype/src/tile.cc	Tue Sep 18 00:34:29 2012 +0300
     8.2 +++ b/prototype/src/tile.cc	Tue Sep 18 04:45:46 2012 +0300
     8.3 @@ -58,7 +58,7 @@
     8.4  const Tile::AudioSourceDesc &Tile::get_audio_source(int idx) const
     8.5  {
     8.6  	if(idx < 0 || idx >= (int)ausrc.size()) {
     8.7 -		static AudioSourceDesc d = { Vector3(), 0 };
     8.8 +		static AudioSourceDesc d = { 0, Vector3(), 0 };
     8.9  		return d;
    8.10  	}
    8.11  	return ausrc[idx];
    8.12 @@ -291,7 +291,7 @@
    8.13  				SampleSet *sampleset = tset->get_samples();
    8.14  				AudioSample *sample = sampleset->get("fire.ogg");
    8.15  				if(sample) {
    8.16 -					AudioSourceDesc adesc = {lt->get_position(), sample};
    8.17 +					AudioSourceDesc adesc = {side, lt->get_position(), sample};
    8.18  					ausrc.push_back(adesc);
    8.19  				}
    8.20  			}
     9.1 --- a/prototype/src/tile.h	Tue Sep 18 00:34:29 2012 +0300
     9.2 +++ b/prototype/src/tile.h	Tue Sep 18 04:45:46 2012 +0300
     9.3 @@ -30,6 +30,7 @@
     9.4  class Tile {
     9.5  public:
     9.6  	struct AudioSourceDesc {
     9.7 +		unsigned int dirmask;
     9.8  		Vector3 pos;
     9.9  		AudioSample *sample;
    9.10  	};