1.1 --- a/src/text.c Mon Apr 01 19:44:00 2024 +0200 1.2 +++ b/src/text.c Tue Apr 09 21:18:52 2024 +0200 1.3 @@ -33,7 +33,7 @@ 1.4 #include <GL/glew.h> 1.5 1.6 static void asc_text_draw(AscText const *node) { 1.7 - if (node->color.alpha == 0 || node->hidden || node->tex_id == 0) { 1.8 + if (node->color.alpha == 0 || node->hidden) { 1.9 return; 1.10 } 1.11 1.12 @@ -44,10 +44,8 @@ 1.13 glUniformMatrix4fv(shader->base.model, 1, 1.14 GL_FALSE, node->base.world_transform); 1.15 1.16 - // Upload surface 1.17 - glActiveTexture(GL_TEXTURE0); 1.18 - glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id); 1.19 - glUniform1i(shader->surface, 0); 1.20 + // Bind texture 1.21 + asc_texture_bind(&node->tex, shader->tex, 0); 1.22 1.23 // Apply depth 1.24 glUniform1f(shader->depth, (float)(node->base.depth)); 1.25 @@ -63,12 +61,8 @@ 1.26 } 1.27 1.28 // Generate new texture, if required 1.29 - if (node->tex_id == 0) { 1.30 - glGenTextures(1, &node->tex_id); 1.31 - glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id); 1.32 - glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1.33 - glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.34 - asc_dprintf("Generated new texture for text node: %u", node->tex_id); 1.35 + if (asc_texture_uninitialized(&node->tex)) { 1.36 + asc_texture_init_rectangle(&node->tex); 1.37 } 1.38 1.39 // Render text onto a surface 1.40 @@ -87,12 +81,7 @@ 1.41 asc_update_transform((AscSceneNode *) node); 1.42 1.43 // Transfer Image Data 1.44 - // TODO: move the image data transfer to a separate function - we will need it more often 1.45 - glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id); 1.46 - glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format->BytesPerPixel); 1.47 - glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, 1.48 - surface->w, surface->h, 1.49 - 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels); 1.50 + asc_texture_from_surface(&node->tex, surface); 1.51 1.52 // Free the surface 1.53 SDL_FreeSurface(surface); 1.54 @@ -100,10 +89,6 @@ 1.55 1.56 AscSceneNode *asc_text(int x, int y, char const *text) { 1.57 AscText *node = calloc(1, sizeof(AscText)); 1.58 - if (node == NULL) { 1.59 - asc_error("Out of memory."); 1.60 - return NULL; 1.61 - } 1.62 1.63 node->base.render_group = ASC_RENDER_GROUP_SPRITE_BLEND; 1.64 node->base.free_func = (asc_scene_free_func) asc_text_free; 1.65 @@ -125,8 +110,7 @@ 1.66 } 1.67 1.68 void asc_text_free(AscText *node) { 1.69 - asc_dprintf("Release text node texture: %u", node->tex_id); 1.70 - glDeleteTextures(1, &node->tex_id); 1.71 + asc_texture_destroy(&node->tex); 1.72 free(node->text); 1.73 free(node); 1.74 } 1.75 \ No newline at end of file