Sound effects

Yesterday, the 5th January I managed to create sound effects.

Since we will use audio like everywhere in our game I decided to initialize the sounds in main.cpp. It could also be initialized in engine but I choosed main for the simplicity, right now main is holding all the important initializes which I think is good practice.
A class that manage SDL-only initializes could have been made but for this project I do not see why this shouldn’t be enough.

If you want to play a sound you can just do this:

SoundManager::get(“NAME_OF_SOUND”)->play(6);

This will play NAME_OF_SOUND 6 times and if you put nothing as a parameter, it will play only once. And if you put -1 it will play forever until you stop it which you can do with ->stop();

I add the sounds this way:
SoundManager::addSound(“NAME_OF_SOUND”, “MY_SOUND.WAV”);

And for the directory path I have a configuration node in my config file that says:
sound_path = ../assets/audio/sounds/

And then in my initialize code:
SoundManager::setSoundDir(Config::get(“sound_path”));

Easy peasy.

Also today in the morning I made the music class come to life.
It was almost identical to the sound class but it can ONLY play one file per time.

About Anthon Fredriksson

2013 Programming