Skip to content

Commit

Permalink
Checks that OpenAL sound source was actually allocated
Browse files Browse the repository at this point in the history
fix for massive lag when lots of blocks are placed at once --> sound 
entity system was filled up with inactive sound objects
  • Loading branch information
xtreme8000 committed Jun 18, 2021
1 parent a56d93b commit 1374354
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/sound.c
Expand Up @@ -112,22 +112,30 @@ static void sound_createEx(enum sound_space option, struct Sound_wav* w, float x
.stick_to_player = player,
};

alGetError();
alGenSources(1, &s.openal_handle);
alSourcef(s.openal_handle, AL_PITCH, 1.0F);
alSourcef(s.openal_handle, AL_GAIN, 1.0F);
alSourcef(s.openal_handle, AL_REFERENCE_DISTANCE, s.local ? 0.0F : w->min * SOUND_SCALE);
alSourcef(s.openal_handle, AL_MAX_DISTANCE, s.local ? 2048.0F : w->max * SOUND_SCALE);
alSource3f(s.openal_handle, AL_POSITION, s.local ? 0.0F : x * SOUND_SCALE, s.local ? 0.0F : y * SOUND_SCALE,
s.local ? 0.0F : z * SOUND_SCALE);
alSource3f(s.openal_handle, AL_VELOCITY, s.local ? 0.0F : vx * SOUND_SCALE, s.local ? 0.0F : vy * SOUND_SCALE,
s.local ? 0.0F : vz * SOUND_SCALE);
alSourcei(s.openal_handle, AL_SOURCE_RELATIVE, s.local);
alSourcei(s.openal_handle, AL_LOOPING, AL_FALSE);
alSourcei(s.openal_handle, AL_BUFFER, w->openal_buffer);

alSourcePlay(s.openal_handle);

entitysys_add(&sound_sources, &s);

if(alGetError() == AL_NO_ERROR) {
alSourcef(s.openal_handle, AL_PITCH, 1.0F);
alSourcef(s.openal_handle, AL_GAIN, 1.0F);
alSourcef(s.openal_handle, AL_REFERENCE_DISTANCE, s.local ? 0.0F : w->min * SOUND_SCALE);
alSourcef(s.openal_handle, AL_MAX_DISTANCE, s.local ? 2048.0F : w->max * SOUND_SCALE);
alSource3f(s.openal_handle, AL_POSITION, s.local ? 0.0F : x * SOUND_SCALE, s.local ? 0.0F : y * SOUND_SCALE,
s.local ? 0.0F : z * SOUND_SCALE);
alSource3f(s.openal_handle, AL_VELOCITY, s.local ? 0.0F : vx * SOUND_SCALE, s.local ? 0.0F : vy * SOUND_SCALE,
s.local ? 0.0F : vz * SOUND_SCALE);
alSourcei(s.openal_handle, AL_SOURCE_RELATIVE, s.local);
alSourcei(s.openal_handle, AL_LOOPING, AL_FALSE);
alSourcei(s.openal_handle, AL_BUFFER, w->openal_buffer);

alSourcePlay(s.openal_handle);

if(alGetError() == AL_NO_ERROR) {
entitysys_add(&sound_sources, &s);
} else {
alDeleteSources(1, &s.openal_handle);
}
}
#endif
}

Expand Down

0 comments on commit 1374354

Please sign in to comment.