diff --git a/ChaosMod/Components/EffectDispatcher.cpp b/ChaosMod/Components/EffectDispatcher.cpp index c4036d427..a30703a1b 100644 --- a/ChaosMod/Components/EffectDispatcher.cpp +++ b/ChaosMod/Components/EffectDispatcher.cpp @@ -281,7 +281,7 @@ void EffectDispatcher::UpdateEffects(int deltaTime) int maxEffects = m_MaxRunningEffects; int activeEffects = 0; - // Reverse order to ensure the effects on top of the list are removed if activeEffects > maxEffects + // Reverse order to ensure the first effects are removed if activeEffects > maxEffects for (auto it = SharedState.ActiveEffects.rbegin(); it != SharedState.ActiveEffects.rend();) { auto &activeEffect = *it; diff --git a/ChaosMod/Components/EffectSound/EffectSound3D.cpp b/ChaosMod/Components/EffectSound/EffectSound3D.cpp index 1881c143b..5c04585a2 100644 --- a/ChaosMod/Components/EffectSound/EffectSound3D.cpp +++ b/ChaosMod/Components/EffectSound/EffectSound3D.cpp @@ -8,6 +8,8 @@ #define MINIAUDIO_IMPLEMENTATION #include +#define MAX_ACTIVE_SOUNDS 10 + EffectSound3D::EffectSound3D() { ma_engine_init(nullptr, &m_maEngine); @@ -70,17 +72,19 @@ void EffectSound3D::OnRun() auto adjPlayerVel = GET_ENTITY_VELOCITY(playerIsInAnyVeh ? playerVeh : playerPed); ma_engine_listener_set_velocity(&m_maEngine, 0, adjPlayerVel.x, adjPlayerVel.y, adjPlayerVel.z); - for (auto it = m_Sounds.begin(); it != m_Sounds.end();) + int activeSounds = 0; + std::lock_guard lock(m_SoundsMutex); + // Reverse order to ensure the first sounds are removed if MAX_ACTIVE_SOUNDS has been reached + for (auto it = m_Sounds.rbegin(); it != m_Sounds.rend();) { - std::lock_guard lock(m_SoundsMutex); - auto &[soundId, sound] = *it; auto uninitSound = [&]() { ma_sound_stop(&sound.Handle); ma_sound_uninit(&sound.Handle); - it = m_Sounds.erase(it); + it = static_cast(m_Sounds.erase(std::next(it).base())); + activeSounds--; }; if (ma_sound_at_end(&sound.Handle)) @@ -135,6 +139,12 @@ void EffectSound3D::OnRun() continue; } + if (++activeSounds > MAX_ACTIVE_SOUNDS) + { + uninitSound(); + continue; + } + it++; } } diff --git a/ChaosMod/Components/EffectSound/EffectSound3D.h b/ChaosMod/Components/EffectSound/EffectSound3D.h index d5aedc31c..f04d1abf8 100644 --- a/ChaosMod/Components/EffectSound/EffectSound3D.h +++ b/ChaosMod/Components/EffectSound/EffectSound3D.h @@ -5,9 +5,9 @@ #include #include +#include #include #include -#include class EffectSound3D : public EffectSoundManager { @@ -19,7 +19,7 @@ class EffectSound3D : public EffectSoundManager ma_sound Handle; EffectSoundPlayOptions PlayOptions; }; - std::unordered_map m_Sounds; + std::map m_Sounds; std::mutex m_SoundsMutex; bool m_IsStopping = false; std::thread m_PauseSoundsThread;