- [Instructor] Welcome to video
Building the SoundManager Class
and Adding it to Game Engine.
In the previous video, we got to know
about spatialization and SFML.
In this video, we're going to take a look
at coding SoundManager.h file, coding SoundManager.cpp file,
and adding sound manager to the game engine.
In the previous project, all the sound code
took up quite a few lines of code.
Now consider that with spatialization
its going to get longer still.
To keep our code manageable, we will code a class
to manage the playing of all of our sound effects.
In addition, to help us with spatialization,
we will add a function to the engine class as well,
but we will discuss that when we come to
it later in the section.
Let's get started by coding and examining the header file.
Right click header files in the solution explorer
and select add new item.
In the add new item window click Header File
and then in the name field, type SoundManager.
Finally, click add.
We are now ready to code the header file
for the SoundManager class.
Add this highlighted code to the file.
There's nothing tricky in the code we just added.
There are five sound buffer object and seven sound objects.
Three of the sound objects will play the same sound buffer.
That is m_Fire1Sound, m_Fire2Sound, and m_Fire3Sound.
This explains the reason for the different number
of sound, sound buffer objects.
We do this so that we can have
multiple roaring sound objects playing
with different spatialized parameters simultaneously.
Notice there is the m_NextSound variable
that will help us keep track of
which of these simultaneous sounds we should use next.
For now, we have set m_NextSound to one.
There is a constructor SoundManager
where we will sub all of our sound effects
and these are the five functions
that will play the sound effects.
Four of these functions simply play normal sound effects
and they're code will be really simply.
One of the functions, playfire, will handle
the spatialized sound effects
and will be a bit more in depth.
Notice the parameters of the playfire function.
If receives a Vector2f, which is the location of the emitter
and the second Vector2f, which is
the location of the listener.
Moving on to coding the SoundManager.cpp file.
We can code the function definitions.
The constructor and the playfire functions
will have a fair amount of code,
so we'll look at them individually.
The other functions will be short and sweet,
so we will handle them all at once.
So first, right click source files in the Solution Explorer.
And click add new item.
In the add new item window, click C++ file
and then in the name field, type SoundManager.
Lastly click add.
We can now code the .cpp file for the SoundManager class.
First, let's see coding the constructor.
Add this code for the include directives
and the constructor to the file.
You have to add this entire highlighted code.
Now, let's go through it.
In this code, we load five sound files
into the five sound buffer objects.
Next, we associated the seven sound objects
with one of the sound buffer objects.
Notice that m_Fire1Sound, m_Fire2Sound,
and m_Fire3Sound are all going to be playing
from the same sound buffer, m_FireBuffer.
Next, we set the minimum distance value as 150
and attenuation as 15.
Then we set attenuation for the three fire sounds.
Next, we set minimum distance for the three fire sounds.
Finally, for the constructor we used the set loop function
on each of the fire related sound objects.
Now when we call play, they will play continuously.
The values of minimum distance and attenuation
were arrived at through experimentation.
Once the game is running, I encourage you to experiment
with these values by changing them around
and seeing rather than hearing the difference.
Now, coding the playfire function.
Add this highlighted playfire function.
You have to add this entire highlighted code.
Let's discuss it.
The first thing we do is call listener setPosition
and set the location of the listener
based on the Vector2f that is passed in as a parameter.
Next, the code enters a switch block
based on the value of m_NextSound.
Each of these case statements does the exact same thing.
But to m_Fire1Sound or m_Fire2Sound or m_Fire3Sound,
in each of the case blocks, we set the position
of the emitter using the passed in parameter
with the setPosition function.
The next part of the code in each case block
checks whether the sound is currently stopped
and if it is, plays the sound.
We will see quite soon how we arrive at the positions
for the emitter and listener that
are passed into this function.
The final part of the playfire function increments
m_NextSound and ensures they can only be equal
to one, two, or three as required by the switch block.
In this if condition, we go back to one
when the third has started.
Next, coding the rest of the SoundManager functions.
Add these four simple functions.
Let's discuss it quickly.
The playFallInFire, playFallInWater,
and playReachGoal functions do just two things.
First they each call setRelativeToListener,
so the sound effect is not spatialized,
making the sound effect normal, not directional.
Then they call paly on the appropriate sound object.
That concludes the SoundManager class.
Now we can use it in the engine class.
So open the Engine.h file.
After #include LevelManager.h,
type #include "SoundManager.h".
We will also add an instance of the new SoundManager class.
We'll add it after LevelManager m_LM.
Write a comment first, create a SoundManager,
SoundManager m_SM.
Here we added an instance of the new class.
At this point, we could use m_SM
to call the various play functions.
Unfortunately, there's still a bit more work to be done
in order to manage the locations
of the emiiters, fire tiles.
In this video, we have learned building
the SoundManager class and adding it to game engine.
Awesome.
Next up, we'll see Populating the Sound Emitters.