- [Instructor] Hi, welcome
to Populating the Sound Emitters.
In the previous video we learned
building the sound manager class
and adding it to the game engine.
In this video we will be coding
the populateEmitters function.
Let's get started then.
First, open the engine.h file
and add a new prototype
for a populateEmitters function.
After bool detectCollision,
write a comment, make a vector of the best places
to emit sounds from,
void populateEmitters vector
Vector2f& vSoundEmitters,
int** arrayLevel.
Next, write another comment,
a vector of Vector2f for the fire emitter locations,
then vector Vector2f m_FireEmitters.
So here we've added a new prototype
for a populateEmitters function,
and a new STL vector of Vector2f objects.
The populateEmitters function
takes as a parameter a vector
of Vector2f objects, as well as a pointer
to int, a two dimensional array.
The vector will hold the location
of each emitter in a level,
and the array is our two dimensional array,
which holds the layout of a level.
Let's code the populateEmitters function.
The job of the populateEmitters function
is to scan through all the elements
of arrayLevel and decide where to put the emitters.
It will store its results in m_FireEmitters.
Now, right click source files
in the solution explorer and select add new item.
In the add new item window click C++ file,
and then in the name field
type PopulateEmitters.
Lastly, click add button.
Now we can code the new function, populateEmitters.
Add the code in its entirety.
We'll study the code and then discuss it.
This is the highlighted code.
Some of it might appear complex at first glance.
Understanding the technique we used
to choose where an emitter will make it simpler.
In our levels, there are regularly large blocks
of fire tiles.
In one of the levels I designed,
there are more than 30 fire tiles together.
This code makes sure
that there is only one emitter
within a given rectangle.
This rectangle is stored in previousEmitter
and is 300 pixels by 300 pixels.
It then sets up a nested for loop
that loops through arrayLevel
looking for fire tiles.
When it finds one with the help of this if loop,
that is fire is present, it makes sure
that it does not intersect with previousEmitter
using this highlighted code.
Only then does it use the pushback function
to add another emitter to feed sound emitters.
After doing so, it also updates previousEmitter
to avoid getting large clusters of sound emitters.
Great, that's all about populating sound emitters.
In this video we coded the populateEmitters function.
Let's make some noise now, if you know what I mean.