1
00:00:00,009 --> 00:00:03,008
- [John Horton] Hey,
welcome to the HUD class.

2
00:00:03,008 --> 00:00:07,001
In the last video, we learnt
how to play various sounds.

3
00:00:07,001 --> 00:00:09,001
In this video, we're
going to take a look at

4
00:00:09,001 --> 00:00:13,001
coding the HUD.h file and HUD.cpp file.

5
00:00:13,001 --> 00:00:15,002
The HUD is supper simple and not really

6
00:00:15,002 --> 00:00:17,000
anything different compared to the other

7
00:00:17,000 --> 00:00:18,006
two projects we saw in beginning

8
00:00:18,006 --> 00:00:21,009
C++ game programming, part one.

9
00:00:21,009 --> 00:00:24,003
What we will do that is
different is wrap all

10
00:00:24,003 --> 00:00:26,005
the code up in a new HUD class.

11
00:00:26,005 --> 00:00:29,006
If we declare all the font,
text and other variables

12
00:00:29,006 --> 00:00:31,005
as members of this new class,

13
00:00:31,005 --> 00:00:34,000
we can then initialize
them in the constructor

14
00:00:34,000 --> 00:00:36,009
and provide getter functions
to all their values.

15
00:00:36,009 --> 00:00:39,004
This will keep the engin
class clear from loads

16
00:00:39,004 --> 00:00:42,008
of declarations and initializations.

17
00:00:42,008 --> 00:00:46,001
Let's get started with coding HUD.h.

18
00:00:46,001 --> 00:00:48,008
First, we will code the HUD.h file with

19
00:00:48,008 --> 00:00:52,003
all the member variables
and function declarations.

20
00:00:52,003 --> 00:00:55,000
Right click Header Files
in the Solution Explorer,

21
00:00:55,000 --> 00:00:57,002
and select Add New Item.

22
00:00:57,002 --> 00:00:59,006
In the opened window, click Header File,

23
00:00:59,006 --> 00:01:02,006
and then in the name field write HUD.

24
00:01:02,006 --> 00:01:04,009
Lastly, click the Add button.

25
00:01:04,009 --> 00:01:06,008
We are now ready to code the Header File

26
00:01:06,008 --> 00:01:08,005
for the HUD class.

27
00:01:08,005 --> 00:01:11,001
Add this highlighted code to the file.

28
00:01:11,001 --> 00:01:13,008
In this code, first we
have one font incidence

29
00:01:13,008 --> 00:01:16,000
and three text incidences.

30
00:01:16,000 --> 00:01:17,008
The text objects will be used to show

31
00:01:17,008 --> 00:01:20,002
a message prompting the user to start,

32
00:01:20,002 --> 00:01:23,007
the time remaining, and
the current level number.

33
00:01:23,007 --> 00:01:26,001
The public functions are more interesting.

34
00:01:26,001 --> 00:01:28,001
First, there is the constructor,

35
00:01:28,001 --> 00:01:30,000
where most of the code will go.

36
00:01:30,000 --> 00:01:31,009
The constructor will initialize the font

37
00:01:31,009 --> 00:01:34,001
and text objects, as well as position them

38
00:01:34,001 --> 00:01:37,005
on the screen relative to the
current screen resolution.

39
00:01:37,005 --> 00:01:40,002
The three getter functions:
get message, get level

40
00:01:40,002 --> 00:01:42,009
and get time, will return
a text object to the

41
00:01:42,009 --> 00:01:45,003
calling code in order
to be able to draw them

42
00:01:45,003 --> 00:01:46,008
to the screen.

43
00:01:46,008 --> 00:01:48,008
The set level and set time functions

44
00:01:48,008 --> 00:01:50,009
will be used to update the text shown

45
00:01:50,009 --> 00:01:56,003
in m_LevelText and m_Time
Text, respectively.

46
00:01:56,003 --> 00:01:58,005
Now, we can code all the definitions for

47
00:01:58,005 --> 00:02:01,001
the functions we have just outlined.

48
00:02:01,001 --> 00:02:04,009
Next we'll be coding the HUD.cpp file.

49
00:02:04,009 --> 00:02:07,001
As always, right click Source Files

50
00:02:07,001 --> 00:02:10,003
in the Solution Explorer,
and select Add New Item.

51
00:02:10,003 --> 00:02:13,006
In the opened window, click C++ File and

52
00:02:13,006 --> 00:02:16,005
then in the name field type HUD.

53
00:02:16,005 --> 00:02:19,000
Finally, click the Add button.

54
00:02:19,000 --> 00:02:21,005
We are now ready to code the .cpp file

55
00:02:21,005 --> 00:02:23,005
for the HUD class.

56
00:02:23,005 --> 00:02:25,007
Add all this highlighted code.

57
00:02:25,007 --> 00:02:28,000
We'll go through it step by step.

58
00:02:28,000 --> 00:02:31,000
After we have added the
necessary include directives,

59
00:02:31,000 --> 00:02:33,002
first we store the horizontal and vertical

60
00:02:33,002 --> 00:02:37,004
resolution in a vector
2u called Resolution.

61
00:02:37,004 --> 00:02:40,002
Next, we load the font
from the fonts directory

62
00:02:40,002 --> 00:02:42,006
that we added back in section one.

63
00:02:42,006 --> 00:02:44,002
This is the font file.

64
00:02:44,002 --> 00:02:46,003
Now, getting back to the code.

65
00:02:46,003 --> 00:02:49,001
This set of code performs
certain operations.

66
00:02:49,001 --> 00:02:51,004
The first line sets the font.

67
00:02:51,004 --> 00:02:53,008
Second one sets the size.

68
00:02:53,008 --> 00:02:57,000
Here it will set the
color, currently white.

69
00:02:57,000 --> 00:03:01,004
And last line sets the
text of m_StartText,

70
00:03:01,004 --> 00:03:03,008
with the help of set String function.

71
00:03:03,008 --> 00:03:07,005
The text is "Press enter when ready!".

72
00:03:07,005 --> 00:03:09,008
The next block of code captures the size

73
00:03:09,008 --> 00:03:13,002
of the rectangle that wraps m_StartText,

74
00:03:13,002 --> 00:03:15,005
and then performs a
calculation to work out

75
00:03:15,005 --> 00:03:17,007
how to position it centrally on the screen

76
00:03:17,007 --> 00:03:20,007
with the help of set position function.

77
00:03:20,007 --> 00:03:23,004
The final two blocks of
code in the constructor set,

78
00:03:23,004 --> 00:03:26,004
the font, the text size, the color,

79
00:03:26,004 --> 00:03:28,009
the position and the actual text for

80
00:03:28,009 --> 00:03:33,002
m_TimeText and m_LevelText using

81
00:03:33,002 --> 00:03:35,004
set string function.

82
00:03:35,004 --> 00:03:37,007
We will see in a moment
however, that these

83
00:03:37,007 --> 00:03:39,008
two text objects will
be updateable through

84
00:03:39,008 --> 00:03:43,003
two setter functions
whenever it is required.

85
00:03:43,003 --> 00:03:45,009
Now, add this highlighted code immediately

86
00:03:45,009 --> 00:03:48,005
after the previous code we just added.

87
00:03:48,005 --> 00:03:50,005
In this highlighted code we're adding

88
00:03:50,005 --> 00:03:52,005
getter and setter functions.

89
00:03:52,005 --> 00:03:54,001
Let's go through it.

90
00:03:54,001 --> 00:03:56,008
The first three functions
that I just highlighted

91
00:03:56,008 --> 00:03:59,002
simply return the appropriate text object

92
00:03:59,002 --> 00:04:03,006
m_StartText, m_LevelText

93
00:04:03,006 --> 00:04:06,004
and m_TimeText.

94
00:04:06,004 --> 00:04:08,006
We will use these functions shortly when

95
00:04:08,006 --> 00:04:11,001
drawing the HUD on the screen.

96
00:04:11,001 --> 00:04:14,006
The final two functions
setLevel and setTime,

97
00:04:14,006 --> 00:04:17,000
use the set string functions to update

98
00:04:17,000 --> 00:04:18,007
the appropriate text object with

99
00:04:18,007 --> 00:04:20,003
the value that will be passed in

100
00:04:20,003 --> 00:04:22,005
from the update function
of the engine class

101
00:04:22,005 --> 00:04:24,008
every 500 frames.

102
00:04:24,008 --> 00:04:27,003
That's all about HUD class.

103
00:04:27,003 --> 00:04:29,003
In this video, we've looked at coding

104
00:04:29,003 --> 00:04:32,006
the HUD.h and HUD.cpp file.

105
00:04:32,006 --> 00:04:35,001
Great, with all that done, we can put the

106
00:04:35,001 --> 00:04:37,008
HUD class to work in our game engine.

107
00:04:37,008 --> 00:04:41,002
So next, we'll learn using the HUD class.

