libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
font.cpp
Go to the documentation of this file.
1/**
2 * @file font.cpp
3 * @brief Implementation of mx::Font and mx::Text SDL_ttf wrappers.
4 */
5#include"font.hpp"
6#include<iostream>
7#include<filesystem>
8namespace mx {
9 Font::Font(const std::string &tf) {
10 loadFont(tf, 14);
11 }
12
14 if(the_font != nullptr)
15 TTF_CloseFont(the_font);
16
17 the_font = nullptr;
18 }
19
20 Font::Font(const std::string &tf, int size) {
21 the_font = nullptr;
22 loadFont(tf, size);
23 }
24
25 Font::Font(TTF_Font *tf) : the_font{tf} {}
26
28 f.the_font = nullptr;
29 }
30
31
32
34 if(this != &f) {
35 if(the_font != nullptr) {
36 TTF_CloseFont(the_font);
37 the_font = nullptr;
38 }
39 the_font = f.the_font;
40 f.the_font = nullptr;
41 }
42 return *this;
43 }
44
45 bool Font::tryLoadFont(const std::string &fname, int size) {
46 if(the_font != nullptr) {
47 TTF_CloseFont(the_font);
48 the_font = nullptr;
49 }
50
51 if(!std::filesystem::exists(fname)) {
52 mx::system_err << "mx: Error loading font: " << fname << "\n";
53 throw mx::Exception("Error loading font: " + fname);
54 return false;
55 }
56
57 the_font = TTF_OpenFont(fname.c_str(), size);
58 if(!the_font) {
59 mx::system_err << "mx: Error opening font: " << TTF_GetError() << "\n";
60 throw mx::Exception("Error opening font");
61 return false;
62 }
63 return true;
64 }
65
66 void Font::loadFont(const std::string &fname, int size) {
67 if(!tryLoadFont(fname, size))
68 throw mx::Exception("Error loading font..");
69
70 }
71
72 void Text::setRenderer(SDL_Renderer *renderer) {
73 this->renderer = renderer;
74 }
75 void Text::setColor(SDL_Color color) {
76 color_value = color;
77 }
78
79 void Text::printText_Solid(const Font &f, int x, int y, const std::string &text) {
80 SDL_Surface *surf = TTF_RenderText_Solid(f.wrapper().unwrap(),text.c_str(), color_value);
81 if(!surf) {
82 mx::system_err << "mx: Error rendering text...\n";
83 return;
84 }
85 SDL_Texture *tex = SDL_CreateTextureFromSurface(renderer, surf);
86 if(!tex) {
87 mx::system_err << "mx: Error creating texture..\n";
88 SDL_FreeSurface(surf);
89 return;
90 }
91 SDL_Rect rc = {x,y,surf->w,surf->h};
92 SDL_RenderCopy(renderer, tex, nullptr, &rc);
93 SDL_FreeSurface(surf);
94 SDL_DestroyTexture(tex);
95 }
96
97 void Text::printText_Blended(const Font &f, int x, int y, const std::string &text) {
98 SDL_Surface *surf = TTF_RenderText_Blended(f.wrapper().unwrap(),text.c_str(), color_value);
99 if(!surf) {
100 mx::system_err << "mx: Error rendering text...\n";
101 return;
102 }
103 SDL_Texture *tex = SDL_CreateTextureFromSurface(renderer, surf);
104 if(!tex) {
105 mx::system_err << "mx: Error creating texture..\n";
106 SDL_FreeSurface(surf);
107 return;
108 }
109 SDL_Rect rc = {x,y,surf->w,surf->h};
110 SDL_RenderCopy(renderer, tex, nullptr, &rc);
111 SDL_FreeSurface(surf);
112 SDL_DestroyTexture(tex);
113
114 }
115
116
117}
General-purpose exception class for the mx library.
Definition exception.hpp:20
RAII wrapper around a TTF_Font handle.
Definition font.hpp:25
bool tryLoadFont(const std::string &fname, int size)
Attempt to load a font, returning false on failure.
Definition font.cpp:45
Wrapper< TTF_Font * > wrapper() const
Return a Wrapper around the TTF_Font handle.
Definition font.hpp:100
~Font()
Destructor — closes the TTF_Font if loaded.
Definition font.cpp:13
Font & operator=(const Font &f)=delete
void loadFont(const std::string &fname, int size)
Load a font, throwing mx::Exception on failure.
Definition font.cpp:66
Font()=default
Default constructor — font is not loaded.
TTF_Font * the_font
Underlying SDL_ttf font handle.
Definition font.hpp:107
void setRenderer(SDL_Renderer *renderer)
Set the SDL renderer used for all draw calls.
Definition font.cpp:72
void printText_Blended(const Font &f, int x, int y, const std::string &text)
Render text with blended (anti-aliased) quality.
Definition font.cpp:97
SDL_Color color_value
Current text colour.
Definition font.hpp:153
void setColor(SDL_Color color)
Set the colour used when rendering text.
Definition font.cpp:75
SDL_Renderer * renderer
Active renderer.
Definition font.hpp:152
void printText_Solid(const Font &f, int x, int y, const std::string &text)
Render text with solid (no anti-aliasing) blending.
Definition font.cpp:79
SDL_ttf font and 2D text rendering wrappers.
Definition cfg.cpp:11
TeeStream system_err(std::cerr, error_file)
Global stderr + log tee stream.