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