102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
controller.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
12#include "pcdogs/math/types.h"
13#include <string.h>
14
15// Original pcdogs.exe: 0x40d220
16// Tests visibility at a sample time using a constant threshold or the selected key's high bit. Constant thresholds use signed comparison, while keyed visibility times exclude the visibility bit.
17uint8_t Animation_CheckKeyframeActive(uint32_t sample_time, struct Animation_SplineChannelVis *channel)
18{
20
21 if ((uintptr_t)keys == UINT32_MAX)
22 return (int32_t)sample_time >= (int32_t)channel->packed_time_bias_and_end_time;
23
24 uint32_t timing = channel->packed_time_bias_and_end_time;
25 uint32_t time = sample_time + ((timing & ANIMATION_TIME_BIAS_MASK) << 6);
26 int last = channel->keyframe_count - 1;
27 uint32_t packed = keys[last].timing_and_visibility.packed;
28 uint32_t last_time = (packed >> 14) & ANIMATION_VISIBILITY_TIME_Q6_MASK;
29
30 if (time >= last_time)
31 {
33 packed = keys[0].timing_and_visibility.packed;
34 }
35 else if (last > 3)
36 {
37 int lo = 1, hi = last;
38
39 do
40 {
41 int mid = (lo + hi) >> 1;
42
43 if (time < ((keys[mid].timing_and_visibility.packed >> 14) & ANIMATION_VISIBILITY_TIME_Q6_MASK))
44 hi = mid - 1;
45 else
46 lo = mid + 1;
47 } while (lo <= hi);
48
49 packed = keys[hi].timing_and_visibility.packed;
50 }
51 else
52 {
53 int index = 0;
54
55 packed = keys[0].timing_and_visibility.packed;
56
57 if (time >= ((packed >> 14) & ANIMATION_VISIBILITY_TIME_Q6_MASK))
58 {
59 while (time >= ((keys[index + 1].timing_and_visibility.packed >> 14) & ANIMATION_VISIBILITY_TIME_Q6_MASK))
60 ++index;
61
62 packed = keys[index].timing_and_visibility.packed;
63 }
64 }
65
66 return (uint8_t)(packed >> 31);
67}
68
69// Original pcdogs.exe: 0x40e470
70// Appends a state to the global ten-entry animation queue without checking capacity or recording an actor identity. The eleventh append aliases the queue count, and further appends overwrite adjacent storage; callers must drain a bounded queue for the intended next actor before teardown.
71void Animation_QueueStateChange(int32_t anim_state)
72{
73 int32_t index = g_animationStateQueueCount;
74 g_animQueuedStateChange[index] = anim_state;
76}
77
78static void scroll_texture_axis(struct Material_Node *node, int32_t frame, bool vertical)
79{
80 struct Material_TableEntry *material = node->material;
81 if (!material || !material->texture_descriptor)
82 return;
83
84 const struct Math_SizeU8 *size = &material->texture_descriptor->dimensions_minus_1;
85 uint32_t half_extent = ((vertical ? size->height : size->width) + 1) / 2;
86 if (half_extent)
87 {
88 uint32_t packed = material->packed_uv_scroll;
89 unsigned shift = vertical ? 8 : 0;
90 uint32_t speed = ((packed >> (16 + shift)) & 0xff) + 1;
91 int32_t offset = (int32_t)(speed * frame * ANIMATION_UV_SCROLL_FRAME_SCALE) >> MATH_Q12_SHIFT;
92 material->packed_uv_scroll = packed & ~(0xffu << shift);
93 node->material->packed_uv_scroll |= ((uint32_t)offset % half_extent) << shift;
94 }
95}
96
97static uint8_t blend_color_byte(uint8_t start, uint8_t end, int32_t weight)
98{
99 return (uint8_t)(start + (((end - start) * weight) >> MATH_Q12_SHIFT));
100}
101
102static void update_material_color(struct Material_Node *node, uint32_t frame)
103{
104 const struct Material_ColorControllerRecord *record = (const struct Material_ColorControllerRecord *)node->anim_record;
105 struct Math_ColorRGB8 *color = &node->material->color_adjust;
106 uint32_t hold = record->hold_frames;
107 uint32_t duration = hold + record->transition_frames;
109 frame %= duration;
111 {
112 frame %= 2 * duration;
113 if (frame > duration)
114 frame = 2 * duration - frame;
115 }
116
117 if (frame <= hold || frame >= duration)
118 {
119 const struct Math_ColorRGB8 *endpoint = frame <= hold ? &record->start_color : &record->end_color;
120 color->r = endpoint->r;
121 color->g = endpoint->g;
122 color->b = endpoint->b;
123 return;
124 }
125
126 int32_t weight = (frame - hold) * MATH_Q12_ONE / record->transition_frames;
127 color->r = blend_color_byte(record->start_color.r, record->end_color.r, weight);
128 color->g = blend_color_byte(record->start_color.g, record->end_color.g, weight);
129 color->b = blend_color_byte(record->start_color.b, record->end_color.b, weight);
130}
131
132// Original pcdogs.exe: 0x413090
134 int32_t frame_count_q12)
135{
136 uint32_t packed = *state;
137 if (packed == ANIM_CTRL_STATE_DISABLED)
138 return true;
139
140 unsigned slot = packed & ANIM_CTRL_STATE_SLOT_MASK;
141 struct Material_Node *node = &(*material_nodes)[slot];
142 int32_t frame = frame_count_q12 >> MATH_Q12_SHIFT;
143 switch (node->node_type)
144 {
146 scroll_texture_axis(node, frame, false);
147 scroll_texture_axis(node, frame, true);
148 return true;
150 update_material_color(node, (uint32_t)frame);
151 return true;
153 break;
154 default:
155 return true;
156 }
157
158 const struct PCDOGS_FrameControllerPrefix *frames = (const struct PCDOGS_FrameControllerPrefix *)node->anim_record;
159 uint32_t position = (uint32_t)(((int64_t)frames->frameRateQ16 * frame) >> 16);
160 uint32_t last = frames->maxFrameIndex;
161 uint32_t modes = packed & ANIM_CTRL_STATE_MODE_MASK;
162 uint32_t period = (modes & ANIM_CTRL_STATE_PINGPONG) ? last * 2 : last + 1;
163 if ((modes & ANIM_CTRL_STATE_ONCE) && position >= period)
164 return false;
165
166 uint32_t phase = position % period;
167 uint32_t selected = phase;
168 uint32_t random_frame = 0;
169 if (modes & ANIM_CTRL_STATE_REVERSE)
170 selected = last - phase;
171 else if (modes & ANIM_CTRL_STATE_PINGPONG)
172 {
173 if (phase > last)
174 selected = 2 * last - phase;
175 }
176 else if ((modes & ANIM_CTRL_STATE_RANDOM) && phase != ((packed & ANIM_CTRL_STATE_PHASE_MASK) >> 9))
177 {
178 uint32_t previous = (packed & ANIM_CTRL_STATE_RANDOM_FRAME_MASK) >> 17;
179 do
180 random_frame = Math_GenerateRandom(period);
181 while (random_frame == previous);
182 selected = random_frame;
183 }
184 node->material->texture_descriptor = node->frame_table[selected];
185 *state = modes | (random_frame << 17) | (phase << 9) | slot;
186 return true;
187}
188
189static int16_t blend_color_delta(int16_t previous, int16_t next, int32_t weight)
190{
191 int32_t complement = (int32_t)((uint32_t)MATH_Q12_ONE - weight);
192 return (int16_t)((((int64_t)complement * next) >> MATH_Q12_SHIFT) +
193 (((int64_t)weight * previous) >> MATH_Q12_SHIFT));
194}
195
197 int32_t frame, bool per_vertex)
198{
199 const struct Animation_VertexColorKeyframe *next = keys;
200 // Per-vertex offsets may move before frame zero. Shared time compares unsigned.
201 if (per_vertex)
202 {
203 while (next->frame_number < frame)
204 ++next;
205 }
206 else
207 {
208 while ((uint32_t)(int32_t)next->frame_number < (uint32_t)frame)
209 ++next;
210 }
211
212 if (next == keys || next->frame_number == frame)
213 return next->delta;
214
215 int32_t weight = (int32_t)((uint32_t)(next->frame_number - frame) * next->inv_frame_delta_q12);
216 const struct Math_ColorRGBI16 *previous = &(next - 1)->delta;
217 return (struct Math_ColorRGBI16){
218 blend_color_delta(previous->r, next->delta.r, weight),
219 blend_color_delta(previous->g, next->delta.g, weight),
220 blend_color_delta(previous->b, next->delta.b, weight)};
221}
222
223static uint8_t clamp_color(int channel)
224{
225 if (channel < 0)
226 return 0;
227 return channel > ANIMATION_COLOR_COMPONENT_MAX ? ANIMATION_COLOR_COMPONENT_MAX : (uint8_t)channel;
228}
229
230// Original pcdogs.exe: 0x413440
232{
233 unsigned count = controller->keyframe_count;
234 uint32_t vertices_left = (int32_t)controller->vertex_count;
235 if (!count || !vertices_left)
236 return;
237
239 const struct Animation_VertexColorKeyframe *keys = controller->keyframe_array;
241 {
242 memcpy(local_keys, keys, count * sizeof(*keys));
243 keys = local_keys;
244 }
245
246 const struct Animation_VertexColorTarget *target = controller->target_array;
247 struct Mesh_RuntimeVertex *vertices = mesh->scene_vertex_data;
248 int32_t frame = controller->sample_time_q12 >> MATH_Q12_SHIFT;
249 int duration = keys[count - 1].frame_number;
250 unsigned mode = controller->flags & ANIMATION_VERTEX_COLOR_TIME_MODE_MASK;
251 bool per_vertex = count > 1 && duration && mode > ANIMATION_VERTEX_COLOR_REFLECT;
252 struct Math_ColorRGBI16 delta = keys[0].delta;
253 if (count > 1 && duration && !per_vertex)
254 {
255 if (mode == ANIMATION_VERTEX_COLOR_REFLECT && frame > duration)
256 frame = 2 * duration - frame;
257 delta = sample_vertex_color(keys, frame, false);
258 }
259
260 for (; vertices_left; --vertices_left, ++target)
261 {
262 if (per_vertex)
263 {
264 int sample = frame + target->frame_offset;
265 if (sample >= duration)
266 sample -= duration;
267 delta = sample_vertex_color(keys, sample, true);
268 }
269
270 struct Math_ColorRGB8 *color = &vertices[target->vertex_index].color;
271 int red = target->base_color.r + delta.r;
272 int green = target->base_color.g + delta.g;
273 int blue = target->base_color.b + delta.b;
274 color->b = clamp_color(blue);
275 color->r = clamp_color(red);
276 color->g = clamp_color(green);
277 }
278}
#define ANIMATION_SPLINE_REPEAT_ENDPOINT
Definition constants.h:31
#define ANIMATION_LOCAL_COLOR_KEY_CAPACITY
Definition constants.h:51
#define ANIMATION_PLAYBACK_PINGPONG
Definition constants.h:28
@ ANIMATION_VERTEX_COLOR_REFLECT
Definition constants.h:18
#define ANIMATION_COLOR_COMPONENT_MAX
Definition constants.h:52
#define ANIMATION_UV_SCROLL_FRAME_SCALE
Definition constants.h:44
#define ANIMATION_SPLINE_TIME_Q6_MASK
Definition constants.h:37
#define ANIMATION_VERTEX_COLOR_TIME_MODE_MASK
Definition constants.h:36
#define ANIMATION_TIME_BIAS_MASK
Definition constants.h:39
@ ANIMATION_CONTROLLER_UV_SCROLL
Definition constants.h:9
@ ANIMATION_CONTROLLER_FRAMES
Definition constants.h:11
@ ANIMATION_CONTROLLER_COLOR
Definition constants.h:10
#define ANIMATION_PLAYBACK_ONCE
Definition constants.h:30
#define ANIMATION_VISIBILITY_TIME_Q6_MASK
Definition constants.h:38
#define ANIM_CTRL_STATE_ONCE
Definition enums.h:35
#define ANIM_CTRL_STATE_MODE_MASK
Definition enums.h:38
#define ANIM_CTRL_STATE_RANDOM_FRAME_MASK
Definition enums.h:23
#define ANIM_CTRL_STATE_PINGPONG
Definition enums.h:29
#define ANIM_CTRL_STATE_SLOT_MASK
Definition enums.h:17
#define ANIM_CTRL_STATE_RANDOM
Definition enums.h:32
uint32_t Animation_PackedControllerState
Definition enums.h:14
#define ANIM_CTRL_STATE_PHASE_MASK
Definition enums.h:20
#define ANIM_CTRL_STATE_DISABLED
Definition enums.h:41
#define ANIMATION_SPLINE_WRAP_SAMPLE_TIME
Definition enums.h:55
#define ANIM_CTRL_STATE_REVERSE
Definition enums.h:26
#define g_animationStateQueueCount
Definition globals.h:25
void Animation_ProcessVertexColor(struct Animation_VertexColorController *controller, struct Mesh_NodeExtended *mesh)
Definition controller.c:231
bool Animation_ProcessController(struct Material_Node **material_nodes, Animation_PackedControllerState *state, int32_t frame_count_q12)
Definition controller.c:133
static void scroll_texture_axis(struct Material_Node *node, int32_t frame, bool vertical)
Definition controller.c:78
static void update_material_color(struct Material_Node *node, uint32_t frame)
Definition controller.c:102
static uint8_t clamp_color(int channel)
Definition controller.c:223
static uint8_t blend_color_byte(uint8_t start, uint8_t end, int32_t weight)
Definition controller.c:97
void Animation_QueueStateChange(int32_t anim_state)
Definition controller.c:71
static int16_t blend_color_delta(int16_t previous, int16_t next, int32_t weight)
Definition controller.c:189
static struct Math_ColorRGBI16 sample_vertex_color(const struct Animation_VertexColorKeyframe *keys, int32_t frame, bool per_vertex)
Definition controller.c:196
uint8_t Animation_CheckKeyframeActive(uint32_t sample_time, struct Animation_SplineChannelVis *channel)
Definition controller.c:17
#define MATH_Q12_SHIFT
Definition constants.h:8
#define MATH_Q12_ONE
Definition constants.h:9
uint32_t Math_GenerateRandom(uint32_t range)
int32_t packed_time_bias_and_end_time
Definition types.h:396
Animation_SplineChannelFlags channel_flags
Definition types.h:400
struct Animation_VisibilityKeyframe * keyframes_or_minus_one_sentinel
Definition types.h:395
struct Animation_VertexColorKeyframe * keyframe_array
Definition types.h:443
Mesh_CommandFlags flags
Definition types.h:434
struct Animation_VertexColorTarget * target_array
Definition types.h:442
struct Math_ColorRGBI16 delta
Definition types.h:854
struct Math_ColorRGB8 base_color
Definition types.h:868
union Animation_VisibilityTiming timing_and_visibility
Definition types.h:1202
struct Math_ColorRGB8 start_color
Definition types.h:194
struct Math_ColorRGB8 end_color
Definition types.h:196
uint8_t controller_flags
Definition types.h:88
uint8_t * anim_record
Definition types.h:91
struct Material_TableEntry * material
Definition types.h:86
uint8_t node_type
Definition types.h:87
struct Material_RuntimeDescriptor ** frame_table
Definition types.h:92
struct Math_SizeU8 dimensions_minus_1
Definition types.h:264
struct Material_RuntimeDescriptor * texture_descriptor
Definition types.h:353
uint32_t packed_uv_scroll
Definition types.h:372
struct Math_ColorRGB8 color_adjust
Definition types.h:375
uint8_t b
Definition types.h:40
uint8_t g
Definition types.h:39
uint8_t r
Definition types.h:38
int16_t b
Definition types.h:103
int16_t g
Definition types.h:102
int16_t r
Definition types.h:101
uint8_t width
Definition types.h:347
uint8_t height
Definition types.h:348
struct Mesh_RuntimeVertex * scene_vertex_data
Definition types.h:996
struct Math_ColorRGB8 color
Definition types.h:1126
#define g_animQueuedStateChange
Definition values.h:174