Skip to content

Commit

Permalink
ChaosMod/EffectSound3D: Implement active sound count limit
Browse files Browse the repository at this point in the history
  • Loading branch information
pongo1231 committed Jan 18, 2025
1 parent d013736 commit 30e2913
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ChaosMod/Components/EffectDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 14 additions & 4 deletions ChaosMod/Components/EffectSound/EffectSound3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#define MINIAUDIO_IMPLEMENTATION
#include <miniaudio.h>

#define MAX_ACTIVE_SOUNDS 10

EffectSound3D::EffectSound3D()
{
ma_engine_init(nullptr, &m_maEngine);
Expand Down Expand Up @@ -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<decltype(it)>(m_Sounds.erase(std::next(it).base()));
activeSounds--;
};

if (ma_sound_at_end(&sound.Handle))
Expand Down Expand Up @@ -135,6 +139,12 @@ void EffectSound3D::OnRun()
continue;
}

if (++activeSounds > MAX_ACTIVE_SOUNDS)
{
uninitSound();
continue;
}

it++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions ChaosMod/Components/EffectSound/EffectSound3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include <miniaudio.h>
#include <scripthookv/inc/types.h>

#include <map>
#include <mutex>
#include <string>
#include <unordered_map>

class EffectSound3D : public EffectSoundManager
{
Expand All @@ -19,7 +19,7 @@ class EffectSound3D : public EffectSoundManager
ma_sound Handle;
EffectSoundPlayOptions PlayOptions;
};
std::unordered_map<DWORD64, Sound> m_Sounds;
std::map<DWORD64, Sound> m_Sounds;
std::mutex m_SoundsMutex;
bool m_IsStopping = false;
std::thread m_PauseSoundsThread;
Expand Down

0 comments on commit 30e2913

Please sign in to comment.