/
textobject.cpp
94 lines (75 loc) · 1.76 KB
/
textobject.cpp
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
#include "textobject.hpp"
//PUBLIC MEMBER METHODS
TextObject::TextObject(SDL_Renderer* r, const string& text, int fontSize, const Point& p)
{
this->renderer = r;
cout << "TextObject Renderer: " << this->renderer << endl;
this->fontLocation = DEFAULT_FONT_LOCATION;
this->fontSize = fontSize;
this->p = p;
this->text = text;
texture = nullptr;
updateFont();
createTextTexture();
}
TextObject::~TextObject()
{
if (texture != nullptr)
SDL_DestroyTexture(texture);
}
void TextObject::draw()
{
SDL_RenderCopy(renderer, texture, nullptr, &rect);
SDL_RenderPresent(renderer);
}
void TextObject::setPoint(const Point& p)
{
this->p = p;
createTextTexture();
}
void TextObject::setFontSize(const int fontSize)
{
this->fontSize = fontSize;
createTextTexture();
}
void TextObject::setText(const string& text)
{
this->text = text;
createTextTexture();
}
void TextObject::setFont(const string& fontLocation)
{
this->fontLocation = fontLocation;
createTextTexture();
}
void TextObject::setColor(const SDL_Color& color)
{
this->color = color;
createTextTexture();
}
// PRIVATE MEMBER METHODS //
void TextObject::updateFont()
{
font = TTF_OpenFont(fontLocation.c_str(), fontSize);
if (font == nullptr)
{
cerr << __LINE__ << " TTF_OpenFont Error: " << SDL_GetError() << endl;
exit(5);
}
}
SDL_Texture* TextObject::surfaceToTexture(SDL_Surface* surface)
{
SDL_Texture *text = SDL_CreateTextureFromSurface(renderer, surface);;
SDL_FreeSurface(surface);
return text;
}
void TextObject::createTextTexture()
{
if (texture != nullptr)
SDL_DestroyTexture(texture);
SDL_Surface* surface = TTF_RenderText_Solid(font, text.c_str(), color);
texture = surfaceToTexture(surface);
SDL_QueryTexture(texture, NULL, NULL, &rect.w, &rect.h);
rect.x = p.getX();
rect.y = p.getY();
}