src/text.c

changeset 29
1d001eb694dc
parent 26
139dac8ef9ee
child 32
86468a71dd73
--- a/src/text.c	Sun Jan 21 14:01:27 2024 +0100
+++ b/src/text.c	Tue Jan 23 21:34:12 2024 +0100
@@ -32,6 +32,36 @@
 
 #include <GL/glew.h>
 
+static void asc_text_draw(AscText const *node) {
+    if (node->color.alpha == 0 || node->hidden || node->internal.tex_id == 0) {
+        return;
+    }
+
+    glUseProgram(ASC_SHADER_FONT.base.id);
+
+    // Upload projection
+    // TODO: when we group UI draw calls, we don't need this
+    glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1,
+                       GL_FALSE, asc_context.active_window->projection);
+
+    // Upload model matrix
+    asc_mat4f model = {0};
+    model[asc_mat4_index(0, 0)] = node->internal.dimension.width;
+    model[asc_mat4_index(1, 1)] = node->internal.dimension.height;
+    model[asc_mat4_index(3, 0)] = node->position.x;
+    model[asc_mat4_index(3, 1)] = node->position.y;
+    model[asc_mat4_index(3, 3)] = 1;
+    glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, GL_FALSE, model);
+
+    // Upload surface
+    glActiveTexture(GL_TEXTURE0);
+    glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
+    glUniform1i(ASC_SHADER_FONT.surface, 0);
+
+    // Draw mesh
+    asc_primitives_draw_plane();
+}
+
 AscText *asc_text(int x, int y, char const *text) {
     AscText *node = calloc(1, sizeof(AscText));
     if (node == NULL) {
@@ -39,6 +69,9 @@
         return NULL;
     }
 
+    node->node.free_func = (asc_scene_free_func) asc_text_free;
+    node->node.draw_func = (asc_scene_draw_func) asc_text_draw;
+
     node->position = (asc_vec2i) {x, y};
     node->font = asc_context.active_font;
     node->color = asc_context.ink;
@@ -93,41 +126,6 @@
     SDL_FreeSurface(surface);
 }
 
-void asc_text_draw(AscText const *node) {
-    if (node->color.alpha == 0 || node->hidden) {
-        return;
-    }
-
-    if (node->internal.tex_id == 0) {
-        asc_error("Tried to redraw text node after destruction");
-        return;
-    }
-
-    glUseProgram(ASC_SHADER_FONT.base.id);
-
-    // Upload projection
-    // TODO: when we group UI draw calls, we don't need this
-    glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1,
-                       GL_FALSE, asc_context.active_window->projection);
-
-    // Upload model matrix
-    asc_mat4f model = {0};
-    model[asc_mat4_index(0, 0)] = node->internal.dimension.width;
-    model[asc_mat4_index(1, 1)] = node->internal.dimension.height;
-    model[asc_mat4_index(3, 0)] = node->position.x;
-    model[asc_mat4_index(3, 1)] = node->position.y;
-    model[asc_mat4_index(3, 3)] = 1;
-    glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, GL_FALSE, model);
-
-    // Upload surface
-    glActiveTexture(GL_TEXTURE0);
-    glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
-    glUniform1i(ASC_SHADER_FONT.surface, 0);
-
-    // Draw mesh
-    asc_primitives_draw_plane();
-}
-
 void asc_text_free(AscText *node) {
     asc_dprintf("Release text node texture: %u", node->internal.tex_id);
     glDeleteTextures(1, &node->internal.tex_id);

mercurial