Game Development Reference
In-Depth Information
DWORD dwStatus;
if( FAILED( hr = m_Sample->GetStatus( &dwStatus ) ) )
return DXUT_ERR( L
GetStatus
,hr);
if( dwStatus & DSBSTATUS_BUFFERLOST )
{
// Since the app could have just been activated, then
// DirectSound may not be giving us control yet, so
// the restoring the buffer may fail.
// If it does, sleep until DirectSound gives us control but fail if
// if it goes on for more than 1 second
int count = 0;
do
{
hr = m_Sample->Restore();
if( hr == DSERR_BUFFERLOST )
Sleep( 10 );
}
while( ( hr = m_Sample->Restore() ) == DSERR_BUFFERLOST &&
++count < 100 );
if( pbWasRestored != NULL )
*pbWasRestored = TRUE;
return S_OK;
}
else
{
return S_FALSE;
}
}
The bug in the method is the termination condition of the do / while loop; it could try
forever, assuming DirectSound was in some wacky state. This could hang your game
and cause your players to curse your name and post all kinds of nasty things on the
Internet. Making the code better depends on what you want to do when this kind of
failure happens. You likely would throw up a dialog box and exit the game. It
s totally
up to you. The lesson here is that just because you grab something directly from a
DirectX sample doesn
'
t mean you should install it into your game unmodified!
The next method is FillBufferWithSound() .Itsjobistocopythesounddatafrom
a sound resource into a prepared and locked sound buffer. There
'
'
s also a bit of code to
handle the special case where the sound resource has no data in that case, the sound
buffer gets filled with silence. Notice that
silence
isn
'
t necessarily a buffer with all zeros.
Search WWH ::




Custom Search