Files | |
file | audio.c |
Audio output code. | |
file | audio.h |
Interface to audio output. | |
Defines | |
#define | DistSq(p1, p2) |
Computes the square of the distance between two players. | |
#define | MAX_SQ_DIST (27500<<2) |
The maximum squared distance that can be heard by the player. | |
#define | PlayLocalTankMove() PlaySound(SND_MOVE, SND_CH_TANK, -1) |
Plays the sound of the local player's tank moving. | |
#define | PlayTankIdle() PlaySound(SND_IDLE, SND_CH_TANK, -1) |
Plays the sound of the local player's tank idling. | |
#define | PlayTankKilled(distsq) PlaySoundDist(SND_DEATH, -1, distsq, 0) |
Plays the sound of the destruction of a tank other than the local player's tank. | |
#define | PlayTankMove(distsq) PlaySoundDist(SND_MOVE, SND_CH_TANK, distsq, -1) |
Plays the sound of a tank, other than the local player's tank, moving. | |
#define | PlayTankShoot(distsq) PlaySoundDist(SND_SHOOT, -1, distsq, 0) |
Plays the sound of a tank, other than the local player's tank, shooting. | |
Enumerations | |
enum | SND_CHANNEL { SND_CH_LOCALSHOOT, SND_CH_TANK, SND_CH_RESERVE, SND_CH_MAX = 8 } |
The audio channels that are mixed together to produce the final output. More... | |
enum | SND_SOUND { SND_IDLE, SND_MOVE, SND_SHOOT, SND_DEATH, SND_MAX } |
The available sounds. More... | |
Functions | |
Bool | InitAudio () |
Prepares for auido output by requesting output resources and loading in the game sounds. | |
void | PauseSound (int chan) |
Pauses the sound on a specific mixing channel. | |
Bool | PlayLocalDeath () |
Stops all sounds and plays the sound of the local player's tank being destroyed. | |
Bool | PlaySound (int snd, int chan, int count) |
Plays a sound; intended to play sounds caused by actions taken by the local player. | |
Bool | PlaySoundDist (int snd, int chan, int distsq, int count) |
Plays a sound with its volume attenuated according to its distance from the local player. | |
void | StopAllSounds () |
Halts all playing sounds on all channels. | |
void | UninitAudio () |
Stops all sounds and frees all resources needed for audio output. |
The sound setup is intentionally simple because I haven't found much motiviation for this part of the project, but the game does need sound.
SDL_mixer is used for the sound output. Several of SDL_mixer's channels are used to easily support several independent sounds that can play simultaneously. Sound volume drops linearly with distance such that a tank 212 units distant (just off-screen at initial window size) produces sound at half the volumne of something 0 units away.
The audio functions are not critical to gameplay, so if InitAudio() fails, the game will continue. All other audio functions will correctly do nothing if called when audio is not initialized.
#define DistSq | ( | p1, | |||
p2 | ) |
Value:
((int)(((p1)->x - (p2)->x) >> 8) * (int)(((p1)->x - (p2)->x) >> 8) + \ (int)(((p1)->y - (p2)->y) >> 8) * (int)(((p1)->y - (p2)->y) >> 8))
p1 | A pointer to a Player. | |
p2 | A pointer to another Player. |
Definition at line 200 of file audio.h.
Referenced by ReadPlayer().
#define MAX_SQ_DIST (27500<<2) |
The maximum squared distance that can be heard by the player.
The value was selected from what seemed to work well during trials.
Definition at line 107 of file audio.h.
Referenced by ReadPlayer().
#define PlayLocalTankMove | ( | ) | PlaySound(SND_MOVE, SND_CH_TANK, -1) |
#define PlayTankIdle | ( | ) | PlaySound(SND_IDLE, SND_CH_TANK, -1) |
#define PlayTankKilled | ( | distsq | ) | PlaySoundDist(SND_DEATH, -1, distsq, 0) |
Plays the sound of the destruction of a tank other than the local player's tank.
distsq | The square of the distance from the player's tank to the source of the sound. |
Definition at line 191 of file audio.h.
Referenced by ReadPlayer().
#define PlayTankMove | ( | distsq | ) | PlaySoundDist(SND_MOVE, SND_CH_TANK, distsq, -1) |
#define PlayTankShoot | ( | distsq | ) | PlaySoundDist(SND_SHOOT, -1, distsq, 0) |
Plays the sound of a tank, other than the local player's tank, shooting.
distsq | The square of the distance from the player's tank to the source of the sound. |
Definition at line 181 of file audio.h.
Referenced by ReadPlayer().
enum SND_CHANNEL |
The audio channels that are mixed together to produce the final output.
SND_CH_LOCALSHOOT | This channel plays the local player's shooting sound. |
SND_CH_TANK |
A channel for the movement sound of tanks in the game.
The local player is moving, the sound will be SND_SOUND::SND_MOVE at full volumne. If not, and another tank close by is moving, the sound will be SND_SOUND::SND_MOVE attenuated according to distance. Otherwise, the sound will be SND_SOUND::SND_IDLE. |
SND_CH_RESERVE | The number of reserved channels. |
SND_CH_MAX |
The total number of mixing channels.
The additional channels used beyond the reservered channels play the sounds of shots and kills of tanks other than the local player. |
enum SND_SOUND |
Bool InitAudio | ( | ) |
Prepares for auido output by requesting output resources and loading in the game sounds.
Referenced by main().
void PauseSound | ( | int | chan | ) |
Pauses the sound on a specific mixing channel.
Intended to pause the sound of tanks moving in the SND_CHANNEL::SND_CH_TANK channel.
Bool PlayLocalDeath | ( | ) |
Stops all sounds and plays the sound of the local player's tank being destroyed.
Referenced by ReadPlayer().
Bool PlaySound | ( | int | snd, | |
int | chan, | |||
int | count | |||
) |
Plays a sound; intended to play sounds caused by actions taken by the local player.
snd | The sound from the SND_SOUND enum to play. | |
chan | The channel on which to play the sound, from the SND_CHANNEL enum. A specific channel must be specified; -1 is not accepted. | |
count | The number of times to repeat the sound, or -1 to loop forever. |
Referenced by HandleInput().
Bool PlaySoundDist | ( | int | snd, | |
int | chan, | |||
int | distsq, | |||
int | count | |||
) |
Plays a sound with its volume attenuated according to its distance from the local player.
This should not be used for sounds corresponding to the local player.
snd | The sound from the SND_SOUND enum to play. | |
chan | The channel on which to play the sound, from the SND_CHANNEL enum, or -1 to select a free channel. | |
distsq | The square of the distance from the player's tank to the source of the sound. | |
count | The number of times to repeat the sound, or -1 to loop forever. If -1, then the specific channel must be specified; chan must not be -1. |
void StopAllSounds | ( | ) |
void UninitAudio | ( | ) |