Tricks of the Windows Game Programming Gurus:Advanced Windows Programming

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

Of course, the symbolic constants would have to be defined elsewhere in an `.H` file, but you knew that!

At this point, we run into a little snag: `WAVE` resources are a little more complex than cursors, icons, and string tables. The problem is, to load them in takes a lot more programming than the other resources, so I’m going to hold off on showing you the way to load `.WAV` resources in a real game until later. For now, I’m just going to show you a trick to load and play a `.WAV` on-the-fly using the `PlaySound()` function. Here’s its prototype:

    BOOL PlaySound(LPCSTR pszSound, // string of sound to play

                   HMODULE hmod,    // instance of application

                   DWORD fdwSound); // flags parameter

Unlike `LoadString()`, `PlaySound()` is a little more complex, so let’s take a closer look at each of the parameters:

  • `PszSound`—This parameter is either the string name of the sound resource in the resource file or a filename on disk. Also, you can use the `MAKEINTRESOURCE()` and use a `WAVE` that is defined with a symbolic constant.
  • `Hmod`—The instance of the application to load the resource from. This is simply the `hinstance` of the application.
  • `FdwSound`—This is the clincher. This parameter controls how the sound is loaded and played. Table 3.1 contains a list of the most useful values for `FdwSound`.

Table 3.1 Values for the `FdwSound` Parameter of `PlaySound()`

Value | Description
--- | ---

`SND_FILENAME` | The `pszSound` parameter is a filename.
`SND_RESOURCE` | The `pszSound` parameter is a resource identifier; `hmod` must identify the instance that contains the resource.
`SND_MEMORY` | A sound event’s file is loaded in RAM. The parameter specified by `pszSound` must point to an image of a sound in memory.
`SND_SYNC` | Synchronous playback of a sound event. `PlaySound()` returns after the sound event is completed.
`SND_ASYNC` | The sound is played asynchronously, and `PlaySound()` returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call `PlaySound()` with `pszSound` set to `NULL`.
`SND_LOOP` | The sound plays repeatedly until `PlaySound()` is called again with the `pszSound` parameter set to `NULL`. You must also specify the `SND_ASYNC` flag to indicate an asynchronous sound event.
`SND_NODEFAULT` | No default sound event is used. If the sound cannot be found, `PlaySound()` returns silently without playing the default sound.
`SND_PURGE` | Sounds are to be stopped for the calling task. If `pszSound` is not `NULL`, all instances of the specified sound are stopped. If `pszSound` is `NULL`, all sounds that are playing on behalf of the calling task are stopped.
`SND_NOSTOP` | The specified sound event will yield to another sound event that is already playing. If a sound cannot be played because the resource needed to generate that sound is busy playing another sound, the function immediately returns `FALSE` without playing the requested sound.
`SND_NOWAIT` | If the driver is busy, the function returns immediately without playing the sound.

To play a `WAVE` sound resource with `PlaySound()`, there are four general steps:

  1. Create the `.WAV` file itself and store it on disk.
  2. Create the `.RC` resource script and associated `H` file.
  3. Compile the resources along with your program code.
  4. In your program, make a call to `PlaySound()` with either the `WAVE` resource name or the `WAVE` resource ID using the `MAKEINTRESOURCE()` macro.

Let’s see some examples, shall we? Let’s begin with a general `RC` file that has two sounds: one with a string name and the other with a symbolic constant. Let’s name them `RESOURCE.RC` and `RESOURCE.H`. The files would look something like this:

The `RESOURCE.H` file would contain

    #define SOUND_ID_ENERGIZE 1

The `RESOURCE.RC` file would contain

    #include "RESOURCE.H"

    // first the string name defined sound resource

    Telporter WAVE teleport.wav

    // and now the symbolically defined sound

    SOUND_ID_ENERGIZE WAVE energize.wav

Within your program, here’s how you would play the sounds in different ways:

    // to play the telport sound asynchronously

    PlaySound("Teleporter", hinstance,

               SND_ASYNC | SND_RESOURCE);

    // to play the telport sound asynchronously with looping

    PlaySound("Teleporter", hinstance,

                SND_ASYNC | SND_LOOP | SND_RESOURCE);

    // to play the energize sound asynchronously

    PlaySound(MAKEINTRESOURCE(SOUND_ID_ENERGIZE), hinstance,

              SND_ASYNC | SND_RESOURCE);

    // and if you simply wanted to play a sound off disk

    // directly then you could do this

    PlaySound("C:\path\filename.wav", hinstance,

              SND_ASYNC | SND_FILENAME);

And to stop all sounds, use the `SND_PURGE` flag with `NULL` as the sound name, like this:

    // stop all sounds

    PlaySound(NULL, hinstance, SND_PURGE);

Obviously, there are myriad flags options that you should feel free to experiment with. Anyway, you don’t have any controls or menus yet, so it’s hard to interact with the demo applications. However, as a simple demo of using sound resources, I have created `DEMO3_2.CPP`, which you can find on the disk. I would list it here, but 99 percent of it is just the standard template you have been using, and the sound code is nothing more than a couple lines of code identical to the earlier examples. The demo is precompiled, and you can run `DEMO3_2.EXE` yourself to see what it does.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.