pause music and ambient sounds when game pauses

This was SVN commit r13347.
This commit is contained in:
stwf 2013-04-07 04:13:15 +00:00
parent 555f06acbe
commit c9a5d5cee5
7 changed files with 79 additions and 1 deletions

View File

@ -19,7 +19,7 @@
#include "Globals.h"
#include "lib/external_libraries/libsdl.h"
#include "soundmanager/SoundManager.h"
bool g_app_minimized = false;
bool g_app_has_focus = true;
@ -68,9 +68,15 @@ InReaction GlobalsInputHandler(const SDL_Event_* ev)
#else
case SDL_ACTIVEEVENT:
if(ev->ev.active.state & SDL_APPACTIVE)
{
g_app_minimized = (ev->ev.active.gain == 0); // negated
g_SoundManager->Pause( g_app_minimized );
}
if(ev->ev.active.state & SDL_APPINPUTFOCUS)
{
g_app_has_focus = (ev->ev.active.gain != 0);
g_SoundManager->Pause( !g_app_has_focus );
}
if(ev->ev.active.state & SDL_APPMOUSEFOCUS)
g_mouse_active = (ev->ev.active.gain != 0);
return IN_PASS;

View File

@ -53,6 +53,7 @@
#include "renderer/Renderer.h"
#include "scriptinterface/ScriptInterface.h"
#include "simulation2/Simulation2.h"
#include "soundmanager/SoundManager.h"
// rationale: the function table is now at the end of the source file to
// avoid the need for forward declarations for every function.
@ -340,6 +341,7 @@ JSBool SetPaused(JSContext* cx, uintN argc, jsval* vp)
try
{
g_Game->m_Paused = ToPrimitive<bool> (JS_ARGV(cx, vp)[0]);
g_SoundManager->Pause(g_Game->m_Paused);
}
catch (PSERROR_Scripting_ConversionFailed)
{

View File

@ -270,6 +270,10 @@ CSoundManager::CSoundManager()
m_BufferCount = 50;
m_BufferSize = 65536;
m_MusicEnabled = true;
m_MusicPaused = false;
m_AmbientPaused = false;
m_ActionPaused = false;
m_DistressTime = 0;
m_DistressErrCount = 0;
@ -649,5 +653,39 @@ void CSoundManager::SetAmbientItem(ISoundItem* anItem)
AL_CHECK
}
void CSoundManager::Pause(bool pauseIt)
{
PauseMusic(pauseIt);
PauseAmbient(pauseIt);
PauseAction(pauseIt);
}
void CSoundManager::PauseMusic (bool pauseIt)
{
if (m_CurrentTune && pauseIt)
m_CurrentTune->Pause();
else if ( m_CurrentTune )
m_CurrentTune->Resume();
m_MusicPaused = pauseIt;
}
void CSoundManager::PauseAmbient (bool pauseIt)
{
if (m_CurrentEnvirons && pauseIt)
m_CurrentEnvirons->Pause();
else if ( m_CurrentEnvirons )
m_CurrentEnvirons->Resume();
m_AmbientPaused = pauseIt;
}
void CSoundManager::PauseAction (bool pauseIt)
{
m_ActionPaused = pauseIt;
}
#endif // CONFIG2_AUDIO

View File

@ -71,6 +71,10 @@ protected:
bool m_MusicEnabled;
bool m_SoundEnabled;
bool m_MusicPaused;
bool m_AmbientPaused;
bool m_ActionPaused;
long m_DistressErrCount;
long m_DistressTime;
@ -122,6 +126,11 @@ public:
void SetDistressThroughShortage();
void SetDistressThroughError();
void Pause(bool pauseIt);
void PauseMusic (bool pauseIt);
void PauseAmbient (bool pauseIt);
void PauseAction (bool pauseIt);
protected:
void InitListener();
virtual Status AlcInit();

View File

@ -381,5 +381,23 @@ CStrW* CSoundBase::GetName()
return NULL;
}
void CSoundBase::Pause()
{
if (m_ALSource != 0)
{
alSourcePause(m_ALSource);
AL_CHECK
}
}
void CSoundBase::Resume()
{
if (m_ALSource != 0)
{
alSourcePlay(m_ALSource);
AL_CHECK
}
}
#endif // CONFIG2_AUDIO

View File

@ -89,6 +89,8 @@ public:
virtual void FadeAndDelete(double fadeTime);
virtual bool SoundStale();
void Pause();
void Resume();
protected:
void SetNameFromPath(VfsPath& itemPath);

View File

@ -64,6 +64,9 @@ public:
virtual void SetGain(ALfloat gain) = 0;
virtual void SetLocation(const CVector3D& position) = 0;
virtual void SetRollOff(ALfloat gain) = 0;
virtual void Pause() = 0;
virtual void Resume() = 0;
};
#endif // CONFIG2_AUDIO