src/text.c

changeset 50
d8d2e4817db1
parent 48
6e5b5ba2752c
child 52
6d100092fcd4
--- a/src/text.c	Mon Apr 01 19:44:00 2024 +0200
+++ b/src/text.c	Tue Apr 09 21:18:52 2024 +0200
@@ -33,7 +33,7 @@
 #include <GL/glew.h>
 
 static void asc_text_draw(AscText const *node) {
-    if (node->color.alpha == 0 || node->hidden || node->tex_id == 0) {
+    if (node->color.alpha == 0 || node->hidden) {
         return;
     }
 
@@ -44,10 +44,8 @@
     glUniformMatrix4fv(shader->base.model, 1,
                        GL_FALSE, node->base.world_transform);
 
-    // Upload surface
-    glActiveTexture(GL_TEXTURE0);
-    glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id);
-    glUniform1i(shader->surface, 0);
+    // Bind texture
+    asc_texture_bind(&node->tex, shader->tex, 0);
 
     // Apply depth
     glUniform1f(shader->depth, (float)(node->base.depth));
@@ -63,12 +61,8 @@
     }
 
     // Generate new texture, if required
-    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->tex_id);
+    if (asc_texture_uninitialized(&node->tex)) {
+        asc_texture_init_rectangle(&node->tex);
     }
 
     // Render text onto a surface
@@ -87,12 +81,7 @@
     asc_update_transform((AscSceneNode *) 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->tex_id);
-    glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format->BytesPerPixel);
-    glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA,
-                 surface->w, surface->h,
-                 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);
+    asc_texture_from_surface(&node->tex, surface);
 
     // Free the surface
     SDL_FreeSurface(surface);
@@ -100,10 +89,6 @@
 
 AscSceneNode *asc_text(int x, int y, char const *text) {
     AscText *node = calloc(1, sizeof(AscText));
-    if (node == NULL) {
-        asc_error("Out of memory.");
-        return NULL;
-    }
 
     node->base.render_group = ASC_RENDER_GROUP_SPRITE_BLEND;
     node->base.free_func = (asc_scene_free_func) asc_text_free;
@@ -125,8 +110,7 @@
 }
 
 void asc_text_free(AscText *node) {
-    asc_dprintf("Release text node texture: %u", node->tex_id);
-    glDeleteTextures(1, &node->tex_id);
+    asc_texture_destroy(&node->tex);
     free(node->text);
     free(node);
 }
\ No newline at end of file

mercurial