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

Changelog:

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

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

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

    skydrive.live.com/edit.aspx

  • Nough said ..

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

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

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