Game Development Reference
In-Depth Information
protected:
float m_maximum;
float m_value;
};
Aprogressbarworksbyfillinganareabasedonapercentage ofsomepredetermined max-
imum, having set the extents for the progress bar, we can draw a rectangle scaled based on
the percentage.
virtual void InternalDraw()
{
m_spriteBatch->Draw(m_texture->GetView(), m_rectangle, nullptr, m_backgroundColor);
math::rectangle progressRect = m_rectangle;
progressRect.Width() = m_progressBar. GetRatio() * m_rectangle.Width();
m_spriteBatch->Draw(m_texture->GetView(),progressRect, nullptr, m_foregroundColor);
}
The first draw call is for the background of the progress bar, this one is fixed to the extents
setwhentheprogressbarwascreated.Fortheactualprogressbar,weusethesameextents,
however, we scale the width by the progress bar's ratio.
We need to set the ratio of the progress bar during the frame, this is given by the current
health of the player divided by the maximum possible health.
float health = element.GetHealth();
float maxHealth = element.GetMaxHealth();
float integrity = (maxHealth > 0.f) ? health / maxHealth : 0.f;
m_progressbar->SetRatio(integrity);
The code for drawing does not need to be a part of the progress bar object, it is better to
leave the progress bar as a purely logical object and allow our health bar class to draw
based on the information therein.
Search WWH ::




Custom Search