-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_renderer.cpp
More file actions
281 lines (221 loc) · 7.64 KB
/
text_renderer.cpp
File metadata and controls
281 lines (221 loc) · 7.64 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "text_renderer.h"
#include <SDL.h>
#include <SDL_ttf.h>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <string_view>
#include <utility>
//#define DEBUG_CACHE
bool operator==(const SDL_Color &a, const SDL_Color &b) {
auto &&t = [](auto &&a) { return std::tie(a.a, a.r, a.g, a.b); };
return t(a) == t(b);
}
namespace {
SDL_RWops *RW_FromString(const std::string &data) {
return SDL_RWFromConstMem(data.data(), static_cast<int>(data.size()));
}
} // namespace
namespace gfx {
void TextRenderer::load_fonts(SDL_Renderer *ren, const FontSpec &spec) {
if (fontRegular != 0) {
TTF_CloseFont(fontRegular);
}
fontRegular = TTF_OpenFontRW(RW_FromString(spec.regular), 1, spec.pointsize);
assert(fontRegular);
if (fontRegularItalic != 0) {
TTF_CloseFont(fontRegularItalic);
}
fontRegularItalic =
TTF_OpenFontRW(RW_FromString(spec.italic), 1, spec.pointsize);
assert(fontRegularItalic);
if (fontBold != 0) {
TTF_CloseFont(fontBold);
}
fontBold = TTF_OpenFontRW(RW_FromString(spec.bold), 1, spec.pointsize);
assert(fontBold);
if (fontBoldItalic != 0) {
TTF_CloseFont(fontBoldItalic);
}
fontBoldItalic =
TTF_OpenFontRW(RW_FromString(spec.bolditalic), 1, spec.pointsize);
assert(fontBoldItalic);
int advance;
TTF_GlyphMetrics(fontRegular, '&', nullptr, nullptr, nullptr, nullptr,
&advance);
font_height = TTF_FontHeight(fontRegular);
cell_height = font_height;
cell_width = advance;
font_point = spec.pointsize;
std::cout << "Loaded fonts\n";
if (cacheTex) {
SDL_DestroyTexture(cacheTex);
}
cacheTex = SDL_CreateTexture(
ren, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
cache_width_cells * cell_width, cache_height_cells * cell_height);
lru_map.clear();
lru_list.clear();
SDL_Color black;
black.r = black.g = black.b = 0;
black.a = 255;
for (int i = 0; i < cache_height_cells * cache_width_cells; i++) {
auto key =
std::make_tuple((TTF_Font *)nullptr, black, black, std::string{" "});
lru_list.push_front(std::make_pair(key, i));
}
std::cout << "Cleared cell cache\n";
}
TextRenderer::~TextRenderer() {
std::cout << "Destroying fonts\n";
if (fontRegular) {
TTF_CloseFont(fontRegular);
}
if (fontBold) {
TTF_CloseFont(fontBold);
}
if (fontRegularItalic) {
TTF_CloseFont(fontRegularItalic);
}
if (fontBoldItalic) {
TTF_CloseFont(fontBoldItalic);
}
std::cout << "Destroying cell cache\n";
if (cacheTex) {
SDL_DestroyTexture(cacheTex);
}
}
TTF_Font *TextRenderer::get_font(bool bold, bool italic) const {
// Cell font;
TTF_Font *font = nullptr;
if (!bold && !italic)
font = fontRegular;
if (!bold && italic)
font = fontRegularItalic;
if (bold && !italic)
font = fontBold;
if (bold && italic)
font = fontBoldItalic;
assert(font);
return font;
}
std::pair<int, int> TextRenderer::cell_size() const {
return {cell_width, cell_height};
}
void TextRenderer::draw_character(SDL_Renderer *ren, TTF_Font *font,
std::string_view glyph, const SDL_Color &fg,
const SDL_Color &bg, int top, int left) {
// Rectangle for the cell. in screen space.
SDL_Rect cell_rect;
cell_rect.x = left;
cell_rect.y = top;
// width and height as yet unknown
auto [cache_index, is_hit] = get_cache_location(font, glyph, fg, bg);
SDL_Rect cached_cell_rect;
cached_cell_rect.x = (cache_index % cache_width_cells) * cell_width;
cached_cell_rect.y = (cache_index / cache_height_cells) * cell_height;
cached_cell_rect.w = cell_rect.w = cell_width;
cached_cell_rect.h = cell_rect.h = cell_height;
if (is_hit) {
SDL_RenderCopy(ren, cacheTex, &cached_cell_rect, &cell_rect);
return;
}
// Begin draw character.
SDL_Surface *cellSurf = TTF_RenderUTF8_Shaded(font, glyph.data(), fg, bg);
SDL_Texture *cellTex = SDL_CreateTextureFromSurface(ren, cellSurf);
// Read back the size of the character
SDL_QueryTexture(cellTex, nullptr, nullptr, &cell_rect.w, &cell_rect.h);
SDL_RenderCopy(ren, cellTex, NULL, &cell_rect);
// Caching
if (cacheTex != nullptr) {
// Rememebr previous render target
SDL_Texture *prevTexTarget = SDL_GetRenderTarget(ren);
// Copy to cache
SDL_SetRenderTarget(ren, cacheTex);
// newly rendered glyph might be slightly different size.
// use the size from the actual rendered glyph.
// use the position from the cache destination
cached_cell_rect.w = cell_rect.w;
cached_cell_rect.h = cell_rect.h;
cell_rect.x = 0;
cell_rect.y = 0;
SDL_RenderCopy(ren, cellTex, &cell_rect, &cached_cell_rect);
SDL_RenderFlush(ren);
// Restore prvious render target
SDL_SetRenderTarget(ren, prevTexTarget);
}
SDL_FreeSurface(cellSurf);
SDL_DestroyTexture(cellTex);
}
std::pair<int, bool> TextRenderer::get_cache_location(TTF_Font *font, std::string_view glyph,
SDL_Color fg, SDL_Color bg) {
auto map_key = std::make_tuple(font, fg, bg, std::string(glyph));
auto map_it = lru_map.find(map_key);
if (map_it != lru_map.end()) {
// Cache hit
++cache_hits;
auto list_it = map_it->second;
auto list_item = *list_it;
#ifdef DEBUG_CACHE
if (!glyph.empty() && !std::iswspace(glyph.front())) {
std::cout << "CACHE: Hit " << std::quoted(glyph) << " @ "
<< list_item.second << std::endl;
}
#endif
// mark entry as recently used
lru_list.erase(list_it);
lru_list.push_front(list_item);
lru_map[map_key] = lru_list.begin();
return {list_item.second, true};
}
++cache_misses;
// Evict oldest member of cache
auto oldest_index = lru_list.back().second;
auto oldest_key = lru_list.back().first;
lru_list.pop_back();
lru_map.erase(oldest_key);
#ifdef DEBUG_CACHE
std::cout << "CACHE: Miss " << std::quoted(glyph) << " + " << oldest_index
<< std::endl;
#endif
// Emplace new item in cache using newly freed index
lru_list.push_front(std::make_pair(map_key, oldest_index));
lru_map[map_key] = lru_list.begin();
return {oldest_index, false};
}
void TextRenderer::dump_cache_stats() {
std::cout << "Cell cache stats: hits:" << cache_hits
<< " misses:" << cache_misses << "\n";
std::cout << "Cell cache stats: efficiency:" << std::setprecision(2)
<< static_cast<float>(cache_hits) / (cache_misses + cache_hits)
<< "\n";
cache_hits = cache_misses = 0;
}
size_t cell_cache_key_hash::operator()(const CellCacheKey &p) const {
auto pointer_hash = std::hash<void *>{}(std::get<0>(p));
auto color1_hash = std::hash<uint32_t>{}(
*reinterpret_cast<const uint32_t *>(&std::get<1>(p)));
auto color2_hash = std::hash<uint32_t>{}(
*reinterpret_cast<const uint32_t *>(&std::get<2>(p)));
auto string_hash = std::hash<std::string>{}(std::get<3>(p));
return string_hash ^ pointer_hash ^ color1_hash ^ color2_hash;
}
void save_texture(std::string_view file_name, SDL_Renderer *renderer,
SDL_Texture *texture) {
SDL_Texture *target = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, texture);
int width, height;
SDL_QueryTexture(texture, NULL, NULL, &width, &height);
SDL_Surface *surface = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0);
SDL_RenderReadPixels(renderer, NULL, surface->format->format, surface->pixels,
surface->pitch);
SDL_SaveBMP(surface, file_name.data());
SDL_FreeSurface(surface);
SDL_SetRenderTarget(renderer, target);
}
void TextRenderer::dump_cache_to_disk(SDL_Renderer *ren) const {
static int idx = 0;
idx++;
save_texture("cell_cache_" + std::to_string(idx), ren, cacheTex);
}
} // namespace gfx