universe@3: /* universe@3: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@3: * Copyright 2023 Mike Becker. All rights reserved. universe@3: * universe@3: * Redistribution and use in source and binary forms, with or without universe@3: * modification, are permitted provided that the following conditions are met: universe@3: * universe@3: * 1. Redistributions of source code must retain the above copyright universe@3: * notice, this list of conditions and the following disclaimer. universe@3: * universe@3: * 2. Redistributions in binary form must reproduce the above copyright universe@3: * notice, this list of conditions and the following disclaimer in the universe@3: * documentation and/or other materials provided with the distribution. universe@3: * universe@3: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@3: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@3: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@3: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@3: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@3: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@3: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@3: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@3: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@3: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@3: * POSSIBILITY OF SUCH DAMAGE. universe@3: */ universe@3: universe@16: #include "ascension/text.h" universe@16: #include "ascension/context.h" universe@16: #include "ascension/error.h" universe@16: #include "ascension/shader.h" universe@3: universe@16: #include universe@11: universe@29: static void asc_text_draw(AscText const *node) { universe@29: if (node->color.alpha == 0 || node->hidden || node->internal.tex_id == 0) { universe@29: return; universe@29: } universe@29: universe@32: // TODO: when we group draw calls, we don't need to activate shader here universe@29: glUseProgram(ASC_SHADER_FONT.base.id); universe@29: universe@32: // TODO: when we group UI draw calls, we don't need to upload matrices here universe@32: // Upload matrices universe@29: glUniformMatrix4fv(ASC_SHADER_FONT.base.projection, 1, universe@29: GL_FALSE, asc_context.active_window->projection); universe@32: glUniformMatrix4fv(ASC_SHADER_FONT.base.model, 1, universe@32: GL_FALSE, node->base.transform); universe@29: universe@29: // Upload surface universe@29: glActiveTexture(GL_TEXTURE0); universe@29: glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id); universe@29: glUniform1i(ASC_SHADER_FONT.surface, 0); universe@29: universe@29: // Draw mesh universe@29: asc_primitives_draw_plane(); universe@29: } universe@29: universe@32: static void asc_text_update(AscText *node) { universe@19: // short circuit if fully transparent or hidden, we don't need anything universe@19: if (node->color.alpha == 0 || node->hidden) { universe@17: return; universe@17: } universe@17: universe@16: // Generate new texture, if required universe@19: if (node->internal.tex_id == 0) { universe@19: glGenTextures(1, &node->internal.tex_id); universe@19: glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id); universe@16: glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR); universe@16: glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR); universe@19: asc_dprintf("Generated new texture for text node: %u", node->internal.tex_id); universe@16: } universe@4: universe@16: // Render text onto a surface universe@18: SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped( universe@19: asc_font_cache_validate(node->font)->ptr, universe@23: node->text, universe@19: asc_col_sdl(node->color), universe@19: node->max_width universe@16: ); universe@16: if (surface == NULL) { universe@16: asc_error(SDL_GetError()); universe@16: return; universe@16: } universe@4: universe@32: // Transform universe@32: asc_transform_scale(node->base.transform, (float) surface->w, (float) surface->h, 0); universe@32: asc_transform_translate2i(node->base.transform, node->position); universe@11: universe@16: // Transfer Image Data universe@16: // TODO: move the image data transfer to a separate function - we will need it more often universe@19: glBindTexture(GL_TEXTURE_RECTANGLE, node->internal.tex_id); universe@16: glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch / surface->format->BytesPerPixel); universe@16: glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, universe@16: surface->w, surface->h, universe@16: 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels); universe@11: universe@16: // Free the surface universe@16: SDL_FreeSurface(surface); universe@16: } universe@16: universe@36: AscSceneNode *asc_text(int x, int y, char const *text) { universe@32: AscText *node = calloc(1, sizeof(AscText)); universe@32: if (node == NULL) { universe@32: asc_error("Out of memory."); universe@32: return NULL; universe@32: } universe@32: universe@32: node->base.free_func = (asc_scene_free_func) asc_text_free; universe@32: node->base.update_func = (asc_scene_update_func) asc_text_update; universe@32: node->base.draw_func = (asc_scene_draw_func) asc_text_draw; universe@32: universe@32: node->position.x = x; universe@32: node->position.y = y; universe@32: node->font = asc_context.active_font; universe@32: node->color = asc_context.ink; universe@32: if (text != NULL) { universe@32: node->text = strdup(text); universe@32: } universe@32: universe@36: return &node->base; universe@32: } universe@32: universe@25: void asc_text_free(AscText *node) { universe@19: asc_dprintf("Release text node texture: %u", node->internal.tex_id); universe@19: glDeleteTextures(1, &node->internal.tex_id); universe@23: free(node->text); universe@19: free(node); universe@16: }