The sound of music

We now turn to the audio soundtrack for our game. Just like a movie soundtrack, the music that is played during a game sets the tone for the game. Many games have huge, orchestrated productions, while others have synthesized or 8-bit music.

As we have already discussed, music files are handled in a different manner from sound effects. This is because sound effects are usually very short sounds that can be best stored as wav files. Music files tend to be much longer, and are stored as MP3 files because the data can be compressed, taking less storage and less memory.

We are going to add a single music track to our game. To keep things simple, we will tell the track to loop so that it runs continuously throughout the game.

We will start by adding a sound pointer. Open RoboRacer2D.cpp and add the following line of code to the variable declarations:

FMOD::Sound* musBackground;

Next, go to the LoadAudio function and add the following line:

result = audiomgr->createSound("resources/jollybot.mp3", FMOD_LOOP_NORMAL | FMOD_2D | FMOD_HARDWARE, 0, &musBackground);
FMOD::Channel* channel;
result = audiomgr->playSound(FMOD_CHANNEL_FREE, musBackground, false, &channel);

Notice that we use createStream instead of createSound to load our music file. As music is so much longer than sound effects, music is streamed from storage rather than loaded directly into memory.

We want the sound track to start when the game starts, so we start playing the music in right after it is loaded using playSound.

That's all there is to it! Our game is now enhanced by a vibrant soundscape.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.213.235