diff --git a/src/sound.c b/src/sound.c new file mode 100644 index 0000000..faafce4 --- /dev/null +++ b/src/sound.c @@ -0,0 +1,128 @@ +/* $Id: sound.c,v 1.1 2003/05/26 20:35:43 stpohle Exp $ */ +/* sound */ + +#include "sound.h" + +_snd snd; + +/* play an soundsample SND_* */ +void +snd_play (int samplenr) +{ +#if HAVE_SDL_MIXER + if (samplenr < SND_max && snd.inited && snd.sample[samplenr] != NULL) + Mix_PlayChannel (-1, snd.sample[samplenr], 0); +#endif +}; + + +/* start playing music */ +void +snd_music_start () +{ +#if HAVE_SDL_MIXER + if (snd.inited && snd.music != NULL) + Mix_PlayMusic (snd.music, -1); +#endif +}; + +/* stop playing music */ +void +snd_music_stop () +{ +#if HAVE_SDL_MIXER + if (snd.inited) + Mix_HaltMusic (); +#endif +}; + +/* init audio, sdl_mixer */ +void +snd_init () +{ +#if HAVE_SDL_MIXER + int audio_rate = 22050; + Uint16 audio_format = AUDIO_S16; + int audio_channels = 2; + + if (Mix_OpenAudio (audio_rate, audio_format, audio_channels, 1024) < 0) { + d_printf ("Couldn't open audio mixer: %s\n", SDL_GetError ()); + snd.inited = 0; + return; + } + + Mix_QuerySpec (&audio_rate, &audio_format, &audio_channels); + d_printf ("Opened audio at %d Hz %d bit %s\n", + audio_rate, (audio_format & 0xFF), (audio_channels > 1 ? "stereo" : "mono")); + + Mix_Volume (-1, 96); + Mix_VolumeMusic (48); + + snd.inited = 1; +#endif + return; +}; + + +/* load the audio files */ +void +snd_load (char *tilesetname) +{ +#if HAVE_SDL_MIXER + char fullname[LEN_PATHFILENAME]; + char filename[LEN_FILENAME]; + + int i; + + // load samples + d_printf ("Loading Audioset\n"); + + for (i = 0; i < SND_max; i++) { + switch (i) { + case (SND_dead): + sprintf (filename, "dead.wav"); + break; + case (SND_explode): + sprintf (filename, "explode.wav"); + break; + default: + sprintf (filename, "drop.wav"); + break; + } + + /* try loading the sample from the tileset or the default */ + sprintf (fullname, "%s/tileset/%s/%s", bman.datapath, tilesetname, filename); + if ((snd.sample[i] = Mix_LoadWAV (fullname)) == NULL) { + sprintf (fullname, "%s/tileset/default/%s", bman.datapath, filename); + if ((snd.sample[i] = Mix_LoadWAV (fullname)) == NULL) + d_printf ("Couldn't load %s: %s\n", fullname, SDL_GetError ()); + } + } + + /* try loading the music from the tileset or the default */ + sprintf (fullname, "%s/tileset/%s/music.ogg", bman.datapath, tilesetname); + if ((snd.music = Mix_LoadMUS (fullname)) == NULL) { + sprintf (fullname, "%s/tileset/default/music.ogg", bman.datapath); + if ((snd.music = Mix_LoadMUS (fullname)) == NULL) + d_printf ("Couldn't load %s: %s\n", fullname, SDL_GetError ()); + } + +#endif + return; +}; + + +void +snd_free () +{ +#if HAVE_SDL_MIXER + int i; + + for (i = 0; i < SND_max; i++) + if (snd.sample[i] != NULL) + Mix_FreeChunk (snd.sample[i]); + + if (snd.music != NULL) + Mix_FreeMusic (snd.music); +#endif +}; diff --git a/src/sound.h b/src/sound.h new file mode 100644 index 0000000..2846370 --- /dev/null +++ b/src/sound.h @@ -0,0 +1,41 @@ +/* $Id: sound.h,v 1.1 2003/05/26 20:35:43 stpohle Exp $ */ +/* include file for the sound */ + +#ifndef _SOUND_H_ +#define _SOUND_H_ + +#include "bomberclone.h" + +#if HAVE_SDL_MIXER +#include +#else +#define Mix_Chunk void +#define Mix_Music void +#endif + +enum _soundsample { + SND_dead = 0, + SND_explode, + SND_bombdrop, + + SND_max +}; + +struct __snd { + unsigned char inited; + + Mix_Chunk *sample[SND_max]; // henqvist + Mix_Music *music; // henqvist + +} typedef _snd; + +extern _snd snd; + +void snd_play(int samplenr); +void snd_music_start(); +void snd_music_stop(); +void snd_init(); +void snd_load(char *tilesetname); +void snd_free (); + +#endif \ No newline at end of file