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

#include <sprite_font.hpp>

Inheritance diagram for mage::rendering::SpriteFont:
mage::Resource< SpriteFont >

Public Member Functions

 SpriteFont (ID3D11Device &device, std::wstring fname, const SpriteFontDescriptor &desc=SpriteFontDescriptor())
 
 SpriteFont (const SpriteFont &font)=delete
 
 SpriteFont (SpriteFont &&font) noexcept
 
virtual ~SpriteFont ()
 
SpriteFontoperator= (const SpriteFont &font)=delete
 
SpriteFontoperator= (SpriteFont &&font) noexcept
 
void DrawText (SpriteBatch &sprite_batch, gsl::span< const ColorString > strings, const SpriteTransform2D &transform, SpriteEffect effects=SpriteEffect::None, const RGBA *color=nullptr) const
 
const XMVECTOR XM_CALLCONV MeasureText (gsl::span< const ColorString > strings) const
 
const RECT MeasureDrawBounds (gsl::span< const ColorString > strings, const F32x2 &top_left) const
 
bool empty () const noexcept
 
std::size_t size () const noexcept
 
F32 GetLineSpacing () const noexcept
 
void SetLineSpacing (F32 spacing) noexcept
 
wchar_t GetDefaultCharacter () const noexcept
 
void SetDefaultCharacter (wchar_t character)
 
bool ContainsCharacter (wchar_t character) const
 
const GlyphGetGlyph (wchar_t character) const
 
ID3D11ShaderResourceView * Get () const noexcept
 
- Public Member Functions inherited from mage::Resource< SpriteFont >
 Resource (std::wstring guid) noexcept
 
 Resource (const Resource &resource)=delete
 
 Resource (Resource &&resource) noexcept
 
virtual ~Resource ()
 
Resourceoperator= (const Resource &resource)=delete
 
Resourceoperator= (Resource &&resource) noexcept
 
const std::wstring_view GetGuid () const noexcept
 
bool IsFileResource () const
 
const std::filesystem::path GetPath () const
 

Private Member Functions

void InitializeSpriteFont (const SpriteFontOutput &output)
 

Private Attributes

ComPtr< ID3D11ShaderResourceView > m_texture_srv
 
std::vector< Glyphm_glyphs
 
const Glyphm_default_glyph
 
F32 m_line_spacing
 

Detailed Description

A class of sprite fonts.

Constructor & Destructor Documentation

◆ SpriteFont() [1/3]

mage::rendering::SpriteFont::SpriteFont ( ID3D11Device &  device,
std::wstring  fname,
const SpriteFontDescriptor desc = SpriteFontDescriptor() 
)
explicit

Constructs a sprite font.

Parameters
[in,out]deviceA reference to the device.
[in]fnameThe filename (the globally unique identifier).
[in]descA reference to the sprite font descriptor.
Exceptions
ExceptionFailed to initialize the sprite font.

◆ SpriteFont() [2/3]

mage::rendering::SpriteFont::SpriteFont ( const SpriteFont font)
delete

Constructs a sprite font from the given sprite font.

Parameters
[in]fontA reference to the sprite font to copy.

◆ SpriteFont() [3/3]

mage::rendering::SpriteFont::SpriteFont ( SpriteFont &&  font)
defaultnoexcept

Constructs a sprite font by moving the given sprite font.

Parameters
[in]fontA reference to the sprite font to move.

◆ ~SpriteFont()

mage::rendering::SpriteFont::~SpriteFont ( )
virtualdefault

Destructs this sprite font.

Member Function Documentation

◆ ContainsCharacter()

bool mage::rendering::SpriteFont::ContainsCharacter ( wchar_t  character) const

Checks whether this sprite font contains a glyp matching the given character.

Parameters
[in]characterThe character.
Returns
true if this sprite font contains a glyph corresponding to the given character. false otherwise.

◆ DrawText()

void mage::rendering::SpriteFont::DrawText ( SpriteBatch sprite_batch,
gsl::span< const ColorString strings,
const SpriteTransform2D transform,
SpriteEffect  effects = SpriteEffect::None,
const RGBA color = nullptr 
) const

Draws the given text with this sprite font using the given sprite batch.

Parameters
[in,out]sprite_batchA reference to the sprite batch used for rendering the given text with this sprite font.
[in]stringsThe strings of the text.
[in]transformA reference to the sprite transform.
[in]effectsThe sprite effects to apply.
[in]colorA pointer to the (linear) color. If this pointer is equal to nullptr, each string will be drawn in its own color. Otherwise, each string is drawn in this color.

◆ empty()

bool mage::rendering::SpriteFont::empty ( ) const
noexcept

Checks whether this sprite font is empty.

Returns
true if this sprite font is empty. false otherwise.

◆ Get()

ID3D11ShaderResourceView* mage::rendering::SpriteFont::Get ( ) const
noexcept

Returns a pointer to the shader resource view of the texture of this sprite font.

Returns
A pointer to the shader resource view of the texture of this sprite font.

◆ GetDefaultCharacter()

wchar_t mage::rendering::SpriteFont::GetDefaultCharacter ( ) const
noexcept

Returns the default character of this sprite font.

If this sprite font has no default glyph, L'0' is returned.

Returns
The default character of this sprite font.

◆ GetGlyph()

const Glyph * mage::rendering::SpriteFont::GetGlyph ( wchar_t  character) const

Returns the glyph of this sprite font corresponding to the given character.

Parameters
[in]characterThe character.
Returns
A pointer to the default glyph of this sprite font if the given character does not match any glyphs of this sprite font and if this sprite font has a default character.
A pointer to the glyph of this sprite font corresponding to the given character.
Exceptions
ExceptionIf the given character does not match any glyphs of this sprite font and if this sprite font has not a default character.

◆ GetLineSpacing()

F32 mage::rendering::SpriteFont::GetLineSpacing ( ) const
noexcept

Returns the line spacing of this sprite font.

Returns
The line spacing of this sprite font.

◆ InitializeSpriteFont()

void mage::rendering::SpriteFont::InitializeSpriteFont ( const SpriteFontOutput output)
private

Initializes this sprite font with the given sprite font output.

Parameters
[in]outputA reference to the sprite font output.
Exceptions
ExceptionThe sprite font glyphs of the given sprite font output are not sorted.

◆ MeasureDrawBounds()

const RECT mage::rendering::SpriteFont::MeasureDrawBounds ( gsl::span< const ColorString strings,
const F32x2 top_left 
) const

Returns a rectangle bounding the given text with this sprite font.

Parameters
[in]stringsThe strings of the text.
[in]top_leftThe top-left position of the text.
Returns
A RECT bounding the given text with this sprite font.

◆ MeasureText()

const XMVECTOR XM_CALLCONV mage::rendering::SpriteFont::MeasureText ( gsl::span< const ColorString strings) const

Returns the size of the given text with this sprite font (in pixels).

Parameters
[in]stringsThe strings of the text.
Returns
A XMVECTOR containing the pixel width as first coordinate and pixel height as second coordinate.
Note
The text size is computed from the origin to the rightmost pixel rendered by any character glyph. This has the effect of ignoring 'trailing spaces'.

◆ operator=() [1/2]

SpriteFont& mage::rendering::SpriteFont::operator= ( const SpriteFont font)
delete

Copies the given sprite font to this sprite font.

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

◆ operator=() [2/2]

SpriteFont & mage::rendering::SpriteFont::operator= ( SpriteFont &&  font)
defaultnoexcept

Moves the given sprite font to this sprite font.

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

◆ SetDefaultCharacter()

void mage::rendering::SpriteFont::SetDefaultCharacter ( wchar_t  character)

Sets the default character of this sprite font to the given character.

If character is equal to 0, the default glyph of this sprite font is set to nullptr.

Parameters
[in]characterThe character.
Exceptions
ExceptionIf the given character does not match any glyphs of this sprite font and if this sprite font has not a default character.

◆ SetLineSpacing()

void mage::rendering::SpriteFont::SetLineSpacing ( F32  spacing)
noexcept

Sets the line spacing of this sprite font to the given spacing value.

Parameters
[in]spacingThe line spacing.

◆ size()

std::size_t mage::rendering::SpriteFont::size ( ) const
noexcept

Returns the number of characters in this sprite font.

Returns
The number of characters in this sprite font.

Member Data Documentation

◆ m_default_glyph

const Glyph* mage::rendering::SpriteFont::m_default_glyph
private

A pointer to the default glyph of this sprite font.

◆ m_glyphs

std::vector< Glyph > mage::rendering::SpriteFont::m_glyphs
private

A vector containing the glyphs of this sprite font.

◆ m_line_spacing

F32 mage::rendering::SpriteFont::m_line_spacing
private

The (extra) line spacing of this sprite font.

◆ m_texture_srv

ComPtr< ID3D11ShaderResourceView > mage::rendering::SpriteFont::m_texture_srv
private

A pointer to the texture of this sprite font.