More aggressively clean up sounds when possible.

This commit is contained in:
Nolan Darilek 2021-07-13 10:40:50 -05:00
parent 76c7701e92
commit 0a65d16d44

View File

@ -252,9 +252,11 @@ fn source_update(
max_distance, max_distance,
rolloff_factor, rolloff_factor,
bypass_global_effects, bypass_global_effects,
state,
.. ..
} = *sound; } = *sound;
match &sound.state { let mut clear = false;
match state {
SoundState::Stopped => { SoundState::Stopped => {
if let Some(source) = sound.source.as_mut() { if let Some(source) = sound.source.as_mut() {
let mut source = source.lock().unwrap(); let mut source = source.lock().unwrap();
@ -265,6 +267,9 @@ fn source_update(
SoundState::Playing => { SoundState::Playing => {
if let Some(source) = sound.source.as_mut() { if let Some(source) = sound.source.as_mut() {
let mut source = source.lock().unwrap(); let mut source = source.lock().unwrap();
if !vec![SourceState::Playing, SourceState::Paused].contains(&source.state()) {
clear = true;
} else {
sync_source_and_components( sync_source_and_components(
&mut source, &mut source,
transform, transform,
@ -278,9 +283,10 @@ fn source_update(
bypass_global_effects, bypass_global_effects,
&mut **global_effects, &mut **global_effects,
); );
if ![SourceState::Playing, SourceState::Stopped].contains(&source.state()) { if source.state() != SourceState::Playing {
source.play(); source.play();
} }
}
} else { } else {
if let Ok(mut source) = context.new_static_source() { if let Ok(mut source) = context.new_static_source() {
if let Some(buffer) = buffers.0.get(&sound.buffer.id) { if let Some(buffer) = buffers.0.get(&sound.buffer.id) {
@ -348,6 +354,10 @@ fn source_update(
} }
} }
} }
if clear {
sound.source = None;
sound.state = SoundState::Stopped;
}
if let Some(source) = sound.source.clone() { if let Some(source) = sound.source.clone() {
let source = source.lock().unwrap(); let source = source.lock().unwrap();
sound.state = match &source.state() { sound.state = match &source.state() {