The LARC Game Engine
A Simple Game Engine from the Laboratory for Recreational Computing
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
CSpriteRenderer Class Reference

The sprite renderer class. More...

#include <SpriteRenderer.h>

Inheritance diagram for CSpriteRenderer:
CRenderer3D CComponent CCameraCommon CWindow CWindowDesc CSettingsManager CSettings

Public Types

enum  eRenderMode { Batched2D, Unbatched2D, Unbatched3D }
 Render mode type. More...
 

Public Member Functions

 CSpriteRenderer (eRenderMode)
 Constructor. More...
 
virtual ~CSpriteRenderer ()
 Destructor. More...
 
void Initialize (size_t n)
 Initialize. More...
 
void BeginFrame ()
 Begin frame. More...
 
void EndFrame ()
 End frame. More...
 
void Draw (const CSpriteDesc2D &)
 Draw single 2D sprite. More...
 
void Draw (int n, const Vector2 &, float=0)
 Draw single 2D sprite. More...
 
void DrawLine (UINT, const Vector2 &, const Vector2 &)
 Draw 2D line. More...
 
void Draw (const CSpriteDesc3D &)
 Draw single 3D sprite. More...
 
void Draw (vector< CSpriteDesc3D > &)
 Draw list of 3D sprites. More...
 
void SetCameraPos (const Vector3 &)
 Set camera position. More...
 
const Vector3 & GetCameraPos () const
 Get camera position. More...
 
void Load (UINT, const char *)
 Load sprite. More...
 
const float GetWidth (UINT) const
 Get sprite width. More...
 
const float GetHeight (UINT) const
 Get sprite height. More...
 
void GetSize (UINT, float &, float &) const
 Get sprite size. More...
 
void GetSize (UINT, UINT, float &, float &) const
 Get sprite size. More...
 
const size_t GetNumFrames (UINT) const
 Get number of frames. More...
 
const BoundingBox GetAabb (int, int) const
 Get bounding box. More...
 
const bool BoxInFrustum (const BoundingBox &) const
 Does the box overlap the view frustum? More...
 
- Public Member Functions inherited from CRenderer3D
 CRenderer3D ()
 Constructor.
 
virtual ~CRenderer3D ()
 Destructor.
 
void Initialize (bool=true)
 Initialize renderer. More...
 
void LoadTexture (const char *, CTextureDesc &)
 Load texture. More...
 
void LoadTextureFile (const char *, CTextureDesc &)
 Load texture from file. More...
 
void BeginResourceUpload ()
 Begin uploading textures. More...
 
void EndResourceUpload ()
 End uploading textures. More...
 
void SetBgColor (const XMVECTORF32 &)
 Set default background color. More...
 
void DrawScreenText (const char *, const Vector2 &, XMVECTORF32=Colors::Black)
 Draw screen text. More...
 
void DrawScreenText (const wchar_t *, const Vector2 &, XMVECTORF32=Colors::Black)
 Draw screen text, wide version. More...
 
void DrawCenteredText (const char *, XMVECTORF32=Colors::Black)
 Draw centered text. More...
 
void DrawCenteredText (const wchar_t *, XMVECTORF32=Colors::Black)
 Draw centered text, wide version. More...
 
void SaveScreenShot ()
 Save a screenshot. More...
 
virtual void OnDeviceLost () override
 Handler for device lost notification. More...
 
virtual void OnDeviceRestored () override
 Handler for device restored notification. More...
 
- Public Member Functions inherited from CWindow
template<typename t0 , typename t1 , typename t2 >
int WinMain (_In_ HINSTANCE, bool, const t0 &, const t1 &, const t2 &)
 WinMain.
 
- Public Member Functions inherited from CSettingsManager
void Load ()
 Load game settings. More...
 

Protected Member Functions

CSpriteLoad (UINT, const char *, const char *, UINT)
 Load sprite. More...
 
void CreateEffect ()
 Create effect. More...
 
void CreateVertexBuffer ()
 Create vertex buffer. More...
 
void CreateIndexBuffer ()
 Create index buffer. More...
 
- Protected Member Functions inherited from CRenderer3D
void CreateDDSTexture (_In_z_ const wchar_t *, CTextureDesc &)
 Load a texture from a DirectDraw surface file (contains mipmaps). More...
 
void CreateWICTexture (_In_z_ const wchar_t *, CTextureDesc &)
 Load a texture from a an image file (does not contain mipmaps). More...
 
void ProcessTexture (_In_ ComPtr< ID3D12Resource >, CTextureDesc &)
 Process a loaded texture. More...
 
void LoadScreenFont ()
 Load screen font. More...
 
- Protected Member Functions inherited from CSettingsManager
void SetWinSize (int, int)
 Set window size information. More...
 

Protected Attributes

eRenderMode m_eRenderMode = Batched2D
 Sprite render mode.
 
CBaseCameram_pCamera = nullptr
 Pointer to the camera.
 
CSprite ** m_pSprite = nullptr
 Sprite pointers.
 
size_t m_nNumSprites = 0
 Number of sprites.
 
unique_ptr< BasicEffect > m_pSpriteEffect
 Sprite effect.
 
GraphicsResource m_VertexBuffer
 Vertex buffer.
 
GraphicsResource m_IndexBuffer
 Index buffer.
 
UINT m_nIndexCount = 0
 Number of indexes in the index buffer.
 
shared_ptr< D3D12_VERTEX_BUFFER_VIEW > m_pVBufView
 Vertex buffer view.
 
shared_ptr< D3D12_INDEX_BUFFER_VIEW > m_pIBufView
 Index buffer view.
 
float m_fCurZ = FLT_MAX
 Current depth for unbatched 2D rendering.
 
- Protected Attributes inherited from CRenderer3D
unique_ptr< GraphicsMemory > m_pGraphicsMemory
 Pointer to graphics memory.
 
unique_ptr< DescriptorHeap > m_pDescriptorHeap
 Pointer to a descriptor heap.
 
UINT m_nNumResourceDesc = 0
 Number of resource descriptors in descriptor heap.
 
unique_ptr< CommonStates > m_pStates
 Pointer to render state object.
 
unique_ptr< SpriteBatch > m_pSpriteBatch
 SpriteBatch object for rendering sprites in screen space (from the DirectXTK).
 
unique_ptr< PrimitiveBatch< VertexPositionColor > > m_pPrimitiveBatch
 PrimitiveBatch object from the DirectXTK.
 
unique_ptr< SpriteBatch > m_pTextSpriteBatch
 SpriteBatch object for rendering text in screen space.
 
unique_ptr< SpriteFont > m_pFont
 Text font.
 
unique_ptr< DeviceResources > m_pDeviceResources
 Pointer to device resources.
 
ID3D12Device * m_pD3DDevice = nullptr
 Pointer to the D3D device.
 
ResourceUploadBatch * m_pResourceUpload = nullptr
 Pointer to the resource upload batch object.
 
ID3D12GraphicsCommandList * m_pCommandList = nullptr
 Pointer to the command list.
 
ID3D12DescriptorHeap * m_pHeaps [2]
 A pair of pointers to heaps.
 
RenderTargetState m_RenderTargetState
 The render target state.
 
XMVECTORF32 m_f32BgColor = Colors::White
 The default background color.
 

Additional Inherited Members

- Static Protected Attributes inherited from CComponent
static CStepTimerm_pStepTimer = &cStepTimer
 Pointer to a step timer. More...
 
static CRandomm_pRandom = &cRandom
 Pointer to a PRNG. More...
 
static CKeyboardm_pKeyboard = &cKeyboard
 Pointer to a keyboard handler. More...
 
static CXBoxControllerm_pController = &cController
 Pointer to a XBox controller. More...
 
static CAudiom_pAudio = &cAudio
 Pointer to an audio player. More...
 
- Static Protected Attributes inherited from CCameraCommon
static XMFLOAT4X4 m_orient
 Orientation matrix.
 
static XMFLOAT4X4 m_view
 View matrix.
 
static XMFLOAT4X4 m_projection
 Projection matrix.
 
- Static Protected Attributes inherited from CWindowDesc
static HWND m_Hwnd = 0
 Window handle.
 
static HINSTANCE m_hInst = 0
 Instance handle.
 
static bool m_bExitSizeMove
 User just finished moving/resizing window.
 
- Static Protected Attributes inherited from CSettingsManager
static XMLElement * m_pXmlSettings = nullptr
 Pointer to the settings tag in the XML settings file.
 
static float m_fAspectRatio = 1.0f
 Aspect ratio, width/ht.
 
- Static Protected Attributes inherited from CSettings
static char m_szName [MAX_PATH]
 Name of this game.
 
static int m_nWinWidth = 0
 Window width in pixels.
 
static int m_nWinHeight = 0
 Window height in pixels.
 
static Vector2 m_vWinCenter = Vector2::Zero
 Window center.
 

Detailed Description

A renderer that will draw sprites in 2D and 3D.

Member Enumeration Documentation

◆ eRenderMode

Render modes supported are batched 2D (which uses SpriteBatch from DirectXTK 12 for speed), and unbatched 2D and 3D.

Constructor & Destructor Documentation

◆ CSpriteRenderer()

CSpriteRenderer::CSpriteRenderer ( eRenderMode  mode)

Construct a 3D renderer and a base camera.

Parameters
modeSprite render mode.

◆ ~CSpriteRenderer()

CSpriteRenderer::~CSpriteRenderer ( )
virtual

Clean up resources from GPU and wait for completion.

Member Function Documentation

◆ BeginFrame()

void CSpriteRenderer::BeginFrame ( )
virtual

Initialize the render pipeline and the SpriteBatch.

Reimplemented from CRenderer3D.

◆ BoxInFrustum()

const bool CSpriteRenderer::BoxInFrustum ( const BoundingBox &  box) const

AABB intersect view frustum test.

Parameters
boxAn AABB.
Returns
true if the box overlaps the view frustum of the camera.

◆ CreateEffect()

void CSpriteRenderer::CreateEffect ( )
protected

Construct a pipeline description for the sprite and use that to create an effect. Set its projection matrix to be the same as the global projection matrix.

◆ CreateIndexBuffer()

void CSpriteRenderer::CreateIndexBuffer ( )
protected

Create an index buffer and index buffer view for the sprite quad.

◆ CreateVertexBuffer()

void CSpriteRenderer::CreateVertexBuffer ( )
protected

Create a vertex buffer containing the four vertices of a quad centered at the origin with unit width and height. The vertices have position and texture coordinates. Create the corresponding vertex buffer view.

◆ Draw() [1/4]

void CSpriteRenderer::Draw ( const CSpriteDesc2D sd)

Draw a sprite in 2D.

Parameters
sd2D sprite descriptor.

◆ Draw() [2/4]

void CSpriteRenderer::Draw ( int  n,
const Vector2 &  pos,
float  a = 0 
)

Shorthand for drawing a 2D sprite with only index, position, and orientation. The other sprite descriptor fields are set to default values.

Parameters
nSprite index.
posPosition in world space.
aOrientation angle (roll) in radians.

◆ Draw() [3/4]

void CSpriteRenderer::Draw ( const CSpriteDesc3D sd)

Draw a sprite in 3D.

Parameters
sd3D sprite descriptor.

◆ Draw() [4/4]

void CSpriteRenderer::Draw ( vector< CSpriteDesc3D > &  renderlist)

Depth sort a render list using a vector of pointers, then draw them from back to front.

Parameters
renderlistA vector of 3D sprite descriptors of the sprites to be rendered.

◆ DrawLine()

void CSpriteRenderer::DrawLine ( UINT  n,
const Vector2 &  p0,
const Vector2 &  p1 
)

Draw a line by or by stretching a sprite, which is assumed to contain a short line segment.

Parameters
nLine sprite index.
p0Position of one end of the sprite in world space.
p1Position of the other end of the sprite in world space.

◆ EndFrame()

void CSpriteRenderer::EndFrame ( )
virtual

End the SpriteBatch frame and present.

Reimplemented from CRenderer3D.

◆ GetAabb()

const BoundingBox CSpriteRenderer::GetAabb ( int  n,
int  m 
) const

Construct the AABB for a sprite frame.

Parameters
nSprite index.
mFrame number.
Returns
AABB for sprite frame.

◆ GetCameraPos()

const Vector3 & CSpriteRenderer::GetCameraPos ( ) const

Reader function for camera position.

Returns
Camera position.

◆ GetHeight()

const float CSpriteRenderer::GetHeight ( UINT  n) const

Reader function for the height of frame zero of a sprite.

Parameters
nSprite index, assumed to be in range.
Returns
Height in pixels.

◆ GetNumFrames()

const size_t CSpriteRenderer::GetNumFrames ( UINT  n) const

Reader function for number of frames in sprite.

Parameters
nSprite index.
Returns
Number of frames in sprite.

◆ GetSize() [1/2]

void CSpriteRenderer::GetSize ( UINT  n,
float &  x,
float &  y 
) const

Reader function for the width and height of frame zero of a sprite.

Parameters
n[in] Sprite index, assumed to be in range.
x[out] Width of frame in pixels.
y[out] Height of frame in pixels.

◆ GetSize() [2/2]

void CSpriteRenderer::GetSize ( UINT  n,
UINT  m,
float &  x,
float &  y 
) const

Reader function for the width and height of a sprite frame.

Parameters
n[in] Sprite index, assumed to be in range.
m[in] Frame number, assumed to be in range.
x[out] Width of frame in pixels.
y[out] Height of frame in pixels.

◆ GetWidth()

const float CSpriteRenderer::GetWidth ( UINT  n) const

Reader function for the width of frame zero of a sprite.

Parameters
nSprite index, assumed to be in range.
Returns
Width in pixels.

◆ Initialize()

void CSpriteRenderer::Initialize ( size_t  n)

Reserve space for the sprites, create sprite effect and create vertex and index buffers if the renderer is not in batched mode.

Parameters
nNumber of sprites.

◆ Load() [1/2]

CSprite * CSpriteRenderer::Load ( UINT  index,
const char *  file,
const char *  ext,
UINT  frames 
)
protected

Given a file name and extension such as foo and png, read in sprite frames from foo0.png, foo1.png, etc.

Parameters
indexSprite index.
fileFile name prefix
extFile name extension
framesNumber of frames

◆ Load() [2/2]

void CSpriteRenderer::Load ( UINT  index,
const char *  name 
)

Load information about the sprite from global variable g_xmlSettings, then load the sprite images as per that information. Abort if something goes wrong.

Parameters
indexSprite index.
nameObject name in XML file.

◆ SetCameraPos()

void CSpriteRenderer::SetCameraPos ( const Vector3 &  pos)

Set the camera position.

Parameters
posNew camera position.