Revert a8d3397fb4 and arase entries from the _sample map after the loop

This commit is contained in:
Erik Hofman
2022-10-03 13:53:24 +02:00
parent 49dd3cad33
commit 47aae88aba
2 changed files with 18 additions and 26 deletions

View File

@@ -85,11 +85,8 @@ void SGSampleGroup::start_playing_sample(SGSoundSample *sample)
_smgr->sample_play( sample ); _smgr->sample_play( sample );
} }
bool SGSampleGroup::check_playing_sample(sample_map::iterator& sample_it) void SGSampleGroup::check_playing_sample(SGSoundSample *sample)
{ {
SGSoundSample *sample = sample_it->second;
bool rv = true;
// check if the sound has stopped by itself // check if the sound has stopped by itself
if (_smgr->is_sample_stopped(sample)) { if (_smgr->is_sample_stopped(sample)) {
// sample is stopped because it wasn't looping // sample is stopped because it wasn't looping
@@ -97,8 +94,7 @@ bool SGSampleGroup::check_playing_sample(sample_map::iterator& sample_it)
sample->no_valid_source(); sample->no_valid_source();
_smgr->release_source( sample->get_source() ); _smgr->release_source( sample->get_source() );
_smgr->release_buffer( sample ); _smgr->release_buffer( sample );
sample_it = remove( sample_it ); remove( sample->get_sample_name() );
rv = false;
} else if ( sample->has_changed() ) { } else if ( sample->has_changed() ) {
if ( !sample->is_playing() ) { if ( !sample->is_playing() ) {
// a request to stop playing the sound has been filed. // a request to stop playing the sound has been filed.
@@ -109,7 +105,6 @@ bool SGSampleGroup::check_playing_sample(sample_map::iterator& sample_it)
update_sample_config( sample ); update_sample_config( sample );
} }
} }
return rv;
} }
void SGSampleGroup::update( double dt ) { void SGSampleGroup::update( double dt ) {
@@ -126,23 +121,27 @@ void SGSampleGroup::update( double dt ) {
_changed = false; _changed = false;
} }
for (auto current = _samples.begin(); current != _samples.end(); ) { for (auto current : _samples) {
SGSoundSample *sample = current->second; SGSoundSample *sample = current.second;
bool increment = true;
if ( !sample->is_valid_source() && sample->is_playing() && !sample->test_out_of_range()) { if ( !sample->is_valid_source() && sample->is_playing() && !sample->test_out_of_range()) {
start_playing_sample(sample); start_playing_sample(sample);
} else if ( sample->is_valid_source() ) { } else if ( sample->is_valid_source() ) {
increment = check_playing_sample(current); check_playing_sample(sample);
} }
if (increment) {
++current;
}
testForMgrError("update"); testForMgrError("update");
} }
for (auto current : _removed_samples) {
for (auto sample = _samples.begin(); sample != _samples.end(); ) {
if (sample->second == current) {
sample = _samples.erase(sample);
} else {
++sample;
}
}
}
} }
// add a sound effect, return true if successful // add a sound effect, return true if successful
@@ -172,18 +171,12 @@ bool SGSampleGroup::remove( const std::string &refname ) {
if ( sample_it->second->is_valid_buffer() ) if ( sample_it->second->is_valid_buffer() )
_removed_samples.push_back( sample_it->second ); _removed_samples.push_back( sample_it->second );
_samples.erase( sample_it ); // Do not erase within the loop
// _samples.erase( sample_it );
return true; return true;
} }
sample_map::iterator SGSampleGroup::remove( sample_map::iterator sample_it ) {
if ( sample_it->second->is_valid_buffer() )
_removed_samples.push_back( sample_it->second );
return _samples.erase( sample_it );
}
// return true of the specified sound exists in the sound manager system // return true of the specified sound exists in the sound manager system
bool SGSampleGroup::exists( const std::string &refname ) { bool SGSampleGroup::exists( const std::string &refname ) {

View File

@@ -223,8 +223,7 @@ protected:
private: private:
void cleanup_removed_samples(); void cleanup_removed_samples();
void start_playing_sample(SGSoundSample *sample); void start_playing_sample(SGSoundSample *sample);
bool check_playing_sample(sample_map::iterator& sample_it ); void check_playing_sample(SGSoundSample *sample);
sample_map::iterator remove( sample_map::iterator sample_it );
bool _changed = false; bool _changed = false;
bool _pause = false; bool _pause = false;