MAGE  v0.171.0
Matthias Advanced Game Engine
mage::rendering::SpriteBatch::Impl Class Reference

Public Member Functions

 Impl (ID3D11Device &device, ID3D11DeviceContext &device_context)
 
 Impl (const Impl &sprite_batch)=delete
 
 Impl (Impl &&sprite_batch) noexcept
 
 ~Impl ()
 
Imploperator= (const Impl &sprite_batch)=delete
 
Imploperator= (Impl &&sprite_batch) noexcept
 
void Begin (SpriteSortMode sort_mode=SpriteSortMode::Deferred)
 
void XM_CALLCONV Draw (ID3D11ShaderResourceView *texture, FXMVECTOR color, SpriteEffect effects, const SpriteTransform2D &transform, const RECT *source=nullptr)
 
void End ()
 

Private Member Functions

void BindFixedState ()
 
void FlushBatch ()
 
void SortSprites ()
 
void Render (ID3D11ShaderResourceView *texture, const SpriteInfo *const *sprites, std::size_t nb_sprites)
 
void XM_CALLCONV PrepareSprite (const SpriteInfo &sprite, VertexPositionColorTexture *vertices, FXMVECTOR texture_size, FXMVECTOR inverse_texture_size) noexcept
 

Private Attributes

std::reference_wrapper< ID3D11DeviceContext > m_device_context
 
UniquePtr< SpriteBatchMeshm_mesh
 
std::size_t m_mesh_position
 
bool m_in_begin_end_pair
 
SpriteSortMode m_sort_mode
 
AlignedVector< SpriteInfo > m_sprites
 
std::vector< const SpriteInfo *> m_sorted_sprites
 

Static Private Attributes

static constexpr std::size_t s_initial_capacity = 64u
 

Detailed Description

A class of sprite batches.

Constructor & Destructor Documentation

◆ Impl() [1/3]

mage::rendering::SpriteBatch::Impl::Impl ( ID3D11Device &  device,
ID3D11DeviceContext &  device_context 
)
explicit

Constructs a sprite batch.

Parameters
[in,out]deviceA reference to the device.
[in,out]device_contextA reference to the device context.

◆ Impl() [2/3]

mage::rendering::SpriteBatch::Impl::Impl ( const Impl sprite_batch)
delete

Constructs a sprite batch from the given sprite batch.

Parameters
[in]sprite_batchA reference to the sprite batch to copy.

◆ Impl() [3/3]

mage::rendering::SpriteBatch::Impl::Impl ( Impl &&  sprite_batch)
defaultnoexcept

Constructs a sprite batch by moving the given sprite batch.

Parameters
[in]sprite_batchA reference to the sprite batch to move.

◆ ~Impl()

mage::rendering::SpriteBatch::Impl::~Impl ( )
default

Destructs this sprite batch.

Member Function Documentation

◆ Begin()

void mage::rendering::SpriteBatch::Impl::Begin ( SpriteSortMode  sort_mode = SpriteSortMode::Deferred)

Begins the processing of a batch of sprites.

Precondition
This sprite batch is not inside a begin/end pair.
Parameters
[in]sort_modeA reference to the sprite sorting mode for the whole batch of sprites.

◆ BindFixedState()

void mage::rendering::SpriteBatch::Impl::BindFixedState ( )
private

Binds the fixed state of this sprite batch.

◆ Draw()

void XM_CALLCONV mage::rendering::SpriteBatch::Impl::Draw ( ID3D11ShaderResourceView *  texture,
FXMVECTOR  color,
SpriteEffect  effects,
const SpriteTransform2D transform,
const RECT *  source = nullptr 
)

Draws a sprite.

Precondition
This sprite batch is inside a begin/end pair.
Parameters
[in]textureA pointer to the shader resource view of the texture to draw.
[in]colorThe (linear) RGBA color.
[in]effectsThe sprite effects to apply.
[in]transformA reference to the sprite transform.
[in]sourceA pointer the rectangular subregion of the texture.

◆ End()

void mage::rendering::SpriteBatch::Impl::End ( )

Ends the processing of a batch of sprites.

Precondition
This sprite batch is inside a begin/end pair.

◆ FlushBatch()

void mage::rendering::SpriteBatch::Impl::FlushBatch ( )
private

Flushes a batch of sprites for rendering if non-immediate rendering is required for the current batch of sprites. Otherwise, the sprites in the current batch are rendered immediately.

Sprites are sorted based on the sprite sorting mode and adjacent sprites are grouped for rendering if sharing the same texture.

Note
This functionality is only used in case of non-immediate rendering.

◆ operator=() [1/2]

Impl& mage::rendering::SpriteBatch::Impl::operator= ( const Impl sprite_batch)
delete

Copies the given sprite batch to this sprite batch.

Parameters
[in]sprite_batchA reference to the sprite batch to copy.
Returns
A reference to the copy of the given sprite batch (i.e. this sprite batch).

◆ operator=() [2/2]

SpriteBatch::Impl & mage::rendering::SpriteBatch::Impl::operator= ( Impl &&  sprite_batch)
defaultnoexcept

Moves the given sprite batch to this sprite batch.

Parameters
[in]sprite_batchA reference to the sprite batch to move.
Returns
A reference to the moved sprite batch (i.e. this sprite batch).

◆ PrepareSprite()

void XM_CALLCONV mage::rendering::SpriteBatch::Impl::PrepareSprite ( const SpriteInfo &  sprite,
VertexPositionColorTexture vertices,
FXMVECTOR  texture_size,
FXMVECTOR  inverse_texture_size 
)
privatenoexcept

Prepares a single sprite for rendering.

Precondition
vertices is not equal to nullptr.
vertices points to an array containing at least mage::rendering::SpriteBatchMesh::s_vertices_per_sprite.
Parameters
[in]spriteA reference to the sprite info data.
[in]verticesA pointer to the vertices for the sprite.
[in]texture_sizeThe size of the texture (in the number of texels).
[in]inverse_texture_sizeThe inverse of texture_size.

◆ Render()

void mage::rendering::SpriteBatch::Impl::Render ( ID3D11ShaderResourceView *  texture,
const SpriteInfo *const *  sprites,
std::size_t  nb_sprites 
)
private

Draws a subbatch of sprites of the current batch of sprites of this sprite batch.

Precondition
sprites is not equal to nullptr.
sprites points to an array containing at least nb_sprites sprite info data pointers which are not equal to nullptr.
Parameters
[in]textureA pointer to the shader resource view of the texture that needs to be rendered.
[in]spritesA pointer to the sprite info data pointers of the sprites which need to be rendered.
[in]nb_spritesThe number of sprites which need to be rendered.

◆ SortSprites()

void mage::rendering::SpriteBatch::Impl::SortSprites ( )
private

Sorts the sprites of the current batch according to the sprite sorting mode of this sprite batch.

Note
This functionality is only used in case of non-immediate rendering.

Member Data Documentation

◆ m_device_context

std::reference_wrapper< ID3D11DeviceContext > mage::rendering::SpriteBatch::Impl::m_device_context
private

A reference to the device context of this sprite batch.

◆ m_in_begin_end_pair

bool mage::rendering::SpriteBatch::Impl::m_in_begin_end_pair
private

A flag indicating whether this sprite batch is in a begin/end pair for processing sprites.

◆ m_mesh

UniquePtr< SpriteBatchMesh > mage::rendering::SpriteBatch::Impl::m_mesh
private

A pointer to the sprite batch mesh used by this sprite batch for drawing the sprites onto.

◆ m_mesh_position

std::size_t mage::rendering::SpriteBatch::Impl::m_mesh_position
private

The current position in the mesh of this sprite batch for adding sprite vertices.

◆ m_sort_mode

SpriteSortMode mage::rendering::SpriteBatch::Impl::m_sort_mode
private

The sprite sorting mode used by this sprite batch for deciding on the draw order of sprites in the current btach of sprites.

◆ m_sorted_sprites

std::vector< const SpriteInfo* > mage::rendering::SpriteBatch::Impl::m_sorted_sprites
private

A vector containing the pointers to the sorted sprites of this sprite batch.

◆ m_sprites

AlignedVector< SpriteInfo > mage::rendering::SpriteBatch::Impl::m_sprites
private

A vector containing the sprites waiting to be drawn by this sprite batch.

◆ s_initial_capacity

constexpr std::size_t mage::rendering::SpriteBatch::Impl::s_initial_capacity = 64u
staticprivate

The initial capacity of the vector containing the sprites waiting to be drawn by a sprite batch.