Game Development Reference
In-Depth Information
The check to see if a sound is playing reuses the code that checks if a sound is
playing on a particular channel. The stop sound function uses an OpenAL
method to stop the sound on a certain channel. These new functions can be
tested in the sound state again.
public void Update(double elapsedTime)
{
_count -= elapsedTime;
if (_count < 0)
{
_count = 3;
Sound soundOne = _soundManager.PlaySound("effect");
Sound soundTwo = _soundManager.PlaySound("effect2");
if (_soundManager.IsSoundPlaying(soundOne))
{
_soundManager.StopSound(soundOne);
}
}
}
Here the first sound is told to play, then immediately after there's a check to see if
it is playing. This returns true and another call stops the sound. The first is never
heard as it's always stopped by the end of the game loop.
float _masterVolume = 1.0f;
public void MasterVolume(float value)
{
_masterVolume = value;
foreach (int channel in _soundChannels)
{
Al.alSourcef(channel, Al.AL_GAIN, value);
}
}
In OpenAL the volume can be altered by specifying the gain on a channel. The
gain goes from 0 to 1. Here the master volume is set for every channel. The
volume is also stored in a class variable. When a new sound is played it will
overwrite the current gain setting for the channel so it needs to be reapplied. This
can be done at the bottom of the PlaySound method.
 
Search WWH ::




Custom Search