SpriteBatch and BasicEffect for C++ Direct3D 11

SpriteBatch and BasicEffect for C++ Direct3D 11

  • Comments 35

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.

  • Did you implement sprite font? :D

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

  • > Did you implement sprite font? :D

    Not yet - would you find that useful?

  • 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...

  • Nice! Also yes to the sprite font.

  • >Not yet - would you find that useful?

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

  • This is freaking awesome -- thank you!

  • Amazing work. =)

    The font rendering would be very nice.

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

    ... you can use SharpDX, instead ...

  • >> Did you implement sprite font? :D

    >Not yet - would you find that useful?

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

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

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

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

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

  • 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.

Page 1 of 3 (35 items) 123
Leave a Comment
  • Please add 2 and 7 and type the answer here:
  • Post