-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTimedTextManager.cpp
More file actions
381 lines (322 loc) · 10.5 KB
/
TimedTextManager.cpp
File metadata and controls
381 lines (322 loc) · 10.5 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
//
// TimedTextManager.cpp
// SuperDuperDisplay
//
// Created by Henri Asseily on 17/07/2025.
//
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <cstring>
#include "glad/glad.h"
#define STB_TRUETYPE_IMPLEMENTATION
#include "TimedTextManager.h"
void TimedTextManager::Initialize()
{
useDefaultFont = true;
glGenTextures(1, &atlasTex);
CreateGLObjects();
shader.Build("shaders/basic_transform_color.vert", "shaders/overlay_text.frag");
texts.resize(100);
verts.reserve(120 * 240 * 6 * 8); // 120 lines of 240 characters per line
idCounter = 0;
}
void TimedTextManager::Initialize(const std::string& ttfPath, float pixelHeight)
{
this->Initialize();
useDefaultFont = false;
LoadFont(ttfPath, pixelHeight);
}
TimedTextManager::~TimedTextManager() {
glDeleteTextures(1, &atlasTex);
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
}
const size_t TimedTextManager::AddText(const std::string& text, int x, int y, uint64_t durationTicks,
float r, float g, float b, float a) {
texts.push_back({ ++idCounter, text, x, y, SDL_GetTicks64() + durationTicks, r,g,b,a });
return idCounter;
}
bool TimedTextManager::DeleteText(const size_t id) {
auto it = std::find_if(texts.begin(), texts.end(),
[id](const TimedText& t) { return t.id == id; });
if (it != texts.end()) {
texts.erase(it);
return true;
}
return false;
}
void TimedTextManager::UpdateAndRender(bool shouldFlipY) {
if (texts.empty())
return;
if (!shader.isReady)
return;
GLenum glerr;
// Check for High DPI scaling and adjust the font size accordingly
auto window = SDL_GL_GetCurrentWindow();
int winW, winH;
SDL_GetWindowSize(window, &winW, &winH);
int fbW, fbH;
SDL_GL_GetDrawableSize(window, &fbW, &fbH);
glGetIntegerv(GL_VIEWPORT, last_viewport); // remember existing viewport to restore it later
glViewport(0, 0, fbW, fbH);
// DPI (pixel) multiplier in X and Y
float dpiScaleX = static_cast<float>(fbW) / winW;
float dpiScaleY = static_cast<float>(fbH) / winH;
// disable depth, disable depth writes
// necessary to have an overlay and not a black screen
GLboolean depthMaskState = GL_FALSE;
glGetBooleanv(GL_DEPTH_WRITEMASK, &depthMaskState);
GLboolean depthTestState = GL_FALSE;
glGetBooleanv(GL_DEPTH_TEST, &depthTestState);
if (depthMaskState == GL_TRUE)
glDepthMask(GL_FALSE);
if (depthTestState == GL_TRUE)
glDisable(GL_DEPTH_TEST);
shader.Use();
if ((glerr = glGetError()) != GL_NO_ERROR) {
std::cerr << "TimedTextManager glUseProgram error: " << glerr << std::endl;
return;
}
glBindVertexArray(vao);
if (useDefaultFont) {
shader.SetUniform("uTex", _TEXUNIT_IMAGE_FONT_ROM_DEFAULT - GL_TEXTURE0);
} else {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, atlasTex);
shader.SetUniform("uTex", GL_TEXTURE0 - GL_TEXTURE0);
}
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// upload projection
glm::mat4 proj = {
2.0f / fbW, 0, 0, 0,
0, 2.0f / fbH, 0, 0,
0, 0, -1, 0,
-1, -1, 0, 1
};
shader.SetUniform("uTransform", proj);
float x0 = 0, y0 = 0, x1 = 0, y1 = 0; // position
float s0 = 0, t0 = 0, s1 = 0, t1 = 0; // tex uv
// precompute
constexpr float invAtlasW = 1.0f / float(TT_ATLAS_W);
constexpr float invAtlasH = 1.0f / float(TT_ATLAS_H);
// build & draw each string
for (int i = int(texts.size()) - 1; i >= 0; --i) {
auto &t = texts[i];
if (t.ticksFinish < SDL_GetTicks64()) {
texts.erase(texts.begin() + i);
continue;
}
// penX and penY are before HIGH DPI scaling
float penX = float(t.x);
float penY = float(t.y);
// Don't draw if out of bounds
if (penY > winH || penY < 0) {
std::cerr << "didn't draw " << t.text <<std::endl;
continue;
}
if (useDefaultFont)
{
int fontXW = (use80ColDefaultFont ? 7 : 14);
int fontYH = 16;
for (unsigned char c : t.text) {
// Kind of map ascii to the apple charset
// Not perfect, but that's what you get for reusing a free texture
if (c < 0x20 || c > 0x7F) continue;
if (c < 0x40) // punctuation
c += 0x80;
else if (c < 0x60) // caps
c += 0x40;
else // lowecase
c += 0x80;
// calculate origin of char in texture
// texture is 16x16 characters, each 14x16 pixels
// There's no ascent
int texS = (c % 16) * 14; // x origin in pixels
int texT = (c / 16) * 16; // y origin
x0 = penX * dpiScaleX;
y0 = penY * dpiScaleY;
x1 = x0 + fontXW * dpiScaleX;
y1 = y0 + fontYH * dpiScaleY;
s0 = texS / (float)(14 * 16);
t0 = texT / (float)(16 * 16);
s1 = s0 + 1.f/16.f;
t1 = t0 + 1.f/16.f;
if (shouldFlipY)
std::swap(t0, t1);
penX += fontXW;
// 2 tris (one quad) per character
verts.insert(verts.end(), {
x0,y0,s0,t0, t.r, t.g, t.b, t.a,
x1,y0,s1,t0, t.r, t.g, t.b, t.a,
x1,y1,s1,t1, t.r, t.g, t.b, t.a,
x0,y0,s0,t0, t.r, t.g, t.b, t.a,
x1,y1,s1,t1, t.r, t.g, t.b, t.a,
x0,y1,s0,t1, t.r, t.g, t.b, t.a
});
}
} else { // custom font, using stb_truetype
penY += ascent;
for (unsigned char c : t.text) {
if (c < TT_FIRST_CHAR) continue;
auto &bc = packedChars[c - TT_FIRST_CHAR];
x0 = (penX + bc.xoff);
y0 = (penY + bc.yoff);
x1 = x0 + (bc.x1 - bc.x0);
y1 = y0 + (bc.y1 - bc.y0);
// Convert to screen pixels
x0 *= dpiScaleX; x1 *= dpiScaleX;
y0 *= dpiScaleY; y1 *= dpiScaleY;
if (shouldFlipY) {
// flip around the top of the glyph
float glyphH = y1 - y0;
y0 = (2*penY)*dpiScaleY - y0 - glyphH;
y1 = y0 + glyphH;
}
s0 = bc.x0 * invAtlasW;
s1 = bc.x1 * invAtlasW;
t0 = bc.y0 * invAtlasH;
t1 = bc.y1 * invAtlasH;
if (shouldFlipY)
std::swap(t0, t1);
penX += bc.xadvance;
// 2 tris (one quad) per character
verts.insert(verts.end(), {
x0,y0,s0,t0, t.r, t.g, t.b, t.a,
x1,y0,s1,t0, t.r, t.g, t.b, t.a,
x1,y1,s1,t1, t.r, t.g, t.b, t.a,
x0,y0,s0,t0, t.r, t.g, t.b, t.a,
x1,y1,s1,t1, t.r, t.g, t.b, t.a,
x0,y1,s0,t1, t.r, t.g, t.b, t.a
});
}
}
// Draw the string given the uniforms
if (!verts.empty()) {
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER,
verts.size() * sizeof(float),
verts.data(),
GL_DYNAMIC_DRAW);
GLsizei count = GLsizei(verts.size() / 8);
glDrawArrays(GL_TRIANGLES, 0, count);
}
verts.clear();
}
// restore depth writes & test for next passes
if (depthMaskState == GL_TRUE)
glDepthMask(GL_TRUE);
if (depthTestState == GL_TRUE)
glEnable(GL_DEPTH_TEST);
// cleanup
glBindVertexArray(0);
shader.Release();
glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
if ((glerr = glGetError()) != GL_NO_ERROR) {
std::cerr << "TimedTextManager UpdateAndRender error: " << glerr << std::endl;
}
}
/*
PRIVATE METHODS
*/
void TimedTextManager::LoadFont(const std::string& path, float pixelHeight) {
std::ifstream f(path, std::ios::binary | std::ios::ate);
if (!f) throw std::runtime_error("Font file open failed");
std::streamsize sz = f.tellg();
f.seekg(0);
fontBuffer.resize(sz);
f.read((char*)fontBuffer.data(), sz);
if (!stbtt_InitFont(&fontInfo, fontBuffer.data(), 0))
throw std::runtime_error("Font init failed");
// initialize packer
stbtt_pack_context pc;
if (!stbtt_PackBegin(&pc, atlas.data(), TT_ATLAS_W, TT_ATLAS_H, /*stride=*/0, /*padding=*/1, /*alloc_context=*/nullptr))
throw std::runtime_error("Failed to init packer");
// pack exactly FIRST_CHAR…FIRST_CHAR+NUM_CHARS‑1
stbtt_PackFontRange(&pc,
fontBuffer.data(), // TTF
/*font_index=*/0,
pixelHeight,
TT_FIRST_CHAR,
TT_NUM_CHARS,
packedChars.data()
);
stbtt_PackEnd(&pc);
float scale = stbtt_ScaleForPixelHeight(&fontInfo, pixelHeight);
stbtt_GetFontVMetrics(&fontInfo, &ascent, nullptr, nullptr);
ascent = static_cast<int>(ascent * scale);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, atlasTex);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, TT_ATLAS_W, TT_ATLAS_H, 0, GL_RED, GL_UNSIGNED_BYTE, atlas.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
}
void TimedTextManager::CreateGLObjects() {
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0); // vec2 position
glEnableVertexAttribArray(1); // vec2 texcoord
glEnableVertexAttribArray(2); // vec4 color
constexpr GLsizei stride = 8 * sizeof(float);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, stride, (void*)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, stride, (void*)(2 * sizeof(float)));
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, stride, (void*)(4 * sizeof(float)));
glBindVertexArray(0);
}
// Decode the next UTF‑8 codepoint from `s` at byte index `pos`.
// Returns the number of bytes consumed, and writes the Unicode codepoint to `cp`.
size_t TimedTextManager::DecodeUTF8(const std::string& s, size_t pos, uint32_t& cp) {
unsigned char c = static_cast<unsigned char>(s[pos]);
if ((c & 0x80) == 0) {
// 1‑byte ASCII
cp = c;
return 1;
}
else if ((c & 0xE0) == 0xC0 && pos + 1 < s.size()) {
// 2‑byte sequence
cp = (c & 0x1F) << 6;
cp |= (static_cast<unsigned char>(s[pos+1]) & 0x3F);
return 2;
}
else if ((c & 0xF0) == 0xE0 && pos + 2 < s.size()) {
// 3‑byte sequence
cp = (c & 0x0F) << 12;
cp |= (static_cast<unsigned char>(s[pos+1]) & 0x3F) << 6;
cp |= (static_cast<unsigned char>(s[pos+2]) & 0x3F);
return 3;
}
else if ((c & 0xF8) == 0xF0 && pos + 3 < s.size()) {
// 4‑byte sequence
cp = (c & 0x07) << 18;
cp |= (static_cast<unsigned char>(s[pos+1]) & 0x3F) << 12;
cp |= (static_cast<unsigned char>(s[pos+2]) & 0x3F) << 6;
cp |= (static_cast<unsigned char>(s[pos+3]) & 0x3F);
return 4;
}
// Invalid or truncated sequence: emit replacement char U+FFFD
cp = 0xFFFD;
return 1;
}
// Computes pixel width of a UTF-8 string at given pixel height, including kerning.
float TimedTextManager::MeasureTextWidth(const std::string& utf8, const float fontSize) {
const float scale = stbtt_ScaleForPixelHeight(&fontInfo, fontSize);
float width = 0.0f;
int prev_cp = 0;
for (size_t i = 0; i < utf8.size(); ) {
uint32_t cp = 0;
i += DecodeUTF8(utf8, i, cp);
int advance, lsb;
stbtt_GetCodepointHMetrics(&fontInfo, cp, &advance, &lsb);
int kern = stbtt_GetCodepointKernAdvance(&fontInfo, prev_cp, cp);
width += (advance + kern) * scale;
prev_cp = cp;
}
return width;
}