MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxvk::Font Class Reference

Small RAII wrapper for an SDL_ttf font handle. More...

#include <mxvk/include/mxvk/mxvk_text.hpp>

Public Member Functions

 Font ()=default
 Font (const Font &)=delete
 Font (const std::string &fontPath, int fontSize)
 Font (Font &&other) noexcept
TTF_Font * get () const noexcept
 operator bool () const noexcept
Font & operator= (const Font &)=delete
Font & operator= (Font &&other) noexcept
const std::string & path () const noexcept
void reset ()
void reset (const std::string &fontPath, int fontSize)
int size () const noexcept
 ~Font ()

Detailed Description

Small RAII wrapper for an SDL_ttf font handle.

This is optional convenience for APIs that accept a font object. Existing code can continue using VK_Text::setFont() and printText() unchanged.

Definition at line 46 of file mxvk_text.hpp.

Constructor & Destructor Documentation

◆ Font() [1/4]

mxvk::Font::Font ( )
default

◆ Font() [2/4]

mxvk::Font::Font ( const std::string & fontPath,
int fontSize )

Definition at line 10 of file mxvk_text.cpp.

10{ reset(fontPath, fontSize); }
void reset()
Definition mxvk_text.cpp:27

◆ ~Font()

mxvk::Font::~Font ( )

Definition at line 12 of file mxvk_text.cpp.

12{ reset(); }

◆ Font() [3/4]

mxvk::Font::Font ( const Font & )
delete

◆ Font() [4/4]

mxvk::Font::Font ( Font && other)
noexcept

Definition at line 14 of file mxvk_text.cpp.

14: font(std::exchange(other.font, nullptr)), font_path(std::move(other.font_path)), font_size(std::exchange(other.font_size, 0)), ownsTtfInit(std::exchange(other.ownsTtfInit, false)) {}

Member Function Documentation

◆ get()

TTF_Font * mxvk::Font::get ( ) const
inlinenodiscardnoexcept

Definition at line 58 of file mxvk_text.hpp.

58{ return font; }

◆ operator bool()

mxvk::Font::operator bool ( ) const
inlineexplicitnodiscardnoexcept

Definition at line 59 of file mxvk_text.hpp.

59{ return font != nullptr; }

◆ operator=() [1/2]

Font & mxvk::Font::operator= ( const Font & )
delete

◆ operator=() [2/2]

Font & mxvk::Font::operator= ( Font && other)
noexcept

Definition at line 16 of file mxvk_text.cpp.

16 {
17 if (this != &other) {
18 reset();
19 font = std::exchange(other.font, nullptr);
20 font_path = std::move(other.font_path);
21 font_size = std::exchange(other.font_size, 0);
22 ownsTtfInit = std::exchange(other.ownsTtfInit, false);
23 }
24 return *this;
25 }

◆ path()

const std::string & mxvk::Font::path ( ) const
inlinenodiscardnoexcept

Definition at line 60 of file mxvk_text.hpp.

60{ return font_path; }

◆ reset() [1/2]

void mxvk::Font::reset ( )

Definition at line 27 of file mxvk_text.cpp.

27 {
28 if (font != nullptr) {
29 TTF_CloseFont(font);
30 font = nullptr;
31 }
32 font_path.clear();
33 font_size = 0;
34 if (ownsTtfInit) {
35 TTF_Quit();
36 ownsTtfInit = false;
37 }
38 }

◆ reset() [2/2]

void mxvk::Font::reset ( const std::string & fontPath,
int fontSize )

Definition at line 40 of file mxvk_text.cpp.

40 {
41 if (fontPath.empty() || fontSize <= 0) {
42 throw mxvk::Exception("Font requires a non-empty path and positive font size");
43 }
44
45 reset();
46
47 if (!TTF_Init()) {
48 throw mxvk::Exception("Failed to initialize SDL_ttf: " + std::string(SDL_GetError()));
49 }
50
51 TTF_Font *newFont = TTF_OpenFont(fontPath.c_str(), fontSize);
52 if (newFont == nullptr) {
53 TTF_Quit();
54 throw mxvk::Exception("Failed to load font: " + std::string(SDL_GetError()));
55 }
56
57 font = newFont;
58 font_path = fontPath;
59 font_size = fontSize;
60 ownsTtfInit = true;
61 }

◆ size()

int mxvk::Font::size ( ) const
inlinenodiscardnoexcept

Definition at line 61 of file mxvk_text.hpp.

61{ return font_size; }

The documentation for this class was generated from the following files: