- [John Horton] Hey, welcome to the HUD class.
In the last video, we learnt how to play various sounds.
In this video, we're going to take a look at
coding the HUD.h file and HUD.cpp file.
The HUD is supper simple and not really
anything different compared to the other
two projects we saw in beginning
C++ game programming, part one.
What we will do that is different is wrap all
the code up in a new HUD class.
If we declare all the font, text and other variables
as members of this new class,
we can then initialize them in the constructor
and provide getter functions to all their values.
This will keep the engin class clear from loads
of declarations and initializations.
Let's get started with coding HUD.h.
First, we will code the HUD.h file with
all the member variables and function declarations.
Right click Header Files in the Solution Explorer,
and select Add New Item.
In the opened window, click Header File,
and then in the name field write HUD.
Lastly, click the Add button.
We are now ready to code the Header File
for the HUD class.
Add this highlighted code to the file.
In this code, first we have one font incidence
and three text incidences.
The text objects will be used to show
a message prompting the user to start,
the time remaining, and the current level number.
The public functions are more interesting.
First, there is the constructor,
where most of the code will go.
The constructor will initialize the font
and text objects, as well as position them
on the screen relative to the current screen resolution.
The three getter functions: get message, get level
and get time, will return a text object to the
calling code in order to be able to draw them
to the screen.
The set level and set time functions
will be used to update the text shown
in m_LevelText and m_Time Text, respectively.
Now, we can code all the definitions for
the functions we have just outlined.
Next we'll be coding the HUD.cpp file.
As always, right click Source Files
in the Solution Explorer, and select Add New Item.
In the opened window, click C++ File and
then in the name field type HUD.
Finally, click the Add button.
We are now ready to code the .cpp file
for the HUD class.
Add all this highlighted code.
We'll go through it step by step.
After we have added the necessary include directives,
first we store the horizontal and vertical
resolution in a vector 2u called Resolution.
Next, we load the font from the fonts directory
that we added back in section one.
This is the font file.
Now, getting back to the code.
This set of code performs certain operations.
The first line sets the font.
Second one sets the size.
Here it will set the color, currently white.
And last line sets the text of m_StartText,
with the help of set String function.
The text is "Press enter when ready!".
The next block of code captures the size
of the rectangle that wraps m_StartText,
and then performs a calculation to work out
how to position it centrally on the screen
with the help of set position function.
The final two blocks of code in the constructor set,
the font, the text size, the color,
the position and the actual text for
m_TimeText and m_LevelText using
set string function.
We will see in a moment however, that these
two text objects will be updateable through
two setter functions whenever it is required.
Now, add this highlighted code immediately
after the previous code we just added.
In this highlighted code we're adding
getter and setter functions.
Let's go through it.
The first three functions that I just highlighted
simply return the appropriate text object
m_StartText, m_LevelText
and m_TimeText.
We will use these functions shortly when
drawing the HUD on the screen.
The final two functions setLevel and setTime,
use the set string functions to update
the appropriate text object with
the value that will be passed in
from the update function of the engine class
every 500 frames.
That's all about HUD class.
In this video, we've looked at coding
the HUD.h and HUD.cpp file.
Great, with all that done, we can put the
HUD class to work in our game engine.
So next, we'll learn using the HUD class.