SpriteBatch and BasicEffect for C++ Direct3D 11

Over the Christmas break I spent some time porting the built-in XNA effects (BasicEffect, SkinnedEffect, etc.) to native C++ using Direct3D 11.  Finding that strangely enjoyable, I went on to make a C++ version of SpriteBatch, and also ported the XNA Primitives 3D sample, built-in state objects, and helper vertex types.  And my colleague Chuck Walbourn chimed in with some code for easily loading Direct3D 11 textures.

Get it here: DirectXTK


  • 3/5 – fixed bug in SpriteBatch with > 2048 sprites in a batch  (thanks Pete Lewis!)
  • 3/8 – added separate project files for Win8 Metro builds  (thanks George Clingerman!)
  • 5/2 – added SpriteFont implementation

Supported operating systems:

  • Windows 8 Consumer Preview (both Metro and Desktop)
  • Windows 7
  • Windows Vista with DirectX 11 update

Supported compilers:

How to use it:

  • Unzip, load the solution into Visual Studio, and build it
  • Link your program with the resulting static lib
  • Add the Inc  folder to your include path

To draw sprites:

    #include "SpriteBatch.h"
using namespace DirectX; std::unique_ptr<SpriteBatch> spriteBatch(new SpriteBatch(deviceContext)); spriteBatch->Begin(); spriteBatch->Draw(texture, XMFLOAT2(x, y)); spriteBatch->End();

To draw geometry using BasicEffect:

    #include "BasicEffect.h"
using namespace DirectX; std::unique_ptr<BasicEffect> effect(new BasicEffect(device)); effect->SetWorld(world); effect->SetView(view); effect->SetProjection(projection); effect->SetTexture(cat); effect->SetTextureEnabled(true); effect->EnableDefaultLighting(); effect->Apply(deviceContext); deviceContext->IASetInputLayout(...); deviceContext->IASetVertexBuffers(...); deviceContext->IASetIndexBuffer(...); deviceContext->IASetPrimitiveTopology(...); deviceContext->DrawIndexed(...);

To draw geometric primitives:

    #include "GeometricPrimitive.h"
using namespace DirectX; std::unique_ptr<GeometricPrimitive> teapot(GeometricPrimitive::CreateTeapot(deviceContext)); teapot->Draw(world, view, projection, Colors::CornflowerBlue);

See the readme inside the zip for more info.

Comments (35)

  1. Louis Castricato says:

    Did you implement sprite font? 😀

  2. jk says:

    Nice work … but Microsoft better provide a good DirectX managed wrapper for Metro-style apps very soon.

  3. ShawnHargreaves says:

    > Did you implement sprite font? 😀

    Not yet – would you find that useful?

  4. Roland "Glatzemann" Rosenkranz says:

    Just finished my own first version of a C++/DirectX-SpriteBatch for a indiedev.de Tutorial a hour ago. Your code is more complete. Nice work…

  5. Tili says:

    Nice! Also yes to the sprite font.

  6. Noogy says:

    >Not yet – would you find that useful?

    I would personally find that useful. I think that these ports are fantastic, thanks Shawn.

  7. Matthew says:

    This is freaking awesome — thank you!

  8. Amazing work. =)

    The font rendering would be very nice.

  9. Ultrahead says:

    > … but Microsoft better provide a good DirectX managed wrapper for Metro-style apps very soon.

    … you can use SharpDX, instead …

  10. Louis Castricato says:

    >> Did you implement sprite font? 😀

    >Not yet – would you find that useful?

    Yes, very much. My current text rendering engine is horrible

  11. Nice.. Might even give some Native code a try just to see how this goes.

  12. ChaosDev says:

    The C++ looks very nice in VS2011 )) Porting XNA to C++ is very nice idea ! Thank you Shawn !

  13. Pooya Eimandar says:

    it was fatastic.but this means that there wont be xna 5?right?

  14. ChaosDev says:

    We may hear news about XNA future on GDC(March 5-9)…

  15. Steve 'Sly' Williams says:

    I've been in C# land since the first prototypes of Windows Phones, that now when I go back to C++ I get depressed.  Not even SpriteBatch in C++ will help those doldrums.  The extra ridiculous syntax of C++11 just makes things worse.  Just this line

     std::unique_ptr<BasicEffect> effect(new BasicEffect(device));

    makes me want to cry.

    I will help do whatever it takes to use C# and XNA for Metro games, and the process is already underway in at least two projects (ANX Framework and soon in MonoGame) using the wonderful SharpDX.  Until Microsoft state the future of XNA one way or another, the people will provide.

  16. ChaosDev says:

    Shawn. Can you make a Game class too ?

  17. ShawnHargreaves says:

    The problem with Game type behavior is the way to do this is very different between Desktop and Metro.

    The Metro D3D samples actually include a pretty good starting point for this stuff.

  18. Filip Skakun says:

    I just imagined XNA released as an open source WinRT component library… 🙂

  19. Jim Theigh says:

    This doesn't bode well for the future of XNA on C#, does it?

  20. Steve 'Sly' Williams says:

    @Filip: XNA as a WinRT component won't happen due to the severe restrictions placed on classes exposed in WinRT components.  It can very easily happen as .NET assemblies though, and it will happen soon enough.

  21. Michael Hansen says:

    thanks for the nice sample as allways

    shawn i have one ?

    my engine is build in c++ as a lib

    ca i do the rest of the programming in c# with a wrapper on my own lib

    and sell it as a metro game , i rember some ware that i have read that metro app or game can not include any native code

    cource of the compiler wicth compile an ARM version and a X86 / version

    so when you sibmit to windwos store you will get an error ?

    can you help out here shawn

    allso so if i build it all as a directx 11 lib for the directx 11 games ca i use c# for my scripting lang

    it this allowed or must the whole game been built in c++ or the metro c#


  22. Giuseppe Maggiore says:


    As a professional XNA developer, having a clear path to migrate from XNA to DX 11 (if XNA 5.0 is not to be) would be wonderful (and it would actually keep me away from other platforms :D). Ideally this path would allow us to retain most of the XNA mindset/terminology, especially for models and the sprite batch.

    One of the main uses for those is the ability to put together a new gameplay prototype (often inside the main game itself) and then optimizing it with buffers and shaders as needed further on. The other important aspect is the ability to quickly see stuff on the screen, which is of paramount importance for students (I also teach in a game dev course at a local university); abandoning XNA would be equivalent to pushing a lot of students in the arms of Unity, which is quite incompatible with Visual Studio and the Microsoft ecosystem in general…

  23. Michael Hansen says:

    for who of you trying to port xna to directx 11

    here is a little help a full xmplantation of xna 4.0 refresh framework for ios 5 for ipad,iphone

    i took me 4 days last year to port my engine and get it to run on ios and have translate this framework to windows 8 c++ directx 11

    i took a round 1 week from ios to directx 11 windows 8

    allso the same framework is running on android it took 3 weeks , this framework is a great help to othere developers

    so you actuly can build you xna app for directx 11 and now with shawn shadres it is more rubust that ever



    so the greates indie framework can live on in our dreams on tablets

    hope this will help you all on how to implant xna in directx 11 c++ for tablets


  24. Javi says:

    Could you post one sample project using this static library?

  25. ShawnHargreaves says:


    Which things specifically would you want to see a sample for?  There is a lot of different code in this lib  🙂

  26. Javi says:

    A example using WICTextureLoader for loading a cat texture XD, and how to draw it using spritebatch.

    Another example using GeometryPrimitive with basicEffect.

  27. Jules says:

    This is pretty dire, and only of interest to C++ Geeks.

    If Microsoft think this is the way to engage Indie Game Developers, then they are way off beam.  XNA with C#  got a lot of us engaged  with Games Development on Windows aand Phones.

    Going back to this geeky C++  and DirectX may all be very clever, but its really only on a path to lose Indie Develoepr base.  

  28. ShawnHargreaves says:

    > only of interest to C++ Geeks

    There are a lot of very talented C++ programmers in the world 🙂  Why should they be deprived of goodness such as SpriteBatch or BasicEffect?  C# has had these things for many years, and now I ported them to C++ as well.  I don't understand how that is a bad thing?

  29. Saagar says:

    How do you create an instance of the type you used for textures in SpriteBatch.Draw?

  30. ShawnHargreaves says:

    Saagar: you can use any of the standard D3D11 APIs to create your textures.  An easy way would be Chuck's DDSTextureLoader code  (also part of DirectXTK).

  31. ChaosDev says:

    Hey Shawn ! How about write something like XNA on SharpDX for our community ?

  32. Saagar says:

    I have created a XNA like winrt api. I used some of your code in it. However, when I attempt to run a test it throws TypeLoadException.The code:


  33. Shawn is the Bomb says:

    Nough said ..

  34. Joe Plante says:

    Thank you for the port! Direct3D 11 didn't seem to have a good way to do a simple text render until this came along!

  35. Dave says:

    thank you so much for this. I used to have fun making many games in XNA. Im lost in c++ 🙁