1
00:00:01,003 --> 00:00:02,009
- [Instructor] Welcome to video

2
00:00:02,009 --> 00:00:04,004
Building the SoundManager Class

3
00:00:04,004 --> 00:00:06,001
and Adding it to Game Engine.

4
00:00:06,001 --> 00:00:07,008
In the previous video, we got to know

5
00:00:07,008 --> 00:00:10,001
about spatialization and SFML.

6
00:00:10,001 --> 00:00:12,008
In this video, we're going to take a look

7
00:00:12,008 --> 00:00:17,006
at coding SoundManager.h file,
coding SoundManager.cpp file,

8
00:00:17,006 --> 00:00:20,006
and adding sound manager
to the game engine.

9
00:00:20,006 --> 00:00:23,002
In the previous project,
all the sound code

10
00:00:23,002 --> 00:00:25,001
took up quite a few lines of code.

11
00:00:25,001 --> 00:00:27,002
Now consider that with spatialization

12
00:00:27,002 --> 00:00:29,004
its going to get longer still.

13
00:00:29,004 --> 00:00:32,000
To keep our code manageable,
we will code a class

14
00:00:32,000 --> 00:00:34,003
to manage the playing of
all of our sound effects.

15
00:00:34,003 --> 00:00:36,009
In addition, to help
us with spatialization,

16
00:00:36,009 --> 00:00:39,006
we will add a function to
the engine class as well,

17
00:00:39,006 --> 00:00:41,009
but we will discuss that when we come to

18
00:00:41,009 --> 00:00:43,006
it later in the section.

19
00:00:43,006 --> 00:00:47,003
Let's get started by coding
and examining the header file.

20
00:00:47,003 --> 00:00:50,000
Right click header files
in the solution explorer

21
00:00:50,000 --> 00:00:52,002
and select add new item.

22
00:00:52,002 --> 00:00:55,001
In the add new item
window click Header File

23
00:00:55,001 --> 00:00:58,000
and then in the name
field, type SoundManager.

24
00:00:58,000 --> 00:00:59,009
Finally, click add.

25
00:00:59,009 --> 00:01:02,001
We are now ready to code the header file

26
00:01:02,001 --> 00:01:04,001
for the SoundManager class.

27
00:01:04,001 --> 00:01:06,008
Add this highlighted code to the file.

28
00:01:06,008 --> 00:01:09,005
There's nothing tricky in
the code we just added.

29
00:01:09,005 --> 00:01:13,006
There are five sound buffer
object and seven sound objects.

30
00:01:13,006 --> 00:01:17,002
Three of the sound objects will
play the same sound buffer.

31
00:01:17,002 --> 00:01:25,005
That is m_Fire1Sound,
m_Fire2Sound, and m_Fire3Sound.

32
00:01:25,005 --> 00:01:27,006
This explains the reason
for the different number

33
00:01:27,006 --> 00:01:30,002
of sound, sound buffer objects.

34
00:01:30,002 --> 00:01:32,007
We do this so that we can have

35
00:01:32,007 --> 00:01:34,002
multiple roaring sound objects playing

36
00:01:34,002 --> 00:01:36,009
with different spatialized
parameters simultaneously.

37
00:01:36,009 --> 00:01:40,008
Notice there is the m_NextSound variable

38
00:01:40,008 --> 00:01:42,004
that will help us keep track of

39
00:01:42,004 --> 00:01:46,003
which of these simultaneous
sounds we should use next.

40
00:01:46,003 --> 00:01:50,004
For now, we have set m_NextSound to one.

41
00:01:50,004 --> 00:01:53,000
There is a constructor SoundManager

42
00:01:53,000 --> 00:01:55,006
where we will sub all of our sound effects

43
00:01:55,006 --> 00:01:57,007
and these are the five functions

44
00:01:57,007 --> 00:02:00,000
that will play the sound effects.

45
00:02:00,000 --> 00:02:02,005
Four of these functions simply
play normal sound effects

46
00:02:02,005 --> 00:02:04,004
and they're code will be really simply.

47
00:02:04,004 --> 00:02:07,005
One of the functions,
playfire, will handle

48
00:02:07,005 --> 00:02:09,006
the spatialized sound effects

49
00:02:09,006 --> 00:02:11,003
and will be a bit more in depth.

50
00:02:11,003 --> 00:02:13,007
Notice the parameters of
the playfire function.

51
00:02:13,007 --> 00:02:17,003
If receives a Vector2f, which
is the location of the emitter

52
00:02:17,003 --> 00:02:20,002
and the second Vector2f, which is

53
00:02:20,002 --> 00:02:22,007
the location of the listener.

54
00:02:22,007 --> 00:02:24,008
Moving on to coding the
SoundManager.cpp file.

55
00:02:24,008 --> 00:02:27,001
We can code the function definitions.

56
00:02:27,001 --> 00:02:29,006
The constructor and the playfire functions

57
00:02:29,006 --> 00:02:31,006
will have a fair amount of code,

58
00:02:31,006 --> 00:02:33,006
so we'll look at them individually.

59
00:02:33,006 --> 00:02:35,009
The other functions
will be short and sweet,

60
00:02:35,009 --> 00:02:38,004
so we will handle them all at once.

61
00:02:38,004 --> 00:02:41,006
So first, right click source
files in the Solution Explorer.

62
00:02:41,006 --> 00:02:44,007
And click add new item.

63
00:02:44,007 --> 00:02:47,009
In the add new item window, click C++ file

64
00:02:47,009 --> 00:02:51,004
and then in the name
field, type SoundManager.

65
00:02:51,004 --> 00:02:53,006
Lastly click add.

66
00:02:53,006 --> 00:02:57,006
We can now code the .cpp file
for the SoundManager class.

67
00:02:57,006 --> 00:03:00,002
First, let's see coding the constructor.

68
00:03:00,002 --> 00:03:03,005
Add this code for the include directives

69
00:03:03,005 --> 00:03:05,006
and the constructor to the file.

70
00:03:05,006 --> 00:03:08,002
You have to add this
entire highlighted code.

71
00:03:08,002 --> 00:03:10,001
Now, let's go through it.

72
00:03:10,001 --> 00:03:13,000
In this code, we load five sound files

73
00:03:13,000 --> 00:03:15,005
into the five sound buffer objects.

74
00:03:15,005 --> 00:03:18,002
Next, we associated
the seven sound objects

75
00:03:18,002 --> 00:03:21,001
with one of the sound buffer objects.

76
00:03:21,001 --> 00:03:26,006
Notice that m_Fire1Sound, m_Fire2Sound,

77
00:03:26,006 --> 00:03:30,000
and m_Fire3Sound are
all going to be playing

78
00:03:30,000 --> 00:03:34,000
from the same sound buffer, m_FireBuffer.

79
00:03:34,000 --> 00:03:37,009
Next, we set the minimum
distance value as 150

80
00:03:37,009 --> 00:03:41,002
and attenuation as 15.

81
00:03:41,002 --> 00:03:44,003
Then we set attenuation
for the three fire sounds.

82
00:03:44,003 --> 00:03:48,004
Next, we set minimum distance
for the three fire sounds.

83
00:03:48,004 --> 00:03:52,000
Finally, for the constructor
we used the set loop function

84
00:03:52,000 --> 00:03:54,007
on each of the fire related sound objects.

85
00:03:54,007 --> 00:03:57,008
Now when we call play, they
will play continuously.

86
00:03:57,008 --> 00:04:01,001
The values of minimum
distance and attenuation

87
00:04:01,001 --> 00:04:03,003
were arrived at through experimentation.

88
00:04:03,003 --> 00:04:05,007
Once the game is running, I
encourage you to experiment

89
00:04:05,007 --> 00:04:08,001
with these values by changing them around

90
00:04:08,001 --> 00:04:10,009
and seeing rather than
hearing the difference.

91
00:04:10,009 --> 00:04:13,006
Now, coding the playfire function.

92
00:04:13,006 --> 00:04:16,004
Add this highlighted playfire function.

93
00:04:16,004 --> 00:04:18,009
You have to add this
entire highlighted code.

94
00:04:18,009 --> 00:04:20,001
Let's discuss it.

95
00:04:20,001 --> 00:04:23,002
The first thing we do is
call listener setPosition

96
00:04:23,002 --> 00:04:25,005
and set the location of the listener

97
00:04:25,005 --> 00:04:28,000
based on the Vector2f that
is passed in as a parameter.

98
00:04:28,000 --> 00:04:30,004
Next, the code enters a switch block

99
00:04:30,004 --> 00:04:33,003
based on the value of m_NextSound.

100
00:04:33,003 --> 00:04:37,005
Each of these case statements
does the exact same thing.

101
00:04:37,005 --> 00:04:44,006
But to m_Fire1Sound or
m_Fire2Sound or m_Fire3Sound,

102
00:04:44,006 --> 00:04:47,007
in each of the case
blocks, we set the position

103
00:04:47,007 --> 00:04:50,003
of the emitter using
the passed in parameter

104
00:04:50,003 --> 00:04:52,005
with the setPosition function.

105
00:04:52,005 --> 00:04:54,004
The next part of the
code in each case block

106
00:04:54,004 --> 00:04:56,005
checks whether the sound
is currently stopped

107
00:04:56,005 --> 00:04:58,004
and if it is, plays the sound.

108
00:04:58,004 --> 00:05:01,009
We will see quite soon how
we arrive at the positions

109
00:05:01,009 --> 00:05:04,000
for the emitter and listener that

110
00:05:04,000 --> 00:05:06,001
are passed into this function.

111
00:05:06,001 --> 00:05:08,006
The final part of the
playfire function increments

112
00:05:08,006 --> 00:05:12,003
m_NextSound and ensures
they can only be equal

113
00:05:12,003 --> 00:05:15,004
to one, two, or three as
required by the switch block.

114
00:05:15,004 --> 00:05:18,004
In this if condition, we go back to one

115
00:05:18,004 --> 00:05:20,008
when the third has started.

116
00:05:20,008 --> 00:05:23,005
Next, coding the rest of
the SoundManager functions.

117
00:05:23,005 --> 00:05:26,000
Add these four simple functions.

118
00:05:26,000 --> 00:05:28,000
Let's discuss it quickly.

119
00:05:28,000 --> 00:05:30,009
The playFallInFire, playFallInWater,

120
00:05:30,009 --> 00:05:33,008
and playReachGoal functions
do just two things.

121
00:05:33,008 --> 00:05:37,004
First they each call
setRelativeToListener,

122
00:05:37,004 --> 00:05:39,009
so the sound effect is not spatialized,

123
00:05:39,009 --> 00:05:42,000
making the sound effect
normal, not directional.

124
00:05:42,000 --> 00:05:44,005
Then they call paly on the
appropriate sound object.

125
00:05:44,005 --> 00:05:47,007
That concludes the SoundManager class.

126
00:05:47,007 --> 00:05:50,004
Now we can use it in the engine class.

127
00:05:50,004 --> 00:05:52,009
So open the Engine.h file.

128
00:05:52,009 --> 00:05:56,005
After #include LevelManager.h,

129
00:05:56,005 --> 00:06:00,001
type #include "SoundManager.h".

130
00:06:00,001 --> 00:06:04,009
We will also add an instance
of the new SoundManager class.

131
00:06:04,009 --> 00:06:08,006
We'll add it after LevelManager m_LM.

132
00:06:08,006 --> 00:06:12,005
Write a comment first,
create a SoundManager,

133
00:06:12,005 --> 00:06:15,001
SoundManager m_SM.

134
00:06:15,001 --> 00:06:17,008
Here we added an instance
of the new class.

135
00:06:17,008 --> 00:06:21,003
At this point, we could use m_SM

136
00:06:21,003 --> 00:06:23,005
to call the various play functions.

137
00:06:23,005 --> 00:06:26,000
Unfortunately, there's still
a bit more work to be done

138
00:06:26,000 --> 00:06:27,007
in order to manage the locations

139
00:06:27,007 --> 00:06:29,008
of the emiiters, fire tiles.

140
00:06:29,008 --> 00:06:31,008
In this video, we have learned building

141
00:06:31,008 --> 00:06:34,006
the SoundManager class and
adding it to game engine.

142
00:06:34,006 --> 00:06:35,008
Awesome.

143
00:06:35,008 --> 00:06:39,005
Next up, we'll see Populating
the Sound Emitters.

