1.1 --- a/src/text.c Sun Jan 21 14:01:27 2024 +0100 1.2 +++ b/src/text.c Tue Jan 23 21:34:12 2024 +0100 1.3 @@ -32,6 +32,36 @@ 1.4 1.5 #include <GL/glew.h> 1.6 1.7 +static void asc_text_draw(AscText const *node) { 1.8 + if (node->color.alpha == 0 || node->hidden || node->internal.tex_id == 0) { 1.9 + return; 1.10 + } 1.11 + 1.12 + glUseProgram(ASC_SHADER_FONT.base.id); 1.13 + 1.14 + // Upload projection 1.15 + // TODO: when we group UI draw calls, we don't need this 1.16 + glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1, 1.17 + GL_FALSE, asc_context.active_window->projection); 1.18 + 1.19 + // Upload model matrix 1.20 + asc_mat4f model = {0}; 1.21 + model[asc_mat4_index(0, 0)] = node->internal.dimension.width; 1.22 + model[asc_mat4_index(1, 1)] = node->internal.dimension.height; 1.23 + model[asc_mat4_index(3, 0)] = node->position.x; 1.24 + model[asc_mat4_index(3, 1)] = node->position.y; 1.25 + model[asc_mat4_index(3, 3)] = 1; 1.26 + glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, GL_FALSE, model); 1.27 + 1.28 + // Upload surface 1.29 + glActiveTexture(GL_TEXTURE0); 1.30 + glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id); 1.31 + glUniform1i(ASC_SHADER_FONT.surface, 0); 1.32 + 1.33 + // Draw mesh 1.34 + asc_primitives_draw_plane(); 1.35 +} 1.36 + 1.37 AscText *asc_text(int x, int y, char const *text) { 1.38 AscText *node = calloc(1, sizeof(AscText)); 1.39 if (node == NULL) { 1.40 @@ -39,6 +69,9 @@ 1.41 return NULL; 1.42 } 1.43 1.44 + node->node.free_func = (asc_scene_free_func) asc_text_free; 1.45 + node->node.draw_func = (asc_scene_draw_func) asc_text_draw; 1.46 + 1.47 node->position = (asc_vec2i) {x, y}; 1.48 node->font = asc_context.active_font; 1.49 node->color = asc_context.ink; 1.50 @@ -93,41 +126,6 @@ 1.51 SDL_FreeSurface(surface); 1.52 } 1.53 1.54 -void asc_text_draw(AscText const *node) { 1.55 - if (node->color.alpha == 0 || node->hidden) { 1.56 - return; 1.57 - } 1.58 - 1.59 - if (node->internal.tex_id == 0) { 1.60 - asc_error("Tried to redraw text node after destruction"); 1.61 - return; 1.62 - } 1.63 - 1.64 - glUseProgram(ASC_SHADER_FONT.base.id); 1.65 - 1.66 - // Upload projection 1.67 - // TODO: when we group UI draw calls, we don't need this 1.68 - glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1, 1.69 - GL_FALSE, asc_context.active_window->projection); 1.70 - 1.71 - // Upload model matrix 1.72 - asc_mat4f model = {0}; 1.73 - model[asc_mat4_index(0, 0)] = node->internal.dimension.width; 1.74 - model[asc_mat4_index(1, 1)] = node->internal.dimension.height; 1.75 - model[asc_mat4_index(3, 0)] = node->position.x; 1.76 - model[asc_mat4_index(3, 1)] = node->position.y; 1.77 - model[asc_mat4_index(3, 3)] = 1; 1.78 - glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, GL_FALSE, model); 1.79 - 1.80 - // Upload surface 1.81 - glActiveTexture(GL_TEXTURE0); 1.82 - glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id); 1.83 - glUniform1i(ASC_SHADER_FONT.surface, 0); 1.84 - 1.85 - // Draw mesh 1.86 - asc_primitives_draw_plane(); 1.87 -} 1.88 - 1.89 void asc_text_free(AscText *node) { 1.90 asc_dprintf("Release text node texture: %u", node->internal.tex_id); 1.91 glDeleteTextures(1, &node->internal.tex_id);