Skip to content

Commit

Permalink
DirectXTK: Added rotation support to SpriteBatch to simplify handling…
Browse files Browse the repository at this point in the history
… of Windows Store/Phone orientation changes
  • Loading branch information
walbourn_cp authored and walbourn_cp committed Jan 22, 2014
1 parent 31b5be9 commit 1760935
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Inc/SpriteBatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ namespace DirectX
void XM_CALLCONV Draw(_In_ ID3D11ShaderResourceView* texture, RECT const& destinationRectangle, FXMVECTOR color = Colors::White);
void XM_CALLCONV Draw(_In_ ID3D11ShaderResourceView* texture, RECT const& destinationRectangle, _In_opt_ RECT const* sourceRectangle, FXMVECTOR color = Colors::White, float rotation = 0, XMFLOAT2 const& origin = Float2Zero, SpriteEffects effects = SpriteEffects_None, float layerDepth = 0);

// Rotation mode to be applied to the sprite transformation
void SetRotation( DXGI_MODE_ROTATION mode );
DXGI_MODE_ROTATION GetRotation() const;

private:
// Private implementation.
class Impl;
Expand Down
67 changes: 56 additions & 11 deletions Src/SpriteBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ __declspec(align(16)) class SpriteBatch::Impl : public AlignedNew<SpriteBatch::I
static_assert((SpriteEffects_FlipBoth & (SourceInTexels | DestSizeInPixels)) == 0, "Flag bits must not overlap");
};

DXGI_MODE_ROTATION mRotation;

private:
// Implementation helper methods.
Expand All @@ -72,7 +73,7 @@ __declspec(align(16)) class SpriteBatch::Impl : public AlignedNew<SpriteBatch::I
static void XM_CALLCONV RenderSprite(_In_ SpriteInfo const* sprite, _Out_cap_c_(VerticesPerSprite) VertexPositionColorTexture* vertices, FXMVECTOR textureSize, FXMVECTOR inverseTextureSize);

static XMVECTOR GetTextureSize(_In_ ID3D11ShaderResourceView* texture);
static XMMATRIX GetViewportTransform(_In_ ID3D11DeviceContext* deviceContext);
static XMMATRIX GetViewportTransform(_In_ ID3D11DeviceContext* deviceContext, DXGI_MODE_ROTATION rotation );


// Constants.
Expand Down Expand Up @@ -329,7 +330,8 @@ void SpriteBatch::Impl::ContextResources::CreateVertexBuffer()

// Per-SpriteBatch constructor.
SpriteBatch::Impl::Impl(_In_ ID3D11DeviceContext* deviceContext)
: mSpriteQueueCount(0),
: mRotation( DXGI_MODE_ROTATION_IDENTITY ),
mSpriteQueueCount(0),
mSpriteQueueArraySize(0),
mInBeginEndPair(false),
mSortMode(SpriteSortMode_Deferred),
Expand Down Expand Up @@ -526,7 +528,7 @@ void SpriteBatch::Impl::PrepareForRendering()
deviceContext->IASetIndexBuffer(mDeviceResources->indexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0);

// Set the viewport transform matrix.
XMMATRIX transformMatrix = mTransformMatrix * GetViewportTransform(deviceContext);
XMMATRIX transformMatrix = mTransformMatrix * GetViewportTransform(deviceContext, mRotation);

mContextResources->constantBuffer.SetData(deviceContext, transformMatrix);

Expand Down Expand Up @@ -862,7 +864,7 @@ XMVECTOR SpriteBatch::Impl::GetTextureSize(_In_ ID3D11ShaderResourceView* textur


// Generates a viewport transform matrix for rendering sprites using x-right y-down screen pixel coordinates.
XMMATRIX SpriteBatch::Impl::GetViewportTransform(_In_ ID3D11DeviceContext* deviceContext)
XMMATRIX SpriteBatch::Impl::GetViewportTransform(_In_ ID3D11DeviceContext* deviceContext, DXGI_MODE_ROTATION rotation )
{
// Look up the current viewport.
D3D11_VIEWPORT viewport;
Expand All @@ -877,13 +879,44 @@ XMMATRIX SpriteBatch::Impl::GetViewportTransform(_In_ ID3D11DeviceContext* devic
float xScale = (viewport.Width > 0) ? 2.0f / viewport.Width : 0.0f;
float yScale = (viewport.Height > 0) ? 2.0f / viewport.Height : 0.0f;

return XMMATRIX
(
xScale, 0, 0, 0,
0, -yScale, 0, 0,
0, 0, 1, 0,
-1, 1, 0, 1
);
switch( rotation )
{
case DXGI_MODE_ROTATION_ROTATE90:
return XMMATRIX
(
0, -yScale, 0, 0,
-xScale, 0, 0, 0,
0, 0, 1, 0,
1, 1, 0, 1
);

case DXGI_MODE_ROTATION_ROTATE270:
return XMMATRIX
(
0, yScale, 0, 0,
xScale, 0, 0, 0,
0, 0, 1, 0,
-1, -1, 0, 1
);

case DXGI_MODE_ROTATION_ROTATE180:
return XMMATRIX
(
-xScale, 0, 0, 0,
0, yScale, 0, 0,
0, 0, 1, 0,
1, -1, 0, 1
);

default:
return XMMATRIX
(
xScale, 0, 0, 0,
0, -yScale, 0, 0,
0, 0, 1, 0,
-1, 1, 0, 1
);
}
}


Expand Down Expand Up @@ -1003,3 +1036,15 @@ void XM_CALLCONV SpriteBatch::Draw(_In_ ID3D11ShaderResourceView* texture, RECT

pImpl->Draw(texture, destination, sourceRectangle, color, originRotationDepth, effects | Impl::SpriteInfo::DestSizeInPixels);
}


void SpriteBatch::SetRotation( DXGI_MODE_ROTATION mode )
{
pImpl->mRotation = mode;
}


DXGI_MODE_ROTATION SpriteBatch::GetRotation() const
{
return pImpl->mRotation;
}

0 comments on commit 1760935

Please sign in to comment.