Skip to content

Commit

Permalink
ChaosMod/EffectSound3D: Pause sounds when game is paused
Browse files Browse the repository at this point in the history
  • Loading branch information
pongo1231 committed Jan 17, 2025
1 parent 4bc60c1 commit a8958c0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
24 changes: 24 additions & 0 deletions ChaosMod/Components/EffectSound/EffectSound3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@
EffectSound3D::EffectSound3D()
{
ma_engine_init(nullptr, &m_maEngine);

m_PauseSoundsThread = std::thread(
[&]()
{
while (!m_IsStopping)
{
Sleep(100);

if (IS_PAUSE_MENU_ACTIVE())
{
std::lock_guard lock(m_SoundsMutex);
for (auto &[soundId, sound] : m_Sounds)
ma_sound_stop(&sound.Handle);
}
}
});
}

void EffectSound3D::FreeSounds()
Expand All @@ -23,6 +39,9 @@ void EffectSound3D::FreeSounds()

void EffectSound3D::OnModPauseCleanup()
{
m_IsStopping = true;
m_PauseSoundsThread.detach();

FreeSounds();

ma_engine_uninit(&m_maEngine);
Expand Down Expand Up @@ -52,6 +71,8 @@ void EffectSound3D::OnRun()

for (auto it = m_Sounds.begin(); it != m_Sounds.end();)
{
std::lock_guard lock(m_SoundsMutex);

auto &[soundId, sound] = *it;

auto uninitSound = [&]()
Expand All @@ -67,6 +88,9 @@ void EffectSound3D::OnRun()
continue;
}

if (!ma_sound_is_playing(&sound.Handle))
ma_sound_start(&sound.Handle);

ma_sound_set_rolloff(&sound.Handle, .1f);

switch (sound.PlayOptions.PlayType)
Expand Down
4 changes: 4 additions & 0 deletions ChaosMod/Components/EffectSound/EffectSound3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <miniaudio.h>
#include <scripthookv/inc/types.h>

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

Expand All @@ -19,6 +20,9 @@ class EffectSound3D : public EffectSoundManager
EffectSoundPlayOptions PlayOptions;
};
std::unordered_map<DWORD64, Sound> m_Sounds;
std::mutex m_SoundsMutex;
bool m_IsStopping = false;
std::thread m_PauseSoundsThread;

public:
EffectSound3D();
Expand Down

0 comments on commit a8958c0

Please sign in to comment.