src/text.c

Wed, 15 Nov 2023 22:51:40 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 15 Nov 2023 22:51:40 +0100
changeset 16
c5dde81b6fb2
parent 14
src/ascension/font.h@9dbfc0031887
child 17
25013a35e07d
permissions
-rw-r--r--

add text rendering and demo FPS counter

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright 2023 Mike Becker. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "ascension/text.h"
#include "ascension/context.h"
#include "ascension/error.h"
#include "ascension/shader.h"

#include <GL/glew.h>

void asc_text_draw_lb(
        AscTextNode *node,
        asc_vec2i position,
        unsigned max_width,
        cxmutstr text) {

    // 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);
    }

    // Render text onto a surface
    SDL_Surface *surface = TTF_RenderText_Blended_Wrapped(
            asc_context.active_font->ptr,
            text.ptr,
            (SDL_Color) {
                    .r = asc_context.ink.red,
                    .g = asc_context.ink.green,
                    .b = asc_context.ink.blue,
                    .a = asc_context.ink.alpha
            },
            max_width
    );
    if (surface == NULL) {
        asc_error(SDL_GetError());
        return;
    }

    // Store basic node information
    node->position = position;
    node->dimension.width = surface->w;
    node->dimension.height = surface->h;

    // 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);

    // Free the surface
    SDL_FreeSurface(surface);

    // Redraw the node
    asc_text_redraw(node);
}

void asc_text_draw(
        AscTextNode *node,
        asc_vec2i position,
        cxmutstr text) {
    unsigned max_width = asc_context.active_window->dimensions.width - position.width;
    asc_text_draw_lb(node, position, max_width, text);
}

void asc_text_redraw(AscTextNode *node) {
    if (node->tex_id == 0) {
        asc_error("Tried to redraw text node after destruction");
        return;
    }

    glUseProgram(ASC_SHADER_FONT.base.id);

    // Upload projection
    // TODO: when we group UI draw calls, we don't need this
    glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1, GL_FALSE, asc_context.active_window->projection);

    // Upload model matrix
    asc_mat4f model = {0};
    model[asc_mat4_index(0, 0)] = node->dimension.width;
    model[asc_mat4_index(1, 1)] = node->dimension.height;
    model[asc_mat4_index(3, 0)] = node->position.x;
    model[asc_mat4_index(3, 1)] = node->position.y;
    model[asc_mat4_index(3, 3)] = 1;
    glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, GL_FALSE, model);

    // Upload surface
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id);
    glUniform1i(ASC_SHADER_FONT.surface, 0);

    // Draw mesh
    asc_primitives_draw_plane();
}

void asc_text_destroy(AscTextNode *node) {
    asc_dprintf("Release text node texture: %u", node->tex_id);
    glDeleteTextures(1, &node->tex_id);
    node->tex_id = 0;
}

mercurial