-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticFont.cpp
More file actions
529 lines (451 loc) · 23 KB
/
StaticFont.cpp
File metadata and controls
529 lines (451 loc) · 23 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#include "StaticFont.hpp"
#include <algorithm>
#include <SDL_surface.h>
#include <SDL_render.h>
#include <array>
#include <iostream>
// *****************************
// TEXT GOOBLERS
// ****************************
void gn::Text::render(SDL_Renderer* renderer) const noexcept {
SDL_RenderCopyEx(
renderer,
texture,
nullptr,
reinterpret_cast<const SDL_Rect*>(&pos),
angle,
nullptr,
flip
);
}
gn::Text gn::Text::clone(SDL_Renderer* renderer) const noexcept {
SDL_Texture* clone = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, r_width, r_height);
Uint8 r, g, b, a;
SDL_GetTextureColorMod(texture, &r, &g, &b);
SDL_GetTextureAlphaMod(texture, &a);
setColor(255, 255, 255, 255);
SDL_Texture* user_render_target = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, clone); //< Swap render targets
SDL_RenderCopy(
renderer,
texture,
nullptr,
nullptr
);
SDL_SetRenderTarget(renderer, user_render_target); //< Swap it back to whatever the user had.
setColor(r, g, b, a);
return Text(clone, {r, g, b, a}, static_cast<int>(this->bw), static_cast<int>(this->bh), sx, sy, flip, angle, pos);
}
void gn::Text::setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a) const noexcept {
SDL_SetTextureColorMod(texture, r, g, b);
SDL_SetTextureAlphaMod(texture, a);
}
void gn::Text::flipHorizontal() noexcept {
flip = SDL_FLIP_HORIZONTAL;
}
void gn::Text::flipVertical() noexcept {
flip = SDL_FLIP_VERTICAL;
}
void gn::Text::setScale(uint32_t scale) noexcept {
r_width = static_cast<int>(bw * scale);
r_height = static_cast<int>(bh * scale);
sx = static_cast<int>(scale);
sy = static_cast<int>(scale);
}
void gn::Text::setScale(std::pair<uint32_t, uint32_t> scale) noexcept {
r_width = static_cast<int>(scale.first * bw);
r_height = static_cast<int>(scale.second * bh);
sx = static_cast<int>(scale.first);
sy = static_cast<int>(scale.second);
}
void gn::Text::center(const SDL_Rect region) noexcept {
pos.x = region.x + (region.w - r_width)/2;
pos.y = region.y + (region.h - r_height)/2;
}
int gn::Text::getWidth() const noexcept {
return r_width;
}
int gn::Text::getHeight() const noexcept {
return r_height;
}
gn::Text::Text(Text&& o) noexcept :
texture(o.texture),
angle(o.angle),
pos(o.pos),
r_width(o.r_width),
r_height(o.r_height),
sx(o.sx),
sy(o.sy),
flip(o.flip),
bw(o.bw),
bh(o.bh)
{
o.texture = nullptr;
}
gn::Text& gn::Text::operator=(Text&& o) noexcept {
std::swap(angle, o.angle);
std::swap(pos, o.pos);
std::swap(r_width, o.r_width);
std::swap(r_height, o.r_height);
std::swap(sx, o.sx);
std::swap(sy, o.sy);
std::swap(flip, o.flip);
std::swap(bw, o.bw);
std::swap(bh, o.bh);
std::swap(texture, o.texture);
return *this;
}
gn::Text::~Text() {
if (texture) {
SDL_DestroyTexture(texture);
}
}
gn::Text::Text(
SDL_Texture* tex,
const SDL_Color col,
const int base_w,
const int base_h,
const uint32_t scale_x,
const uint32_t scale_y,
const SDL_RendererFlip flip,
const float angle,
const SDL_Point pos
)
: angle(angle),
pos(pos),
r_width(base_w),
r_height(base_h),
texture(tex),
bw(base_w),
bh(base_h),
sx(static_cast<int>(scale_x)),
sy(static_cast<int>(scale_y)),
flip(flip)
{
r_width *= this->sx;
r_height *= this->sy;
// Set the texture to be the color that we want.
SDL_SetTextureColorMod(texture, col.r, col.g, col.b);
SDL_SetTextureAlphaMod(texture, col.a);
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
}
// *****************************
// FONT GOOBLERS
// ****************************
// STATIC VARIABLE INITIALIZATION
SDL_Renderer* gn::StaticFont::sdl_renderer = nullptr;
SDL_Texture* gn::StaticFont::font_texture = nullptr;
uint32_t gn::StaticFont::font_scale = 1;
SDL_Color gn::StaticFont::font_color{255, 255, 255, 255};
// Width, height, and pitch of the final font render
static constexpr uint32_t fontWidth = 132, fontHeight = 28, pitch = 528;
// Compression & encoding o the font
static constexpr int fontEncoding[] =
{ 5, (1 | 128u), 11, (1 | 128u), 7, (2 | 128u), 5, (1 | 128u), 8, (1 | 128u), 4, (1 | 128u), 37, (1 | 128u), 47,
5, (1 | 128u), 11, (1 | 128u), 6, (1 | 128u), 7, (1 | 128u), 4, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 4, (1 | 128u), 37, (1 | 128u), 47,
1, (2 | 128u), 2, (3 | 128u), 3, (2 | 128u), 2, (3 | 128u), 2, (2 | 128u), 2, (2 | 128u), 2, (2 | 128u), 2, (3 | 128u), 6, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (2 | 128u), 1, (2 | 128u), 3, (2 | 128u), 3, (2 | 128u), 2, (3 | 128u), 3, (2 | 128u), 3, (2 | 128u), 2, (2 | 128u), 1, (2 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 5, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (3 | 128u), 11,
3, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 11,
1, (3 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 1, (4 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 1, (2 | 128u), 3, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 4, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 12,
0, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 4, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 5, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 13,
1, (3 | 128u), 1, (3 | 128u), 3, (2 | 128u), 2, (3 | 128u), 2, (2 | 128u), 2, (1 | 128u), 3, (3 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (2 | 128u), 2, (3 | 128u), 3, (3 | 128u), 1, (1 | 128u), 3, (2 | 128u), 3, (2 | 128u), 2, (2 | 128u), 4, (1 | 128u), 4, (2 | 128u), 1, (2 | 128u), 2, (1 | 128u), 3, (1 | 128u), 2, (3 | 128u), 1, (3 | 128u), 11,
30, (1 | 128u), 8, (1 | 128u), 26, (1 | 128u), 7, (1 | 128u), 41, (1 | 128u), 15,
28, (2 | 128u), 8, (1 | 128u), 27, (1 | 128u), 7, (2 | 128u), 38, (2 | 128u), 16,
127, 5,
1, (2 | 128u), 2, (3 | 128u), 3, (2 | 128u), 1, (3 | 128u), 2, (3 | 128u), 1, (3 | 128u), 2, (2 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 1, (2 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (2 | 128u), 2, (3 | 128u), 3, (2 | 128u), 2, (3 | 128u), 3, (2 | 128u), 1, (3 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 1, (3 | 128u), 10,
0, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 4, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 4, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 10,
0, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 4, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 3, (2 | 128u), 1, (2 | 128u), 1, (2 | 128u), 1, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 4, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 11,
0, (4 | 128u), 1, (3 | 128u), 2, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 1, (3 | 128u), 1, (3 | 128u), 1, (1 | 128u), 1, (2 | 128u), 1, (4 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (2 | 128u), 3, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 1, (2 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (3 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (3 | 128u), 3, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 5, (1 | 128u), 4, (1 | 128u), 11,
0, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 4, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (2 | 128u), 1, (2 | 128u), 2, (1 | 128u), 1, (1 | 128u), 4, (1 | 128u), 4, (1 | 128u), 11,
0, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 4, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 12,
0, (1 | 128u), 2, (1 | 128u), 1, (3 | 128u), 3, (2 | 128u), 1, (3 | 128u), 2, (3 | 128u), 1, (1 | 128u), 4, (2 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 1, (3 | 128u), 1, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (2 | 128u), 2, (1 | 128u), 5, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (2 | 128u), 3, (1 | 128u), 3, (2 | 128u), 4, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 3, (3 | 128u), 10,
127, 5,
1, (2 | 128u), 2, (2 | 128u), 2, (2 | 128u), 2, (2 | 128u), 2, (1 | 128u), 4, (3 | 128u), 2, (2 | 128u), 2, (3 | 128u), 2, (2 | 128u), 3, (2 | 128u), 3, (2 | 128u), 9, (2 | 128u), 1, (2 | 128u), 2, (1 | 128u), 1, (1 | 128u), 4, (1 | 128u), 1, (1 | 128u), 4, (2 | 128u), 2, (1 | 128u), 1, (5 | 128u), 43,
0, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 4, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 6, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 8, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 1, (2 | 128u), 1, (2 | 128u), 43,
0, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 4, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 6, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 4, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 5, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 43,
0, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 2, (4 | 128u), 1, (2 | 128u), 2, (3 | 128u), 3, (1 | 128u), 3, (2 | 128u), 3, (3 | 128u), 1, (1 | 128u), 2, (1 | 128u), 8, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 4, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 43,
0, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 5, (1 | 128u), 3, (1 | 128u), 4, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 4, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 8, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 1, (1 | 128u), 43,
0, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 2, (1 | 128u), 5, (1 | 128u), 3, (1 | 128u), 4, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 2, (1 | 128u), 4, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 4, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 3, (1 | 128u), 1, (1 | 128u), 5, (1 | 128u), 8, (2 | 128u), 1, (2 | 128u), 43,
1, (2 | 128u), 2, (3 | 128u), 1, (3 | 128u), 1, (2 | 128u), 4, (1 | 128u), 2, (2 | 128u), 3, (2 | 128u), 2, (1 | 128u), 4, (2 | 128u), 3, (2 | 128u), 3, (2 | 128u), 2, (1 | 128u), 4, (1 | 128u), 1, (2 | 128u), 1, (2 | 128u), 2, (1 | 128u), 1, (1 | 128u), 2, (1 | 128u), 5, (1 | 128u), 2, (1 | 128u), 3, (1 | 128u), 1, (5 | 128u), 43,
54, (1 | 128u), 77,
127, 5,
127, 5,
};
// Rectangles that bind to the src texture for each letter.
static constexpr std::array<SDL_Rect, 75> character_rects = {
SDL_Rect{0, 0, 4, 9}, // a
{5, 0, 4, 9}, // b
{10, 0, 3, 9}, // c
{14, 0, 4, 9}, // d
{19, 0, 4, 9}, // e
{24, 0, 3, 9}, // f
{27, 0, 4, 9}, // g
{32, 0, 4, 9},
{37, 0, 1, 9}, // i
{38, 0, 2, 9}, // j
{41, 0, 4, 9}, // k
{46, 0, 1, 9},
{48, 0, 7, 9},
{56, 0, 4, 9},
{61, 0, 4, 9},
{66, 0, 4, 9},
{71, 0, 5, 9},
{76, 0, 3, 9},
{80, 0, 3, 9}, // s
{84, 0, 3, 9},
{88, 0, 4, 9},
{93, 0, 5, 9}, // v
{99, 0, 7, 9},
{107, 0, 5, 9},
{113, 0, 4, 9},
{118, 0, 3, 9}, // z
{0, 10, 4, 7}, // A
{5, 10, 4, 7},
{10, 10, 3, 7},
{14, 10, 4, 7},
{19, 10, 3, 7},
{23, 10, 3, 7},
{27, 10, 4, 7},
{32, 10, 4, 7}, // H
{37, 10, 1, 7},
{39, 10, 2, 7},
{42, 10, 4, 7},
{47, 10, 3, 7},
{51, 10, 5, 7},
{57, 10, 4, 7},
{62, 10, 4, 7},
{67, 10, 4, 7}, // P
{72, 10, 4, 7},
{77, 10, 4, 7},
{82, 10, 3, 7},
{86, 10, 3, 7},
{90, 10, 4, 7},
{95, 10, 5, 7}, // V
{101, 10, 5, 7},
{107, 10, 5, 7},
{113, 10, 5, 7},
{119, 10, 3, 7}, // Z
{0, 18, 4, 8}, // 0
{5, 18, 3, 8}, // 1
{9, 18, 3, 8}, // 2
{13, 18, 3, 8}, // 3
{17, 18, 4, 8}, // 4
{22, 18, 3, 8}, // 5
{26, 18, 4, 8}, // 6
{31, 18, 3, 8}, // 7
{35, 18, 4, 8}, // 8
{40, 18, 4, 8}, // 9
{45, 18, 4, 8}, // .
{50, 18, 1, 8}, // :
{52, 18, 1, 8}, // ,
{54, 18, 2, 8}, // [
{57, 18, 2, 8}, // ]
{60, 18, 2, 8}, // (
{63, 18, 2, 8}, // )
{66, 18, 2, 8}, // /
{69, 18, 3, 8}, // '\'
{73, 18, 3, 8}, // ?
{77, 18, 4, 8}, // ?
{82, 18, 1, 8},
{0, 0, 2, 0} // space
};
// Constructs a c-style array into a constexpr std::array
template <typename T, std::size_t N, std::size_t... I>
constexpr std::array<T, N> to_array_constexpr(const T (&raw)[N], std::index_sequence<I...>) {
return {{ raw[I]... }};
}
// Creates a link table between characters and their respective character rectangles. (performance optimization)
constexpr std::array<const SDL_Rect*, 256> create_link_table() {
const SDL_Rect* link_table[256]{};
for (int i = 'a'; i <= 'z'; ++i)
link_table[i] = &character_rects[i - 'a'];
for (int i = 'A'; i <= 'Z'; ++i)
link_table[i] = &character_rects[i - 'A' + 26];
for (int i = '0'; i <= '9'; ++i)
link_table[i] = &character_rects[i - '0' + 52];
link_table['.'] = &character_rects[63];
link_table[':'] = &character_rects[64];
link_table[','] = &character_rects[65];
link_table['['] = &character_rects[66];
link_table[']'] = &character_rects[67];
link_table['('] = &character_rects[68];
link_table[')'] = &character_rects[69];
link_table['/'] = &character_rects[70];
link_table['\\'] = &character_rects[71];
link_table['?'] = &character_rects[72];
link_table['!'] = &character_rects[73];
link_table[' '] = &character_rects[74];
return to_array_constexpr(link_table, std::make_index_sequence<256>{});
}
static constexpr auto link_table = create_link_table();
[[nodiscard]] SDL_Surface* decodeFontDataToSurface() {
SDL_Surface* surf =
SDL_CreateRGBSurfaceWithFormat(
0, // flags
fontWidth, fontHeight, // width and height
1,
SDL_PIXELFORMAT_RGBA8888 // depth
);
// Static cast our surface to unsigned ints so it's easier to update (usually void*)
auto* pixels = static_cast<uint32_t*>(surf->pixels);
const int expected_pixels = surf->w * surf->h;
// Loop through and fill in relevant pixels.
int pixel_idx = 0;
for (const uint_fast8_t val : fontEncoding) {
// If our pixel is currently filled or not
const bool fill = val & 128u;
// The number of times we fill in pixels.
const uint32_t count = val & 127u;
for (size_t i = 0; i < count && pixel_idx < expected_pixels; i++) {
pixels[pixel_idx++] = fill ? 0xFFFFFFFF : 0x00000000;
}
}
// Return the surface back to the user.
return surf;
}
void gn::StaticFont::initialize(SDL_Renderer* renderer) noexcept {
// initialized ensures we don't accidentally not destroy a texture
// we aren't guaranteed font_texture is initialized because it is a static member var.
static bool initialized = false;
if (initialized && font_texture != nullptr) {
SDL_DestroyTexture(font_texture);
}
initialized = true;
SDL_Surface* surf = decodeFontDataToSurface();
font_texture = SDL_CreateTextureFromSurface(renderer, surf);
sdl_renderer = renderer;
SDL_FreeSurface(surf);
SDL_SetTextureBlendMode(font_texture, SDL_BLENDMODE_BLEND);
SDL_SetTextureAlphaMod(font_texture, 255);
SDL_SetTextureColorMod(font_texture, 255, 255, 255);
}
void gn::StaticFont::renderLine(SDL_Renderer* renderer, const char text[], size_t num_chars, const SDL_Point pos) {
SDL_Rect dst = { pos.x, pos.y, 0, 0 };
for (size_t i = 0; i < num_chars; i++) {
const char c = text[i];
if (const SDL_Rect* src = link_table[c]) {
dst.w = src->w * font_scale;
dst.h = src->h * font_scale;
SDL_RenderCopy(sdl_renderer, font_texture, src, &dst);
dst.x += (src->w + 1) * font_scale;
}
}
}
SDL_Point getLineSize(const char text[], size_t& end_pos, size_t start_pos = 0) {
end_pos = start_pos;
while (text[end_pos] != '\n' && text[end_pos] != '\0') { end_pos++; }
int x = 0, max_y = 0;
for (int i = start_pos; i < end_pos; i++) {
x += (link_table[text[i]]->w + 1);
max_y = std::max(max_y, link_table[text[i]]->h);
}
return { x, max_y };
}
SDL_Point getTextSize(const char text[], int scale) {
int tot_y{}, curr_y{};
int max_x{}, curr_x{};
const size_t len = strlen(text);
for (size_t i = 0; i < len; i++) {
const char c = text[i];
if (const SDL_Rect* r = link_table[c]) {
curr_y = std::max(curr_y, r->h);
curr_x += (r->w + 1);
} else if (c == '\n') {
max_x = std::max(max_x, curr_x);
tot_y += curr_y + 1;
curr_x = 0;
curr_y = 0;
}
}
tot_y += curr_y;
max_x = std::max(max_x, curr_x);
return { max_x * scale, tot_y * scale};
}
void gn::StaticFont::render(SDL_Renderer* renderer, const char text[], const SDL_Point pos, const Align align) noexcept {
SDL_Rect dst = { pos.x, pos.y, 0, 0 };
constexpr static auto safe_strncpy = [](const char* src, size_t size) {
char* arr = static_cast<char*>(malloc(size + 3));
strncpy(arr, src, size);
arr[size] = '\0';
return arr;
};
size_t start_pos = 0, end_pos = 0;
SDL_Point pencil_pos = { pos.x, pos.y };
SDL_Point dim;
const size_t len = strlen(text);
switch (align) {
case Align::LEFT:
for (size_t i = start_pos; i < len; i++) {
const unsigned char c = text[i];
if (const SDL_Rect* src = link_table[c]) {
dst.w = src->w * font_scale;
dst.h = src->h * font_scale;
SDL_RenderCopy(sdl_renderer, font_texture, src, &dst);
dst.x += (src->w + 1) * font_scale;
} else if (c == '\n') {
dst.x = pos.x;
dst.y += (character_rects[26].h + 3) * font_scale;
}
}
break;
case Align::CENTER:
dim = getTextSize(text, font_scale);
while (end_pos < len) {
SDL_Point lineSize = getLineSize(text, end_pos, start_pos);
lineSize.x *= font_scale;
lineSize.y *= font_scale;
pencil_pos.x = (dim.x - lineSize.x)/2 + pos.x;
auto line = safe_strncpy(text + start_pos, end_pos - start_pos);
renderLine(renderer, line, end_pos - start_pos + 1, pencil_pos);
free(line);
start_pos = end_pos + 1;
pencil_pos.y += (lineSize.y + font_scale*3);
}
break;
case Align::RIGHT:
dim = getTextSize(text, font_scale);
while (end_pos < len) {
SDL_Point lineSize = getLineSize(text, end_pos, start_pos);
lineSize.x *= font_scale;
lineSize.y *= font_scale;
pencil_pos.x = (dim.x - lineSize.x) + pos.x;
auto line = safe_strncpy(text + start_pos, end_pos - start_pos);
renderLine(renderer, line, end_pos - start_pos + 1, pencil_pos);
free(line);
start_pos = end_pos + 1;
pencil_pos.y += (lineSize.y + font_scale*3);
}
break;
}
}
gn::Text gn::StaticFont::createText(const char text[], const uint32_t scale, const Align align) {
// First reset so we don't run into issue.
SDL_SetTextureColorMod(font_texture, 255, 255, 255);
SDL_SetTextureAlphaMod(font_texture, 255);
const SDL_Point dim = getTextSize(text, 1);
SDL_Texture* tex = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, dim.x, dim.y);
if (!tex) {
throw std::runtime_error("gn::StaticFont::createText(): Could not create texture for font.");
}
SDL_Texture* user_target = SDL_GetRenderTarget(sdl_renderer);
SDL_SetRenderTarget(sdl_renderer, tex);
// Save the scale for later.
const auto temp_scale = font_scale;
setScale(1);
render(sdl_renderer, text, { 0, 0 }, align);
setColor(font_color.r, font_color.g, font_color.b, font_color.a);
setScale(temp_scale);
SDL_SetRenderTarget(sdl_renderer, user_target);
return Text{tex, {255, 255, 255, 255}, dim.x, dim.y, scale, scale};
}
void gn::StaticFont::setColor(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a) noexcept {
font_color = { r, g, b, a };
SDL_SetTextureColorMod(font_texture, r, g, b);
SDL_SetTextureAlphaMod(font_texture, a);
}
void gn::StaticFont::setScale(const uint32_t scale) noexcept {
font_scale = static_cast<int>(scale);
}
void gn::StaticFont::destroy() noexcept {
SDL_DestroyTexture(font_texture);
font_texture = nullptr;
}