removed images
[tennix.git] / sound.c
blobe3dcb345e7fcb5f404a8bfa01fcba531ba94bb91
1 #include "tennix.h"
2 #include "sound.h"
4 static char datadir[MAXPATHLEN];
6 void init_sound( const char *data_dir) {
7 strcpy( datadir, data_dir);
9 if( Mix_OpenAudio( 44100, AUDIO_S16SYS, 2, 1024) < 0) {
10 fprintf( stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError());
14 void play_sample( const char* filename, unsigned char channel, int loop) {
15 static Mix_Chunk* chunks[CH_MAX] = { 0 };
16 char fn[MAXPATHLEN];
18 strcpy( fn, datadir);
19 strcat( fn, filename);
21 if( chunks[channel] != NULL) {
22 Mix_FreeChunk( chunks[channel]);
23 chunks[channel] = NULL;
26 chunks[channel] = Mix_LoadWAV( fn);
27 if( chunks[channel] != NULL) {
28 Mix_PlayChannel( channel, chunks[channel], loop);
29 } else {
30 fprintf( stderr, "Error: %s\n", Mix_GetError());
34 void fade_out( unsigned char channel) {
35 Mix_FadeOutChannel( channel, FADE_OUT_MS);
38 void sound_ground() {
39 const char* sounds[] = { "ground1.wav", "ground2.wav" };
40 play_sample( sounds[rand()%2], CH_GROUND, 0);
43 void sound_racket( unsigned char powershot) {
44 const char* sounds[] = { "racket1.wav", "racket2.wav" };
45 if( powershot) {
46 play_sample( "powershot.wav", CH_RACKET, 0);
47 } else {
48 play_sample( sounds[rand()%2], CH_RACKET, 0);
52 void sound_audience() {
53 play_sample( "audience.wav", CH_AUDIENCE, -1);
56 void sound_applause() {
57 play_sample( "applause.wav", CH_APPLAUSE, 0);
60 void sound_applause_stop() {
61 fade_out( CH_APPLAUSE);