Updating the HUD each frame

As you might expect, we will update the HUD variables in the update section of our code. We will not, however, do so every frame. The reason for this is that it is unnecessary and it also slows our game loop down.

As an example, consider the scenario where the player kills a zombie and gets some more points. It doesn't matter whether the Text object that holds the score is updated in a thousandth, hundredth, or even tenth of a second. The player will discern no difference. This means there is no point rebuilding strings that we set to the Text objects every frame.

So we can time when and how often we update the HUD, add the following variables:

// When did we last update the HUD?
int framesSinceLastHUDUpdate = 0;

// How often (in frames) should we update the HUD
int fpsMeasurementFrameInterval = 1000; 
 
// The main game loop 
while (window.isOpen()) 

In the previous code, we have variables to track how many frames it has been since the last time the HUD was updated and the interval, measured in frames, we would like to wait between HUD updates.

Now we can use these new variables and actually update the HUD each frame. We won't actually see all the HUD elements change, however, until we begin to manipulate the final variables, such as wave, in the next chapter.

Add the highlighted code in the update section of the game loop as shown:

   // Has the player touched ammo pickup 
   if (player.getPosition().intersects 
      (ammoPickup.getPosition()) && ammoPickup.isSpawned()) 
   { 
      bulletsSpare += ammoPickup.gotIt(); 
       
   } 
 
   // size up the health bar
   healthBar.setSize(Vector2f(player.getHealth() * 3, 50));
   // Increment the number of frames since the previous update
   framesSinceLastHUDUpdate++;

   // re-calculate every fpsMeasurementFrameInterval frames
   if (framesSinceLastHUDUpdate > fpsMeasurementFrameInterval)
   {
     // Update game HUD text
     std::stringstream ssAmmo;
     std::stringstream ssScore;
     std::stringstream ssHiScore;
     std::stringstream ssWave;
     std::stringstream ssZombiesAlive;

     // Update the ammo text
     ssAmmo << bulletsInClip << "/" << bulletsSpare;
     ammoText.setString(ssAmmo.str());

     // Update the score text
     ssScore << "Score:" << score;
     scoreText.setString(ssScore.str());

     // Update the high score text
     ssHiScore << "Hi Score:" << hiScore;
     hiScoreText.setString(ssHiScore.str());

     // Update the wave
     ssWave << "Wave:" << wave;
     waveNumberText.setString(ssWave.str());

     // Update the high score text
     ssZombiesAlive << "Zombies:" << numZombiesAlive;
     zombiesRemainingText.setString(ssZombiesAlive.str());

     framesSinceLastHUDUpdate = 0;

   }// End HUD update 
 
}// End updating the scene 

In the new code, we update the size of the healthBar sprite, increment the timeSinceLastUpdate object, then increment the framesSinceLastUpdate variable.

Next, we start an if block that tests whether framesSinceLastHUDUpdate is greater than our preferred interval, which is stored in fpsMeasurementFrameInterval.

Inside this if block is where all the action takes place. First, we declare a string stream object for each string that we need to set to a Text object.

Then we use each of those string stream objects in turn, and use the setString function to set the result to the appropriate Text object.

Finally, before the if block is exited, the framesSinceLastHUDUpdate is set back to zero so that the count can begin again.

Now, when we redraw the scene, the new values will appear in the player's HUD.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.239.214