Game Development Reference
3.14 D IRECT XTK
The DirectX ToolKit is a useful collection of classes that provide efficient and straightfor-
ward interfaces for writing Direct3D 11 applications in C++. To people with experience de-
veloping with XNA in C# these classes will be familiar.
This section will provide a light reference to the DirectX Toolkit to introduce the two most
used objects in the example code, the SpriteBatch and the SpriteFont , these are two very
useful classes that provide an efficient interface for rendering 2D elements and text.
A sprite batch as the name suggests allows us to batch 2D elements to be drawn at a time,
it does a great job at abstracting a lot of the Direct3D low level code that can be daunting
to manage. Roughly speaking a SpriteBatch allows us to draw a texture at some position on
the screen by calling one of its Draw functions.
We start by calling Begin, where we can specify how we want our batch to be drawn, we
can provide a sort mode, blend, sampler, depth/stencil states and rasterizer states as well
as provide a custom set of shaders to use and a transformation matrix to be applied on the
batch. This will save the state of the device and change it to the states we pass it, or use the
defaults if no states are given.
We then can issue any number of Draw calls, there are different function overloads depend-
ing on what we need to draw. For a sprite sheet for example, we would use the overload that
sprite frame within the larger sprite sheet texture.
of the SpriteBatch, this will restore the device's state to the way it was before the call to Be-
Calling Begin on a SpriteBatch allows us to set the device states we need when the batch
is drawn, first we must specify when the device states need to be applied, this is done by
specifying the SpriteSortMode.