Game Development Reference
In-Depth Information
At this point, we need to go back and add a line of code into the constructor to call
this new InitDirectSound() method that we have created. For that, we simply
add the following if statement to the end of the constructor:
if (m_UseDirectSound)
InitDirectSound();
We are not quite finished though. We still need to dispose of our DirectSound ob-
jects when we are done with them. So, add the following code into the managed
section of our Dispose(bool) method:
if (m_DSoundBuffer != null)
m_DSoundBuffer.Dispose();
if (m_DSoundPrimaryBuffer != null)
m_DSoundPrimaryBuffer.Dispose();
if (m_DirectSound != null)
m_DirectSound.Dispose();
With that, our DirectSound code is done. If you run the program now, you should no-
tice music playing in our 2D tile-based world. You may also notice that the music will
pause if the window loses focus. If you then click on the window again to give it the
focus, the music will start back up where it left off.
Weshouldalsotalkaboutthesoundbuffer's Status property,whichyoucanaccess
to find out the current status of your sound buffer. For example, you can use this to
check if the sound is currently playing or looping, among other things.
Volume control
In this section of code, we mentioned several of the buffer flags, and we set
the BufferFlags.ControlVolume flag on our SecondarySoundBuffer object,
which tells DirectSound that we want the capability to change the volume of the
sound. We didn't actually mess with this, but to change the volume, you would simply
change the value of the sound buffer's Volume property. In DirectSound, volume is
Search WWH ::




Custom Search