/
Texture_Text.cpp
90 lines (75 loc) · 2.39 KB
/
Texture_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
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
#include "Texture_Text.h"
#include <sstream>
Texture_Text::Texture_Text()
: Texture_Text( nullptr, { 0, 0, 0, 255 }, { 255, 255, 255, 255 } )
{
}
Texture_Text::Texture_Text( TTF_Font *font, const SDL_Color &bgColor, const SDL_Color &fgColor )
: Texture()
, textFont( font )
, textColor( { fgColor } )
, backgroundColor( { bgColor } )
, prevValue_d( -1.0 )
, prevValue_i( -1 )
{
}
void Texture_Text::Init( TTF_Font *font, const SDL_Color &bgColor, const SDL_Color &fgColor )
{
textFont = font;
textColor = bgColor;
backgroundColor = fgColor;
SetSizeFromTexture();
}
void Texture_Text::RenderText_Solid( SDL_Renderer* renderer, const std::string &str )
{
// Just render the text ( background will be transparent )
SDL_Surface* surface = TTF_RenderText_Solid( textFont, str.c_str(), textColor );
SetTexture( renderer, surface );
}
void Texture_Text::RenderText_Shaded( SDL_Renderer* renderer, const std::string &str )
{
// Render with a text color
SDL_Surface* surface = TTF_RenderText_Shaded( textFont, str.c_str(), textColor, backgroundColor );
SetTexture( renderer, surface );
}
void Texture_Text::RenderText_Blended( SDL_Renderer* renderer, const std::string &str )
{
// Render with a text color, nicer than TTF_RenderText_Shaded, but slower
SDL_Surface* surface = TTF_RenderText_Blended( textFont, str.c_str(), textColor );
SetTexture( renderer, surface );
}
// The two RenderValue uses RenderText_Solid() as this is more efficient than RenderText_Blanded.
// Feel free to change to Blanded if you want nicer text
void Texture_Text::RenderValue( SDL_Renderer* renderer, const std::string &str, int32_t value )
{
// Don't re-render the same value
if ( prevValue_i == value )
return;
prevValue_d = -1.0;
prevValue_i = value;
std::stringstream ss;
ss << str << " : " << value;
RenderText_Solid( renderer, ss.str() );
}
void Texture_Text::RenderNumber( SDL_Renderer* renderer, int32_t value )
{
// Don't re-render the same value
if ( prevValue_i == value )
return;
prevValue_d = -1.0;
prevValue_i = value;
std::stringstream ss;
ss << value;
RenderText_Solid( renderer, ss.str() );
}
void Texture_Text::RenderValue( SDL_Renderer* renderer, const std::string &str, double value )
{
// Don't re-render the same value
if ( prevValue_d == value )
return;
prevValue_i = -1;
prevValue_d = value;
std::stringstream ss;
ss << str << " : " << value;
RenderText_Solid( renderer, ss.str() );
}