Skip to content

Commit

Permalink
When removing truck, delete it's soundsources.
Browse files Browse the repository at this point in the history
Tip: use demo_script.as to show list of soundsources, more info: #3027
  • Loading branch information
ohlidalp authored and tritonas00 committed May 4, 2023
1 parent 84fee23 commit 137ef9e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
65 changes: 65 additions & 0 deletions source/main/audio/SoundScriptManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,71 @@ SoundScriptInstancePtr SoundScriptManager::createInstance(Ogre::String templaten
return inst;
}

void SoundScriptManager::removeInstance(SoundScriptInstancePtr& ssi)
{
// Find lookup table entries
int trigsPos = -1;
for (int i = 0; i < free_trigs[ssi->templ->trigger_source]; i++)
{
if (trigs[ssi->templ->trigger_source + i * SS_MAX_TRIG] == ssi)
{
trigsPos = i;
}
}

int gainsPos = -1;
for (int i = 0; i < free_gains[ssi->templ->gain_source]; i++)
{
if (gains[ssi->templ->gain_source + i * SS_MAX_MOD] == ssi)
{
gainsPos = i;
}
}

int pitchesPos = -1;
for (int i = 0; i < free_gains[ssi->templ->pitch_source]; i++)
{
if (pitches[ssi->templ->pitch_source + i * SS_MAX_MOD] == ssi)
{
pitchesPos = i;
}
}

// Erase lookup entries
if (trigsPos != -1)
{
for (int i = trigsPos + 1; i < free_trigs[ssi->templ->trigger_source]; i++)
{
trigs[ssi->templ->trigger_source + (i - 1) * SS_MAX_TRIG]
= trigs[ssi->templ->trigger_source + i * SS_MAX_TRIG];
}
free_trigs[ssi->templ->trigger_source]--;
}

if (gainsPos != -1)
{
for (int i = gainsPos + 1; i < free_gains[ssi->templ->gain_source]; i++)
{
gains[ssi->templ->gain_source + (i - 1) * SS_MAX_MOD]
= gains[ssi->templ->gain_source + i * SS_MAX_MOD];
}
free_gains[ssi->templ->gain_source]--;
}

if (pitchesPos != -1)
{
for (int i = pitchesPos + 1; i < free_pitches[ssi->templ->pitch_source]; i++)
{
pitches[ssi->templ->pitch_source + (i - 1) * SS_MAX_MOD]
= pitches[ssi->templ->pitch_source + i * SS_MAX_MOD];
}
free_pitches[ssi->templ->pitch_source]--;
}

// Finally remove the instance from list
EraseIf(instances, [ssi](SoundScriptInstancePtr& instance) { return ssi == instance; });
}

void SoundScriptManager::parseScript(DataStreamPtr& stream, const String& groupName)
{
SoundScriptTemplatePtr sst = nullptr;
Expand Down
1 change: 1 addition & 0 deletions source/main/audio/SoundScriptManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ class SoundScriptManager : public Ogre::ScriptLoader, public ZeroedMemoryAllocat
Ogre::Real getLoadingOrder(void) const;

SoundScriptInstancePtr createInstance(Ogre::String templatename, int actor_id, int soundLinkType=SL_DEFAULT, int soundLinkItemId=-1);
void removeInstance(SoundScriptInstancePtr& ssi);
std::vector<SoundScriptInstancePtr>& getAllInstances() { return instances; }
SoundScriptTemplatePtr getTemplate(Ogre::String name) { return templates[name]; }
std::map <Ogre::String, SoundScriptTemplatePtr>& getAllTemplates() { return templates; }
Expand Down
11 changes: 10 additions & 1 deletion source/main/physics/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,17 @@ void Actor::dispose()
{
SOUND_STOP(this, i);
}
#endif // USE_OPENAL
muteAllSounds();
for (int i = 0; i < ar_num_soundsources; i++)
{
if (ar_soundsources[i].ssi)
{
App::GetSoundScriptManager()->removeInstance(ar_soundsources[i].ssi);
ar_soundsources[i].ssi = nullptr;
}
}
ar_num_soundsources = 0;
#endif // USE_OPENAL

if (ar_engine != nullptr)
{
Expand Down
6 changes: 6 additions & 0 deletions source/main/utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ std::string SanitizeUtf8CString(const char* start, const char* end = nullptr);
inline std::string& TrimStr(std::string& s) { Ogre::StringUtil::trim(s); return s; }
std::string Sha1Hash(std::string const & data);

// for std::vector
template <class T, class A, class Predicate>
inline void EraseIf(std::vector<T, A>& c, Predicate pred)
{
c.erase(std::remove_if(c.begin(), c.end(), pred), c.end());
}

/// @author http://www.ogre3d.org/forums/viewtopic.php?p=463232#p463232
/// @author http://www.ogre3d.org/tikiwiki/tiki-index.php?page=GetScreenspaceCoords&structure=Cookbook
Expand Down

0 comments on commit 137ef9e

Please sign in to comment.