src/text.c

Wed, 06 Mar 2024 23:38:17 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 06 Mar 2024 23:38:17 +0100
changeset 36
e26b4ac1661c
parent 33
e7ddb52facd3
child 37
8a8cc6725b48
permissions
-rw-r--r--

invert the logic of converting between specialized nodes and the generic interface

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  * Copyright 2023 Mike Becker. All rights reserved.
     4  *
     5  * Redistribution and use in source and binary forms, with or without
     6  * modification, are permitted provided that the following conditions are met:
     7  *
     8  *   1. Redistributions of source code must retain the above copyright
     9  *      notice, this list of conditions and the following disclaimer.
    10  *
    11  *   2. Redistributions in binary form must reproduce the above copyright
    12  *      notice, this list of conditions and the following disclaimer in the
    13  *      documentation and/or other materials provided with the distribution.
    14  *
    15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    25  * POSSIBILITY OF SUCH DAMAGE.
    26  */
    28 #include "ascension/text.h"
    29 #include "ascension/context.h"
    30 #include "ascension/error.h"
    31 #include "ascension/shader.h"
    33 #include <GL/glew.h>
    35 static void asc_text_draw(AscText const *node) {
    36     if (node->color.alpha == 0 || node->hidden || node->internal.tex_id == 0) {
    37         return;
    38     }
    40     // TODO: when we group draw calls, we don't need to activate shader here
    41     glUseProgram(ASC_SHADER_FONT.base.id);
    43     // TODO: when we group UI draw calls, we don't need to upload matrices here
    44     // Upload matrices
    45     glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1,
    46                        GL_FALSE, asc_context.active_window->projection);
    47     glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1,
    48                        GL_FALSE, node->base.transform);
    50     // Upload surface
    51     glActiveTexture(GL_TEXTURE0);
    52     glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
    53     glUniform1i(ASC_SHADER_FONT.surface, 0);
    55     // Draw mesh
    56     asc_primitives_draw_plane();
    57 }
    59 static void asc_text_update(AscText *node) {
    60     // short circuit if fully transparent or hidden, we don't need anything
    61     if (node->color.alpha == 0 || node->hidden) {
    62         return;
    63     }
    65     // Generate new texture, if required
    66     if (node->internal.tex_id == 0) {
    67         glGenTextures(1, &node->internal.tex_id);
    68         glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
    69         glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    70         glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    71         asc_dprintf("Generated new texture for text node: %u", node->internal.tex_id);
    72     }
    74     // Render text onto a surface
    75     SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(
    76             asc_font_cache_validate(node->font)->ptr,
    77             node->text,
    78             asc_col_sdl(node->color),
    79             node->max_width
    80     );
    81     if (surface == NULL) {
    82         asc_error(SDL_GetError());
    83         return;
    84     }
    86     // Transform
    87     asc_transform_scale(node->base.transform, (float) surface->w, (float) surface->h, 0);
    88     asc_transform_translate2i(node->base.transform, node->position);
    90     // Transfer Image Data
    91     // TODO: move the image data transfer to a separate function - we will need it more often
    92     glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
    93     glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format->BytesPerPixel);
    94     glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA,
    95                  surface->w, surface->h,
    96                  0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);
    98     // Free the surface
    99     SDL_FreeSurface(surface);
   100 }
   102 AscSceneNode *asc_text(int x, int y, char const *text) {
   103     AscText *node = calloc(1, sizeof(AscText));
   104     if (node == NULL) {
   105         asc_error("Out of memory.");
   106         return NULL;
   107     }
   109     node->base.free_func = (asc_scene_free_func) asc_text_free;
   110     node->base.update_func = (asc_scene_update_func) asc_text_update;
   111     node->base.draw_func = (asc_scene_draw_func) asc_text_draw;
   113     node->position.x = x;
   114     node->position.y = y;
   115     node->font = asc_context.active_font;
   116     node->color = asc_context.ink;
   117     if (text != NULL) {
   118         node->text = strdup(text);
   119     }
   121     return &node->base;
   122 }
   124 void asc_text_free(AscText *node) {
   125     asc_dprintf("Release text node texture: %u", node->internal.tex_id);
   126     glDeleteTextures(1, &node->internal.tex_id);
   127     free(node->text);
   128     free(node);
   129 }

mercurial