Adding Sound


Driving a tank around, shooting these bullets off, and yet never hearing a sound would be somewhat strange, I believe you'd agree. Although you don't focus on any in-depth sound functionality for this game (you do some more for the next game), unlike in Blockers, having no sound at all would be terrible. Besides, it's the perfect opportunity to introduce the basic sound concepts now.

First, you add a reference to the sound libraries to your project. In the Add References dialog, add the reference to Microsoft.DirectX.DirectSound.dll now.

In keeping with the theme, create a new code file called soundengine.cs and add it to your project file as well. It will be the main source of sound for your game. Look at the basic implementation in Listing 15.11.

Listing 15.11. The Sound Engine
 using System; using System.Drawing; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.DirectX.DirectSound; namespace Tankers {     /// <summary>     /// This class will maintain the sound engine for this game     /// </summary>     public class SoundEngine : IDisposable     {         private const string FireFile = @"sounds\fire.wav";         private const string ExplosionFile = @"sounds\hit.wav";         private Device soundDevice = null;         private SecondaryBuffer fireBuffer = null;         private SecondaryBuffer hitBuffer = null;         private bool canUseSounds = true;         /// <summary>         /// Done with this object, clean it up         /// </summary>         public void Dispose()         {             // Clean up all data here             if (fireBuffer != null)             {                 fireBuffer.Dispose();             }             if (hitBuffer != null)             {                 hitBuffer.Dispose();             }             if (soundDevice != null)             {                 soundDevice.Dispose();             }         }     } } 

The constants define where the sound files are loaded from. These particular sound files come from the DirectX SDK, but you can simply get them from the included CD. Think of the Device object as your sound card. It is the base object that is required to play any sounds. The two buffers are the actual sound data that is loaded. Because it's possible for the computer you're using to not support sounds (for example, maybe it has no sound card), you want to know whether sounds should be played. Not having a sound card or being able to play sounds shouldn't require the game to be exited (it's a nonfatal error), so just store that information.

Naturally, you need to clean up your sound objects as well. Because the sound object is created globally, you can simply dispose the buffers and the device at the same time. Look at the constructor in Listing 15.12 and add it to this class now.

Listing 15.12. Creating the Sound Engine
 /// <summary> /// Constructor where the device will be created. /// </summary> public SoundEngine(IntPtr parent) {     try     {         // Create a default sound object         soundDevice = new Device();         // Set the coop level         soundDevice.SetCooperativeLevel(parent, CooperativeLevel.Normal);         // Now create the two buffers         fireBuffer = new SecondaryBuffer(GameEngine.MediaPath +                                          FireFile, soundDevice);         hitBuffer = new SecondaryBuffer(GameEngine.MediaPath +                                         ExplosionFile, soundDevice);     }     catch     {         // We cannot use sounds for some reason         canUseSounds = false;     } } 

Here the default sound device is created first. Other overloads for creating a sound device take a Guid that uniquely identifies the device you're wanting to use, but for now, the default device should be good enough. (See the DirectX documentation for more information on creating devices.) Just as you had to do for your DirectInput device, you are required to call the SetCooperativeLevel on the device. In most cases, you want to use Normal, as you do here.

Creating the sound buffers is amazingly easy, as you can see. Simply pass in the device and the filename, and you're all set. You could use much more complicated options, but they aren't required for this example. In the next game, you'll have more in-depth sound features.

Luckily, playing sounds is just as easy. Add the two methods in Listing 15.13 to your class to control the playing of these sounds you've created.

Listing 15.13. Playing Sounds
 /// <summary> /// Will play the fire weapon sound /// </summary> public void FireWeapon() {     if ((canUseSounds) && (fireBuffer != null))     {         fireBuffer.Play(0, BufferPlayFlags.Default);     } } /// <summary> /// Will play the explosion sound /// </summary> public void Explosion() {     if ((canUseSounds) && (hitBuffer != null))     {         hitBuffer.Play(0, BufferPlayFlags.Default);     } } 

Simple, just as I promised! Assuming the sounds can be played, you just call Play, passing in 0 (start at the beginning), and play with the Default flags (play once and stop).



Beginning 3D Game Programming
Beginning 3D Game Programming
ISBN: 0672326612
EAN: 2147483647
Year: 2003
Pages: 191
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net