102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
resources.c
Go to the documentation of this file.
1#include "pcdogs/game/enums.h"
7
8// Original pcdogs.exe: 0x42dcd0
9int32_t Material_FindIndex(struct Material_SectionHeader *section, struct Material_TableEntry *entry, int32_t node_type)
10{
11 if (!section)
13
14 uint32_t count = (int32_t)section->node_count;
15 for (uint32_t i = 0; i < count; ++i)
16 {
17 const struct Material_Node *node = &section->node_table[i];
18 if (node->material == entry && node->node_type == node_type)
19 {
20 uint32_t mode = node_type ? node->controller_flags & MATERIAL_CONTROLLER_MODE_MASK : 0;
21 return (int32_t)((mode << MATERIAL_CONTROLLER_MODE_SHIFT) | i);
22 }
23 }
25}
26
27// Original pcdogs.exe: 0x42dd30
28int32_t Material_BuildStructure(struct Material_SectionHeader *section, struct Material_RefEntry *ref, struct Material_FrameSet *frame_set)
29{
30 uint32_t nodes[MATERIAL_MAX_FRAME_LAYERS];
31 int count = 0;
32 ref->material_section = section;
33 if (ref->material_table_entry)
34 {
35 for (int type = 0; type < MATERIAL_MAX_FRAME_LAYERS; ++type)
36 {
37 uint32_t index = Material_FindIndex(section, ref->material_table_entry, type);
39 nodes[count++] = index;
40 }
41 }
42 if (!count)
43 {
44 ref->frame_set = NULL;
45 return 0;
46 }
47
48 size_t bytes = sizeof(*frame_set) + count * (sizeof(*frame_set->frame_ptr_array) + sizeof(struct Material_FrameData));
49 if (!frame_set)
50 frame_set = Pkg_AllocateResourceMemory(bytes);
51 ref->frame_set = frame_set;
52 frame_set->reserved_00 = 0;
53 frame_set->frame_count = (int16_t)count;
54 frame_set->frame_ptr_array = (struct Material_FrameData **)(frame_set + 1);
55 struct Material_FrameData *frames = (struct Material_FrameData *)(frame_set->frame_ptr_array + count);
56 for (int i = 0; i < count; ++i)
57 {
58 frame_set->frame_ptr_array[i] = &frames[i];
59 frames[i] = (struct Material_FrameData){.material_node_ref_packed = nodes[i]};
60 }
61 return (int32_t)(bytes / sizeof(uint32_t));
62}
63
64// Original pcdogs.exe: 0x43ca10
65void Material_LoadAndFixup(struct Material_SectionHeaderRaw *section, uint8_t *texture_data)
66{
67 uint32_t base = (uintptr_t)section;
68 section->material_entries_offset += base;
70 for (int i = 0; i < section->material_entry_count; ++i)
71 {
72 uint32_t link = entries[i].next_material_index_1based;
73 if (link)
74 entries[i].next_material_index_1based = (uintptr_t)&entries[link - 1];
75 }
76
77 if (section->node_table_offset)
78 {
79 section->node_table_offset += base;
80 struct Material_NodeFixupView *node = (struct Material_NodeFixupView *)section->node_table_offset;
81 uint32_t count = (int32_t)section->node_count;
82 do
83 {
84 if (node->material_index_or_ptr)
85 node->material_index_or_ptr = (uintptr_t)&entries[node->material_index_or_ptr - 1];
87 node->animation_offset_or_ptr += base;
89 node->frame_table_offset_or_ptr += base;
90 ++node;
91 } while (--count);
92 }
93 Material_LoadAllEntries((struct Material_SectionLoadView *)section, texture_data);
94}
95
96// Original pcdogs.exe: 0x43e990
97void Material_MarkReferencedByParent(struct Material_SectionHeader *section, struct Material_TableEntry *entry, struct Material_RuntimeDescriptor *parent, int32_t count)
98{
99 if (count <= 0)
100 return;
101 struct Material_RuntimeDescriptor *descriptor = entry->texture_descriptor;
102 for (int i = 0; i < count; ++i)
103 {
104 const struct Material_TableEntry *candidate = &section->material_entries[i];
105 if (candidate->texture_descriptor == descriptor && (candidate->pixel_format_flags & MATERIAL_PIXEL_SHARED_REFERENCE))
106 {
108 break;
109 }
110 if (candidate->texture_descriptor == parent && (candidate->flags & MATERIAL_SHARED_TEXTURE))
112 }
113}
114
115static void relocate_pixel_data(struct Material_RuntimeDescriptor *descriptor, uint8_t *base)
116{
117 if (descriptor->pixel_data)
118 descriptor->pixel_data = base + (uintptr_t)descriptor->pixel_data;
119 if (descriptor->palette)
120 descriptor->palette = (uint16_t *)(base + (uintptr_t)descriptor->palette);
121}
122
123// Original pcdogs.exe: 0x43ea90
124void Material_LoadAllEntries(struct Material_SectionLoadView *section, uint8_t *texture_base)
125{
126 struct Material_TableEntry *entry = section->material_entries;
127 for (int i = 0; i < section->material_entry_count; ++i, ++entry)
128 {
129 if (!entry->texture_descriptor)
130 continue;
131 struct Material_RuntimeDescriptor *descriptor = (struct Material_RuntimeDescriptor *)((uintptr_t)section + (uintptr_t)entry->texture_descriptor);
132 entry->texture_descriptor = descriptor;
134 Material_MarkSharedTextureReference(section, entry, i);
135 else
136 {
137 relocate_pixel_data(descriptor, texture_base);
140 }
141 }
142
143 struct Material_Node *node = section->node_table;
144 if (node)
145 {
146 uint32_t count = (int32_t)section->node_count;
147 do
148 {
150 {
151 unsigned frame = 0;
152 do
153 {
154 uint32_t offset = (uintptr_t)node->frame_table[frame];
155 if (offset)
156 {
157 struct Material_RuntimeDescriptor *descriptor = (struct Material_RuntimeDescriptor *)((uint8_t *)section + offset);
158 node->frame_table[frame] = descriptor;
159 if (!(descriptor->flags_low & MATERIAL_DESCRIPTOR_PROCESSED))
160 {
161 node->material->texture_descriptor = descriptor;
162 relocate_pixel_data(descriptor, texture_base);
165 }
166 }
167 } while (++frame <= node->anim_record[5]);
168 }
169 ++node;
170 } while (--count);
171 }
172
175}
@ GAME_MODE_COLLISION_CHECK
Definition enums.h:10
#define g_currentGameMode
Definition globals.h:74
#define MATERIAL_REFERENCE_SHARED_DESCRIPTOR
Definition constants.h:13
@ MATERIAL_NODE_TEXTURE_FRAMES
Definition constants.h:45
#define MATERIAL_CONTROLLER_MODE_SHIFT
Definition constants.h:20
#define MATERIAL_SHARED_TEXTURE
Definition constants.h:14
#define MATERIAL_NODE_REFERENCE_NONE
Definition constants.h:18
#define MATERIAL_DESCRIPTOR_PROCESSED
Definition constants.h:10
#define MATERIAL_MAX_FRAME_LAYERS
Definition constants.h:21
#define MATERIAL_CONTROLLER_MODE_MASK
Definition constants.h:19
#define MATERIAL_PIXEL_SHARED_REFERENCE
Definition constants.h:12
void Material_MarkSharedTextureReference(struct Material_SectionLoadView *section, struct Material_TableEntry *entry, int32_t entry_index)
Definition textures.c:405
void Material_MarkNodeTextureReferences(struct Material_SectionHeader *section)
Definition textures.c:419
void Material_LoadTexture(struct Material_TableEntry *material_entry)
Definition textures.c:237
static void relocate_pixel_data(struct Material_RuntimeDescriptor *descriptor, uint8_t *base)
Definition resources.c:115
void Material_LoadAllEntries(struct Material_SectionLoadView *section, uint8_t *texture_base)
Definition resources.c:124
int32_t Material_FindIndex(struct Material_SectionHeader *section, struct Material_TableEntry *entry, int32_t node_type)
Definition resources.c:9
void Material_MarkReferencedByParent(struct Material_SectionHeader *section, struct Material_TableEntry *entry, struct Material_RuntimeDescriptor *parent, int32_t count)
Definition resources.c:97
void Material_LoadAndFixup(struct Material_SectionHeaderRaw *section, uint8_t *texture_data)
Definition resources.c:65
int32_t Material_BuildStructure(struct Material_SectionHeader *section, struct Material_RefEntry *ref, struct Material_FrameSet *frame_set)
Definition resources.c:28
void * Pkg_AllocateResourceMemory(uint32_t size)
Definition resources.c:317
uint32_t material_node_ref_packed
Definition types.h:51
struct Material_FrameData ** frame_ptr_array
Definition types.h:63
uint16_t reserved_00
Definition types.h:61
int16_t frame_count
Definition types.h:62
uint32_t material_index_or_ptr
Definition types.h:102
uint32_t frame_table_offset_or_ptr
Definition types.h:107
uint32_t animation_offset_or_ptr
Definition types.h:106
uint8_t controller_flags
Definition types.h:88
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 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
int16_t material_entry_count
Definition types.h:167
uint32_t node_table_offset
Definition types.h:162
uint32_t material_entries_offset
Definition types.h:165
struct Material_Node * node_table
Definition types.h:146
struct Material_TableEntry * material_entries
Definition types.h:149
struct Material_TableEntry * material_entries
Definition types.h:181
struct Material_Node * node_table
Definition types.h:178
int16_t material_entry_count
Definition types.h:183
int32_t next_material_index_1based
Definition types.h:397
uint8_t pixel_format_flags
Definition types.h:348
struct Material_RuntimeDescriptor * texture_descriptor
Definition types.h:353
uint32_t flags
Definition types.h:342
uint8_t texture_reference_flags
Definition types.h:347