Game Development Reference
In-Depth Information
device context, use the
ComPtr
type to help convert and give us a device we can
use.
After that, we just need to provide that device, as well as a
wchar_t
path to our font
file, relative to the application directory. In our case we are storing our font in the root
game directory.
Now we have the font loaded and ready, and it is a simple matter of drawing it within
the same batch we use for the sprites.
Just before we end the sprite batch, add the following lines:
auto textPos = DirectX::XMFLOAT2(10, 10);
auto textVec = DirectX::XMLoadFloat2(&textPos);
_spriteFont->DrawString(
_spriteBatch.get(),
L"Learning Game Development",
textVec,
DirectX::Colors::Green
);
Here we need to specify the position of the text, so as before we need to get
FXMVECTOR
, which defines that point. To create the vector, we always have to start
with
XFLOAT2
or a similar type, so we'll define that as a local variable for this demon-
stration.
Once we have that vector, we can call
DrawString
on the sprite font. This will
handle all of the drawing, and provide us with some parameters to customize the text
at runtime.
SpriteFont
uses
SpriteBatch
to do rendering, so it needs a referen-
ce to that, and we need to specify the text to actually draw as a
wchar_t
string. The
last mandatory parameter is the position of the text, represented by our vector from
the given line.
The third parameter is one we haven't seen before, and is something that can be
quite useful at runtime, even for rendering sprites. This is the
TintColor
parameter,
which defines a tint to be applied to the image being drawn. Our sprite fonts are gen-
erated as pure white characters so that you can easily apply a color tint at runtime,
or stick to the default white color for text.