SAGE
A Simple Academic Game Engine
Loading...
Searching...
No Matches
Sage::CSpriteRenderer Class Reference

The sprite renderer class. More...

#include <SageSpriteRenderer.h>

Inheritance diagram for Sage::CSpriteRenderer:
Collaboration diagram for Sage::CSpriteRenderer:

Public Member Functions

 CSpriteRenderer (eSpriteMode)
 Constructor.
 
virtual ~CSpriteRenderer ()
 Destructor.
 
template<class t >
void LoadMT (Sage::CMediaList< t > &)
 Load sprites multithreaded.
 
template<class t >
void Load (Sage::CMediaList< t > &)
 Load sprites.
 
const bool Loaded () const
 Sprite loading completed.
 
template<class t >
void Load (t, const char *)
 Load sprite.
 
void Initialize (size_t)
 Initialize.
 
void BeginFrame ()
 Begin frame.
 
void EndFrame ()
 End frame.
 
void Draw (const CSpriteDesc2D *)
 Draw single 2D sprite.
 
void Draw (const CSpriteDesc3D *)
 Draw single 3D sprite.
 
void Draw (std::vector< CSpriteDesc3D > &)
 Draw list of 3D sprites.
 
template<class t >
void Draw (t, const Vector2 &, float=0)
 Draw single 2D sprite.
 
template<class t >
void DrawLine (t, const Vector2 &, const Vector2 &)
 Draw 2D line.
 
template<class t >
void DrawLine (t, const Vector2 &, const Vector2 &, XMVECTORF32 c)
 Draw 2D line.
 
template<class t >
void DrawCircle (t, const Vector2 &, float)
 Draw circle.
 
template<class t >
void DrawBoundingBox (t, const BoundingBox &)
 Draw AABB.
 
void SetCameraPos (const Vector3 &)
 Set camera position.
 
const Vector3 & GetCameraPos () const
 Get camera position.
 
template<class t >
const float GetWidth (t) const
 Get sprite width.
 
template<class t >
const float GetHeight (t) const
 Get sprite height.
 
template<class t >
void GetSize (t, float &, float &) const
 Get sprite size.
 
template<class t >
void GetSize (t, UINT, float &, float &) const
 Get sprite size.
 
template<class t >
const size_t GetNumFrames (t) const
 Get number of frames.
 
const BoundingBox GetAabb (UINT, UINT) const
 Get bounding box.
 
const bool BoxInFrustum (const BoundingBox &) const
 Does the box overlap the view frustum?
 
- Public Member Functions inherited from Sage::CRenderer3D
 CRenderer3D ()
 Constructor.
 
virtual ~CRenderer3D ()
 Destructor.
 
void Initialize (bool=true)
 Initialize renderer.
 
void LoadTexture (const char *, CTextureDesc &)
 Load texture.
 
void LoadTextureFile (const char *, CTextureDesc &)
 Load texture from file.
 
void BeginResourceUpload ()
 Begin uploading textures.
 
void EndResourceUpload ()
 End uploading textures.
 
void SetBgColor (const XMVECTORF32 &)
 Set default background color.
 
void DrawScreenText (const char *, const Vector2 &, XMVECTORF32=Colors::Black, float=1.0f)
 Draw screen text.
 
void DrawScreenText (const wchar_t *, const Vector2 &, XMVECTORF32=Colors::Black, float=1.0f)
 Draw screen text, wide version.
 
void DrawCenteredText (const char *, XMVECTORF32=Colors::Black)
 Draw centered text.
 
void DrawCenteredText (const wchar_t *, XMVECTORF32=Colors::Black)
 Draw centered text, wide version.
 
void SaveScreenShot ()
 Save a screenshot.
 
virtual void OnDeviceLost () override
 Handler for device lost notification.
 
virtual void OnDeviceRestored () override
 Handler for device restored notification.
 
- Public Member Functions inherited from Sage::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 Sage::CSettingsManager
void Load ()
 Load game settings.
 

Protected Member Functions

void LoadByIndex (UINT, const char *)
 Load sprite by index.
 
CSpriteLoad (UINT, const char *, const char *, UINT)
 Load sprite.
 
void CreateEffect ()
 Create effect.
 
void CreateVertexBuffer ()
 Create vertex buffer.
 
void CreateIndexBuffer ()
 Create index buffer.
 
- Protected Member Functions inherited from Sage::CRenderer3D
void CreateDDSTexture (_In_z_ const wchar_t *, CTextureDesc &)
 Load a texture from a DirectDraw surface file (contains mipmaps).
 
void CreateWICTexture (_In_z_ const wchar_t *, CTextureDesc &)
 Load a texture from a an image file (does not contain mipmaps).
 
void ProcessTexture (_In_ Microsoft::WRL::ComPtr< ID3D12Resource >, CTextureDesc &)
 Process a loaded texture.
 
void LoadScreenFont ()
 Load screen font.
 
- Protected Member Functions inherited from Sage::CWindow
HWND CreateGameWindow (HINSTANCE)
 Create default window.
 
bool InitGameWindow (_In_ HINSTANCE)
 Initialize the game window.
 
void OpenDebugConsole ()
 Open debug console window.
 
void CloseDebugConsole ()
 Close debug console window.
 
- Protected Member Functions inherited from Sage::CSettingsManager
void SetWinSize (int, int)
 Set window size information.
 

Protected Attributes

eSpriteMode m_eRenderMode = eSpriteMode::Batched2D
 Sprite render mode.
 
CCameram_pCamera = nullptr
 Pointer to the camera.
 
CSprite ** m_pSprite = nullptr
 Sprite pointers.
 
size_t m_nNumSprites = 0
 Number of sprites.
 
bool m_bLoaded = false
 Sounds loaded flag.
 
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 Sage::CRenderer3D
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< DX::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] = {nullptr}
 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 Member Functions inherited from Sage::CWindow
static LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM)
 The Window Procedure.
 
static void GetBorderSizes (int &w, int &h)
 Get window border width and height.
 
static void EnforceAspectRatio (WPARAM, RECT *)
 Enforce aspect ratio.
 
static void EnforceAspectRatio (MINMAXINFO *)
 Enforce aspect ratio.
 
- Static Protected Attributes inherited from Sage::CComponent
static std::unique_ptr< CTimerm_pTimer = std::make_unique<CTimer>()
 Pointer to a timer.
 
static std::unique_ptr< CRandomm_pRandom = std::make_unique<CRandom>()
 Pointer to a PRNG.
 
static std::unique_ptr< CSoundm_pSound = std::make_unique<CSound>()
 Pointer to sound manager.
 
static std::unique_ptr< CKeyboardm_pKeyboard
 Pointer to a keyboard handler.
 
static std::unique_ptr< CMousem_pMouse = std::make_unique<CMouse>()
 Pointer to a mouse handler.
 
static std::unique_ptr< CControllerm_pController
 Pointer to a controller.
 
- Static Protected Attributes inherited from Sage::CCameraCommon
static XMFLOAT4X4 m_f4x4Orient
 Orientation matrix.
 
static XMFLOAT4X4 m_f4x4View
 View matrix.
 
static XMFLOAT4X4 m_f4x4Projection
 Projection matrix.
 
- Static Protected Attributes inherited from Sage::CWindow
static Vector2 m_vMousePos
 Mouse position.
 
- Static Protected Attributes inherited from Sage::CWindowDesc
static HWND m_Hwnd = 0
 Window handle.
 
static HINSTANCE m_hInst = 0
 Instance handle.
 
static bool m_bExitSizeMove = true
 User just finished moving/resizing window.
 
- Static Protected Attributes inherited from Sage::CSettingsManager
static tinyxml2::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 Sage::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.
 
- Static Protected Attributes inherited from Sage::CScreenshotFlag
static bool m_bScreenShot = false
 Screenshot flag.
 

Detailed Description

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

Constructor & Destructor Documentation

◆ CSpriteRenderer()

CSpriteRenderer::CSpriteRenderer ( eSpriteMode mode)

Construct a 3D renderer and a base camera.

Parameters
modeSprite render mode.
Here is the call graph for this function:

◆ ~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 Sage::CRenderer3D.

Here is the call graph for this function:

◆ 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.
Here is the call graph for this function:

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

Here is the caller graph for this function:

◆ CreateIndexBuffer()

void CSpriteRenderer::CreateIndexBuffer ( )
protected

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

Here is the caller graph for this function:

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

Here is the caller graph for this function:

◆ Draw() [1/4]

void CSpriteRenderer::Draw ( const CSpriteDesc2D * sd)

Draw a sprite in 2D.

Parameters
sdConst pointer to 2D sprite descriptor.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Draw() [2/4]

void CSpriteRenderer::Draw ( const CSpriteDesc3D * sd)

Draw a sprite in 3D.

Parameters
sdConst pointer to 3D sprite descriptor.

◆ Draw() [3/4]

void CSpriteRenderer::Draw ( std::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.
Here is the call graph for this function:

◆ Draw() [4/4]

template<class t >
void Sage::CSpriteRenderer::Draw ( t 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.
Here is the call graph for this function:

◆ DrawBoundingBox()

template<class t >
void Sage::CSpriteRenderer::DrawBoundingBox ( t 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.
Here is the call graph for this function:

◆ DrawCircle()

template<class t >
void Sage::CSpriteRenderer::DrawCircle ( t line,
const Vector2 & pos,
float r )

Draw a circle by drawing multiple copies of a sprite, which is assumed to contain a short line segment, tangent to a circle.

Template Parameters
tEnumerated type for sprites.
Parameters
lineLine A line sprite.
posPosition of center of circle in world space.
rRadius of circle in world space units.
Here is the call graph for this function:

◆ DrawLine() [1/2]

template<class t >
void Sage::CSpriteRenderer::DrawLine ( t line,
const Vector2 & p0,
const Vector2 & p1 )

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

Template Parameters
tEnumerated type for sprites.
Parameters
lineLine A line sprite.
p0Position of one end of the sprite in world space.
p1Position of the other end of the sprite in world space.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DrawLine() [2/2]

template<class t >
void Sage::CSpriteRenderer::DrawLine ( t line,
const Vector2 & p0,
const Vector2 & p1,
XMVECTORF32 c )

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

Template Parameters
tEnumerated type for sprites.
Parameters
lineLine A line sprite.
p0Position of one end of the sprite in world space.
p1Position of the other end of the sprite in world space.
cline color, such as Colors::Aqua.
Here is the call graph for this function:

◆ EndFrame()

void CSpriteRenderer::EndFrame ( )
virtual

End the SpriteBatch frame and present.

Reimplemented from Sage::CRenderer3D.

Here is the call graph for this function:

◆ GetAabb()

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

Construct the AABB for a sprite frame.

Parameters
nSprite index.
mFrame number.
Returns
AABB for sprite frame.
Here is the call graph for this function:

◆ GetCameraPos()

const Vector3 & CSpriteRenderer::GetCameraPos ( ) const

Reader function for camera position.

Returns
Camera position.
Here is the call graph for this function:

◆ GetHeight()

template<class t >
const float Sage::CSpriteRenderer::GetHeight ( t 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.
Here is the call graph for this function:

◆ GetNumFrames()

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

Reader function for number of frames in sprite.

Template Parameters
tSprite enumerated type.
Parameters
eSprite type.
Returns
Number of frames in sprite.
Here is the call graph for this function:

◆ GetSize() [1/2]

template<class t >
void Sage::CSpriteRenderer::GetSize ( t 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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetSize() [2/2]

template<class t >
void Sage::CSpriteRenderer::GetSize ( t 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.
Here is the call graph for this function:

◆ GetWidth()

template<class t >
const float Sage::CSpriteRenderer::GetWidth ( t 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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Load() [1/3]

template<class t >
void Sage::CSpriteRenderer::Load ( Sage::CMediaList< t > & media)

Load sprites synchronously from a media descriptor.

Template Parameters
tSprite enumerated type.
Parameters
mediaMedia list.
Here is the call graph for this function:

◆ Load() [2/3]

template<class t >
void Sage::CSpriteRenderer::Load ( t 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.
Here is the call graph for this function:

◆ Load() [3/3]

CSprite * CSpriteRenderer::Load ( UINT nIndex,
const char * szFile,
const char * szExt,
UINT nFrames )
protected

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

Parameters
nIndexSprite index.
szFileNull-terminated file name (without extension).
szExtNull-terminated file extension.
nFramesNumber of frames.
Returns
Pointer to the sprite created.
Here is the call graph for this function:

◆ LoadByIndex()

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

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

Parameters
indexSprite index.
nameObject name in XML file.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Loaded()

const bool CSpriteRenderer::Loaded ( ) const

Reader function for the loaded flag m_bLoaded.

Returns
true if loading completed.

◆ LoadMT()

template<class t >
void Sage::CSpriteRenderer::LoadMT ( Sage::CMediaList< t > & media)

Load sounds asynchronously from a media descriptor, that is, spawn a thread to do the loading. Member variable m_bLoaded will be set to true when loading is finished.

Template Parameters
tSprite enumerated type.
Parameters
mediaMedia list.
Here is the call graph for this function:

◆ SetCameraPos()

void CSpriteRenderer::SetCameraPos ( const Vector3 & pos)

Set the camera position.

Parameters
posNew camera position.
Here is the call graph for this function: