src/text.c

Mon, 18 Dec 2023 13:04:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 13:04:04 +0100
changeset 20
b101c1ef13c7
parent 19
d0e88022e209
child 23
ab07757004b4
permissions
-rw-r--r--

add pseudo-rule s.t. dry-runs won't fail

     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 AscTextNode *asc_text(int x, int y, char const *text) {
    36     AscTextNode *node = calloc(1, sizeof(AscTextNode));
    37     if (node == NULL) {
    38         asc_error("Out of memory.");
    39         return NULL;
    40     }
    42     node->position = (asc_vec2i) {x, y};
    43     node->font = asc_context.active_font;
    44     node->color = asc_context.ink;
    45     cxBufferInit(&node->text, NULL, strlen(text)+8,
    46                  cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND);
    47     cxBufferPutString(&node->text, text);
    49     return node;
    50 }
    52 void asc_text_update(AscTextNode *node) {
    53     // short circuit if fully transparent or hidden, we don't need anything
    54     if (node->color.alpha == 0 || node->hidden) {
    55         return;
    56     }
    58     // Generate new texture, if required
    59     if (node->internal.tex_id == 0) {
    60         glGenTextures(1, &node->internal.tex_id);
    61         glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
    62         glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    63         glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    64         asc_dprintf("Generated new texture for text node: %u", node->internal.tex_id);
    65     }
    67     // ensure the text is zero-terminated
    68     CxBuffer* text = &(node->text);
    69     cxBufferMinimumCapacity(text, text->size+1);
    70     text->space[text->size] = '\0';
    72     // Render text onto a surface
    73     SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(
    74             asc_font_cache_validate(node->font)->ptr,
    75             text->space,
    76             asc_col_sdl(node->color),
    77             node->max_width
    78     );
    79     if (surface == NULL) {
    80         asc_error(SDL_GetError());
    81         return;
    82     }
    84     // Store basic node information
    85     node->position = node->position;
    86     node->internal.dimension.width = surface->w;
    87     node->internal.dimension.height = surface->h;
    89     // Transfer Image Data
    90     // TODO: move the image data transfer to a separate function - we will need it more often
    91     glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
    92     glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format->BytesPerPixel);
    93     glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA,
    94                  surface->w, surface->h,
    95                  0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels);
    97     // Free the surface
    98     SDL_FreeSurface(surface);
    99 }
   101 void asc_text_draw(AscTextNode *node) {
   102     if (node->color.alpha == 0 || node->hidden) {
   103         return;
   104     }
   106     if (node->internal.tex_id == 0) {
   107         asc_error("Tried to redraw text node after destruction");
   108         return;
   109     }
   111     glUseProgram(ASC_SHADER_FONT.base.id);
   113     // Upload projection
   114     // TODO: when we group UI draw calls, we don't need this
   115     glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1,
   116                        GL_FALSE, asc_context.active_window->projection);
   118     // Upload model matrix
   119     asc_mat4f model = {0};
   120     model[asc_mat4_index(0, 0)] = node->internal.dimension.width;
   121     model[asc_mat4_index(1, 1)] = node->internal.dimension.height;
   122     model[asc_mat4_index(3, 0)] = node->position.x;
   123     model[asc_mat4_index(3, 1)] = node->position.y;
   124     model[asc_mat4_index(3, 3)] = 1;
   125     glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, GL_FALSE, model);
   127     // Upload surface
   128     glActiveTexture(GL_TEXTURE0);
   129     glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id);
   130     glUniform1i(ASC_SHADER_FONT.surface, 0);
   132     // Draw mesh
   133     asc_primitives_draw_plane();
   134 }
   136 void asc_text_free(AscTextNode *node) {
   137     asc_dprintf("Release text node texture: %u", node->internal.tex_id);
   138     glDeleteTextures(1, &node->internal.tex_id);
   139     cxBufferDestroy(&node->text);
   140     free(node);
   141 }

mercurial