102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
text.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
15#include "pcdogs/ui/functions.h"
16#include <math.h>
17#include <stdarg.h>
18#include <string.h>
19
20// Original pcdogs.exe: 0x427d60
21// Draws a centered HUD number using at most three decimal glyphs. Inputs must be within 0 through 999 for valid glyph indices.
22void UI_RenderCenteredNumber(uint32_t packed_center_xy, int32_t number)
23{
24 struct Graphics_Render_SpriteContext *glyph_context;
25
27
28 struct Graphics_Render_SpriteContext *reference_glyph_context = glyph_context;
29 struct Material_RuntimeDescriptor *texture_descriptor;
30
31 if (reference_glyph_context)
32 texture_descriptor = reference_glyph_context->textureDescriptor;
33
34 int32_t total_number_width;
35
36 if (!reference_glyph_context || !texture_descriptor)
37 total_number_width = 1;
38 else
39 {
40 int32_t reference_width_minus_one = (uint8_t)(texture_descriptor->dimensions_minus_1.width);
41
42 total_number_width = reference_width_minus_one + 1;
43 }
44
45 int32_t hundreds_digit;
46
47 if (number >= 0x64)
48 {
49 hundreds_digit = number / 0x64;
50
51 struct Material_RuntimeDescriptor *hundreds_measure_descriptor;
52
53 if (reference_glyph_context)
54 hundreds_measure_descriptor = reference_glyph_context->textureDescriptor;
55
56 int32_t hundreds_measure_width;
57
58 if (!reference_glyph_context || !hundreds_measure_descriptor)
59 hundreds_measure_width = 1;
60 else
61 {
62 int32_t hundreds_measure_width_minus_one = (uint8_t)(hundreds_measure_descriptor->dimensions_minus_1.width);
63
64 hundreds_measure_width = hundreds_measure_width_minus_one + 1;
65 }
66
67 total_number_width = total_number_width + hundreds_measure_width + 2;
68 }
69
70 int32_t tens_digit;
71
72 if (number >= 0xa)
73 {
74 tens_digit = (int64_t)number % 0x64 / 0xa;
75
76 struct Material_RuntimeDescriptor *tens_measure_descriptor;
77
78 if (reference_glyph_context)
79 tens_measure_descriptor = reference_glyph_context->textureDescriptor;
80
81 if (!reference_glyph_context || !tens_measure_descriptor)
82 total_number_width += 3;
83 else
84 {
85 int32_t tens_measure_width_minus_one = (uint8_t)(tens_measure_descriptor->dimensions_minus_1.width);
86
87 total_number_width = total_number_width + tens_measure_width_minus_one + 1 + 2;
88 }
89 }
90
91 int32_t packed_xy = packed_center_xy - (total_number_width >> 1);
92
93 if (number >= 0x64)
94 {
95 Material_FindTextureByFrame(Shared_FontDataCursor, hundreds_digit, &glyph_context);
96 // Direct Font_RenderGlyph call - PS1-scale vertex colors, no 2x.
97 Graphics_RenderTexturedSprite(packed_xy, 0, glyph_context, 0, 0, 0);
98
99 struct Graphics_Render_SpriteContext *hundreds_glyph_context = glyph_context;
100 struct Material_RuntimeDescriptor *hundreds_draw_descriptor;
101
102 if (hundreds_glyph_context)
103 hundreds_draw_descriptor = hundreds_glyph_context->textureDescriptor;
104
105 int32_t hundreds_draw_width;
106
107 if (!hundreds_glyph_context || !hundreds_draw_descriptor)
108 hundreds_draw_width = 1;
109 else
110 {
111 int32_t hundreds_draw_width_minus_one = (uint8_t)(hundreds_draw_descriptor->dimensions_minus_1.width);
112
113 hundreds_draw_width = hundreds_draw_width_minus_one + 1;
114 }
115
116 packed_xy = packed_xy + hundreds_draw_width + 2;
117 }
118
119 if (number >= 0xa)
120 {
121 Material_FindTextureByFrame(Shared_FontDataCursor, tens_digit, &glyph_context);
122 // Direct Font_RenderGlyph call - PS1-scale vertex colors, no 2x.
123 Graphics_RenderTexturedSprite(packed_xy, 0, glyph_context, 0, 0, 0);
124
125 struct Graphics_Render_SpriteContext *tens_glyph_context = glyph_context;
126 struct Material_RuntimeDescriptor *tens_draw_descriptor;
127
128 if (tens_glyph_context)
129 tens_draw_descriptor = tens_glyph_context->textureDescriptor;
130
131 int32_t tens_draw_width;
132
133 if (!tens_glyph_context || !tens_draw_descriptor)
134 tens_draw_width = 1;
135 else
136 {
137 int32_t tens_draw_width_minus_one = (uint8_t)(tens_draw_descriptor->dimensions_minus_1.width);
138
139 tens_draw_width = tens_draw_width_minus_one + 1;
140 }
141
142 packed_xy = packed_xy + tens_draw_width + 2;
143 }
144
145 Material_FindTextureByFrame(Shared_FontDataCursor, (int64_t)number % 0xa, &glyph_context);
146 // Direct Font_RenderGlyph call - PS1-scale vertex colors, no 2x.
147 Graphics_RenderTexturedSprite(packed_xy, 0, glyph_context, 0, 0, 0);
148}
149
150// Original pcdogs.exe: 0x42c7b0
151// Measures text using signed byte glyph indices and returns -1 for an empty string. Font data and glyph indices must be valid, and high-bit bytes are interpreted differently from the text renderer.
152int32_t UI_ComputeStringWidth(char const *text)
153{
155 struct Material_FrameSet *atlas = font[4].frame_set;
156 struct Material_SectionHeader *section = font[4].material_section;
157 uint32_t node_index = atlas->frame_ptr_array[atlas->frame_count - 1]->material_node_ref_packed & UI_MATERIAL_NODE_INDEX_MASK;
158 struct Material_RuntimeDescriptor **glyphs = section->node_table[node_index].frame_table;
159 int32_t width = -1;
160 int32_t glyph;
161
162 while ((glyph = *(const int8_t *)text++) != 0)
163 width += glyphs[glyph - 32]->dimensions_minus_1.width + 2;
164
165 return width;
166}
167
168static uint32_t glyph_width(const struct Material_TableEntry *material)
169{
170 return material->texture_descriptor ? material->texture_descriptor->dimensions_minus_1.width + 1u : 1u;
171}
172
173static void render_text(uint32_t packed_xy, const char *text, uint32_t flags,
174 struct Material_RefEntry *font, bool fixed_spacing, uint32_t advance)
175{
178
179 bool use_gradient = font == NULL;
180 if (use_gradient)
181 font = &Shared_FontDataCursor[4];
182
183 struct Material_FrameSet *atlas = font->frame_set;
186 struct Material_TableEntry *material = font->material_table_entry;
187 const uint8_t *bytes = (const uint8_t *)text;
188 uint32_t alignment = flags & UI_TEXT_ALIGNMENT_MASK;
189 if (alignment)
190 {
191 int32_t width = -1;
192 for (const uint8_t *ch = bytes; *ch; ++ch)
193 {
194 material->texture_descriptor = glyphs[*ch - UI_TEXT_GLYPH_FIRST_CHARACTER];
195 width += (fixed_spacing ? advance : glyph_width(material)) + 1;
196 }
197 packed_xy -= alignment == UI_TEXT_ALIGN_CENTER ? width >> 1 : width;
198 }
199
200 uint32_t render_flags = ((flags & UI_TEXT_BLEND_MASK) >> UI_TEXT_BLEND_SHIFT)
202 uint32_t top = 0, bottom = 0;
203 if (use_gradient)
204 Graphics_ComputeColorGradient(&top, &bottom, flags);
207
208 for (const uint8_t *ch = bytes; *ch; ++ch)
209 {
210 material->texture_descriptor = glyphs[*ch - UI_TEXT_GLYPH_FIRST_CHARACTER];
211 if (!use_gradient)
212 {
213 Graphics_RenderTexturedSprite(packed_xy, fixed_spacing ? advance : 0,
214 (struct Graphics_Render_SpriteContext *)material, render_flags, 0, 0);
215 }
216 else if (fixed_spacing)
217 Graphics_RenderTexturedQuad(packed_xy, material, render_flags, top, bottom, advance);
218 else
219 Graphics_RenderTexturedQuadMaterialSize(packed_xy, material, render_flags, top, bottom);
220
221 packed_xy += (fixed_spacing ? advance : glyph_width(material)) + 1;
222 }
223}
224
225// Original pcdogs.exe: 0x42c840
226void UI_RenderText(uint32_t packed_xy, const char *text, uint32_t text_flags, void *font_context)
227{
228 render_text(packed_xy, text, text_flags, font_context, false, 0);
229}
230
231// Original pcdogs.exe: 0x42ca20
232void UI_RenderFixedAdvanceText(uint32_t packed_xy, const char *text, uint32_t text_flags,
233 void *font_context, uint32_t glyph_advance)
234{
235 render_text(packed_xy, text, text_flags, font_context, true, glyph_advance);
236}
#define SPRITE_RENDER_EXPLICIT_COLOR
Definition constants.h:33
#define SPRITE_RENDER_BLEND_MODE_SHIFT
Definition constants.h:36
#define GRAPHICS_COLOR_RGB_MASK
Definition constants.h:38
void Graphics_ComputeColorGradient(uint32_t *out_top_color, uint32_t *out_bottom_color, uint32_t text_flags)
Definition effects.c:385
void Graphics_RenderTexturedQuad(uint32_t packed_xy, struct Material_TableEntry *material, uint32_t render_flags, uint32_t top_color, uint32_t bottom_color, int32_t quad_size)
Definition primitives.c:167
void Graphics_RenderTexturedQuadMaterialSize(uint32_t packed_xy, struct Material_TableEntry *material, uint32_t render_flags, uint32_t top_color, uint32_t bottom_color)
Definition primitives.c:241
void Graphics_RenderTexturedSprite(uint32_t packed_xy, uint32_t packed_wh, struct Graphics_Render_SpriteContext *sprite, uint32_t render_flags, uint32_t rotation_angle, uint32_t rotation_pivot_xy)
Definition sprites.c:43
#define graphics_text_rendering_mode
Definition globals.h:293
int32_t Material_FindTextureByFrame(struct Material_RefEntry *material_ref, int32_t frame_index, struct Graphics_Render_SpriteContext **out_sprite)
Definition textures.c:325
#define Shared_FontDataCursor
Definition globals.h:111
uint8_t D3D_SetBlendMode(int32_t blend_mode)
struct Material_RuntimeDescriptor * textureDescriptor
Definition types.h:570
uint32_t material_node_ref_packed
Definition types.h:51
struct Material_FrameData ** frame_ptr_array
Definition types.h:63
int16_t frame_count
Definition types.h:62
struct Material_RuntimeDescriptor ** frame_table
Definition types.h:92
struct Material_SectionHeader * material_section
Definition types.h:134
struct Material_TableEntry * material_table_entry
Definition types.h:135
struct Material_FrameSet * frame_set
Definition types.h:136
struct Math_SizeU8 dimensions_minus_1
Definition types.h:264
struct Material_Node * node_table
Definition types.h:146
struct Material_RuntimeDescriptor * texture_descriptor
Definition types.h:353
uint32_t flags
Definition types.h:342
uint8_t width
Definition types.h:347
static void render_text(uint32_t packed_xy, const char *text, uint32_t flags, struct Material_RefEntry *font, bool fixed_spacing, uint32_t advance)
Definition text.c:173
void UI_RenderText(uint32_t packed_xy, const char *text, uint32_t text_flags, void *font_context)
Definition text.c:226
void UI_RenderCenteredNumber(uint32_t packed_center_xy, int32_t number)
Definition text.c:22
void UI_RenderFixedAdvanceText(uint32_t packed_xy, const char *text, uint32_t text_flags, void *font_context, uint32_t glyph_advance)
Definition text.c:232
static uint32_t glyph_width(const struct Material_TableEntry *material)
Definition text.c:168
int32_t UI_ComputeStringWidth(char const *text)
Definition text.c:152
#define UI_TEXT_ALIGNMENT_MASK
Definition constants.h:8
#define UI_TEXT_ALIGN_CENTER
Definition constants.h:6
#define UI_TEXT_BLEND_SHIFT
Definition constants.h:13
#define UI_TEXT_BLEND_MASK
Definition constants.h:12
#define UI_MATERIAL_NODE_INDEX_MASK
Definition constants.h:18
#define UI_TEXT_GLYPH_FIRST_CHARACTER
Definition constants.h:83