Over a million developers have joined DZone.

DirectX Tool Kit – Native “XNA” for Windows 8

· Mobile Zone

For all those who miss XNA in Metro applications, DirectX Tool Kit is a handy set of classes that mimic popular XNA classes such as SpriteBatch, SpriteFont and BasicEffect. Let’s see how easy it is to use it. For my first trick, I shall load a texture and render it on screen. Nothing fancy, but it will quickly establish the similarities between old and proven XNA code with new DX11 code. Just to be clear, the code in this blog post is written using C++.

Go and download DirectXTK from Codeplex/. Open the DirectXTK_11_Metro solution and you will see only one project inside. Build it and you will get a static library. Include files are located in the Inc folder.

Now let’s add a new project inside the same solution. In my case I have named it Sample1. Choose Direct3D App as the project type, you can find it under Visual C++/Windows Metro Style. Also add a project dependency to the DirectXTK_11_Metro project to ensure that it will compile properly.

In case you are experienced C++ programmer, you can skip the next few paragraphs. Those unfamiliar with C++, here is an explanation how to consume the library.

Linking against the library

In case you have come from C#, things are more complicated than simply adding a reference. When you compile a static library project, you get a .lib file that you must link to in your main project. You can do this in several ways, but let’s add it without too much hassle.

Open the Sample1 project properties dialog and go to the Linker/Input tab. Click on the small drop down in the right corner of the Additional Dependencies editable combo box (visible on the image below).

Click on the Edit… option and add the following line at the bottom of the first textbox:

But this is not enough, even though you have linked your project to the static library, in order to compile code, you must include the necessary header files in your source code (those that have the .h extension). Files are included by adding the following statement at the beginning of the .h file:

#include "..\DirectXTK\Inc\WICTextureLoader.h"

This includes just that file (and other files that that file includes) with all class and function declarations which you can then use in your code.

Loading a simple texture

Open the CubeRenderer.h and add the following line at the end of the file:

Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_pTextureView;

Notice that we are using the ID3D11ShaderResourceView class and not ID3D11Texture2D. This will be fed to the SpriteBatch::Draw function later.

To load the texture, we must call the appropriate function at the end of the CreateDeviceResources function. We will use the helper function from DirectX Tool Kit:


Notice that I use the existing image added by default to the project.

Now that we have the texture, let’s render it.


Since this is ordinary C++ class, we will use std::unique_ptr to hold it. Add the following lines in the CubeRenderer.h file:

// Last include
#include "..\DirectXTK\Inc\SpriteBatch.h"
// at the end of the class
std::unique_ptr<DirectX::SpriteBatch> m_pSpriteBatch;

To initialize it, add the following line at the end of the CreateDeviceResources function:

m_pSpriteBatch = std::unique_ptr<DirectX::SpriteBatch>(new DirectX::SpriteBatch(m_d3dContext.Get()));

And now we can finally render.

Rendering sprite

We can now finally render our sprite using the following (simple) snippet:

m_pSpriteBatch->Draw(m_pTextureView.Get(), XMFLOAT2(0, 0));

The final result is the small texture rendered in the upper left corner. Since I have a 24” monitor, here is a cropped screenshot:


The most basic sample, loading a texture and rendering it as a sprite on screen took a little more code to write than the analogous XNA sample, but it is easy to see that the basic building block for most 2D based games are there. It is possible to port XNA code to DX11 and DirectX Tool Kit is a nice library to ease the transition.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}