SpriteBatch and BasicEffect for C++ Direct3D 11

Originally posted to Shawn Hargreaves Blog on MSDN, Friday, March 2, 2012

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


Supported operating systems:

Supported compilers:

How to use it:

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.

Blog index   -   Back to my homepage