Wed, 10 Apr 2024 19:43:34 +0200
implement centered wrapped text
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@48 | 28 | #include "ascension/ui/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@44 | 36 | // Obtain shader |
universe@44 | 37 | AscShaderSprite *shader = &asc_context.active_window->glctx.shader.sprite; |
universe@44 | 38 | |
universe@37 | 39 | // Upload model matrix |
universe@44 | 40 | glUniformMatrix4fv(shader->base.model, 1, |
universe@45 | 41 | GL_FALSE, node->base.world_transform); |
universe@29 | 42 | |
universe@50 | 43 | // Bind texture |
universe@50 | 44 | asc_texture_bind(&node->tex, shader->tex, 0); |
universe@29 | 45 | |
universe@41 | 46 | // Apply depth |
universe@44 | 47 | glUniform1f(shader->depth, (float)(node->base.depth)); |
universe@41 | 48 | |
universe@29 | 49 | // Draw mesh |
universe@29 | 50 | asc_primitives_draw_plane(); |
universe@29 | 51 | } |
universe@29 | 52 | |
universe@32 | 53 | static void asc_text_update(AscText *node) { |
universe@16 | 54 | // Generate new texture, if required |
universe@50 | 55 | if (asc_texture_uninitialized(&node->tex)) { |
universe@50 | 56 | asc_texture_init_rectangle(&node->tex); |
universe@16 | 57 | } |
universe@4 | 58 | |
universe@16 | 59 | // Render text onto a surface |
universe@57 | 60 | TTF_Font *font = asc_font_cache_validate(node->font)->ptr; |
universe@57 | 61 | TTF_SetFontWrappedAlign( |
universe@57 | 62 | font, node->centered ? |
universe@57 | 63 | TTF_WRAPPED_ALIGN_CENTER : TTF_WRAPPED_ALIGN_LEFT |
universe@57 | 64 | ); |
universe@18 | 65 | SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped( |
universe@57 | 66 | font, node->text, asc_col_sdl(node->color), node->max_width |
universe@16 | 67 | ); |
universe@16 | 68 | if (surface == NULL) { |
universe@16 | 69 | asc_error(SDL_GetError()); |
universe@16 | 70 | return; |
universe@16 | 71 | } |
universe@52 | 72 | asc_set_scale2d(&node->base, surface->w, surface->h); |
universe@45 | 73 | asc_update_transform((AscSceneNode *) node); |
universe@11 | 74 | |
universe@16 | 75 | // Transfer Image Data |
universe@50 | 76 | asc_texture_from_surface(&node->tex, surface); |
universe@11 | 77 | |
universe@16 | 78 | // Free the surface |
universe@16 | 79 | SDL_FreeSurface(surface); |
universe@16 | 80 | } |
universe@16 | 81 | |
universe@36 | 82 | AscSceneNode *asc_text(int x, int y, char const *text) { |
universe@32 | 83 | AscText *node = calloc(1, sizeof(AscText)); |
universe@32 | 84 | |
universe@41 | 85 | node->base.render_group = ASC_RENDER_GROUP_SPRITE_BLEND; |
universe@32 | 86 | node->base.free_func = (asc_scene_free_func) asc_text_free; |
universe@32 | 87 | node->base.update_func = (asc_scene_update_func) asc_text_update; |
universe@32 | 88 | node->base.draw_func = (asc_scene_draw_func) asc_text_draw; |
universe@32 | 89 | |
universe@45 | 90 | node->base.position.x = (float) x; |
universe@45 | 91 | node->base.position.y = (float) y; |
universe@32 | 92 | node->font = asc_context.active_font; |
universe@32 | 93 | node->color = asc_context.ink; |
universe@32 | 94 | if (text != NULL) { |
universe@32 | 95 | node->text = strdup(text); |
universe@32 | 96 | } |
universe@32 | 97 | |
universe@37 | 98 | // initialize |
universe@37 | 99 | asc_text_update(node); |
universe@37 | 100 | |
universe@36 | 101 | return &node->base; |
universe@32 | 102 | } |
universe@32 | 103 | |
universe@25 | 104 | void asc_text_free(AscText *node) { |
universe@50 | 105 | asc_texture_destroy(&node->tex); |
universe@23 | 106 | free(node->text); |
universe@19 | 107 | free(node); |
universe@16 | 108 | } |