Game Development Reference
In-Depth Information
Most of the previous code has a similar structure and is a lightweight wrapper
around IDirectSoundBuffer8 . The first few lines check to see if the audio system
is running, the audio buffer has been initialized, and parameters have reasonable
values. Take note of the VSetVolume method; it has to renormalize the volume
value from 0
100 to a range compatible with DirectSound, and it does so with a log-
arithmic scale, since sound intensity is logarithmic in nature.
The last three methods in this class are a little trickier, so I
-
ll give you a little more detail
on them. The first, VRestore() , is called to restore sound buffers if they are ever lost.
If that happens, you have to restore it with some DirectSound calls and then fill it with
sound data again
'
t get restored with its data intact. The VRestore() method
calls RestoreBuffer() to restore the sound buffer, and if that is successful, it calls
FillBufferWithSound() to put the sound data back where it belongs.
it doesn
'
bool DirectSoundAudioBuffer::VRestore()
{
HRESULT hr;
BOOL
bRestored;
// Restore the buffer if it was lost
if( FAILED( hr = RestoreBuffer( &bRestored ) ) )
return NULL;
if( bRestored )
{
// The buffer was restored, so we need to fill it with new data
if( FAILED( hr = FillBufferWithSound( ) ) )
return NULL;
}
return true;
}
This implementation of RestoreBuffer() is pretty much lifted from the Direct-
Sound samples. Hey, at least I admit to it! If you
'
'
re paying attention, you
ll notice
an unfortunate bug in the code see if you can find it:
HRESULT DirectSoundAudioBuffer::RestoreBuffer( BOOL* pbWasRestored )
{
HRESULT hr;
if( m_Sample )
return CO_E_NOTINITIALIZED;
if( pbWasRestored )
*pbWasRestored = FALSE;
Search WWH ::




Custom Search