dungeon_crawler

diff prototype/src/main.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 d52711f2b9a1
children 303743485aba
line diff
     1.1 --- a/prototype/src/main.cc	Sun Sep 16 08:16:50 2012 +0300
     1.2 +++ b/prototype/src/main.cc	Mon Sep 17 08:40:59 2012 +0300
     1.3 @@ -5,7 +5,7 @@
     1.4  #include "opengl.h"
     1.5  #include "psys/psys.h"
     1.6  #include "level.h"
     1.7 -#include "texman.h"
     1.8 +#include "texture.h"
     1.9  #include "camera.h"
    1.10  #include "datapath.h"
    1.11  #include "tileset.h"
    1.12 @@ -14,7 +14,8 @@
    1.13  #include "cmdcon.h"
    1.14  #include "cfg.h"
    1.15  #include "timer.h"
    1.16 -#include "audio.h"
    1.17 +#include "audio/audio.h"
    1.18 +#include "audio/source.h"
    1.19  
    1.20  bool init(int xsz, int ysz);
    1.21  void cleanup();
    1.22 @@ -43,6 +44,8 @@
    1.23  
    1.24  static bool show_con;
    1.25  
    1.26 +static AudioSource *move_sound;
    1.27 +
    1.28  int main(int argc, char **argv)
    1.29  {
    1.30  	glutInit(&argc, argv);
    1.31 @@ -91,8 +94,17 @@
    1.32  
    1.33  	add_data_path(".");
    1.34  	add_data_path("data");
    1.35 +	add_data_path("data/audio");
    1.36  	add_data_path("sdr");
    1.37  
    1.38 +	if(cfg.sound && !init_audio()) {
    1.39 +		fprintf(stderr, "failed to initialize audio, continuing silently\n");
    1.40 +		cfg.sound = false;
    1.41 +	}
    1.42 +	if(cfg.sound) {
    1.43 +		move_sound = new AudioSource;
    1.44 +	}
    1.45 +
    1.46  	rend = new DeferredRenderer();
    1.47  	if(!cfg.use_deferred || !rend->init(xsz, ysz)) {
    1.48  		printf("falling back to crappy renderer...\n");
    1.49 @@ -126,17 +138,13 @@
    1.50  
    1.51  	cam.input_move(0, 0.5, 0);
    1.52  
    1.53 -	if(cfg.sound && !init_audio()) {
    1.54 -		fprintf(stderr, "failed to initialize audio, continuing silently\n");
    1.55 -		cfg.sound = false;
    1.56 -	}
    1.57 -
    1.58  	return true;
    1.59  }
    1.60  
    1.61  void cleanup()
    1.62  {
    1.63  	if(cfg.sound) {
    1.64 +		delete move_sound;
    1.65  		destroy_audio();
    1.66  	}
    1.67  
    1.68 @@ -238,17 +246,22 @@
    1.69  	float dx = 0, dy = 0;
    1.70  
    1.71  	// handle key input
    1.72 +	bool did_move = false;
    1.73  	if(keystate['w'] || keystate['W']) {
    1.74  		dy -= offs;
    1.75 +		did_move = true;
    1.76  	}
    1.77  	if(keystate['s'] || keystate['S']) {
    1.78  		dy += offs;
    1.79 +		did_move = true;
    1.80  	}
    1.81  	if(keystate['d'] || keystate['D']) {
    1.82  		dx += offs;
    1.83 +		did_move = true;
    1.84  	}
    1.85  	if(keystate['a'] || keystate['A']) {
    1.86  		dx -= offs;
    1.87 +		did_move = true;
    1.88  	}
    1.89  
    1.90  	cam.input_move(dx, 0, dy);
    1.91 @@ -256,6 +269,24 @@
    1.92  	tileset->update_tiles(msec);
    1.93  	level->update(msec, dt);
    1.94  
    1.95 +	// play the walking sound if we're walking
    1.96 +	int cellx, celly;
    1.97 +	level->get_cell_coords_at(cam.get_position(), &cellx, &celly);
    1.98 +
    1.99 +	const AudioSample *move_sample;
   1.100 +	if(did_move && (move_sample = level->get_sample(cellx, celly, TILE_SAMPLE_WALK))) {
   1.101 +		if(move_sample != move_sound->get_sample()) {
   1.102 +			move_sound->stop();
   1.103 +			move_sound->set_sample(move_sample);
   1.104 +			move_sound->play();
   1.105 +		}
   1.106 +	} else {
   1.107 +		if(move_sound->get_sample()) {
   1.108 +			move_sound->stop();
   1.109 +			move_sound->set_sample(0);
   1.110 +		}
   1.111 +	}
   1.112 +
   1.113  	last_upd = msec;
   1.114  }
   1.115  
   1.116 @@ -287,6 +318,27 @@
   1.117  		stereo_shift_pressed = true;
   1.118  		break;
   1.119  
   1.120 +	case 'p':
   1.121 +		{
   1.122 +			Vector3 pos = cam.get_position();
   1.123 +			int cell_x, cell_y;
   1.124 +			level->get_cell_coords_at(pos, &cell_x, &cell_y);
   1.125 +			printf("Current position: [%.2f %.2f %.2f]  cell: [%d %d]\n", pos.x, pos.y, pos.z,
   1.126 +					cell_x, cell_y);
   1.127 +		}
   1.128 +		break;
   1.129 +
   1.130 +	case 'P':
   1.131 +		{
   1.132 +			Vector3 pos = cam.get_position();
   1.133 +			int cell_x, cell_y;
   1.134 +			level->get_cell_coords_at(pos, &cell_x, &cell_y);
   1.135 +			AudioSample *sample = level->get_sample(cell_x, cell_y, TILE_SAMPLE_WALK);
   1.136 +			printf("walk sample: %p\n", (void*)sample);
   1.137 +		}
   1.138 +		break;
   1.139 +
   1.140 +
   1.141  	case '\n':
   1.142  	case '\r':
   1.143  		{
   1.144 @@ -375,5 +427,5 @@
   1.145  unsigned int load_psys_tex(const char *fname, void *cls)
   1.146  {
   1.147  	TextureSet *texset = tileset->get_textures();
   1.148 -	return texset->get_texture(fname);
   1.149 +	return texset->get(fname);
   1.150  }