tb1/tb1_linux/sound.c

161 lines
3.1 KiB
C
Raw Permalink Normal View History

2000-09-05 03:57:00 +00:00
/* "borrowed" from gltron */
2000-10-28 02:09:00 +00:00
#ifdef SDL_MIXER_SOUND
2000-11-02 06:45:00 +00:00
#include <SDL.h>
#include <SDL_mixer.h>
2000-10-28 02:09:00 +00:00
#endif
2000-09-05 03:57:00 +00:00
#include "sound.h"
#include <stdlib.h>
2000-10-16 03:18:00 +00:00
/* Function definition */
char *tb1_data_file(char *name,char *path);
2000-09-05 03:57:00 +00:00
/* linux only, at the moment */
#define NUM_GAME_FX 8
2000-10-28 02:09:00 +00:00
#ifdef SDL_MIXER_SOUND
static Mix_Music *music;
2000-09-05 03:57:00 +00:00
static Mix_Chunk *game_fx[NUM_GAME_FX];
2000-10-28 02:09:00 +00:00
#endif
2000-09-05 03:57:00 +00:00
static char *game_fx_names[] = {
2000-09-18 03:50:00 +00:00
"sound/tb_ahh.wav",
"sound/tb_cc.wav",
"sound/tb_kapow.wav",
"sound/tb_scream.wav",
"sound/tb_bonk.wav",
"sound/tb_click.wav",
"sound/tb_ow.wav",
"sound/tb_zrrp.wav"
2000-09-05 03:57:00 +00:00
};
void loadFX(char *path_to_data) {
2000-10-28 02:09:00 +00:00
#ifdef SDL_MIXER_SOUND
2000-09-05 03:57:00 +00:00
int i;
char *path;
for(i = 0; i < NUM_GAME_FX; i++) {
path=tb1_data_file(game_fx_names[i],path_to_data);
if(path) {
game_fx[i] = Mix_LoadWAV(path);
}
}
2000-10-28 02:09:00 +00:00
#endif
2000-09-05 03:57:00 +00:00
}
int initSound(char *path_to_data) {
/* open the audio device */
2000-10-28 02:09:00 +00:00
#ifdef SDL_MIXER_SOUND
2004-09-26 19:18:00 +00:00
int audio_rate;
Uint16 audio_format;
int audio_channels;
/* Initialize variables */
audio_rate = MIX_DEFAULT_FREQUENCY;
audio_format = MIX_DEFAULT_FORMAT;
audio_channels = 2;
/* Initialize the SDL library */
if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
return(255);
}
/* Open the audio device */
if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
return(2);
} else {
Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
2004-09-29 04:52:00 +00:00
printf(" + Opened audio at %d Hz %d bit %s\n", audio_rate,
2004-09-26 19:18:00 +00:00
(audio_format&0xFF),
(audio_channels > 1) ? "stereo" : "mono");
}
/*
2000-09-05 03:57:00 +00:00
if(Mix_OpenAudio(22050, AUDIO_U16, 1, 1024) < 0) {
fprintf(stderr, "can't open audio: %s\n", SDL_GetError());
2000-09-22 06:13:00 +00:00
return -1;
2000-09-05 03:57:00 +00:00
}
2004-09-26 19:18:00 +00:00
*/
2000-09-05 03:57:00 +00:00
loadFX(path_to_data);
2000-10-28 02:09:00 +00:00
#endif
2000-09-05 03:57:00 +00:00
return 0;
}
void shutdownSound() {
2000-10-28 02:09:00 +00:00
#ifdef SDL_MIXER_SOUND
2000-09-05 03:57:00 +00:00
Mix_CloseAudio();
2000-10-28 02:09:00 +00:00
#endif
2000-09-05 03:57:00 +00:00
}
int loadSound(char *name) {
2000-10-28 02:09:00 +00:00
#ifdef SDL_MIXER_SOUND
2000-09-05 03:57:00 +00:00
music = Mix_LoadMUS(name);
2000-10-28 02:09:00 +00:00
#endif
2000-09-05 03:57:00 +00:00
return 0;
}
int playSound() {
2000-10-28 02:09:00 +00:00
#ifdef SDL_MIXER_SOUND
2000-09-05 03:57:00 +00:00
if( ! Mix_PlayingMusic() )
Mix_PlayMusic(music, -1);
/* todo: remove the following once the bug in SDL_mixer is fixed */
/* we don't want too many references to game objects here */
// setMusicVolume(game->settings->musicVolume);
2000-10-28 02:09:00 +00:00
#endif
2000-09-05 03:57:00 +00:00
return 0;
}
int stopSound() {
2000-10-28 02:09:00 +00:00
#ifdef SDL_MIXER_SOUND
2000-09-05 03:57:00 +00:00
if( Mix_PlayingMusic() )
Mix_HaltMusic();
2000-10-28 02:09:00 +00:00
#endif
2000-09-05 03:57:00 +00:00
return 0;
}
void soundIdle() {
/* sdl_mixer uses pthreads, so no work here */
return;
}
void playGameFX(int fx) {
2000-10-28 02:09:00 +00:00
#ifdef SDL_MIXER_SOUND
2000-09-05 03:57:00 +00:00
Mix_PlayChannel(-1, game_fx[fx], 0);
// fprintf(stderr, "fx on channel %d\n", Mix_PlayChannel(-1, game_fx[fx], 0));
2000-10-28 02:09:00 +00:00
#endif
2000-09-05 03:57:00 +00:00
}
void setMusicVolume(float volume) {
2000-10-28 02:09:00 +00:00
#ifdef SDL_MIXER_SOUND
2000-09-05 03:57:00 +00:00
if(volume > 1) volume = 1;
if(volume < 0) volume = 0;
Mix_VolumeMusic((int)(volume * 128));
2000-10-28 02:09:00 +00:00
#endif
2000-09-05 03:57:00 +00:00
}
void setFxVolume(float volume) {
2000-10-28 02:09:00 +00:00
#ifdef SDL_MIXER_SOUND
2000-09-05 03:57:00 +00:00
if(volume > 1) volume = 1;
if(volume < 0) volume = 0;
Mix_Volume(-1, (int)(volume * 128));
2000-10-28 02:09:00 +00:00
#endif
2000-09-05 03:57:00 +00:00
}