MXVK Vulkan Framework 0.24.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
Fontoperator= (const Font &)=delete
Fontoperator= (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 {
11 reset(fontPath, fontSize);
12 }
void reset()
Definition mxvk_text.cpp:35

◆ ~Font()

mxvk::Font::~Font ( )

Definition at line 14 of file mxvk_text.cpp.

14 {
15 reset();
16 }

◆ Font() [3/4]

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

◆ Font() [4/4]

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

Definition at line 18 of file mxvk_text.cpp.

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)) {}

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 24 of file mxvk_text.cpp.

24 {
25 if (this != &other) {
26 reset();
27 font = std::exchange(other.font, nullptr);
28 font_path = std::move(other.font_path);
29 font_size = std::exchange(other.font_size, 0);
30 ownsTtfInit = std::exchange(other.ownsTtfInit, false);
31 }
32 return *this;
33 }

◆ 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 35 of file mxvk_text.cpp.

35 {
36 if (font != nullptr) {
37 TTF_CloseFont(font);
38 font = nullptr;
39 }
40 font_path.clear();
41 font_size = 0;
42 if (ownsTtfInit) {
43 TTF_Quit();
44 ownsTtfInit = false;
45 }
46 }

◆ reset() [2/2]

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

Definition at line 48 of file mxvk_text.cpp.

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

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