Table of Contents
- Initializing Allegro
- Keyboard Input
- Drawing Graphics
- Playing Sound
- Drawing Text
- Regulating FPS
- Enumerations and Random Numbers
- Bounding Box Collision Detection
- Planning the Game
- Super Basic Game Structure
- Wrapping up
Part 5 – Playing Sound
Most games have sound and music. Without it, many games would seem unpolished. You can find public domain sound effects and music by doing a Google search.
Usually, wave files are sound effects and midi files should be the background music since wave takes up a lot of room.
Allegro code for wave files
You can also find other libraries for use with Allegro to let you load in other formats of sound, but on it’s own Allegro just loads wave and midi files.
Creating a sample
‘Sample’ types are used for wave files in Allegro.
Loading in the sample
Playing the sample
The parameters for play_sample are:
play_sample(sample, volume, pan, frequency, loop)
(taken from Allegro Quick Reference)
Volume ranges from 0 to 255.
Pan is how much sound is played on the left/right speakers. 128 means equally (or “center”, 0 is left speaker, 255 is rights speaker).
Frequency is playback rate, with 1000 being normal speed. 500 will be half speed and 2000 will be double speed.
If the loop parameter is true, the sound will continue to loop until
Destroying the sample
Always destroy your sounds afterwards!
Allegro code for midi files
Creating a midi
Midis are similar to samples:
Loading in the midi
Playing the midi
First parameter is the midi file, and second one is whether it loops or not.
Destroying the midi
© Rachel J. Morris, 2009