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

The sprite renderer class. More...

#include <SpriteRenderer.h>

Inheritance diagram for LSpriteRenderer:
LRenderer3D LComponent LCameraCommon LWindow LWindowDesc LSettingsManager LSettings

Public Member Functions

 LSpriteRenderer (eSpriteMode)
 Constructor. More...
 
virtual ~LSpriteRenderer ()
 Destructor. More...
 
template<class t >
void Initialize (t n)
 Initialize. More...
 
void BeginFrame ()
 Begin frame. More...
 
void EndFrame ()
 End frame. More...
 
void Draw (const LSpriteDesc2D *)
 Draw single 2D sprite. More...
 
template<class t >
void Draw (t, const Vector2 &, float=0)
 Draw single 2D sprite. More...
 
template<class t >
void DrawLine (t, const Vector2 &, const Vector2 &)
 Draw 2D line. More...
 
template<class t >
void DrawBoundingBox (t, const BoundingBox &)
 Draw AABB. More...
 
void Draw (const LSpriteDesc3D *)
 Draw single 3D sprite. More...
 
void Draw (std::vector< LSpriteDesc3D > &)
 Draw list of 3D sprites. More...
 
void SetCameraPos (const Vector3 &)
 Set camera position. More...
 
const Vector3 & GetCameraPos () const
 Get camera position. More...
 
template<class t >
void Load (t, const char *)
 Load sprite. More...
 
template<class t >
const float GetWidth (t) const
 Get sprite width. More...
 
template<class t >
const float GetHeight (t) const
 Get sprite height. More...
 
template<class t >
void GetSize (t, float &, float &) const
 Get sprite size. More...
 
template<class t >
void GetSize (t, UINT, float &, float &) const
 Get sprite size. More...
 
template<class t >
const size_t GetNumFrames (t) 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 LRenderer3D
 LRenderer3D ()
 Constructor.
 
virtual ~LRenderer3D ()
 Destructor.
 
void Initialize (bool=true)
 Initialize renderer. More...
 
void LoadTexture (const char *, LTextureDesc &)
 Load texture. More...
 
void LoadTextureFile (const char *, LTextureDesc &)
 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 LWindow
template<typename t0 , typename t1 , typename t2 >
int WinMain (_In_ HINSTANCE, bool, const t0 &, const t1 &, const t2 &)
 WinMain.
 
- Public Member Functions inherited from LSettingsManager
void Load ()
 Load game settings. More...
 

Protected Member Functions

void LoadByIndex (UINT, const char *)
 Load sprite by index. More...
 
LSpriteLoad (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 LRenderer3D
void CreateDDSTexture (_In_z_ const wchar_t *, LTextureDesc &)
 Load a texture from a DirectDraw surface file (contains mipmaps). More...
 
void CreateWICTexture (_In_z_ const wchar_t *, LTextureDesc &)
 Load a texture from a an image file (does not contain mipmaps). More...
 
void ProcessTexture (_In_ ComPtr< ID3D12Resource >, LTextureDesc &)
 Process a loaded texture. More...
 
void LoadScreenFont ()
 Load screen font. More...
 
- Protected Member Functions inherited from LSettingsManager
void SetWinSize (int, int)
 Set window size information. More...
 

Protected Attributes

eSpriteMode m_eRenderMode = eSpriteMode::Batched2D
 Sprite render mode.
 
LBaseCameram_pCamera = nullptr
 Pointer to the camera.
 
LSprite ** m_pSprite = nullptr
 Sprite pointers.
 
size_t m_nNumSprites = 0
 Number of sprites.
 
std::unique_ptr< BasicEffect > m_pSpriteEffect
 Sprite effect.
 
std::map< std::string, UINT > m_mapNameToIndex
 Map tag name to sprite index.
 
GraphicsResource m_VertexBuffer
 Vertex buffer.
 
GraphicsResource m_IndexBuffer
 Index buffer.
 
UINT m_nIndexCount = 0
 Number of indexes in the index buffer.
 
std::shared_ptr< D3D12_VERTEX_BUFFER_VIEW > m_pVBufView
 Vertex buffer view.
 
std::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 LRenderer3D
std::unique_ptr< GraphicsMemory > m_pGraphicsMemory
 Pointer to graphics memory.
 
std::unique_ptr< DescriptorHeap > m_pDescriptorHeap
 Pointer to a descriptor heap.
 
UINT m_nNumResourceDesc = 0
 Number of resource descriptors in descriptor heap.
 
std::unique_ptr< CommonStates > m_pStates
 Pointer to render state object.
 
std::unique_ptr< SpriteBatch > m_pSpriteBatch
 SpriteBatch object for rendering sprites in screen space (from the DirectXTK).
 
std::unique_ptr< PrimitiveBatch< VertexPositionColor > > m_pPrimitiveBatch
 PrimitiveBatch object from the DirectXTK.
 
std::unique_ptr< SpriteBatch > m_pTextSpriteBatch
 SpriteBatch object for rendering text in screen space.
 
std::unique_ptr< SpriteFont > m_pFont
 Text font.
 
std::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 LComponent
static LTimerm_pTimer = &cTimer
 Pointer to a timer. More...
 
static LRandomm_pRandom = &cRandom
 Pointer to a PRNG. More...
 
static LKeyboardm_pKeyboard = &cKeyboard
 Pointer to a keyboard handler. More...
 
static LXBoxControllerm_pController = &cController
 Pointer to a XBox controller. More...
 
static LSoundm_pAudio = &cAudio
 Pointer to an audio player. More...
 
- Static Protected Attributes inherited from LCameraCommon
static XMFLOAT4X4 m_orient
 Orientation matrix.
 
static XMFLOAT4X4 m_view
 View matrix.
 
static XMFLOAT4X4 m_projection
 Projection matrix.
 
- Static Protected Attributes inherited from LWindowDesc
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 LSettingsManager
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 LSettings
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.

Constructor & Destructor Documentation

◆ LSpriteRenderer()

LSpriteRenderer::LSpriteRenderer ( eSpriteMode  mode)

Construct a 3D renderer and a base camera.

Parameters
modeSprite render mode.

◆ ~LSpriteRenderer()

LSpriteRenderer::~LSpriteRenderer ( )
virtual

Clean up resources from GPU and wait for completion.

Member Function Documentation

◆ BeginFrame()

void LSpriteRenderer::BeginFrame ( )
virtual

Initialize the render pipeline and the SpriteBatch.

Reimplemented from LRenderer3D.

◆ BoxInFrustum()

const bool LSpriteRenderer::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 LSpriteRenderer::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 LSpriteRenderer::CreateIndexBuffer ( )
protected

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

◆ CreateVertexBuffer()

void LSpriteRenderer::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 LSpriteRenderer::Draw ( const LSpriteDesc2D sd)

Draw a sprite in 2D.

Parameters
sdConst pointer to 2D sprite descriptor.

◆ Draw() [2/4]

void LSpriteRenderer::Draw ( const LSpriteDesc3D sd)

Draw a sprite in 3D.

Parameters
sdConst pointer to 3D sprite descriptor.

◆ Draw() [3/4]

void LSpriteRenderer::Draw ( std::vector< LSpriteDesc3D > &  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.

◆ Draw() [4/4]

template<class t >
void LSpriteRenderer::Draw ( 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.

Template Parameters
tEnumerated type for sprites.
Parameters
nSprite type.
posPosition in world space.
aOrientation angle (roll) in radians.

◆ DrawBoundingBox()

template<class t >
void LSpriteRenderer::DrawBoundingBox ( n,
const BoundingBox &  aabb 
)

Draw an axially aligned bounding box.

Template Parameters
tEnumerated type for sprites.
Parameters
nLine sprite type for AABB edges.
aabbAn axially aligned bounding box.

◆ DrawLine()

template<class t >
void LSpriteRenderer::DrawLine ( 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.

Template Parameters
tEnumerated type for sprites.
Parameters
nLine sprite type.
p0Position of one end of the sprite in world space.
p1Position of the other end of the sprite in world space.

◆ EndFrame()

void LSpriteRenderer::EndFrame ( )
virtual

End the SpriteBatch frame and present.

Reimplemented from LRenderer3D.

◆ GetAabb()

const BoundingBox LSpriteRenderer::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 & LSpriteRenderer::GetCameraPos ( ) const

Reader function for camera position.

Returns
Camera position.

◆ GetHeight()

template<class t >
const float LSpriteRenderer::GetHeight ( e) const

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

Template Parameters
tSprite enumerated type.
Parameters
eSprite type.
Returns
Height in pixels.

◆ GetNumFrames()

template<class t >
const size_t LSpriteRenderer::GetNumFrames ( e) const

Reader function for number of frames in sprite.

Template Parameters
tSprite enumerated type.
Parameters
eSprite type.
Returns
Number of frames in sprite.

◆ GetSize() [1/2]

template<class t >
void LSpriteRenderer::GetSize ( e,
float &  x,
float &  y 
) const

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

Template Parameters
tSprite enumerated type.
Parameters
eSprite type.
x[out] Width of frame in pixels.
y[out] Height of frame in pixels.

◆ GetSize() [2/2]

template<class t >
void LSpriteRenderer::GetSize ( e,
UINT  m,
float &  x,
float &  y 
) const

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

Template Parameters
tSprite enumerated type.
Parameters
eSprite type.
m[in] Frame number, assumed to be in range.
x[out] Width of frame in pixels.
y[out] Height of frame in pixels.

◆ GetWidth()

template<class t >
const float LSpriteRenderer::GetWidth ( e) const

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

Template Parameters
tSprite enumerated type.
Parameters
eSprite type.
Returns
Width in pixels.

◆ Initialize()

template<class t >
void LSpriteRenderer::Initialize ( n)

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

Template Parameters
tSprite enumerated type.
Parameters
nNumber of sprites.

◆ Load() [1/2]

template<class t >
void LSpriteRenderer::Load ( e,
const char *  name 
)

Load information about a sprite from global variable g_xmlSettings, then load the sprite images as per that information. Abort if something goes wrong. Uses LoadByIndex() to do the actual work.

Template Parameters
tSprite enumerated type.
Parameters
eSprite type.
nameSprite name in XML file.

◆ Load() [2/2]

LSprite * LSpriteRenderer::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.

◆ LoadByIndex()

void LSpriteRenderer::LoadByIndex ( UINT  index,
const char *  name 
)
protected

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 LSpriteRenderer::SetCameraPos ( const Vector3 &  pos)

Set the camera position.

Parameters
posNew camera position.