[Fix] [Phase 4] Clean up sounds a better. Fixed segfaults. Added some more sounds.
This commit is contained in:
parent
f191a1d824
commit
fb326610f6
BIN
snd/sounds/afterburner.wav
Normal file
BIN
snd/sounds/afterburner.wav
Normal file
Binary file not shown.
Binary file not shown.
BIN
snd/sounds/mass.wav
Normal file
BIN
snd/sounds/mass.wav
Normal file
Binary file not shown.
BIN
snd/sounds/neutron.wav
Normal file
BIN
snd/sounds/neutron.wav
Normal file
Binary file not shown.
70
src/sound.c
70
src/sound.c
@ -86,7 +86,7 @@ static int source_nstack = 0;
|
||||
struct alVoice {
|
||||
alVoice* next; // Yes it's a linked list.
|
||||
|
||||
ALuint id; // Unique id for the voice.
|
||||
//ALuint id; // Unique id for the voice.
|
||||
|
||||
ALuint source; // Source itself, 0 if not set.
|
||||
ALuint buffer; // Buffer.
|
||||
@ -94,7 +94,7 @@ struct alVoice {
|
||||
int priority; // Base priority.
|
||||
|
||||
double px, py; // Position.
|
||||
double vx, vy; // Velocity.
|
||||
//double vx, vy; // Velocity.
|
||||
|
||||
unsigned int start; // time started in ms.
|
||||
unsigned int flags; // Flags to set properties.
|
||||
@ -105,10 +105,11 @@ static alVoice* voice_end = NULL;
|
||||
// Volume.
|
||||
static ALfloat svolume = 0.3;
|
||||
|
||||
static int sound_makeList(void);
|
||||
static int sound_load(ALuint* buffer, char* filename);
|
||||
static int sound_makeList(void);
|
||||
static int sound_load(ALuint* buffer, char* filename);
|
||||
static void sound_free(alSound* snd);
|
||||
static int voice_getSource(alVoice* voc);
|
||||
static int voice_getSource(alVoice* voc);
|
||||
static void voice_rm(alVoice* prev, alVoice* voice);
|
||||
|
||||
int sound_init(void) {
|
||||
int mem, ret = 0;
|
||||
@ -224,6 +225,14 @@ void sound_exit(void) {
|
||||
music_exit();
|
||||
}
|
||||
|
||||
// Clean up the voices.
|
||||
while(voice_start != NULL)
|
||||
voice_rm(NULL, voice_start);
|
||||
|
||||
// Clean up the sources.
|
||||
if(source_stack)
|
||||
alDeleteSources(source_nstack, source_stack);
|
||||
|
||||
if(sound_lock) {
|
||||
SDL_mutexP(sound_lock);
|
||||
|
||||
@ -296,7 +305,7 @@ static int sound_makeList(void) {
|
||||
free(files[i]);
|
||||
free(files);
|
||||
|
||||
DEBUG("Loaded %d sound%s", nsound_list, (nsound_list==1)?"":'s');
|
||||
DEBUG("Loaded %d sound%s", nsound_list, (nsound_list==1)?"":"s");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -345,7 +354,6 @@ static void sound_free(alSound* snd) {
|
||||
|
||||
// Update the sounds and prioritize them.
|
||||
void sound_update(void) {
|
||||
ALint stat;
|
||||
alVoice* voice, *prev, *next;
|
||||
|
||||
if(sound_lock == NULL) return; // Sound system is off.
|
||||
@ -364,34 +372,38 @@ void sound_update(void) {
|
||||
voice->px, voice->py, 0.);
|
||||
/*alSource3f(voice->source, AL_VELOCITY,
|
||||
voice->vx, voice->vy, 0.);*/
|
||||
} else { // Delete them.
|
||||
if(voice->source != 0) {
|
||||
// Sources must exist.
|
||||
|
||||
// Stop it if playing.
|
||||
alGetSourcei(voice->source, AL_SOURCE_STATE, &stat);
|
||||
if(stat == AL_PLAYING) alSourceStop(voice->source);
|
||||
|
||||
// Clear it and get rid of it.
|
||||
source_stack[source_nstack++] == voice->source; // throw it back.
|
||||
}
|
||||
|
||||
// Delete from linked list.
|
||||
if(prev == NULL)
|
||||
voice_start = voice->next;
|
||||
else // Not first member.
|
||||
prev->next = voice->next;
|
||||
if(voice_end == voice)
|
||||
voice_end = prev;
|
||||
free(voice);
|
||||
}
|
||||
prev = voice;
|
||||
prev = voice; // Only case where voice will stay.
|
||||
} else // Delete them.
|
||||
voice_rm(prev, voice);
|
||||
voice = next;
|
||||
} while(voice != NULL);
|
||||
|
||||
SDL_mutexV(sound_lock);
|
||||
}
|
||||
|
||||
// Remove a voice.
|
||||
static void voice_rm(alVoice* prev, alVoice* voice) {
|
||||
ALint stat;
|
||||
|
||||
if(voice->source != 0) { // Source must exist.
|
||||
// Stop it if playing.
|
||||
alGetSourcei(voice->source, AL_SOURCE_STATE, &stat);
|
||||
if(stat == AL_PLAYING) alSourceStop(voice->source);
|
||||
|
||||
// Clear it and get rid of it.
|
||||
source_stack[source_nstack++] = voice->source; // Throw it back.
|
||||
}
|
||||
|
||||
// Delete from linked list.
|
||||
if(prev == NULL) // Was the first member.
|
||||
voice_start = voice->next;
|
||||
else // Not first memmber.
|
||||
prev->next = voice->next;
|
||||
if(voice_end == voice) // Last voice in linked list.
|
||||
voice_end = prev;
|
||||
free(voice);
|
||||
}
|
||||
|
||||
// Set all the sounds volume to vol.
|
||||
void sound_volume(const double vol) {
|
||||
if(sound_lock == NULL) return;
|
||||
|
Loading…
Reference in New Issue
Block a user