102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
sprites.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
11#include "pcdogs/game/globals.h"
15#include "pcdogs/level/types.h"
18#include "pcdogs/math/types.h"
23#include "pcdogs/ui/functions.h"
24#include <math.h>
25#include <stdarg.h>
26#include <string.h>
27
28// Original pcdogs.exe: 0x4417b0
29// Updates linked package sprites, sorts them and renders their animated transforms and colors. Links require a valid sentinel-terminated list; failure to allocate the sort-map header is unchecked, while individual node failures omit sprites.
31{
33 struct PKG_SpriteEntry *linked_sprite_base;
34 struct PKG_SpriteEntry *sprite_list_local;
35
36 if (level_data)
37 {
38 struct PKG_SpriteEntry *sprite_list = level_data->sprite_list;
39
40 linked_sprite_base = sprite_list;
41 sprite_list_local = sprite_list;
42 }
43 else
44 {
45 sprite_list_local = NULL;
46 linked_sprite_base = NULL;
47 }
48
49 if (!sprite_list_local)
50 return;
51
52 struct Tree_Map *tree = Tree_CreateMap(4, (int32_t (*)(void *, void *))(UI_CompareSpriteDepth));
53 struct Tree_Map *sort_tree = tree;
54 struct PKG_SpriteEntry *sprite_cursor = sprite_list_local;
55 uint8_t state_flags;
56
57 do
58 {
59 if (sprite_cursor->state_flags & UI_SPRITE_STATE_CACHED)
60 {
61 uint8_t link_index = sprite_cursor->link_index;
62
63 if (link_index != 0xff)
64 {
65 struct PKG_SpriteEntry *ancestor_sprite = &sprite_list_local[link_index];
66
67 while (ancestor_sprite->state_flags & UI_SPRITE_STATE_CACHED)
68 {
69 if ((uintptr_t)sprite_cursor >= (uintptr_t)ancestor_sprite)
70 break;
71
72 uint8_t ancestor_link_index = ancestor_sprite->link_index;
73
74 if (ancestor_link_index == 0xff)
75 break;
76
77 ancestor_sprite = &sprite_list_local[ancestor_link_index];
78 }
79
80 if (!(ancestor_sprite->state_flags & UI_SPRITE_STATE_CACHED))
81 {
82 uint8_t invalidated_state_flags = sprite_cursor->state_flags;
83
84 sprite_cursor->bounds.w = 0;
85 sprite_cursor->state_flags = invalidated_state_flags & (uint8_t)~UI_SPRITE_STATE_CACHED;
86 }
87 }
88 }
89
91 {
92 struct PKG_SpriteEntry **node_payload = Tree_AllocateMapNode(tree);
93
94 if (node_payload)
95 {
96 *node_payload = sprite_cursor;
97 Tree_InitializeMapNode(tree, node_payload);
98 }
99 }
100
101 state_flags = sprite_cursor->state_flags;
102 sprite_cursor = &sprite_cursor[1];
104
105 int32_t bounds_width = 0, bounds_height = 0;
106 uint32_t composed_render_flags = 0;
107 for (struct PKG_SpriteEntry **sorted_node_payload = Tree_GetFirstMapNode(tree); sorted_node_payload;
108 sorted_node_payload = Tree_GetFirstMapNode(tree))
109 {
110 struct PKG_SpriteEntry *sprite = *sorted_node_payload;
111
112 Tree_FreeMapNode(sorted_node_payload);
113
114 uint16_t initial_control_flags;
115 uint8_t initial_state_flags = sprite->state_flags;
116 int32_t pivot_y;
117 int16_t pivot_x;
118 int32_t draw_y;
119 int32_t draw_x;
120
121 if (!(initial_state_flags & UI_SPRITE_STATE_CACHED) || !sprite->bounds.w)
122 {
123 initial_state_flags &= UI_SPRITE_STATE_LIST_END;
124 sprite->state_flags = initial_state_flags;
125 initial_control_flags = sprite->control_flags;
126
127 uint8_t initialized_state_flags = ((char)(initial_control_flags >> 6) & UI_PACKED_BLEND_MODE_MASK) | initial_state_flags | UI_SPRITE_STATE_CACHED;
128
129 sprite->state_flags = initialized_state_flags;
130
132 sprite->state_flags = initialized_state_flags | UI_SPRITE_STATE_VISIBLE;
133
134 int32_t color_target_word = sprite->color_target_word;
135
136 sprite->current_color = (struct Math_ColorRGB8){
137 .r = (uint8_t)color_target_word,
138 .g = (uint8_t)((uint32_t)color_target_word >> 8),
139 .b = (uint8_t)((uint32_t)color_target_word >> 16)};
140
141 sprite->current_color_reserved_high = (uint8_t)((uint32_t)color_target_word >> 24);
142
143 int32_t color_end_frame = sprite->color_frame_range.max;
144
145 if (color_end_frame)
146 {
147 int32_t color_current_frame = g_frameCounter;
148
149 if ((uint32_t)color_current_frame < (uint32_t)color_end_frame)
150 {
151 int32_t color_start_frame = sprite->color_frame_range.min;
152 int32_t color_ease_factor = Math_EaseInOut(
153 (uint32_t)((color_start_frame * UI_SPRITE_INTERPOLATION_NEGATE_MASK + color_current_frame) << MATH_Q12_SHIFT) / (uint32_t)(color_end_frame - color_start_frame + 1),
154 sprite->color_ease.a, sprite->color_ease.b);
155 uint8_t start_color_channel = sprite->color_start.r;
156 int32_t target_red = (uint8_t)(sprite->current_color.r);
157
158 sprite->current_color.r =
159 (char)(((target_red - (uint32_t)start_color_channel) * color_ease_factor) >> MATH_Q12_SHIFT) + start_color_channel;
160 start_color_channel = sprite->color_start.g;
161
162 int32_t target_green = (uint8_t)(sprite->current_color.g);
163
164 sprite->current_color.g =
165 (char)(((target_green - (uint32_t)start_color_channel) * color_ease_factor) >> MATH_Q12_SHIFT) + start_color_channel;
166 start_color_channel = sprite->color_start.b;
167
168 int32_t target_blue = (uint8_t)(sprite->current_color.b);
169 uint8_t interpolated_blue =
170 (char)(((target_blue - (uint32_t)start_color_channel) * color_ease_factor) >> MATH_Q12_SHIFT) + start_color_channel;
171
172 sprite->current_color.b = interpolated_blue;
173 }
174 else
175 sprite->color_frame_range.max = 0;
176
177 sprite->state_flags &= (uint8_t)~UI_SPRITE_STATE_CACHED;
178 }
179
181
182 int32_t rotation_end_frame = sprite->rotation_frame_range.max;
183
184 if (rotation_end_frame)
185 {
186 int32_t rotation_current_frame = g_frameCounter;
187
188 if ((uint32_t)rotation_current_frame < (uint32_t)rotation_end_frame)
189 {
190 int32_t rotation_start_frame = sprite->rotation_frame_range.min;
191 int32_t rotation_ease_factor = Math_EaseInOut(
192 (uint32_t)((rotation_start_frame * UI_SPRITE_INTERPOLATION_NEGATE_MASK + rotation_current_frame) << MATH_Q12_SHIFT) / (uint32_t)(rotation_end_frame - rotation_start_frame + 1),
193 sprite->rotation_ease.a, sprite->rotation_ease.b);
194 int64_t rotation_product = (int64_t)(sprite->rotation_angle_fp12 - sprite->rotation_start_angle) * rotation_ease_factor;
195
196 sprite->rotation_angle_fp12 =
197 sprite->rotation_start_angle + ((int32_t)((uint64_t)rotation_product >> MATH_Q12_SHIFT));
198 }
199 else
200 sprite->rotation_frame_range.max = 0;
201
202 sprite->state_flags &= (uint8_t)~UI_SPRITE_STATE_CACHED;
203 }
204
205 int32_t target_scale_x = sprite->scale_target.x;
206
207 sprite->current_scale.y = sprite->scale_target.y;
208
209 int32_t scale_end_frame = sprite->scale_frame_range.max;
210
211 sprite->current_scale.x = target_scale_x;
212
213 if (scale_end_frame)
214 {
215 int32_t scale_current_frame = g_frameCounter;
216
217 if ((uint32_t)scale_current_frame < (uint32_t)scale_end_frame)
218 {
219 int32_t scale_start_frame = sprite->scale_frame_range.min;
220 int32_t scale_ease_factor = Math_EaseInOut(
221 (uint32_t)((scale_start_frame * UI_SPRITE_INTERPOLATION_NEGATE_MASK + scale_current_frame) << MATH_Q12_SHIFT) / (uint32_t)(scale_end_frame - scale_start_frame + 1),
222 sprite->scale_ease.a, sprite->scale_ease.b);
223 int64_t scale_x_product = (int64_t)(sprite->current_scale.x - sprite->scale_start.x) * scale_ease_factor;
224 int32_t target_scale_y = sprite->current_scale.y;
225
226 sprite->current_scale.x =
227 sprite->scale_start.x + ((int32_t)((uint64_t)scale_x_product >> MATH_Q12_SHIFT));
228
229 int64_t scale_y_product = (int64_t)(target_scale_y - sprite->scale_start.y) * scale_ease_factor;
230
231 sprite->current_scale.y =
232 ((int32_t)((uint64_t)scale_y_product >> MATH_Q12_SHIFT)) + sprite->scale_start.y;
233 }
234 else
235 sprite->scale_frame_range.max = 0;
236
237 sprite->state_flags &= (uint8_t)~UI_SPRITE_STATE_CACHED;
238 }
239
240 int32_t target_move_x = (int32_t)sprite->move_target.x;
241 int32_t move_y_or_layer_index = (int32_t)sprite->move_target.y;
242 int32_t move_end_frame_or_start_y = sprite->move_frame_range.max;
243
244 draw_x = target_move_x;
245 draw_y = move_y_or_layer_index;
246
247 if (move_end_frame_or_start_y)
248 {
249 if ((uint32_t)g_frameCounter < (uint32_t)move_end_frame_or_start_y)
250 {
251 int32_t move_start_frame = sprite->move_frame_range.min;
252 int32_t move_ease_factor = Math_EaseInOut(
253 (uint32_t)((move_start_frame * UI_SPRITE_INTERPOLATION_NEGATE_MASK + g_frameCounter) << MATH_Q12_SHIFT) / (uint32_t)(move_end_frame_or_start_y - move_start_frame + 1),
254 sprite->move_ease.a, sprite->move_ease.b);
255 int32_t start_move_x = (int32_t)sprite->move_start.x;
256
257 move_end_frame_or_start_y = (int32_t)sprite->move_start.y;
258 draw_x = (((target_move_x - start_move_x) * move_ease_factor) >> MATH_Q12_SHIFT) + start_move_x;
259 move_y_or_layer_index = (((move_y_or_layer_index - move_end_frame_or_start_y) * move_ease_factor) >> MATH_Q12_SHIFT) + move_end_frame_or_start_y;
260 draw_y = move_y_or_layer_index;
261 }
262 else
263 sprite->move_frame_range.max = 0;
264
265 sprite->state_flags &= (uint8_t)~UI_SPRITE_STATE_CACHED;
266 }
267
268 uint8_t parent_link_byte = sprite->link_index;
269 int16_t layer_sort_key = (int16_t)((uint16_t)sprite->layer_index << 6);
270
271 sprite->sort_key = layer_sort_key;
272
273 if (parent_link_byte != 0xff)
274 {
275 uint32_t parent_link_index = (uint32_t)parent_link_byte;
276 struct PKG_SpriteEntry *parent_sprite = &linked_sprite_base[parent_link_index];
277 uint16_t control_flags;
278
280 {
281 uint8_t own_flags = sprite->state_flags & (uint8_t)~UI_SPRITE_STATE_VISIBLE;
282
283 sprite->state_flags = own_flags;
284 sprite->state_flags = own_flags | (parent_sprite->state_flags & UI_SPRITE_STATE_VISIBLE);
285 }
286
288 {
289 uint8_t own_flags = sprite->state_flags & (uint8_t)~UI_PACKED_BLEND_MODE_MASK;
290
291 sprite->state_flags = own_flags;
292 sprite->state_flags = own_flags | (parent_sprite->state_flags & UI_PACKED_BLEND_MODE_MASK);
293 }
294
296
297 if (!((uint8_t)((uint32_t)control_flags >> 8) & UI_SPRITE_NO_PARENT_DEPTH_HIGH_BYTE))
298 {
299 sprite->sort_key = (uint16_t)(parent_sprite->sort_key + layer_sort_key + 1);
300 }
301
302 if (!((uint8_t)((uint32_t)control_flags >> 8) & UI_SPRITE_NO_PARENT_COLOR_HIGH_BYTE))
303 {
304 int32_t parent_color_word = parent_sprite->current_color_word;
305
306 sprite->current_color = (struct Math_ColorRGB8){
307 .r = (uint8_t)parent_color_word,
308 .g = (uint8_t)((uint32_t)parent_color_word >> 8),
309 .b = (uint8_t)((uint32_t)parent_color_word >> 16)};
310
311 sprite->current_color_reserved_high = (uint8_t)((uint32_t)parent_color_word >> 24);
312 }
313
314 int32_t parent_scale_x;
315
317 parent_scale_x = (uint32_t)parent_sprite->current_scale.x;
318
319 int32_t parent_scaled_y;
320
321 if (!(sprite->inheritance_control_flags & UI_SPRITE_NO_PARENT_SCALE) && (parent_scale_x != MATH_Q12_ONE || (int32_t)parent_sprite->current_scale.y != parent_scale_x))
322 {
323 parent_scaled_y = ((int32_t)parent_sprite->current_scale.y * draw_y) >> MATH_Q12_SHIFT;
324 draw_x = (parent_scale_x * draw_x) >> MATH_Q12_SHIFT;
325 draw_y = parent_scaled_y;
326
327 int64_t inherited_scale_x_product = (int64_t)parent_scale_x * sprite->current_scale.x;
328
329 sprite->current_scale.x = (int32_t)((uint64_t)inherited_scale_x_product >> MATH_Q12_SHIFT);
330
331 int64_t inherited_scale_y_product = (int64_t)(int32_t)parent_sprite->current_scale.y * sprite->current_scale.y;
332
333 sprite->current_scale.y = (int32_t)((uint64_t)inherited_scale_y_product >> MATH_Q12_SHIFT);
334 }
335 else
336 parent_scaled_y = draw_y;
337
339 {
340 int32_t anchor_code = (uint8_t)(sprite->anchor_code);
341
342 draw_x += (int32_t)(int16_t)parent_sprite->bounds.x;
343
344 int32_t parent_height = (int32_t)(int16_t)parent_sprite->bounds.h;
345 int32_t parent_translated_y = parent_scaled_y + (int32_t)(int16_t)parent_sprite->bounds.y;
346 int32_t parent_width = (int32_t)(int16_t)parent_sprite->bounds.w;
347
348 draw_y = parent_translated_y;
349 bounds_width = parent_width;
350 bounds_height = parent_height;
351
352 if (anchor_code - 1 <= 7)
353 switch (anchor_code)
354 {
356 {
357 draw_x = draw_x + parent_width - 1;
358
359 break;
360 }
361
363 {
364 draw_y = parent_translated_y + parent_height - 1;
365
366 break;
367 }
368
370 {
371 draw_x = draw_x + parent_width - 1;
372 draw_y = parent_translated_y + parent_height - 1;
373
374 break;
375 }
376
378 {
379 draw_y = parent_translated_y + (parent_height >> 1);
380
381 break;
382 }
383
385 {
386 draw_x += parent_width >> 1;
387
388 break;
389 }
390
392 {
393 draw_x = draw_x + parent_width - 1;
394 draw_y = parent_translated_y + (parent_height >> 1);
395
396 break;
397 }
398
400 {
401 draw_x += parent_width >> 1;
402 draw_y = parent_translated_y + parent_height - 1;
403
404 break;
405 }
406
407 case UI_ANCHOR_CENTER:
408 {
409 draw_x += parent_width >> 1;
410 draw_y = parent_translated_y + (parent_height >> 1);
411
412 break;
413 }
414 }
415 }
416
417 control_flags = sprite->control_flags;
418
419 if (!((uint8_t)((uint32_t)control_flags >> 8) & UI_SPRITE_NO_PARENT_ROTATION_HIGH_BYTE))
420 {
421 int32_t parent_rotation = (uint32_t)parent_sprite->rotation_angle_fp12;
422
423 if (parent_rotation)
424 {
425 sprite->rotation_angle_fp12 += parent_rotation;
426
427 int32_t parent_control_flags = (uint16_t)parent_sprite->control_flags;
428 int32_t parent_bounds_x = (int32_t)(int16_t)parent_sprite->bounds.x;
429 int32_t parent_pivot_y = (int32_t)(int16_t)parent_sprite->bounds.y;
430 int32_t parent_pivot_x = parent_bounds_x;
431 int32_t parent_horizontal_anchor = parent_control_flags >> 2 & UI_SPRITE_ANCHOR_AXIS_MASK;
432 int32_t saved_parent_pivot_y = parent_pivot_y;
433
434 if (parent_horizontal_anchor == UI_AXIS_ANCHOR_CENTER)
435 parent_pivot_x = parent_bounds_x + ((int32_t)(int16_t)parent_sprite->bounds.w >> 1);
436 else if (parent_horizontal_anchor == UI_AXIS_ANCHOR_END)
437 parent_pivot_x = parent_bounds_x + (int32_t)(int16_t)parent_sprite->bounds.w - 1;
438
439 int32_t parent_vertical_anchor = parent_control_flags >> 4 & UI_SPRITE_ANCHOR_AXIS_MASK;
440
441 if (parent_vertical_anchor == UI_AXIS_ANCHOR_CENTER)
442 {
443 parent_pivot_y += (int32_t)(int16_t)parent_sprite->bounds.h >> 1;
444 saved_parent_pivot_y = parent_pivot_y;
445 }
446 else if (parent_vertical_anchor == UI_AXIS_ANCHOR_END)
447 {
448 parent_pivot_y = parent_pivot_y + (int32_t)(int16_t)parent_sprite->bounds.h - 1;
449 saved_parent_pivot_y = parent_pivot_y;
450 }
451
452 int32_t angle = (uint32_t)parent_sprite->rotation_angle_fp12 >> MATH_Q12_SHIFT & UI_ROTATION_ANGLE_MASK;
453
454 if ((uint16_t)angle)
455 {
456 int32_t negative_parent_sin = -((Math_SinQ14(angle) >> 2));
457 int32_t relative_pivot_x = draw_x - parent_pivot_x;
458 int32_t relative_pivot_y = draw_y - parent_pivot_y;
459 int32_t parent_cos = Math_SinQ14(0x400 - angle) >> 2;
460
461 draw_x = ((relative_pivot_x * parent_cos - relative_pivot_y * negative_parent_sin) >> MATH_Q12_SHIFT) + parent_pivot_x;
462 draw_y = ((relative_pivot_y * parent_cos + relative_pivot_x * negative_parent_sin) >> MATH_Q12_SHIFT) + saved_parent_pivot_y;
463 }
464 }
465 }
466 }
467
468 int16_t computed_screen_x = (uint16_t)draw_x;
469
470 pivot_x = computed_screen_x;
471 sprite->screen.x = computed_screen_x;
472 pivot_y = draw_y;
473 sprite->screen.y = (uint16_t)draw_y;
474 }
475 else
476 {
477 int32_t cached_screen_x = (int32_t)sprite->screen.x;
478
479 draw_x = cached_screen_x;
480 pivot_x = (uint16_t)cached_screen_x;
481
482 int32_t cached_screen_y = (int32_t)sprite->screen.y;
483
484 draw_y = cached_screen_y;
485 pivot_y = cached_screen_y;
486 }
487
488 uint8_t render_state = sprite->state_flags;
489 struct Graphics_Render_SpriteContext *sprite_ctx1 = sprite->sprite_ctx1;
490
491 if (render_state & UI_SPRITE_STATE_VISIBLE)
492 {
493 if (sprite_ctx1)
494 {
495 Graphics_ProcessMeshCommands((struct Mesh_CmdList *)(sprite->anim_frames1), (struct Material_Node **)(sprite->texture_db1), NULL);
496
497 int32_t material_red;
498 int32_t sprite_red;
499
500 material_red = sprite_ctx1->color_mod.r;
501 sprite_red = sprite->current_color.r;
502
503 int32_t modulated_red = material_red + sprite_red - 0x80;
504
505 if (modulated_red < 0)
506 modulated_red = 0;
507 else if (modulated_red > 0xff)
508 modulated_red = 0xff;
509
510 int32_t material_green;
511 int32_t sprite_green;
512
513 material_green = sprite_ctx1->color_mod.g;
514 sprite_green = sprite->current_color.g;
515 composed_render_flags = (uint8_t)modulated_red;
516
517 int32_t modulated_green = material_green + sprite_green - 0x80;
518
519 if (modulated_green < 0)
520 modulated_green = 0;
521 else if (modulated_green > 0xff)
522 modulated_green = 0xff;
523
524 composed_render_flags |= (uint32_t)(uint8_t)modulated_green << 8;
525
526 int32_t material_blue = (uint8_t)(sprite_ctx1->color_mod.b);
527 int32_t sprite_blue = (uint8_t)(sprite->current_color.b);
528 int32_t modulated_blue = material_blue + sprite_blue - 0x80;
529
530 if (modulated_blue < 0)
531 modulated_blue = 0;
532 else if (modulated_blue > 0xff)
533 modulated_blue = 0xff;
534
535 composed_render_flags |= (uint32_t)(uint8_t)modulated_blue << 16;
537 }
538 else
539 {
540 uint32_t current_color_word = sprite->current_color.r |
541 ((uint32_t)sprite->current_color.g << 8) |
542 ((uint32_t)sprite->current_color.b << 16) |
543 ((uint32_t)sprite->current_color_reserved_high << 24);
544
545 composed_render_flags = current_color_word;
546 }
547
548 composed_render_flags = (composed_render_flags & GRAPHICS_COLOR_RGB_MASK) |
549 ((uint32_t)(sprite->state_flags & UI_PACKED_BLEND_MODE_MASK) << 24);
550 composed_render_flags |= SPRITE_RENDER_EXPLICIT_COLOR;
551 }
552
553 uint8_t sprite_type = sprite->type;
554
555 if (sprite_type == UI_SPRITE_IMAGE)
556 {
557 if (sprite_ctx1)
558 {
559 int32_t glyph_width;
560
562 {
563 glyph_width = (int32_t)sprite->bounds.w;
564 bounds_width = glyph_width;
565 }
566
567 int32_t glyph_height;
568
570 {
571 struct Material_RuntimeDescriptor *glyph_descriptor =
572 sprite_ctx1->textureDescriptor;
573 int32_t unscaled_glyph_width;
574 uint32_t unscaled_glyph_height;
575
576 if (glyph_descriptor)
577 {
578 int32_t glyph_width_minus_one;
579 int32_t glyph_height_minus_one;
580
581 glyph_width_minus_one = glyph_descriptor->dimensions_minus_1.width;
582 glyph_height_minus_one = glyph_descriptor->dimensions_minus_1.height;
583 unscaled_glyph_width = glyph_width_minus_one + 1;
584 unscaled_glyph_height = glyph_height_minus_one + 1;
585 }
586 else
587 {
588 unscaled_glyph_width = (uint16_t)sprite->content_value;
589 unscaled_glyph_height = sprite->content_value >> 0x10;
590 }
591
592 int32_t glyph_control_flags = (uint16_t)sprite->control_flags;
593 int32_t glyph_horizontal_anchor = glyph_control_flags >> 2 & UI_SPRITE_ANCHOR_AXIS_MASK;
594
595 glyph_width = (sprite->current_scale.x * unscaled_glyph_width) >> MATH_Q12_SHIFT;
596 glyph_height = (int32_t)(sprite->current_scale.y * unscaled_glyph_height) >> MATH_Q12_SHIFT;
597 bounds_width = glyph_width;
598 bounds_height = glyph_height;
599
600 if (glyph_horizontal_anchor == UI_AXIS_ANCHOR_CENTER)
601 draw_x -= glyph_width >> 1;
602 else if (glyph_horizontal_anchor == UI_AXIS_ANCHOR_END)
603 draw_x += 1 - glyph_width;
604
605 int32_t glyph_vertical_anchor = glyph_control_flags >> 4 & UI_SPRITE_ANCHOR_AXIS_MASK;
606
607 if (glyph_vertical_anchor == UI_AXIS_ANCHOR_CENTER)
608 draw_y -= glyph_height >> 1;
609 else if (glyph_vertical_anchor == UI_AXIS_ANCHOR_END)
610 draw_y += 1 - glyph_height;
611 }
612 else
613 {
614 draw_x = (int32_t)sprite->bounds.x;
615 draw_y = (int32_t)sprite->bounds.y;
616 glyph_height = (int32_t)sprite->bounds.h;
617 bounds_height = glyph_height;
618 }
619
621 {
622 uint32_t glyph_render_flags = composed_render_flags;
623
624 // Direct Font_RenderGlyph call from sprite UI - PS1-scale colors.
625 Graphics_RenderTexturedSprite(draw_y << UI_PACKED_Y_SHIFT | (uint32_t)(uint16_t)draw_x,
626 glyph_height << UI_PACKED_Y_SHIFT | (glyph_width & UI_PACKED_COORDINATE_LOW_MASK), sprite_ctx1, glyph_render_flags,
627 sprite->rotation_angle_fp12 >> MATH_Q12_SHIFT, pivot_y << UI_PACKED_Y_SHIFT | (uint32_t)(uint16_t)pivot_x);
628 }
629 }
630 else
631 {
632 bounds_height = 1;
633 bounds_width = 1;
634 }
635 }
636 else if (sprite_type > UI_SPRITE_IMAGE && sprite_type <= UI_SPRITE_NUMBER)
637 {
638 struct Material_RefEntry *font_ref = (struct Material_RefEntry *)&sprite->texture_db2;
639 struct Material_RefEntry *saved_font_ref = font_ref;
640
641 if (!sprite->sprite_ctx2)
642 {
643 font_ref = &Shared_FontDataCursor[4];
644 saved_font_ref = font_ref;
645 }
646
647 if (sprite->sprite_ctx2 || ((struct Graphics_Render_SpriteContext *)font_ref->material_table_entry))
648 {
649 char (*text_buffer)[0x14];
650 char buffer[0x14];
651
652 if (sprite_type != UI_SPRITE_NUMBER)
653 {
654 int32_t string_resource_id = sprite->content_value;
655 char (*resolved_string)[0x14];
656
657 if (string_resource_id >= 0)
658 resolved_string = (char (*)[20])(String_GetByIndex(string_resource_id));
659 else
660 resolved_string = (char (*)[20])(String_GetRuntimeByNegativeIndex(string_resource_id));
661
662 text_buffer = resolved_string;
663 }
664 else
665 {
666 text_buffer = &buffer;
667 String_FormatCRTString((char *)(&buffer), "%u", sprite->content_value);
668 }
669
670 int32_t scale_text_glyphs;
671
672 if (sprite->current_scale.x == MATH_Q12_ONE)
673 scale_text_glyphs = 0;
674
675 if (sprite->current_scale.x != MATH_Q12_ONE || sprite->current_scale.y != MATH_Q12_ONE)
676 scale_text_glyphs = 1;
677
678 struct Animation_FrameData *anim_frames2 = (struct Animation_FrameData *)font_ref->frame_set;
679 struct Graphics_Render_SpriteContext *sprite_ctx2 = ((struct Graphics_Render_SpriteContext *)font_ref->material_table_entry);
680 struct Material_FrameSet *font_frames = (struct Material_FrameSet *)anim_frames2;
681 struct Material_Entry *texture_db2 = (struct Material_Entry *)font_ref->material_section;
682 struct Material_SectionHeader *font_material_section = (struct Material_SectionHeader *)texture_db2;
683 struct Material_RuntimeDescriptor **glyph_descriptor_table = font_material_section->node_table[font_frames->frame_ptr_array[(int32_t)font_frames->frame_count - 1]->material_node_ref_packed & UI_MATERIAL_NODE_INDEX_MASK].frame_table;
684 int32_t text_width;
685
687 {
688 text_width = (int32_t)sprite->bounds.w;
689 bounds_width = text_width;
690 }
691
692 if (!(sprite->state_flags & UI_SPRITE_STATE_CACHED) || !text_width)
693 {
694 int32_t text_height = 0;
695 int32_t measure_character = (int32_t)*(int8_t *)text_buffer;
696
697 text_width = -((int32_t)sprite_ctx2->glyph_advance_adjust);
698 bounds_width = text_width;
699 bounds_height = 0;
700
701 char *measure_cursor = &((char *)text_buffer)[1];
702
703 if (measure_character)
704 {
705 do
706 {
707 struct Material_RuntimeDescriptor *measure_glyph_descriptor =
708 glyph_descriptor_table[measure_character - 32];
709
710 sprite_ctx2->textureDescriptor = measure_glyph_descriptor;
711
712 int32_t measure_glyph_width;
713
714 if (!sprite_ctx2 || !measure_glyph_descriptor)
715 measure_glyph_width = 1;
716 else
717 {
718 int32_t measure_width_minus_one = (uint8_t)(measure_glyph_descriptor->dimensions_minus_1.width);
719
720 measure_glyph_width = measure_width_minus_one + 1;
721 }
722
723 text_width +=
724 (int32_t)((struct Graphics_Render_SpriteContext *)saved_font_ref->material_table_entry)->glyph_advance_adjust + measure_glyph_width;
725
726 int32_t measure_glyph_height;
727
728 if (!sprite_ctx2 || !measure_glyph_descriptor)
729 measure_glyph_height = 1;
730 else
731 {
732 int32_t measure_height_minus_one = (uint8_t)(measure_glyph_descriptor->dimensions_minus_1.height);
733
734 measure_glyph_height = measure_height_minus_one + 1;
735 }
736
737 text_height = bounds_height;
738
739 if (measure_glyph_height > text_height)
740 {
741 if (!sprite_ctx2 || !measure_glyph_descriptor)
742 text_height = 1;
743 else
744 {
745 int32_t max_glyph_height_minus_one = (uint8_t)(measure_glyph_descriptor->dimensions_minus_1.height);
746
747 text_height = max_glyph_height_minus_one + 1;
748 }
749
750 bounds_height = text_height;
751 }
752
753 measure_character = (int32_t)*(int8_t *)measure_cursor;
754 measure_cursor = &measure_cursor[1];
755 } while (measure_character);
756
757 bounds_width = text_width;
758 }
759
760 if (scale_text_glyphs)
761 {
762 text_width = (sprite->current_scale.x * text_width) >> MATH_Q12_SHIFT;
763 text_height = (sprite->current_scale.y * text_height) >> MATH_Q12_SHIFT;
764 bounds_width = text_width;
765 bounds_height = text_height;
766 }
767
768 int32_t text_control_flags = (uint16_t)sprite->control_flags;
769 int32_t text_horizontal_anchor = text_control_flags >> 2 & UI_SPRITE_ANCHOR_AXIS_MASK;
770
771 if (text_horizontal_anchor == UI_AXIS_ANCHOR_CENTER)
772 {
773 draw_x -= text_width >> 1;
774 text_height = bounds_height;
775 }
776 else if (text_horizontal_anchor == UI_AXIS_ANCHOR_END)
777 {
778 draw_x += 1 - text_width;
779 text_height = bounds_height;
780 }
781
782 int32_t text_vertical_anchor = text_control_flags >> 4 & UI_SPRITE_ANCHOR_AXIS_MASK;
783
784 if (text_vertical_anchor == UI_AXIS_ANCHOR_CENTER)
785 draw_y -= text_height >> 1;
786 else if (text_vertical_anchor == UI_AXIS_ANCHOR_END)
787 draw_y += 1 - text_height;
788 }
789 else
790 {
791 draw_x = (int32_t)sprite->bounds.x;
792 draw_y = (int32_t)sprite->bounds.y;
793 bounds_height = (int32_t)sprite->bounds.h;
794 }
795
796 int32_t content_value = sprite->content_value;
797
798 if (content_value == 0x352 && g_currentGameMode == (int32_t)GAME_MODE_SPECIAL_TEXT_LAYOUT)
799 {
800 char (*special_text_scan)[0x14] = text_buffer;
801 int32_t special_text_scan_remaining = UI_UNBOUNDED_SCAN_COUNT;
802
803 while (special_text_scan_remaining)
804 {
805 bool special_text_char_nonzero = 0 != *(uint8_t *)special_text_scan;
806
807 special_text_scan = (char (*)[20])(&((char *)special_text_scan)[1]);
808 special_text_scan_remaining -= 1;
809
810 if (!special_text_char_nonzero)
811 break;
812 }
813
814 text_width = bounds_width;
815 draw_x = (0x20 - (~special_text_scan_remaining - 1)) * 0xa;
816 }
817
819 {
820 char (*render_text_buffer)[0x14] = text_buffer;
821
822 composed_render_flags |= SPRITE_RENDER_COLOR_OVERRIDE;
823
824 char (*saved_render_text_buffer)[0x14] = render_text_buffer;
825
826 if (content_value == 0x453 && draw_y > 0x1d6)
827 draw_y = 0x1e1;
828
829 int32_t rotation_pivot_xy = pivot_y << UI_PACKED_Y_SHIFT | (uint32_t)(uint16_t)pivot_x;
830 int32_t rotation_angle = sprite->rotation_angle_fp12 >> MATH_Q12_SHIFT;
831 int16_t special_text_x_offset = 0;
832
833 if (content_value == 0xa)
834 {
835 content_value = g_currentGameMode;
836
837 if ((content_value == 0x18 || content_value == 0x17) && rotation_pivot_xy == 0x1b60140)
838 {
839 render_text_buffer = (char (*)[20])(String_GetByIndex(0x3eb));
840 saved_render_text_buffer = render_text_buffer;
841 special_text_x_offset = 0x96;
842 }
843 }
844
845 if (scale_text_glyphs)
846 {
847 int32_t scaled_character = (int32_t)*(int8_t *)render_text_buffer;
848 int32_t unscaled_text_advance = 0;
849 char *scaled_text_cursor = &((char *)render_text_buffer)[1];
850
851 while (scaled_character)
852 {
853 struct Material_RuntimeDescriptor *scaled_glyph_descriptor =
854 glyph_descriptor_table[scaled_character - 32];
855
856 sprite_ctx2->textureDescriptor = scaled_glyph_descriptor;
857
858 int32_t scaled_glyph_base_height;
859
860 if (!sprite_ctx2 || !scaled_glyph_descriptor)
861 scaled_glyph_base_height = 1;
862 else
863 {
864 int32_t scaled_glyph_height_minus_one = (uint8_t)(scaled_glyph_descriptor->dimensions_minus_1.height);
865
866 scaled_glyph_base_height = scaled_glyph_height_minus_one + 1;
867 }
868
869 int32_t scaled_glyph_base_width;
870
871 if (!sprite_ctx2 || !scaled_glyph_descriptor)
872 scaled_glyph_base_width = 1;
873 else
874 {
875 int32_t scaled_glyph_width_minus_one = (uint8_t)(scaled_glyph_descriptor->dimensions_minus_1.width);
876
877 scaled_glyph_base_width = scaled_glyph_width_minus_one + 1;
878 }
879
880 int32_t glyph_scale_x = sprite->current_scale.x;
881
882 // Direct Font_RenderGlyph call from sprite UI - PS1-scale colors.
884 (uint32_t)(uint16_t)(((glyph_scale_x * unscaled_text_advance) >> MATH_Q12_SHIFT) + draw_x) | draw_y << UI_PACKED_Y_SHIFT,
885 (uint32_t)(uint16_t)((scaled_glyph_base_width * glyph_scale_x) >> MATH_Q12_SHIFT) | ((scaled_glyph_base_height * sprite->current_scale.y) & UI_FIXED_POINT_INTEGER_MASK) << 4,
886 sprite_ctx2, composed_render_flags, rotation_angle, rotation_pivot_xy);
887
888 struct Material_RuntimeDescriptor *scaled_advance_descriptor;
889
890 if (sprite_ctx2)
891 scaled_advance_descriptor = sprite_ctx2->textureDescriptor;
892
893 int32_t scaled_advance_width;
894
895 if (!sprite_ctx2 || !scaled_advance_descriptor)
896 scaled_advance_width = 1;
897 else
898 {
899 int32_t scaled_advance_width_minus_one = (uint8_t)(scaled_advance_descriptor->dimensions_minus_1.width);
900
901 scaled_advance_width = scaled_advance_width_minus_one + 1;
902 }
903
904 scaled_character = (int32_t)*(int8_t *)scaled_text_cursor;
905 unscaled_text_advance +=
906 (int32_t)((struct Graphics_Render_SpriteContext *)saved_font_ref->material_table_entry)->glyph_advance_adjust + scaled_advance_width;
907 scaled_text_cursor = &scaled_text_cursor[1];
908 }
909 }
910 else
911 {
912 int32_t packed_xy = (uint32_t)(uint16_t)(draw_x - special_text_x_offset) | draw_y << UI_PACKED_Y_SHIFT;
913 int32_t unscaled_character = (int32_t)*(int8_t *)saved_render_text_buffer;
914 char *unscaled_text_cursor = &((char *)saved_render_text_buffer)[1];
915
916 while (unscaled_character)
917 {
918 sprite_ctx2->textureDescriptor =
919 glyph_descriptor_table[unscaled_character - 32];
920
921 uint32_t render_flags = composed_render_flags;
922
923 // Direct Font_RenderGlyph call from sprite UI - PS1-scale colors.
924 Graphics_RenderTexturedSprite(packed_xy, 0, sprite_ctx2, render_flags,
925 rotation_angle, rotation_pivot_xy);
926
927 struct Material_RuntimeDescriptor *texture_descriptor;
928
929 if (sprite_ctx2)
930 texture_descriptor = sprite_ctx2->textureDescriptor;
931
932 int32_t unscaled_advance_width;
933
934 if (!sprite_ctx2 || !texture_descriptor)
935 unscaled_advance_width = 1;
936 else
937 {
938 int32_t unscaled_advance_width_minus_one = (uint8_t)(texture_descriptor->dimensions_minus_1.width);
939
940 unscaled_advance_width = unscaled_advance_width_minus_one + 1;
941 }
942
943 packed_xy +=
944 (int32_t)((struct Graphics_Render_SpriteContext *)saved_font_ref->material_table_entry)->glyph_advance_adjust + unscaled_advance_width;
945 unscaled_character = (int32_t)*(int8_t *)unscaled_text_cursor;
946 unscaled_text_cursor = &unscaled_text_cursor[1];
947 }
948 }
949 }
950 }
951 else
952 {
953 bounds_height = 1;
954 bounds_width = 1;
955 }
956 }
957
958 tree = sort_tree;
959 sprite->bounds.x = (uint16_t)draw_x;
960 sprite->bounds.y = (uint16_t)draw_y;
961 sprite->bounds.w = (uint16_t)bounds_width;
962 sprite->bounds.h = (uint16_t)bounds_height;
963 }
964
965 Tree_DestroyMap(tree);
966}
967
968// Original pcdogs.exe: 0x4424d0
969// Compares sprite depth keys as unsigned 16-bit values and returns their signed difference.
970int32_t UI_CompareSpriteDepth(struct PKG_SpriteEntry *const *lhs, struct PKG_SpriteEntry *const *rhs)
971{
972 int32_t left_sort_key = (*lhs)->sort_key;
973 int32_t right_sort_key = (*rhs)->sort_key;
974
975 return left_sort_key - right_sort_key;
976}
struct Tree_Map * Tree_CreateMap(int32_t payload_size, int32_t(*compare_func)(void *, void *))
Definition tree.c:264
void * Tree_AllocateMapNode(struct Tree_Map *tree)
Definition tree.c:244
void * Tree_GetFirstMapNode(struct Tree_Map *tree)
Definition tree.c:197
void Tree_DestroyMap(struct Tree_Map *tree)
Definition tree.c:280
void Tree_InitializeMapNode(struct Tree_Map *tree, void *node_payload)
Definition tree.c:10
void Tree_FreeMapNode(void *node_payload)
Definition tree.c:256
#define GAME_OVERLAY_SKIP_NODE_ANIMATION
Definition constants.h:34
@ GAME_MODE_SPECIAL_TEXT_LAYOUT
Definition enums.h:50
#define Game_FrameOverlayFlags
Definition globals.h:44
#define g_currentGameMode
Definition globals.h:74
#define SPRITE_RENDER_COLOR_OVERRIDE
Definition constants.h:32
#define SPRITE_RENDER_EXPLICIT_COLOR
Definition constants.h:33
#define GRAPHICS_COLOR_RGB_MASK
Definition constants.h:38
void Graphics_ProcessMeshCommands(struct Mesh_CmdList *cmd_list, struct Material_Node **material_nodes, void *owner_context)
Definition meshes.c:70
void Graphics_UpdateMeshCommandState(struct Mesh_CmdList *cmd_list)
Definition meshes.c:109
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 MATH_Q12_SHIFT
Definition constants.h:8
#define MATH_Q12_ONE
Definition constants.h:9
int32_t Math_SinQ14(int32_t angle)
int32_t Math_EaseInOut(int32_t t_q12, int32_t ease_in_q12, int32_t ease_out_q12)
#define Shared_FontDataCursor
Definition globals.h:111
#define pkg_resource_current_level_data
Definition globals.h:117
int32_t String_FormatCRTString(char *buffer, char const *format,...)
Definition formatting.c:18
struct Math_ColorRGB8 color_mod
Definition types.h:575
struct Material_RuntimeDescriptor * textureDescriptor
Definition types.h:570
struct PKG_SpriteEntry * sprite_list
Definition types.h:57
Definition types.h:219
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
uint8_t b
Definition types.h:40
uint8_t g
Definition types.h:39
uint8_t r
Definition types.h:38
int32_t b
Definition types.h:148
int32_t a
Definition types.h:147
int32_t min
Definition types.h:242
int32_t max
Definition types.h:243
int16_t w
Definition types.h:268
int16_t y
Definition types.h:267
int16_t x
Definition types.h:266
int16_t h
Definition types.h:269
uint8_t width
Definition types.h:347
uint8_t height
Definition types.h:348
int16_t x
Definition types.h:402
int16_t y
Definition types.h:403
int32_t y
Definition types.h:425
int32_t x
Definition types.h:424
struct Math_Vec2I32 current_scale
Definition types.h:1026
int32_t rotation_start_angle
Definition types.h:1022
uint8_t state_flags
Definition types.h:1008
struct Animation_FrameData * anim_frames1
Definition types.h:986
struct Graphics_Render_SpriteContext * sprite_ctx1
Definition types.h:985
struct Math_EasePairI32 color_ease
Definition types.h:1003
struct Math_RangeI32 color_frame_range
Definition types.h:1002
struct Math_Vec2I16 move_start
Definition types.h:991
uint8_t inheritance_control_flags
Definition types.h:980
struct Math_RangeI32 scale_frame_range
Definition types.h:997
struct Math_EasePairI32 rotation_ease
Definition types.h:1025
struct Math_Vec2I16 move_target
Definition types.h:992
uint8_t current_color_reserved_high
Definition types.h:1016
uint16_t control_flags
Definition types.h:975
struct Math_Vec2I32 scale_target
Definition types.h:996
struct Material_Entry * texture_db1
Definition types.h:984
struct Material_Entry * texture_db2
Definition types.h:987
uint8_t type
Definition types.h:970
struct Math_ScreenPointI16 screen
Definition types.h:1027
struct Math_Vec2I32 scale_start
Definition types.h:995
uint8_t anchor_code
Definition types.h:1007
int32_t content_value
Definition types.h:990
int32_t rotation_angle_fp12
Definition types.h:1004
int32_t rotation_target_angle
Definition types.h:1023
int32_t color_target_word
Definition types.h:1001
struct Math_EasePairI32 move_ease
Definition types.h:994
struct Math_EasePairI32 scale_ease
Definition types.h:998
uint32_t current_color_word
Definition types.h:1019
uint8_t link_index
Definition types.h:1006
struct Math_ColorRGB8 color_start
Definition types.h:999
struct Math_RectI16 bounds
Definition types.h:1005
uint16_t sort_key
Definition types.h:1028
uint8_t layer_index
Definition types.h:971
struct Math_RangeI32 rotation_frame_range
Definition types.h:1024
struct Math_RangeI32 move_frame_range
Definition types.h:993
struct Graphics_Render_SpriteContext * sprite_ctx2
Definition types.h:988
struct Math_ColorRGB8 current_color
Definition types.h:1015
char * String_GetRuntimeByNegativeIndex(int32_t negative_string_id)
Definition strings.c:34
char * String_GetByIndex(int32_t string_id)
Definition strings.c:66
static uint32_t glyph_width(const struct Material_TableEntry *material)
Definition text.c:168
#define UI_ROTATION_ANGLE_MASK
Definition constants.h:72
#define UI_PACKED_BLEND_MODE_MASK
Definition constants.h:19
@ UI_ANCHOR_BOTTOM_RIGHT
Definition constants.h:50
@ UI_ANCHOR_CENTER_LEFT
Definition constants.h:51
@ UI_ANCHOR_CENTER
Definition constants.h:55
@ UI_ANCHOR_CENTER_RIGHT
Definition constants.h:53
@ UI_ANCHOR_BOTTOM_CENTER
Definition constants.h:54
@ UI_ANCHOR_BOTTOM_LEFT
Definition constants.h:49
@ UI_ANCHOR_TOP_CENTER
Definition constants.h:52
@ UI_ANCHOR_TOP_RIGHT
Definition constants.h:48
#define UI_PACKED_COORDINATE_LOW_MASK
Definition constants.h:69
@ UI_SPRITE_NUMBER
Definition constants.h:62
@ UI_SPRITE_IMAGE
Definition constants.h:60
#define UI_FIXED_POINT_INTEGER_MASK
Definition constants.h:71
@ UI_AXIS_ANCHOR_CENTER
Definition constants.h:104
@ UI_AXIS_ANCHOR_END
Definition constants.h:105
#define UI_SPRITE_CONTROL_DRAW_OVERLAY
Definition constants.h:24
#define UI_SPRITE_NO_PARENT_POSITION
Definition constants.h:25
#define UI_SPRITE_STATE_LIST_END
Definition constants.h:20
#define UI_SPRITE_NO_PARENT_BLEND
Definition constants.h:28
#define UI_SPRITE_NO_PARENT_VISIBILITY
Definition constants.h:27
#define UI_SPRITE_NO_PARENT_ROTATION_HIGH_BYTE
Definition constants.h:29
#define UI_SPRITE_STATE_VISIBLE
Definition constants.h:22
#define UI_PACKED_Y_SHIFT
Definition constants.h:93
#define UI_SPRITE_NO_PARENT_DEPTH_HIGH_BYTE
Definition constants.h:31
#define UI_SPRITE_STATE_CACHED
Definition constants.h:21
#define UI_MATERIAL_NODE_INDEX_MASK
Definition constants.h:18
#define UI_SPRITE_NO_PARENT_SCALE
Definition constants.h:26
#define UI_SPRITE_CONTROL_VISIBLE
Definition constants.h:23
#define UI_SPRITE_INTERPOLATION_NEGATE_MASK
Definition constants.h:96
#define UI_SPRITE_NO_PARENT_COLOR_HIGH_BYTE
Definition constants.h:30
#define UI_SPRITE_ANCHOR_AXIS_MASK
Definition constants.h:32
#define UI_UNBOUNDED_SCAN_COUNT
Definition constants.h:97
int32_t UI_CompareSpriteDepth(struct PKG_SpriteEntry *const *lhs, struct PKG_SpriteEntry *const *rhs)
Definition sprites.c:970
void UI_UpdateAndRenderSprites()
Definition sprites.c:30
int32_t UI_CompareSpriteDepth(struct PKG_SpriteEntry *const *lhs, struct PKG_SpriteEntry *const *rhs)
Definition sprites.c:970
#define g_frameCounter
Definition values.h:501