-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathText.cpp
52 lines (40 loc) · 1.43 KB
/
Text.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
#include "Text.h"
using namespace std;
Text::Text(){}
Text::Text(Global_variables_container* GVC, string text, int font_size, string font_name) : Sprite(GVC)
{
m_text = text;
m_font_name = "sprites/fonts/";
m_font_name += font_name;
m_font_name += ".ttf";
m_font_size = font_size;
m_font = TTF_OpenFont(m_font_name.c_str(), m_font_size);
if(!m_font) {
cout << "TTF_OpenFont: " << TTF_GetError();
// récuperrer l'erreur
}
m_original_image = TTF_RenderText_Solid(m_font, text.c_str(), {100,100,100});
m_image = SDL_CreateTextureFromSurface(m_GVC->renderer(), m_original_image);
}
Text::~Text()
{
TTF_CloseFont(m_font);
}
void Text::change_text(string new_text, bool underline, int R, int G, int B, int new_size)
{
SDL_DestroyTexture(m_image);
SDL_FreeSurface(m_original_image);
if(new_text == "SAME" or new_text == ""){new_text = m_text;}
m_text = new_text;
if(new_size == 0){new_size = m_font_size;}
else
{
TTF_CloseFont(m_font);
m_font = TTF_OpenFont(m_font_name.c_str(), new_size);
}
m_font_size = new_size;
if(underline){TTF_SetFontStyle(m_font, TTF_STYLE_UNDERLINE);}
else{TTF_SetFontStyle(m_font, TTF_STYLE_NORMAL);}
m_original_image = TTF_RenderText_Solid(m_font, new_text.c_str(), {R,G,B});
m_image = SDL_CreateTextureFromSurface(m_GVC->renderer(), m_original_image);
}