Wed, 14 Aug 2024 21:09:52 +0200
centralize draw functions
/* * 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/ui/text.h" #include "ascension/context.h" #include "ascension/error.h" #include "ascension/shader.h" #include <cx/printf.h> #include <GL/glew.h> static void asc_text_update(AscSceneNode *node) { AscSprite *sprite = (AscSprite*) node; AscText *text = (AscText*) node; // Generate new texture, if required if (asc_texture_uninitialized(&sprite->tex)) { asc_texture_init_rectangle(&sprite->tex); } // Render text onto a surface TTF_Font *font = asc_font_load(text->font); static int alignments[] = { TTF_WRAPPED_ALIGN_LEFT, TTF_WRAPPED_ALIGN_CENTER, TTF_WRAPPED_ALIGN_RIGHT }; TTF_SetFontWrappedAlign( font, alignments[text->base.data.flags & ASC_TEXT_ALIGNMENT_MASK]); SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped( font, text->text.ptr, asc_col_sdl(text->color), text->max_width ); if (surface == NULL) { asc_error(SDL_GetError()); return; } asc_set_scale2d(node, surface->w, surface->h); if (asc_test_flag(text->base.data.flags, ASC_TEXT_CENTERED_FLAG)) { unsigned short newoffx = surface->w / 2; asc_vec2i pos = asc_get_position2d(node); asc_set_position2d(node, pos.x + text->offx - newoffx, pos.y); text->offx = newoffx; } // Transfer Image Data asc_texture_from_surface(&sprite->tex, surface); // Free the surface SDL_FreeSurface(surface); } static void asc_text_free(AscSceneNode *node) { AscText *text = (AscText*) node; AscSprite *sprite = (AscSprite*) node; asc_texture_destroy(&sprite->tex); cx_strfree(&text->text); free(node); } AscSceneNode *asc_text_create(struct asc_text_create_args args) { AscText *text = calloc(1, sizeof(AscText)); AscSceneNode *node = (AscSceneNode*) text; node->render_group = ASC_RENDER_GROUP_SPRITE_BLEND; node->free_func = asc_text_free; node->update_func = asc_text_update; node->flags = args.alignment; node->position.x = (float) args.x; node->position.y = (float) args.y; text->max_width = args.max_width; text->font = asc_active_font; text->color = asc_context.ink; if (args.text == NULL) { text->text = cx_mutstr(strdup(" ")); } else { text->text = cx_mutstr(strdup(args.text)); } // initialize asc_text_update(node); return node; } void asc_text_printf( AscSceneNode *node, char const* format, ... ) { cxmutstr text = ((AscText*)node)->text; va_list ap; va_start(ap, format); cx_vsprintf(&text.ptr, &text.length, format, ap); va_end(ap); asc_node_update(node); }