Fundamentals of Audio and Video Programming for Games (Pro-Developer)

Now things get a bit more complicated. You will want to attach sound buffers to the objects in your game world, and to do this you need to call the getSoundID method. However, first you need to become familiar with soundBufferRef , which is a fairly simple structure.

struct soundBufferRef { WORD iSound; // Sound index. BYTE iBuffer; // Buffer index. };

Each object in your game that you want to attach a sound to must have a soundBufferRef structure. You do not do anything with the values within the structure, because the Concertina framework will fill it in with a call to getSoundID , and then use it in most of the other playing methods .

For example, if you were writing a tank simulation, and had a class containing all the methods and data for a tank, you could add the following array into that class.

soundBufferRef soundReference[eNUM_tanksounds];

Where eNUM_tanksounds comes from an enumeration like the following.

Enum tankSounds { Engine_sound = 0, Tracks_sound, Turrent_rotating_sound, Main_gun_firing_sound, Machine_gun_firing_sound, Squeaky_tracks_sound, eNUM_tanksounds };

Also, you must have set up the bestGuessAudio structure and its associated enumerations with appropriate tank noises.

Then, to fill in the soundReference array, add the following loop.

For (int sound=0; sound< eNUM_tanksounds; sound++) { if (getSoundID(sound,&soundReference[sound]) == false) { // Handle the failure condition here if necessary, but do nothing yet. // Failure to play will be recorded in the Rejections which // you can view later. } }

The soundBufferRef structure is the only one you need from the  Concertina.h source file in your own code.

The actual syntax of getSoundID is as follows .

bool getSoundID(soundIDenum ID,soundBufferRef* sID);

Note that it takes a sound ID (such as ID_prop1 or ID_rain ) as its first parameter, although it might be more efficient to use an integer for this parameter and get the sound IDs in a loop (as in the previous tank example).

The purpose of getting a sound ID is that you can then be sure that your sound will play, as a buffer has been secured for it. This would obviously be important in the tank in which the game player is riding , as you would want to be sure they could hear the sounds of their own tank. However, it may be less critical if a tank somewhere else on the battlefield was unable to secure a sound ID, and you may not know when writing the game exactly how many tanks you will have on the battlefield at once.

For this reason, secure the sound IDs for the most important objects first.

bool clearSoundBuffer(soundBufferRef* sID);

This is used to return the sound buffer to the pool, so it (and obviously its associated buffers) can be used later in the program. For example, this would happen if a tank was destroyed , or left the battlefield for any other reason.

Call the clearSoundBuffer function whenever you are sure that the object will not need to make that noise again.

Finally, there are two housekeeping commands, one to stop a sound buffer from playing, and the other to stop all sounds from playing.

bool stopSoundBuffer(soundBufferRef* sID); void stopAllSounds();

Категории