summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2022-08-29 07:51:09 +0200
committerBad Diode <bd@badd10de.dev>2022-08-29 07:51:09 +0200
commit2b660a013704fd71111c919491b6f5d404c76fbc (patch)
treebd166dbb9dbc306a4c0ed3a0ea9bf1dafe60bd30
parentd5f10240f4598af6486317100f9b995cecd0eb29 (diff)
downloadogl-monotext-2b660a013704fd71111c919491b6f5d404c76fbc.tar.gz
ogl-monotext-2b660a013704fd71111c919491b6f5d404c76fbc.zip
Add initial text system
-rw-r--r--src/main.c69
1 files changed, 36 insertions, 33 deletions
diff --git a/src/main.c b/src/main.c
index 60faaea..7a8b6e7 100644
--- a/src/main.c
+++ b/src/main.c
@@ -7,7 +7,7 @@
7#include "shorthand.h" 7#include "shorthand.h"
8#include "bd-font.c" 8#include "bd-font.c"
9 9
10#define TEXT_OVERLAY_SIZE 10 10#define TEXT_OVERLAY_SIZE 5
11 11
12// 12//
13// Callbacks. 13// Callbacks.
@@ -225,31 +225,21 @@ init_debug_overlay(Context *ctx) {
225 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ctx->text_overlay.ebo); 225 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ctx->text_overlay.ebo);
226 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); 226 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
227 227
228 // Texture atlas. 228 // Unpack texture atlas.
229 // TODO: Full 256 ascii font.
230 u8 texture[256 * 8 * 8] = {0}; 229 u8 texture[256 * 8 * 8] = {0};
231 unpack_letter(&bd_font['A' * 2], (u32*)&texture[0]); 230 for (size_t i = 0; i < 256; i++) {
232 for (size_t i = 0; i < 8; ++i) { 231 unpack_letter(&bd_font[i * 2], (u32*)&texture[i * 8 * 8]);
233 printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
234 texture[0 + i * 8],
235 texture[1 + i * 8],
236 texture[2 + i * 8],
237 texture[3 + i * 8],
238 texture[4 + i * 8],
239 texture[5 + i * 8],
240 texture[6 + i * 8],
241 texture[7 + i * 8]);
242 } 232 }
233
234 // Create texture for text.
243 glGenTextures(1, &ctx->text_overlay.tex_id); 235 glGenTextures(1, &ctx->text_overlay.tex_id);
244 glBindTexture(GL_TEXTURE_2D, ctx->text_overlay.tex_id); 236 glBindTexture(GL_TEXTURE_2D, ctx->text_overlay.tex_id);
245 glTexImage2D(GL_TEXTURE_2D, 0, 237 glTexImage2D(GL_TEXTURE_2D, 0,
246 GL_R8, 238 GL_R8,
247 8, // Width 239 8, // Width
248 1 * 8, // Height 240 256 * 8, // Height
249 0, GL_RED, 241 0, GL_RED,
250 GL_UNSIGNED_BYTE, &texture); 242 GL_UNSIGNED_BYTE, &texture);
251 // glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, sizeof(bd_font), 8, 0, GL_RED,
252 // GL_UNSIGNED_BYTE, &bd_font);
253 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 243 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
254 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 244 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
255 glBindTexture(GL_TEXTURE_2D, 0); 245 glBindTexture(GL_TEXTURE_2D, 0);
@@ -272,7 +262,7 @@ init_debug_overlay(Context *ctx) {
272"uniform float size;\n" 262"uniform float size;\n"
273 263
274"void main() {\n" 264"void main() {\n"
275" float N = 3;\n" // TODO: should be 256 for the full font. 265" float N = 256;\n"
276" float idx = offset.z;\n" 266" float idx = offset.z;\n"
277" float m = 1.0 / N;\n" 267" float m = 1.0 / N;\n"
278" float k = m * idx;\n" 268" float k = m * idx;\n"
@@ -289,8 +279,7 @@ init_debug_overlay(Context *ctx) {
289" pos += scalar * offset.xy;\n" 279" pos += scalar * offset.xy;\n"
290" pos = mix(vec2(-1.0), vec2(1.0), pos);\n" 280" pos = mix(vec2(-1.0), vec2(1.0), pos);\n"
291" gl_Position = vec4(pos, 0.0, 1.0);\n" 281" gl_Position = vec4(pos, 0.0, 1.0);\n"
292// " tex = tex_coords * vec2(1.0, m) + vec2(0.0, k);\n" 282" tex = tex_coords * vec2(1.0, m) + vec2(0.0, k);\n"
293" tex = tex_coords;\n"
294"}"; 283"}";
295 284
296 // Fragment shader. 285 // Fragment shader.
@@ -341,19 +330,33 @@ update_frame_time(Context *ctx) {
341} 330}
342 331
343void 332void
333clear_text(Context *ctx) {
334 ctx->text_overlay.cur_x = 0;
335 ctx->text_overlay.cur_y = 0;
336 ctx->text_overlay.n_chars = 0;
337}
338
339void
340add_text(Context *ctx, char *txt) {
341 while (*txt != '\0') {
342 if (*txt == '\n') {
343 ctx->text_overlay.cur_x = 0;
344 ctx->text_overlay.cur_y++;
345 txt++;
346 continue;
347 }
348 ctx->text_overlay.letters[ctx->text_overlay.n_chars].x = ctx->text_overlay.cur_x++;
349 ctx->text_overlay.letters[ctx->text_overlay.n_chars].y = ctx->text_overlay.cur_y;
350 ctx->text_overlay.letters[ctx->text_overlay.n_chars].idx = *txt++;
351 ctx->text_overlay.n_chars++;
352 }
353}
354
355void
344update_text(Context *ctx) { 356update_text(Context *ctx) {
345 // TODO: Test with the full font atlas and characters. 357 clear_text(ctx);
346 // TODO: add_text function for setting up letters[] based on a char* + n_chars. 358 add_text(ctx, "hello world!");
347 ctx->text_overlay.letters[0].x = 0; 359
348 ctx->text_overlay.letters[0].y = 0;
349 ctx->text_overlay.letters[0].idx = 0;
350 ctx->text_overlay.letters[1].x = 1;
351 ctx->text_overlay.letters[1].y = 0;
352 ctx->text_overlay.letters[1].idx = 1;
353 ctx->text_overlay.letters[2].x = 2;
354 ctx->text_overlay.letters[2].y = 0;
355 ctx->text_overlay.letters[2].idx = 2;
356 ctx->text_overlay.n_chars = 3;
357 glBindBuffer(GL_ARRAY_BUFFER, ctx->text_overlay.vbo); 360 glBindBuffer(GL_ARRAY_BUFFER, ctx->text_overlay.vbo);
358 glBufferData(GL_ARRAY_BUFFER, 361 glBufferData(GL_ARRAY_BUFFER,
359 sizeof(struct Letter) * ctx->text_overlay.n_chars, 362 sizeof(struct Letter) * ctx->text_overlay.n_chars,