src/text.c

Thu, 15 Aug 2024 17:33:42 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 15 Aug 2024 17:33:42 +0200
changeset 72
84472fb3adbd
parent 71
baa73a0be3ce
child 74
25fb39f8863a
permissions
-rw-r--r--

define macro for predefined shader

3
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 * Copyright 2023 Mike Becker. All rights reserved.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 *
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * modification, are permitted provided that the following conditions are met:
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 *
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 *
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 * 2. Redistributions in binary form must reproduce the above copyright
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer in the
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * documentation and/or other materials provided with the distribution.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 *
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * POSSIBILITY OF SUCH DAMAGE.
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 */
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27
48
6e5b5ba2752c create new UI subdir
Mike Becker <universe@uap-core.de>
parents: 45
diff changeset
28 #include "ascension/ui/text.h"
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
29 #include "ascension/context.h"
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
30 #include "ascension/error.h"
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
31 #include "ascension/shader.h"
11
d83af80eb09b add font management
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
32
58
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
33 #include <cx/printf.h>
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
34
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
35 #include <GL/glew.h>
3
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
37 static void asc_text_update(AscSceneNode *node) {
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
38 AscSprite *sprite = (AscSprite*) node;
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
39 AscText *text = (AscText*) node;
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
40
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
41 // Generate new texture, if required
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
42 if (asc_texture_uninitialized(&sprite->tex)) {
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
43 asc_texture_init_rectangle(&sprite->tex);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
44 }
4
b7acda6a4476 add simple color data types
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
45
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
46 // Render text onto a surface
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
47 TTF_Font *font = asc_font_load(text->font);
58
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
48 static int alignments[] = {
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
49 TTF_WRAPPED_ALIGN_LEFT,
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
50 TTF_WRAPPED_ALIGN_CENTER,
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
51 TTF_WRAPPED_ALIGN_RIGHT
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
52 };
57
7daec2f067a9 implement centered wrapped text
Mike Becker <universe@uap-core.de>
parents: 54
diff changeset
53 TTF_SetFontWrappedAlign(
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
54 font, alignments[text->base.data.flags & ASC_TEXT_ALIGNMENT_MASK]);
18
00c0632f0f40 fix missing UTF8 support for text rendering
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
55 SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
56 font, text->text.ptr, asc_col_sdl(text->color), text->max_width
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
57 );
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
58 if (surface == NULL) {
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
59 asc_error(SDL_GetError());
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
60 return;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
61 }
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
62 asc_set_scale2d(node, surface->w, surface->h);
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
63 if (asc_test_flag(text->base.data.flags, ASC_TEXT_CENTERED_FLAG)) {
58
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
64 unsigned short newoffx = surface->w / 2;
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
65 asc_vec2i pos = asc_get_position2d(node);
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
66 asc_set_position2d(node, pos.x + text->offx - newoffx, pos.y);
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
67 text->offx = newoffx;
58
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
68 }
4
b7acda6a4476 add simple color data types
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
69
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
70 // Transfer Image Data
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
71 asc_texture_from_surface(&sprite->tex, surface);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
72
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
73 // Free the surface
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
74 SDL_FreeSurface(surface);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
75 }
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 14
diff changeset
76
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
77 static void asc_text_free(AscSceneNode *node) {
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
78 AscText *text = (AscText*) node;
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
79 AscSprite *sprite = (AscSprite*) node;
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
80 asc_texture_destroy(&sprite->tex);
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
81 cx_strfree(&text->text);
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
82 free(node);
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
83 }
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
84
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
85 AscSceneNode *asc_text_create(struct asc_text_create_args args) {
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
86 AscText *text = calloc(1, sizeof(AscText));
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
87 AscSceneNode *node = (AscSceneNode*) text;
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
88
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
89 node->render_group = ASC_RENDER_GROUP_SPRITE_BLEND;
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
90 node->free_func = asc_text_free;
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
91 node->update_func = asc_text_update;
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
92
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
93 node->flags = args.alignment;
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
94 node->position.x = (float) args.x;
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
95 node->position.y = (float) args.y;
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
96 text->max_width = args.max_width;
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
97 text->font = asc_active_font;
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
98 text->color = asc_context.ink;
59
764fbb013252 use fancy named parameters for asc_text()
Mike Becker <universe@uap-core.de>
parents: 58
diff changeset
99 if (args.text == NULL) {
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
100 text->text = cx_mutstr(strdup(" "));
58
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
101 } else {
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
102 text->text = cx_mutstr(strdup(args.text));
32
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
103 }
86468a71dd73 add transformation matrix
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
104
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
105 // initialize
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
106 asc_text_update(node);
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
107
71
baa73a0be3ce centralize draw functions
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
108 return node;
58
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
109 }
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
110
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
111 void asc_text_printf(
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
112 AscSceneNode *node,
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
113 char const* format,
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
114 ...
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
115 ) {
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
116 cxmutstr text = ((AscText*)node)->text;
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
117 va_list ap;
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
118 va_start(ap, format);
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
119 cx_vsprintf(&text.ptr, &text.length, format, ap);
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
120 va_end(ap);
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
121 asc_node_update(node);
26ebb2f1e6e6 improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents: 57
diff changeset
122 }

mercurial