From: Erik Hofman Date: Mon, 12 Dec 2011 12:19:43 +0000 (+0100) Subject: Free OpenAL sounds of objects that are farther away than the max_distance. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8d3de8dee530e0f367378c0d6390d064c6d7e7dd;p=simgear.git Free OpenAL sounds of objects that are farther away than the max_distance. --- diff --git a/simgear/sound/sample_group.cxx b/simgear/sound/sample_group.cxx index e48cd0ab..bdc00ee6 100644 --- a/simgear/sound/sample_group.cxx +++ b/simgear/sound/sample_group.cxx @@ -406,6 +406,17 @@ void SGSampleGroup::update_pos_and_orientation() { sample->set_rotation( ec2body ); sample->set_position( position ); sample->set_velocity( velocity ); + + // Test if a sample is farther away than max distance, if so + // stop the sound playback and free it's source. + float max2 = sample->get_max_dist() * sample->get_max_dist(); + float dist2 = position[0]*position[0] + + position[1]*position[1] + position[2]*position[2]; + if (sample->is_playing() && (dist2 > max2)) { + sample->stop(); + } else if (!sample->is_playing() && (dist2 < max2)) { + sample->play(sample->is_looping()); + } } }