Enabling sound in-game works even for engines / looped sounds now

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@6255 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
auria 2010-10-11 20:26:31 +00:00
parent 7434604183
commit 12cb80f9d0
5 changed files with 56 additions and 7 deletions

View File

@ -57,6 +57,7 @@ public:
virtual void volume(float gain) = 0;
virtual SFXManager::SFXStatus
getStatus() = 0;
virtual void onSoundEnabledBack() = 0;
}; // SfxBase

View File

@ -93,6 +93,8 @@ public:
bool isPositional() const { return m_positional; }
float getRolloff() const { return m_rolloff; }
float getGain() const { return m_gain; }
std::string getFileName() const { return m_file; }
};

View File

@ -113,6 +113,12 @@ void SFXManager::soundToggled(const bool on)
}
resumeAll();
const int sfx_amount = m_all_sfx.size();
for (int n=0; n<sfx_amount; n++)
{
m_all_sfx[n]->onSoundEnabledBack();
}
}
else
{

View File

@ -41,7 +41,9 @@ SFXOpenAL::SFXOpenAL(SFXBuffer* buffer, bool positional, float gain) : SFXBase()
m_ok = false;
m_positional = positional;
m_defaultGain = gain;
m_loop = false;
m_gain = -1.0f;
// Don't initialise anything else if the sfx manager was not correctly
// initialised. First of all the initialisation will not work, and it
// will not be used anyway.
@ -55,7 +57,10 @@ SFXOpenAL::SFXOpenAL(SFXBuffer* buffer, bool positional, float gain) : SFXBase()
SFXOpenAL::~SFXOpenAL()
{
alDeleteSources(1, &m_soundSource);
if (m_ok)
{
alDeleteSources(1, &m_soundSource);
}
} // ~SFXOpenAL
//-----------------------------------------------------------------------------
@ -79,12 +84,21 @@ bool SFXOpenAL::init()
alSource3f(m_soundSource, AL_VELOCITY, 0.0, 0.0, 0.0);
alSource3f(m_soundSource, AL_DIRECTION, 0.0, 0.0, 0.0);
alSourcef (m_soundSource, AL_ROLLOFF_FACTOR, m_soundBuffer->getRolloff());
alSourcef (m_soundSource, AL_GAIN, m_defaultGain);
if (m_gain < 0.0f)
{
alSourcef (m_soundSource, AL_GAIN, m_defaultGain);
}
else
{
alSourcef (m_soundSource, AL_GAIN, m_gain);
}
if (m_positional) alSourcei (m_soundSource, AL_SOURCE_RELATIVE, AL_FALSE);
else alSourcei (m_soundSource, AL_SOURCE_RELATIVE, AL_TRUE);
alSourcei(m_soundSource, AL_LOOPING, m_loop ? AL_TRUE : AL_FALSE);
m_ok = SFXManager::checkError("setting up the source");
return m_ok;
@ -117,6 +131,8 @@ void SFXOpenAL::speed(float factor)
*/
void SFXOpenAL::volume(float gain)
{
m_gain = m_defaultGain * gain;
if(!m_ok) return;
alSourcef(m_soundSource, AL_GAIN, m_defaultGain * gain);
@ -128,6 +144,8 @@ void SFXOpenAL::volume(float gain)
*/
void SFXOpenAL::setLoop(bool status)
{
m_loop = status;
if(!m_ok) return;
alSourcei(m_soundSource, AL_LOOPING, status ? AL_TRUE : AL_FALSE);
@ -141,6 +159,7 @@ void SFXOpenAL::stop()
{
if(!m_ok) return;
m_loop = false;
alSourcei(m_soundSource, AL_LOOPING, AL_FALSE);
alSourceStop(m_soundSource);
SFXManager::checkError("stoping");
@ -162,8 +181,15 @@ void SFXOpenAL::pause()
*/
void SFXOpenAL::resume()
{
if(!m_ok) return;
if (!m_ok)
{
// lazily create OpenAL source when needed
init();
// creation of OpenAL source failed, giving up
if (!m_ok) return;
}
alSourcePlay(m_soundSource);
SFXManager::checkError("resuming");
} // resume

View File

@ -36,11 +36,24 @@
class SFXOpenAL : public SFXBase
{
private:
SFXBuffer* m_soundBuffer; // Buffers hold sound data.
ALuint m_soundSource; // Sources are points emitting sound.
SFXBuffer* m_soundBuffer; //!< Buffers hold sound data.
ALuint m_soundSource; //!< Sources are points emitting sound.
bool m_ok;
bool m_positional;
float m_defaultGain;
/** The OpenAL source contains this info, but if audio is disabled initially then
the sound source won't be created and we'll be left with no clue when enabling
sounds later */
bool m_loop;
/** Contains a volume if set through the "volume" method, or a negative number if
this method was not called.
The OpenAL source contains this info, but if audio is disabled initially then
the sound source won't be created and we'll be left with no clue when enabling
sounds later. */
float m_gain;
public:
SFXOpenAL(SFXBuffer* buffer, bool positional, float gain);
virtual ~SFXOpenAL();
@ -57,6 +70,7 @@ public:
virtual void position(const Vec3 &position);
virtual void volume(float gain);
virtual SFXManager::SFXStatus getStatus();
virtual void onSoundEnabledBack() { if (m_loop) resume(); }
}; // SFXOpenAL