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:
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:
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:
DX::ThrowIfFailed(CreateWICTextureFromFile( m_d3dDevice.Get(), m_d3dContext.Get(), L"Assets\\Logo.png", 0, &m_pTextureView));
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.
We can now finally render our sprite using the following (simple) snippet:
m_pSpriteBatch->Begin(); m_pSpriteBatch->Draw(m_pTextureView.Get(), XMFLOAT2(0, 0)); m_pSpriteBatch->End();
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.