-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText.cpp
More file actions
110 lines (91 loc) · 1.71 KB
/
Text.cpp
File metadata and controls
110 lines (91 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "Text.h"
//SDL_Surface *Text::m_font = SDL_LoadBMP("..\\resources\\font50.bmp");
Text::Text()
{
init();
}
Text::Text(const char *text, int size, SDL_Color color)
{
init();
m_text = text;
m_size = size;
m_color = color;
}
Text::Text(
const char *text,
int size,
Uint8 red,
Uint8 green,
Uint8 blue,
Uint8 alpha
) {
SDL_Color color = { red, green, blue, alpha };
init();
m_text = text;
m_size = size;
m_color = color;
}
Text::~Text()
{
if (m_texture) {
SDL_DestroyTexture(m_texture);
}
}
void
Text::setSize(int size)
{
m_size = size;
reset();
}
void
Text::setColor(SDL_Color color)
{
m_color = color;
reset();
}
void
Text::setColor(Uint8 red, Uint8 green, Uint8 blue, Uint8 alpha)
{
m_color.r = red;
m_color.g = green;
m_color.b = blue;
m_color.a = alpha;
reset();
}
void
Text::setText(const char *text)
{
m_text = text;
reset();
}
void
Text::init()
{
m_texture = NULL;
}
void
Text::reset()
{
// reset previous calculations
if (m_texture) {
SDL_DestroyTexture(m_texture);
m_texture = NULL;
}
}
SDL_Texture *
Text::getTexture(SDL_Renderer *renderer)
{
if (m_texture != NULL) {
return m_texture;
}
TTF_Font *font = TTF_OpenFont("resources/nk57-monospace-sc-rg.ttf", m_size);
if (!font) {
SDL_ShowSimpleMessageBox(0, "Error", "Unable to open resources/nk57-monospace-sc-rg.ttf", NULL);
exit(1);
}
SDL_Surface *textSurface = TTF_RenderText_Blended(font, m_text.c_str(), m_color);
TTF_CloseFont(font);
m_texture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_FreeSurface(textSurface);
return m_texture;
}