All the code from the next three code blocks goes in the drawing phase of our game loop. All we need to do is to draw the appropriate Text
objects during the appropriate states in the draw section of the main game loop.
In the PLAYING
state, add the following highlighted code:
//Draw the crosshair
window.draw(spriteCrosshair);
// Switch to the HUD view
window.setView(hudView);
// Draw all the HUD elements
window.draw(spriteAmmoIcon);
window.draw(ammoText);
window.draw(scoreText);
window.draw(hiScoreText);
window.draw(healthBar);
window.draw(waveNumberText);
window.draw(zombiesRemainingText);
}
if (state == State::LEVELING_UP)
{
}
The vital thing to notice in the previous block of code is that we switch views to the HUD view. This causes everything to be drawn at the precise screen positions we gave to each of the elements of the HUD. They will never move.
In the LEVELING_UP
state, add the following highlighted code:
if (state == State::LEVELING_UP)
{
window.draw(spriteGameOver);
window.draw(levelUpText);
}
In the PAUSED
state, add the following highlighted code:
if (state == State::PAUSED)
{
window.draw(pausedText);
}
In the GAME_OVER
state, add the following highlighted code:
if (state == State::GAME_OVER)
{
window.draw(spriteGameOver);
window.draw(gameOverText);
window.draw(scoreText);
window.draw(hiScoreText);
}
Now we can run the game and see our HUD update during gameplay.
This shows the HI SCORE and score on the home/game over screen:
Next we see text to show what the player's level up options are, although these options don't do anything yet:
And here we see a helpful message on the pause screen:
SFML Views are more powerful than this simple HUD can demonstrate. For an insight into the potential of SFML Views and how easy to use they are, take a look at the SFML website's tutorial on View
at http://www.sfml-dev.org/tutorials/2.0/graphics-view.php.
3.22.74.66