--- a/src/text.c Wed Mar 06 23:38:17 2024 +0100 +++ b/src/text.c Fri Mar 15 00:06:59 2024 +0100 @@ -33,29 +33,28 @@ #include <GL/glew.h> static void asc_text_draw(AscText const *node) { - if (node->color.alpha == 0 || node->hidden || node->internal.tex_id == 0) { + if (node->color.alpha == 0 || node->hidden || node->tex_id == 0) { return; } - // TODO: when we group draw calls, we don't need to activate shader here - glUseProgram(ASC_SHADER_FONT.base.id); - - // TODO: when we group UI draw calls, we don't need to upload matrices here - // Upload matrices - glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1, - GL_FALSE, asc_context.active_window->projection); + // Upload model matrix glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, GL_FALSE, node->base.transform); // Upload surface glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id); + glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id); glUniform1i(ASC_SHADER_FONT.surface, 0); // Draw mesh asc_primitives_draw_plane(); } +static void asc_text_update_transform(AscText *node) { + asc_transform_scale(node->base.transform, (float) node->dimension.width, (float) node->dimension.height, 0); + asc_transform_translate2i(node->base.transform, node->position); +} + static void asc_text_update(AscText *node) { // short circuit if fully transparent or hidden, we don't need anything if (node->color.alpha == 0 || node->hidden) { @@ -63,12 +62,12 @@ } // Generate new texture, if required - if (node->internal.tex_id == 0) { - glGenTextures(1, &node->internal.tex_id); - glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id); + if (node->tex_id == 0) { + glGenTextures(1, &node->tex_id); + glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - asc_dprintf("Generated new texture for text node: %u", node->internal.tex_id); + asc_dprintf("Generated new texture for text node: %u", node->tex_id); } // Render text onto a surface @@ -82,14 +81,13 @@ asc_error(SDL_GetError()); return; } - - // Transform - asc_transform_scale(node->base.transform, (float) surface->w, (float) surface->h, 0); - asc_transform_translate2i(node->base.transform, node->position); + node->dimension.width = surface->w; + node->dimension.height = surface->h; + asc_node_update_transform(node); // Transfer Image Data // TODO: move the image data transfer to a separate function - we will need it more often - glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id); + glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id); glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format->BytesPerPixel); glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, surface->w, surface->h, @@ -106,8 +104,10 @@ return NULL; } + node->base.render_group = ASC_RENDER_GROUP_FONTS; node->base.free_func = (asc_scene_free_func) asc_text_free; node->base.update_func = (asc_scene_update_func) asc_text_update; + node->base.transform_update_func = (asc_scene_update_func) asc_text_update_transform; node->base.draw_func = (asc_scene_draw_func) asc_text_draw; node->position.x = x; @@ -118,12 +118,16 @@ node->text = strdup(text); } + // initialize + asc_text_update(node); + asc_text_update_transform(node); + return &node->base; } void asc_text_free(AscText *node) { - asc_dprintf("Release text node texture: %u", node->internal.tex_id); - glDeleteTextures(1, &node->internal.tex_id); + asc_dprintf("Release text node texture: %u", node->tex_id); + glDeleteTextures(1, &node->tex_id); free(node->text); free(node); } \ No newline at end of file