Open Engine.h
, add an include for our new class, declare an instance of the new HUD
class, and also declare and initialize two new member variables that will keep track of how often we update the HUD. As we have learned in the two previous projects, we don't need to do this for every frame.
Add the highlighted code to Engine.h
:
#pragma once #include <SFML/Graphics.hpp> #include "TextureHolder.h" #include "Thomas.h" #include "Bob.h" #include "LevelManager.h" #include "SoundManager.h" #include "HUD.h" using namespace sf; class Engine { private: // The texture holder TextureHolder th; // Thomas and his friend, Bob Thomas m_Thomas; Bob m_Bob; // A class to manage all the levels LevelManager m_LM; // Create a SoundManager SoundManager m_SM; // The Hud Hud m_Hud; int m_FramesSinceLastHUDUpdate = 0; int m_TargetFramesPerHUDUpdate = 500; const int TILE_SIZE = 50;
Next, we need to add some code to the update
function of the Engine
class. Open Update.cpp
and add the highlighted code to update the HUD once every 500 frames:
// Set the appropriate view around the appropriate character
if (m_SplitScreen)
{
m_LeftView.setCenter(m_Thomas.getCenter());
m_RightView.setCenter(m_Bob.getCenter());
}
else
{
// Centre full screen around appropriate character
if (m_Character1)
{
m_MainView.setCenter(m_Thomas.getCenter());
}
else
{
m_MainView.setCenter(m_Bob.getCenter());
}
}
// Time to update the HUD?
// Increment the number of frames since the last HUD calculation
m_FramesSinceLastHUDUpdate++;
// Update the HUD every m_TargetFramesPerHUDUpdate frames
if (m_FramesSinceLastHUDUpdate > m_TargetFramesPerHUDUpdate)
{
// Update game HUD text
stringstream ssTime;
stringstream ssLevel;
// Update the time text
ssTime << (int)m_TimeRemaining;
m_Hud.setTime(ssTime.str());
// Update the level text
ssLevel << "Level:" << m_LM.getCurrentLevel();
m_Hud.setLevel(ssLevel.str());
m_FramesSinceLastHUDUpdate = 0;
}
}// End of update function
In the previous code, m_FramesSinceLastUpdate
is incremented each frame. When m_FramesSinceLastUpdate
exceeds m_TargetFramesPerHUDUpdate
, execution enters the if
block. Inside the if
block, we use stringstream
objects to update our Text
, as we have done in both previous projects. As you probably expected, however, in this project we are using the HUD
class, so we call the setTime
and setLevel
functions, passing in the current values that the Text
objects need to be set to.
The final step in the if
block is to set m_FramesSinceLastUpdate
back to zero so it can start counting toward the next update.
Finally, open the Draw.cpp
file and add the highlighted code to draw the HUD, each frame:
else
{
// Split-screen view is active
// First draw Thomas' side of the screen
// Switch to background view
m_Window.setView(m_BGLeftView);
// Draw the background
m_Window.draw(m_BackgroundSprite);
// Switch to m_LeftView
m_Window.setView(m_LeftView);
// Draw the Level
m_Window.draw(m_VALevel, &m_TextureTiles);
// Draw thomas
m_Window.draw(m_Bob.getSprite());
// Draw thomas
m_Window.draw(m_Thomas.getSprite());
// Now draw Bob's side of the screen
// Switch to background view
m_Window.setView(m_BGRightView);
// Draw the background
m_Window.draw(m_BackgroundSprite);
// Switch to m_RightView
m_Window.setView(m_RightView);
// Draw the Level
m_Window.draw(m_VALevel, &m_TextureTiles);
// Draw thomas
m_Window.draw(m_Thomas.getSprite());
// Draw bob
m_Window.draw(m_Bob.getSprite());
}
// Draw the HUD
// Switch to m_HudView
m_Window.setView(m_HudView);
m_Window.draw(m_Hud.getLevel());
m_Window.draw(m_Hud.getTime());
if (!m_Playing)
{
m_Window.draw(m_Hud.getMessage());
}
// Show everything we have just drawn
m_Window.display();
}// End of draw
The previous code draws the HUD by using the getter functions from the HUD class. Notice that the call to draw the message to that prompts the player to start is only used when the game is not currently playing (!m_Playing)
.
Run the game and play a few levels to see the time tick down and the levels tick up. When you get back to level one again, notice that you have 10% less time than before.
3.15.31.22