Game Development Reference
In-Depth Information
5.3D IGITAL D ISPLAYS
Often in games we want to present information to players using digital displays; that is to
representsomeintegral valueasacollection ofdigits.Typically thisisusedforammocoun-
ters,health orenergyamountsandanywherewewanttodisplaynumeric valuesinwhichan
amount may change over the course of gameplay.
Using a mono-space font for numbers is a potential solution that works in some of the situ-
ations, since the size of the font glyphs are constant there is no need to do much else, as the
font will behave properly with changing values. However, when using a variable-width font
(the letters differ in width with one another) then we will see our numeric field growing and
shrinking depending on the number being shown, this is an undesirable effect.
We can break apart the numeric value into its respective digits and extract them individually
ascharacters.Bydoingthis,wehavecompletecontrolonhowtodrawtheindividualglyphs,
we can space them apart at a fixed width, even if using a variable-width font.
The first step is to make a class that will extract the digits in a straightforward, easy to use
way, the goal will be to create a class that can give us this ability.
digits d(4150);
d(0); // returns '4'
d(1); // returns '1'
d(2); // returns '5'
d(3); // returns '0'
d(4); // out of bounds, returns 0
Withthisavailable tous,wehavetheabilitytopopulatethedifferentpositionsofourdigital
display.Wecanconstructthedigitsobjectbypassingthenumberweneedtobreakapart,we
takethenumberandcontinuouslydivideitbytenwhilestoringtheremainderofthenumber
by ten, we will then extract the digits in reverse order, what this allows us to do is retrieve
the digits using indices in which zero is the most significant digit, and n-1 is the lowest sig-
nificant digit; this if course could allow us to index out of bounds, in which case we will
return 0.
class digits {
public:
digits(int number)
: m_number(number) {
int n = number;
while ( n > 10 )
Search WWH ::




Custom Search