-
Notifications
You must be signed in to change notification settings - Fork 5
/
Text.cpp
141 lines (110 loc) · 3.24 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Text.cpp
*
* Created on: 2014/11/23
* Author: Dimitri Kourkoulis
* http://dimitros.be
* License: BSD 3-Clause License (see LICENSE file)
*/
#include "Text.hpp"
#include "Exception.hpp"
#include <cstdio>
#include <cstring>
using namespace std;
bool ttfInitialised = false;
namespace small3d {
string intToStr( int number )
{
char buffer[100];
#if defined(_WIN32) && !defined(__MINGW32__)
sprintf_s(buffer,"%d",number);
#else
sprintf(buffer,"%d",number);
#endif
return string(buffer);
}
Text::Text(shared_ptr<Renderer> renderer,
const string &ttfFontPath, const int &size)
{
this->renderer = renderer;
if (!ttfInitialised) {
if(TTF_Init()==-1)
{
LOGERROR(TTF_GetError());
throw Exception("Unable to initialise font system");
}
else
{
ttfInitialised = true;
}
}
string fontPath = SDL_GetBasePath() + ttfFontPath;
LOGINFO("Loading font from " + fontPath);
font = TTF_OpenFont(fontPath.c_str(), size);
this->size = size;
if (!font)
{
LOGERROR(TTF_GetError());
throw Exception("Failed to load font");
}
else
{
LOGINFO("TTF font loaded successfully");
}
}
Text::~Text()
{
TTF_CloseFont(font);
//TTF_Quit();
}
void Text::renderText(const string &text, const SDL_Color &colour,
const float &topX, const float &topY, const float &bottomX, const float &bottomY)
{
GLuint textHandle = renderer->getTextureHandle(intToStr(size) + "text_" + text);
if (textHandle == 0)
{
SDL_Surface *textSurface = TTF_RenderText_Blended(font,
text.c_str(), colour);
int numPixels = textSurface->h * textSurface->w;
Uint32 *pix = static_cast<Uint32*>(textSurface->pixels);
float *texturef = new float[numPixels * 4];
for (int pidx = 0; pidx < numPixels; ++pidx)
{
Uint32 r = pix[pidx] & textSurface->format->Rmask;
Uint32 g = pix[pidx] & textSurface->format->Gmask;
Uint32 b = pix[pidx] & textSurface->format->Bmask;
Uint32 a = pix[pidx] & textSurface->format->Amask;
r = r >> textSurface->format->Rshift;
g = g >> textSurface->format->Gshift;
b = b >> textSurface->format->Bshift;
a = a >> textSurface->format->Ashift;
float ttuple[4] = {static_cast<float>(r),
static_cast<float>(g),
static_cast<float>(b),
static_cast<float>(a)
};
ttuple[0]= floorf(100.0f * (ttuple[0] / 255.0f) + 0.5f) / 100.0f;
ttuple[1]= floorf(100.0f * (ttuple[1] / 255.0f) + 0.5f) / 100.0f;
ttuple[2]= floorf(100.0f * (ttuple[2] / 255.0f) + 0.5f) / 100.0f;
ttuple[3]= floorf(100.0f * (ttuple[3] / 255.0f) + 0.5f) / 100.0f;
memcpy(&texturef[pidx * 4], &ttuple, sizeof(ttuple));
}
textHandle = renderer->generateTexture(intToStr(size) + "text_" + text, texturef, textSurface->w, textSurface->h);
delete[] texturef;
texturef = NULL;
SDL_FreeSurface(textSurface);
}
float boxVerts[16] =
{
topX, bottomY, -0.5f, 1.0f,
bottomX, bottomY, -0.5f, 1.0f,
bottomX, topY, -0.5f, 1.0f,
topX, topY, -0.5f, 1.0f
};
renderer->renderImage(boxVerts, intToStr(size) + "text_" + text);
}
void Text::deleteTextTexture( const string &text )
{
renderer->deleteTexture(intToStr(size) + "text_" + text);
}
}