In this article we add a heads up display.
The Heads Up Display, or HUD, is used to display information to the player like score, shield strength, current weapon etc. SDL does not have any text handling functionality built in, but there are a number of third party SDL libraries that provide this functionality. We will use one of these libraries, SDL_TTF, to create a HUD for our game.
Before we create the HUD, first we need some information for it to display. Until now all the enemies and the player have died on contact, either with each other or with a weapon. Now we will give the player and the enemies the ability to have a shield, which will be decreased when these objects collide or are shot.
DamageableObject.h / DamageableObject.cpp
Because the functionality of the shield is common to both the enemies and the player, we create a new class called DamageableObject. It is here that we define the shield property, as well as the GetShields function to get the current shields value. [code]
The Damage function is designed to be called whenever an object suffers some sort of damage, like when it is shot. By default this function decreases the shield value, calling the Die function when the shields are depleted. [code]
The Die function is called when the shields have been depleted. The default implementation is empty, and it is up to the extending classes to add the logic to be executed when the object is destroyed. [code]
BaseEnemy.h / BaseEnemy.cpp
The BaseEnemy extends the DamageableObject class (along with the Die function), gains a new property called score, and a function called GetScore. [code]
In the Collide function, instead of being destroyed straight away when shot with a weapon, the Damage function is now called with the damage property of the colliding weapon. Likewise, when there is a collision with the player the Damage function is called supplying the current enemy’s shield strength, ensuring that it will be destroyed. [code]
The BaseEnemy class also implements the Die function, in which the enemy is removed from the game, an explosion is created, and the new score property of the APplicationManager is updated via the AddToScore function. [code]