-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextRenderer.cpp
177 lines (158 loc) · 4.2 KB
/
TextRenderer.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <apollo/Graphics/TextRenderer.h>
#include <apollo/Graphics/opengl.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include <apollo/Apollo.h>
#include <map>
#include <apollo/Utilities/ResourceManager.h>
#include <apollo/Utilities/GameTime.h>
#include <apollo/Logging.h>
namespace
{
uint32_t Hash ( const std::string& input )
{
uint32_t base = 0xBAADF00D;
uint32_t a = 0, b = 0x8D470010;
const unsigned char* data = (const unsigned char*)input.data();
size_t len = input.length();
for (size_t i = 0; i < len; i++)
{
a <<= 5;
b ^= data[i];
a ^= b;
base ^= a;
a += b;
if (i != 0)
a ^= (data[i - 1] << 16);
}
return b ^ a;
}
struct TextEntry
{
uint32_t hash;
SDL_Surface* surface;
GLuint texID;
float lastUse;
~TextEntry ()
{
SDL_FreeSurface(surface);
if (texID)
glDeleteTextures(1, &texID);
}
};
typedef std::map<uint32_t, TextEntry*> TextEntryTable;
TextEntryTable textEntries;
std::map<std::string, TTF_Font*> fonts;
#define DEFAULT_FONT "prototype"
static bool ttf_initted = false;
// [ADAM, ALISTAIR]: this is where size affects things... how would we figure out how to correct the size?
TTF_Font* GetFont ( const std::string& name, float size )
{
if (!ttf_initted)
{
TTF_Init();
ttf_initted = true;
}
std::map<std::string, TTF_Font*>::iterator iter = fonts.find(name);
if (iter != fonts.end())
{
return iter->second;
}
SDL_RWops* rwops = ResourceManager::OpenFile("Fonts/" + name + ".ttf");
TTF_Font* loadedFont;
if (!rwops)
goto loadFail;
loadedFont = TTF_OpenFontRW(rwops, 1, size);
if (!loadedFont)
goto loadFail;
fonts[name] = loadedFont;
return loadedFont;
loadFail:
if (name == DEFAULT_FONT)
{
LOG("Graphics::TextRenderer", LOG_ERROR, "Unable to load default font: %s", DEFAULT_FONT);
abort();
}
loadedFont = GetFont(DEFAULT_FONT, size);
fonts[name] = loadedFont;
LOG("Graphics::TextRenderer", LOG_WARNING, "Unable to load font '%s', defaulted to '%s'", name.c_str(), DEFAULT_FONT);
return loadedFont;
}
TextEntry* GetEntry ( const std::string& font, const std::string& text, float size )
{
uint32_t hash = Hash(font + "@" + text);
TextEntryTable::iterator iter;
iter = textEntries.find(hash);
if (iter != textEntries.end())
{
return iter->second;
}
else
{
TextEntry* newEntry = new TextEntry;
newEntry->hash = hash;
newEntry->texID = 0;
newEntry->surface = NULL;
newEntry->lastUse = 0.0f;
TTF_Font* fontObject = GetFont(font, size);
const SDL_Color fg = { 0xFF, 0xFF, 0xFF, 0xFF };
newEntry->surface = TTF_RenderUTF8_Blended(fontObject, text.c_str(), fg);
assert(newEntry->surface);
// generate textures
glGenTextures(1, &(newEntry->texID));
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, newEntry->texID);
#ifdef __BIG_ENDIAN__
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, newEntry->surface->w, newEntry->surface->h, 0, GL_ABGR_EXT, GL_UNSIGNED_BYTE, newEntry->surface->pixels);
#else
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, newEntry->surface->w, newEntry->surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, newEntry->surface->pixels);
#endif
textEntries[hash] = newEntry;
return newEntry;
}
}
}
namespace Graphics
{
namespace TextRenderer
{
vec2 TextDimensions ( const std::string& font, const std::string& text, float size )
{
int xcoord = 0;
int ycoord = 0;
TTF_Font* fontObject = GetFont(font, size);
if (TTF_SizeUTF8(fontObject, text.c_str(), &xcoord, &ycoord) != 0)
{
printf("Trouble with text '%s', line %d\n", text.c_str(), __LINE__);
}
return vec2(xcoord, ycoord);
}
GLuint TextObject ( const std::string& font, const std::string& text, float size )
{
TextEntry* entry = GetEntry(font, text, size);
assert(entry);
entry->lastUse = GameTime();
return entry->texID;
}
void Prune ()
{
float time = GameTime();
for (TextEntryTable::iterator iter = textEntries.begin(); iter != textEntries.end(); iter++)
{
if (time - iter->second->lastUse > 14.0f) // text objects expire in 14 seconds
{
delete iter->second;
textEntries.erase(iter);
return; // difficulties arise here with the iterator once erased, might as well just prune one per pass
}
}
}
void Flush ()
{
for (TextEntryTable::iterator iter = textEntries.begin(); iter != textEntries.end(); iter++)
{
delete iter->second;
}
textEntries.clear();
}
}
}