Sun, 11 Aug 2024 15:43:01 +0200
update to recent snapshot of UCX 3.1
3 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * Copyright 2023 Mike Becker. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * | |
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
25 | * POSSIBILITY OF SUCH DAMAGE. | |
26 | */ | |
27 | ||
48 | 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 | 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 | 36 | |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
37 | static void asc_text_draw(AscText const *node) { |
44
b3da4096c607
create own compilation unit for GL context - fixes shader not being created per context
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
38 | // Obtain shader |
65
9c44c55d327a
consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents:
64
diff
changeset
|
39 | AscShaderSprite *shader = &asc_active_window->glctx.shader.sprite; |
44
b3da4096c607
create own compilation unit for GL context - fixes shader not being created per context
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
40 | |
37
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
41 | // Upload model matrix |
44
b3da4096c607
create own compilation unit for GL context - fixes shader not being created per context
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
42 | glUniformMatrix4fv(shader->base.model, 1, |
45
18de2af03531
simplify how transforms work
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
43 | GL_FALSE, node->base.world_transform); |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
44 | |
50 | 45 | // Bind texture |
46 | asc_texture_bind(&node->tex, shader->tex, 0); | |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
47 | |
41
df81d493716e
add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
48 | // Apply depth |
44
b3da4096c607
create own compilation unit for GL context - fixes shader not being created per context
Mike Becker <universe@uap-core.de>
parents:
41
diff
changeset
|
49 | glUniform1f(shader->depth, (float)(node->base.depth)); |
41
df81d493716e
add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
50 | |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
51 | // Draw mesh |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
52 | asc_primitives_draw_plane(); |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
53 | } |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
54 | |
32
86468a71dd73
add transformation matrix
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
55 | static void asc_text_update(AscText *node) { |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
56 | // Generate new texture, if required |
50 | 57 | if (asc_texture_uninitialized(&node->tex)) { |
58 | asc_texture_init_rectangle(&node->tex); | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
59 | } |
4
b7acda6a4476
add simple color data types
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
60 | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
61 | // Render text onto a surface |
66
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
62 | TTF_Font *font = asc_font_load(node->font); |
58
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
63 | static int alignments[] = { |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
64 | TTF_WRAPPED_ALIGN_LEFT, |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
65 | TTF_WRAPPED_ALIGN_CENTER, |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
66 | TTF_WRAPPED_ALIGN_RIGHT |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
67 | }; |
57
7daec2f067a9
implement centered wrapped text
Mike Becker <universe@uap-core.de>
parents:
54
diff
changeset
|
68 | TTF_SetFontWrappedAlign( |
58
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
69 | font, alignments[node->base.flags & ASC_TEXT_ALIGNMENT_MASK]); |
18
00c0632f0f40
fix missing UTF8 support for text rendering
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
70 | SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped( |
58
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
71 | font, node->text.ptr, asc_col_sdl(node->color), node->max_width |
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 | if (surface == NULL) { |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
74 | asc_error(SDL_GetError()); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
75 | return; |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
76 | } |
52
6d100092fcd4
use new scale2d function in text update
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
77 | asc_set_scale2d(&node->base, surface->w, surface->h); |
58
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
78 | if (asc_test_flag(node->base.flags, ASC_TEXT_CENTERED_FLAG)) { |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
79 | unsigned short newoffx = surface->w / 2; |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
80 | asc_vec2i pos = asc_get_position2d(&node->base); |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
81 | asc_set_position2d(&node->base, pos.x + node->offx - newoffx, pos.y); |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
82 | node->offx = newoffx; |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
83 | } |
4
b7acda6a4476
add simple color data types
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
84 | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
85 | // Transfer Image Data |
50 | 86 | asc_texture_from_surface(&node->tex, surface); |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
87 | |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
88 | // Free the surface |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
89 | SDL_FreeSurface(surface); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
90 | } |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
14
diff
changeset
|
91 | |
59
764fbb013252
use fancy named parameters for asc_text()
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
92 | AscSceneNode *asc_text_create(struct asc_text_create_args args) { |
32
86468a71dd73
add transformation matrix
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
93 | AscText *node = calloc(1, sizeof(AscText)); |
86468a71dd73
add transformation matrix
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
94 | |
41
df81d493716e
add correct interleaving of opaque and transparent sprites
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
95 | node->base.render_group = ASC_RENDER_GROUP_SPRITE_BLEND; |
32
86468a71dd73
add transformation matrix
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
96 | node->base.free_func = (asc_scene_free_func) asc_text_free; |
86468a71dd73
add transformation matrix
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
97 | node->base.update_func = (asc_scene_update_func) asc_text_update; |
86468a71dd73
add transformation matrix
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
98 | node->base.draw_func = (asc_scene_draw_func) asc_text_draw; |
86468a71dd73
add transformation matrix
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
99 | |
59
764fbb013252
use fancy named parameters for asc_text()
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
100 | node->base.flags = args.alignment; |
764fbb013252
use fancy named parameters for asc_text()
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
101 | node->base.position.x = (float) args.x; |
764fbb013252
use fancy named parameters for asc_text()
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
102 | node->base.position.y = (float) args.y; |
764fbb013252
use fancy named parameters for asc_text()
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
103 | node->max_width = args.max_width; |
66
8297afa1c29c
replaces broken font cache with improved cache - fixes #387
Mike Becker <universe@uap-core.de>
parents:
65
diff
changeset
|
104 | node->font = asc_active_font; |
32
86468a71dd73
add transformation matrix
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
105 | node->color = asc_context.ink; |
59
764fbb013252
use fancy named parameters for asc_text()
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
106 | if (args.text == NULL) { |
58
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
107 | node->text = cx_mutstr(strdup(" ")); |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
108 | } else { |
59
764fbb013252
use fancy named parameters for asc_text()
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
109 | node->text = cx_mutstr(strdup(args.text)); |
32
86468a71dd73
add transformation matrix
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
110 | } |
86468a71dd73
add transformation matrix
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
111 | |
37
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
112 | // initialize |
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
113 | asc_text_update(node); |
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
114 | |
36
e26b4ac1661c
invert the logic of converting between specialized nodes and the generic interface
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
115 | return &node->base; |
32
86468a71dd73
add transformation matrix
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
116 | } |
86468a71dd73
add transformation matrix
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
117 | |
25
601b3f4e17a2
rename AscTextNode to just AscText
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
118 | void asc_text_free(AscText *node) { |
50 | 119 | asc_texture_destroy(&node->tex); |
58
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
120 | cx_strfree(&node->text); |
19 | 121 | free(node); |
58
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
122 | } |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
123 | |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
124 | void asc_text_printf( |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
125 | AscSceneNode *node, |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
126 | char const* format, |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
127 | ... |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
128 | ) { |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
129 | cxmutstr text = ((AscText*)node)->text; |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
130 | va_list ap; |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
131 | va_start(ap, format); |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
132 | 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
|
133 | va_end(ap); |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
134 | asc_node_update(node); |
26ebb2f1e6e6
improve ui/text.h interface a lot
Mike Becker <universe@uap-core.de>
parents:
57
diff
changeset
|
135 | } |