You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bomberclone/src/sound.c

129 lines
2.9 KiB

/* $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
};