-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.cpp
More file actions
80 lines (68 loc) · 1.28 KB
/
text.cpp
File metadata and controls
80 lines (68 loc) · 1.28 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
#include <iostream>
#include <utility>
#include "text.h"
#include "render.h"
#include "control.h"
#include "util.h"
using namespace std;
unique_ptr<TTF_Font, function<void(TTF_Font*)>> Text::_font(
nullptr,
[](TTF_Font* p) {
TTF_CloseFont(p);
TTF_Quit();
}
);
Text::Text(const string& s)
: Widget()
, _color{ 0xFF, 0xFF, 0xFF, 0xFF }
{
set_string(s);
}
Text::Text(Text&& other)
: Widget(forward<Widget>(other))
, _color(exchange(other._color, {0}))
, _string(move(other._string))
{}
void Text::set_string(const string& s)
{
aquire(_mut);
_string = s;
// Free old surface
SDL_FreeSurface(_surface);
// Load text into surface
_surface = TTF_RenderUTF8_Blended(
Text::_font.get(),
_string.c_str(),
_color
);
if (!_surface) {
cerr << "Failed to create surface: " << TTF_GetError() << endl;
exit(1);
}
// Set widget size
_w = _surface->w;
_h = _surface->h;
_uflag = true;
}
string Text::get_string() const
{
aquire(_mut);
return _string;
}
void Text::set_color(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
aquire(_mut);
_color = { r, g, b, a };
set_string(_string);
}
SDL_Color Text::get_color() const
{
aquire(_mut);
return _color;
}
void Text::draw() const
{
aquire(_mut);
SDL_Rect dst = { _x, _y, _w, _h };
RenderWindow::get_instance().render(_texture, &dst);
}