Game Development Reference
In-Depth Information
// Unlock the buffer, we don
t need it anymore.
m_Sample->Unlock( pDSLockedBuffer, dwDSLockedBufferSize, NULL, 0 );
'
return S_OK;
}
There ' s also some special case code that handles the case where the DirectSound
buffer is longer than the sound data
any space left over is filled with silence.
There
'
s one last method to implement
in the IAudioBuffer interface,
the
VGetProgress() method:
float DirectSoundAudioBuffer::VGetProgress()
{
LPDIRECTSOUNDBUFFER pDSB = (LPDIRECTSOUNDBUFFER)VGet();
DWORD progress = 0;
pDSB->GetCurrentPosition(&progress, NULL);
float length = (float)m_Resource->Size();
return (float)progress / length;
}
This useful little routine calculates the current progress of a sound buffer as it is
being played. Sound plays at a constant rate, so things like music and speech will
sound exactly as they were recorded. It
s up to you, the skilled programmer, to get
your game to display everything exactly in sync with the sound. You do this by poll-
ing the sound effect
'
'
s progress when your game is about to start or change an
animation.
Perhaps you have an animation of a window cracking and then shattering. You
d
launch the sound effect and animation simultaneously, call VGetProgress() on
your sound effect every frame, and set your animation progress accordingly. This is
especially important because players can detect even tiny miscues between sound
effects and animation.
'
Sound Processes
All of the classes you
'
ve seen so far form the bare bones of an audio system for a
computer game. What
s missing is some way to launch and monitor a sound effect
as it is playing, perhaps to coordinate it with an animation. If you paid some atten-
tion in Chapter 7, you
'
'
ll remember the Process class. It turns out to be perfect for
this job.
class SoundProcess : public Process
{
 
 
Search WWH ::




Custom Search