Engine
The Game Engine for Ian Parberry's "Introduction to Game Physics with Box2D"
|
The sound manager. More...
#include <Sound.h>
Public Member Functions | |
CSoundManager (HWND hwnd) | |
Constructor. | |
~CSoundManager () | |
Destructor. | |
void | LoadSettings () |
Load settings from XML document. | |
void | LoadSounds (int level=0) |
Load sounds from list in XML document. | |
void | beginframe (void) |
To be called at start of frame. | |
void | clear () |
Clear all sounds from buffers. | |
void | load (char *filename, int copies=1) |
Load sound from file. | |
void | play (int index, BOOL looping=FALSE) |
Play sound. | |
void | stop (int index) |
Stop playing sound. | |
void | stop (void) |
Stop playing all sounds. | |
The sound manager.
The sound manager lets you play multiple overlapping copies of sounds simultaneously. It reads settings from the XML settings file, including a list of file names to be loaded. It can load WAV format sounds. The code is currently written so that all sound files must have the same bit depth and sample rate. Multiple copies of sounds are made by sharing the sound data, to save memory.
CSoundManager::CSoundManager | ( | HWND | hwnd | ) |
Constructor.
CSoundManager constructor. Sets member variables to sensible values and starts DirectSound. Member variable m_bOperational is set to TRUE is it is able to start DirectSound and set the cooperative level correctly.
CSoundManager::~CSoundManager | ( | ) |
Destructor.
CSoundManager destructor. Reclaims all dynamic memory and releases DirectSound.
void CSoundManager::beginframe | ( | void | ) |
To be called at start of frame.
Record the start of a new animation frame by resetting the array m_bStartedThisFrame. If two copies of the same sound start at exactly the same time (meaning in the same frame of animation), then the two sounds reinforce each other. That is, the sound simply plays twice as loud. m_bStartedThisFrame is used to prevent this from happening. If a sound has already been started, it cannot be started again until this function has been called.
void CSoundManager::clear | ( | ) |
Clear all sounds from buffers.
Clear the sound manager. Return the sound manager to a pristine state by releasing and deleting all sound buffers, then deletie all dynamic arrays.
void CSoundManager::load | ( | char * | filename, |
int | copies = 1 |
||
) |
Load sound from file.
Load a sound from a file into a sound buffer. Loads a sound from a file into a temporary buffer. Creates a DirectSound sound buffer of the right size and copies it over. Creates a number of copies of the sound so that we can play overlapping copies of the sound.
filename | name of file to be loaded |
copies | number of copies of sound that can be played simultaneously |
void CSoundManager::LoadSettings | ( | ) |
Load settings from XML document.
Load the basic sound settings from g_xmlSettings. Looks for a <sound> tag with attributes cps, bps, and rate.
void CSoundManager::LoadSounds | ( | int | level = 0 | ) |
Load sounds from list in XML document.
Load the sound files for a given level from the xml file list in g_xmlSettings. Search for a <sounds> tag with a "level" attribute equal to the parameter. Process sound file names in <sound> tags within the <sounds></sounds> pair. Starts by counting the number of sound files needed, and creating arrays of the right size.
level | sounds are to be loaded for this level |
void CSoundManager::play | ( | int | index, |
BOOL | looping = FALSE |
||
) |
Play sound.
If the sound has multiple copies, locates a copy that is not currently playing, and plays it. If there is no copy available, nothing happens.
index | index of sound to be played |
looping | TRUE if sound is to be looped |
void CSoundManager::stop | ( | int | index | ) |
Stop playing sound.
Stop all copies of a sound from playing, and reset them back to the start of the sound so that they start properly next time they are played.
index | index of sound to stop |
void CSoundManager::stop | ( | void | ) |
Stop playing all sounds.
Stop all sounds from playing. It simply loops through all sounds and calls the other stop function to do all the work.