Artifact : SoundManager

What?
The soundmanager is an essential part of the game. and required to play both sound effect and music. It allows for in the code easily use the soundmanager and call its play function with the given path parameter. if such a sound has already been played it will simply play the sound. otherwise it will add the sound before playing it.  more details on this in the next section with how it was made.

How?

The SoundManager I’ve created is done by having two Structs, one for handling the sound effects while the other one handles the music tracks. The Music and Sound Struct differs a little bit, due to how SFML handles sound and music. The Sound Struct keeps tracks on the Path to the sound file, a sf::SoundBuffer that contains the sound and the sf::Sound itself. This is as the Sound actually stores the sound effect inside the buffer that was created. The Music Struct however does not have the buffer, so it contains the Path to the file and the sf::Music. This is because the way that SFML uses the music is that it Streams the data that should be played directly from the path. and never stores it.

These are then turned into vectors that i will call SoundBank and MusicBank

struct SoundsStruct
{
string path;
sf::Sound* soundData;
sf::SoundBuffer* buffer;
};
struct MusicStruct
{
string path;
sf::Music* soundHandle;
};

vector m_SoundBank;
vector m_MusicBank;

PlaySound

By having these banks sorted, it will be easy to go through what sound has already been used before. by simply comparing the path with the vector. and when its not found. It will then go over to the AddSound or AddMusic part of the play function.  This is done with a new kind of for loop I just learned how to use, the syntax is shown in the picture above and it will go through every sound vector inside the soundbank. So it’s  a For loop and an Iterator at the same time. Turned out very handy in a situation like this.  addsoundss

Why?

By having this sound manager the sound effects will easily be stored and reused rather than loading them in every time they are called. It also makes using sound where you want to use it much easier. It now takes one line of code, well assuming the sound manager is already initialized where the you want the sound to occur. as it would just be as simple as this :

Soundmanager->PlaySound(“song.mp3″);

Very easy to use and fail safe friendly as it will automaticly add not loaded sounds already.