Wed, 15 Nov 2023 22:51:40 +0100
add text rendering and demo FPS counter
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@16 | 35 | void asc_text_draw_lb( |
universe@16 | 36 | AscTextNode *node, |
universe@16 | 37 | asc_vec2i position, |
universe@16 | 38 | unsigned max_width, |
universe@16 | 39 | cxmutstr text) { |
universe@4 | 40 | |
universe@16 | 41 | // Generate new texture, if required |
universe@16 | 42 | if (node->tex_id == 0) { |
universe@16 | 43 | glGenTextures(1, &node->tex_id); |
universe@16 | 44 | glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id); |
universe@16 | 45 | glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
universe@16 | 46 | glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
universe@16 | 47 | asc_dprintf("Generated new texture for text node: %u", node->tex_id); |
universe@16 | 48 | } |
universe@4 | 49 | |
universe@16 | 50 | // Render text onto a surface |
universe@16 | 51 | SDL_Surface *surface = TTF_RenderText_Blended_Wrapped( |
universe@16 | 52 | asc_context.active_font->ptr, |
universe@16 | 53 | text.ptr, |
universe@16 | 54 | (SDL_Color) { |
universe@16 | 55 | .r = asc_context.ink.red, |
universe@16 | 56 | .g = asc_context.ink.green, |
universe@16 | 57 | .b = asc_context.ink.blue, |
universe@16 | 58 | .a = asc_context.ink.alpha |
universe@16 | 59 | }, |
universe@16 | 60 | max_width |
universe@16 | 61 | ); |
universe@16 | 62 | if (surface == NULL) { |
universe@16 | 63 | asc_error(SDL_GetError()); |
universe@16 | 64 | return; |
universe@16 | 65 | } |
universe@4 | 66 | |
universe@16 | 67 | // Store basic node information |
universe@16 | 68 | node->position = position; |
universe@16 | 69 | node->dimension.width = surface->w; |
universe@16 | 70 | node->dimension.height = surface->h; |
universe@11 | 71 | |
universe@16 | 72 | // Transfer Image Data |
universe@16 | 73 | // TODO: move the image data transfer to a separate function - we will need it more often |
universe@16 | 74 | glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id); |
universe@16 | 75 | glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format->BytesPerPixel); |
universe@16 | 76 | glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, |
universe@16 | 77 | surface->w, surface->h, |
universe@16 | 78 | 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels); |
universe@11 | 79 | |
universe@16 | 80 | // Free the surface |
universe@16 | 81 | SDL_FreeSurface(surface); |
universe@3 | 82 | |
universe@16 | 83 | // Redraw the node |
universe@16 | 84 | asc_text_redraw(node); |
universe@16 | 85 | } |
universe@16 | 86 | |
universe@16 | 87 | void asc_text_draw( |
universe@16 | 88 | AscTextNode *node, |
universe@16 | 89 | asc_vec2i position, |
universe@16 | 90 | cxmutstr text) { |
universe@16 | 91 | unsigned max_width = asc_context.active_window->dimensions.width - position.width; |
universe@16 | 92 | asc_text_draw_lb(node, position, max_width, text); |
universe@16 | 93 | } |
universe@16 | 94 | |
universe@16 | 95 | void asc_text_redraw(AscTextNode *node) { |
universe@16 | 96 | if (node->tex_id == 0) { |
universe@16 | 97 | asc_error("Tried to redraw text node after destruction"); |
universe@16 | 98 | return; |
universe@16 | 99 | } |
universe@16 | 100 | |
universe@16 | 101 | glUseProgram(ASC_SHADER_FONT.base.id); |
universe@16 | 102 | |
universe@16 | 103 | // Upload projection |
universe@16 | 104 | // TODO: when we group UI draw calls, we don't need this |
universe@16 | 105 | glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1, GL_FALSE, asc_context.active_window->projection); |
universe@16 | 106 | |
universe@16 | 107 | // Upload model matrix |
universe@16 | 108 | asc_mat4f model = {0}; |
universe@16 | 109 | model[asc_mat4_index(0, 0)] = node->dimension.width; |
universe@16 | 110 | model[asc_mat4_index(1, 1)] = node->dimension.height; |
universe@16 | 111 | model[asc_mat4_index(3, 0)] = node->position.x; |
universe@16 | 112 | model[asc_mat4_index(3, 1)] = node->position.y; |
universe@16 | 113 | model[asc_mat4_index(3, 3)] = 1; |
universe@16 | 114 | glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, GL_FALSE, model); |
universe@16 | 115 | |
universe@16 | 116 | // Upload surface |
universe@16 | 117 | glActiveTexture(GL_TEXTURE0); |
universe@16 | 118 | glBindTexture(GL_TEXTURE_RECTANGLE, node->tex_id); |
universe@16 | 119 | glUniform1i(ASC_SHADER_FONT.surface, 0); |
universe@16 | 120 | |
universe@16 | 121 | // Draw mesh |
universe@16 | 122 | asc_primitives_draw_plane(); |
universe@16 | 123 | } |
universe@16 | 124 | |
universe@16 | 125 | void asc_text_destroy(AscTextNode *node) { |
universe@16 | 126 | asc_dprintf("Release text node texture: %u", node->tex_id); |
universe@16 | 127 | glDeleteTextures(1, &node->tex_id); |
universe@16 | 128 | node->tex_id = 0; |
universe@16 | 129 | } |