diff options
Diffstat (limited to 'in_practice/breakout/text.c')
-rw-r--r-- | in_practice/breakout/text.c | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/in_practice/breakout/text.c b/in_practice/breakout/text.c new file mode 100644 index 0000000..994ffb3 --- /dev/null +++ b/in_practice/breakout/text.c @@ -0,0 +1,127 @@ +#include "text.h" +#include "shader.h" +#include "sys.h" +#include <GL/glew.h> +#include <ft2build.h> +#include FT_FREETYPE_H + +struct text_renderer init_text_renderer(i32 width, i32 height) +{ + struct text_renderer renderer = {0}; + renderer.shader = get_shader("text"); + glUseProgram(renderer.shader); + mat projection = ortho(0.0f, width, height, 0.0f, -1.0f, 1.0f); + uniform_mat(renderer.shader, "projection", projection); + uniform_i32(renderer.shader, "glyph", 0); + glUseProgram(0); + glGenVertexArrays(1, &renderer.vao); + glGenBuffers(1, &renderer.vbo); + glBindVertexArray(renderer.vao); + glBindBuffer(GL_ARRAY_BUFFER, renderer.vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(f32) * 6 * 4, 0, GL_DYNAMIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(f32), 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + return renderer; +} + +void load_font(struct text_renderer *renderer, const char *dir, const char *filename, u32 size) +{ + for (i32 i = 0; i < 128; i++) { + struct text_char *c = renderer->chars + i; + c->texture = 0; + c->size = (v2){0.0f, 0.0f}; + c->bearing = (v2){0.0f, 0.0f}; + c->advance = 0; + } + FT_Library ft; + if (FT_Init_FreeType(&ft)) + die("failed to initialize freetype library"); + FT_Face face; + char path[512] = {0}; + snprintf(path, 512, "%s/%s", (dir ? dir : "."), filename); + if (FT_New_Face(ft, path, 0, &face)) + die("failed to load \"%s\"", path); + FT_Set_Pixel_Sizes(face, 0, size); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + for (u8 c = 0; c < 128; c++) { + if (FT_Load_Char(face, c, FT_LOAD_RENDER)) + die("failed to load char \"%c\"", c); + u32 texture; + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, + face->glyph->bitmap.width, + face->glyph->bitmap.rows, + 0, GL_RED, GL_UNSIGNED_BYTE, + face->glyph->bitmap.buffer); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + renderer->chars[c] = (struct text_char){ + texture, + {face->glyph->bitmap.width, face->glyph->bitmap.rows}, + {face->glyph->bitmap_left, face->glyph->bitmap_top}, + face->glyph->advance.x + }; + glBindTexture(GL_TEXTURE_2D, 0); + } + FT_Done_Face(face); + FT_Done_FreeType(ft); + info("font \"%s\" loaded successfully", path); +} + +v2 get_text_size(struct text_renderer renderer, const char *text, f32 scale) +{ + v2 size = {0}; + for (const char *c = text; *c; c++) { + struct text_char ch = renderer.chars[(u8)*c]; + f32 h = ch.size.y * scale; + if (h > size.y) + size.y = h; + size.x += (ch.advance >> 6) * scale; + } + return size; +} + +void render_text(struct text_renderer renderer, const char *text, v2 pos, f32 scale, v3 color) +{ + glUseProgram(renderer.shader); + uniform_v3(renderer.shader, "text_color", color); + glActiveTexture(GL_TEXTURE0); + glBindVertexArray(renderer.vao); + for (const char *c = text; *c; c++) { + struct text_char ch = renderer.chars[(u8)*c]; + f32 x = pos.x + ch.bearing.x * scale; + f32 y = pos.y + (renderer.chars['H'].bearing.y - ch.bearing.y) * scale; + f32 w = ch.size.x * scale; + f32 h = ch.size.y * scale; + f32 vertices[] = { + x, y + h, 0.0f, 1.0f, + x + w, y, 1.0f, 0.0f, + x, y, 0.0f, 0.0f, + + x, y + h, 0.0f, 1.0f, + x + w, y + h, 1.0f, 1.0f, + x + w, y, 1.0f, 0.0f, + }; + glBindTexture(GL_TEXTURE_2D, ch.texture); + glBindBuffer(GL_ARRAY_BUFFER, renderer.vbo); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glDrawArrays(GL_TRIANGLES, 0, 6); + glBindTexture(GL_TEXTURE_2D, 0); + pos.x += (ch.advance >> 6) * scale; + } + glBindVertexArray(0); + glUseProgram(0); +} + +void render_text_centered(struct text_renderer renderer, const char *text, v2 pos, f32 scale, v3 color) +{ + v2 size = get_text_size(renderer, text, scale); + v2 top_left = sub_v2(pos, scale_v2(size, 0.5f)); + render_text(renderer, text, top_left, scale, color); +} |