-
Notifications
You must be signed in to change notification settings - Fork 2
/
IconLoader.c
executable file
·187 lines (160 loc) · 9.77 KB
/
IconLoader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "IconLoader.h"
#define ITOF10(x) ((float)(x) / 1024.0f)
#define ITOF12(x) ((float)(x) / 4096.0f)
#define FTOI10(x) ((short)((x)*1024.0f))
#define FTOI12(x) ((short)((x)*4096.0f))
static inline int DecompressRLEData(void *output, unsigned int OutputBufferLength, const void *PackedData)
{
const void *PackedDataPayload;
unsigned int PackedDataLength, offset;
short int *pInput, BlockLength;
unsigned short int *pOutput, i;
PackedDataLength = *(unsigned int *)PackedData;
PackedDataPayload = (void *)((unsigned int)PackedData + 4);
for (offset = 0, pInput = (short int *)PackedDataPayload, pOutput = (unsigned short int *)output; offset < PackedDataLength && offset < OutputBufferLength;) {
BlockLength = *pInput++;
offset += 2;
if (BlockLength > 0) {
for (i = 0; i < BlockLength; i++, pOutput++)
*pOutput = *pInput;
pInput++;
offset += sizeof(unsigned short);
} else {
BlockLength = -BlockLength;
memcpy(pOutput, pInput, BlockLength * sizeof(unsigned short));
pOutput += BlockLength;
pInput += BlockLength;
offset += BlockLength * sizeof(unsigned short);
}
}
return 0;
}
int LoadPS2IconModel(const char *path, struct PS2IconModel *IconModel)
{
int result, size;
unsigned int vertex, i, sequence, TextureLength;
FILE *IconFile;
void *buffer;
struct IconFileVersionHeader *VersionHeader;
struct IconFileModelSectionHeader *ModelSectionHeader;
struct Vector *PointVector;
void *IconFileDataOffset;
struct IconFileModelAnimSectionHeader *AnimSectionHeader;
struct IconFileModelAnimSequence *AnimSequence;
struct IconFileModelAnimFrame *AnimeFrame;
memset(IconModel, 0, sizeof(struct PS2IconModel));
result = 0;
if ((IconFile = fopen(path, "rb")) != NULL) {
fseek(IconFile, 0, SEEK_END);
size = ftell(IconFile);
rewind(IconFile);
if ((buffer = malloc(size)) != NULL) {
result = fread(buffer, 1, size, IconFile);
}
fclose(IconFile);
if (buffer != NULL) {
if (result == size) {
VersionHeader = (struct IconFileVersionHeader *)buffer;
if (VersionHeader->version == ICON_FILE_VERSION) {
ModelSectionHeader = (struct IconFileModelSectionHeader *)((unsigned int)buffer + sizeof(struct IconFileVersionHeader));
// printf("LoadIconModel: attrib: 0x%08x, nbvtx: %u, nbsp: %u\n", ModelSectionHeader->attrib, ModelSectionHeader->nbvtx, ModelSectionHeader->nbsp);
memcpy(&IconModel->ModelSectionHeader, ModelSectionHeader, sizeof(struct IconFileModelSectionHeader));
IconModel->ModelVertices = (struct IconModelVertex *)malloc(ModelSectionHeader->nbvtx * sizeof(struct IconModelVertex));
IconFileDataOffset = (void *)((unsigned int)ModelSectionHeader + sizeof(struct IconFileModelSectionHeader));
for (vertex = 0; vertex < ModelSectionHeader->nbvtx; vertex++) {
// Start with the points of the vertex.
IconModel->ModelVertices[vertex].vtx = malloc(ModelSectionHeader->nbsp * sizeof(struct Vector));
for (i = 0; i < ModelSectionHeader->nbsp; i++) {
PointVector = &IconModel->ModelVertices[vertex].vtx[i];
PointVector->x = ITOF10(((unsigned short int *)IconFileDataOffset)[0]);
PointVector->y = ITOF10(((unsigned short int *)IconFileDataOffset)[1]);
PointVector->z = ITOF10(((unsigned short int *)IconFileDataOffset)[2]);
IconFileDataOffset = (void *)((unsigned int)IconFileDataOffset + 8);
}
IconModel->ModelVertices[vertex].normal[0] = ITOF12(((unsigned short int *)IconFileDataOffset)[0]);
IconModel->ModelVertices[vertex].normal[1] = ITOF12(((unsigned short int *)IconFileDataOffset)[1]);
IconModel->ModelVertices[vertex].normal[2] = ITOF12(((unsigned short int *)IconFileDataOffset)[2]);
IconFileDataOffset = (void *)((unsigned int)IconFileDataOffset + 8);
IconModel->ModelVertices[vertex].st[0] = ITOF12(((unsigned short int *)IconFileDataOffset)[0]);
IconModel->ModelVertices[vertex].st[1] = ITOF12(((unsigned short int *)IconFileDataOffset)[1]);
IconFileDataOffset = (void *)((unsigned int)IconFileDataOffset + 4);
memcpy(IconModel->ModelVertices[vertex].color, IconFileDataOffset, 4);
IconFileDataOffset = (void *)((unsigned int)IconFileDataOffset + 4);
}
AnimSectionHeader = (struct IconFileModelAnimSectionHeader *)IconFileDataOffset;
IconFileDataOffset = (void *)((unsigned int)IconFileDataOffset + sizeof(struct IconFileModelAnimSectionHeader));
IconModel->ModelAnimSequences = (struct IconModelAnimSequence *)malloc(AnimSectionHeader->nbseq * sizeof(struct IconModelAnimSequence));
memcpy(&IconModel->ModelAnimSectionHeader, AnimSectionHeader, sizeof(struct IconFileModelAnimSectionHeader));
// printf("AnimSectionHeader->nbseq: %d\n", AnimSectionHeader->nbseq);
for (sequence = 0; sequence < AnimSectionHeader->nbseq; sequence++) {
AnimSequence = (struct IconFileModelAnimSequence *)IconFileDataOffset;
IconModel->ModelAnimSequences[sequence].nbframe = AnimSequence->nbframe;
IconModel->ModelAnimSequences[sequence].speed = AnimSequence->speed;
IconModel->ModelAnimSequences[sequence].offset = AnimSequence->offset;
IconModel->ModelAnimSequences[sequence].nbksp = AnimSequence->nbksp;
IconModel->ModelAnimSequences[sequence].frames = (struct IconModelAnimFrame *)malloc(AnimSequence->nbksp * sizeof(struct IconModelAnimFrame));
// printf("AnimSequence->nbksp: %d\n", AnimSequence->nbksp);
IconFileDataOffset = (void *)((unsigned int)IconFileDataOffset + sizeof(struct IconFileModelAnimSequence));
for (i = 0; i < AnimSequence->nbksp; i++) {
AnimeFrame = (struct IconFileModelAnimFrame *)IconFileDataOffset;
IconModel->ModelAnimSequences[sequence].frames[i].kspid = AnimeFrame->kspid;
IconModel->ModelAnimSequences[sequence].frames[i].nbkf = AnimeFrame->nbkf;
IconModel->ModelAnimSequences[sequence].frames[i].keys = (struct IconFileModelAnimFrameKeyData *)malloc(AnimSequence->nbksp * sizeof(struct IconFileModelAnimFrameKeyData));
memcpy(IconModel->ModelAnimSequences[sequence].frames[i].keys, (void *)((unsigned int)AnimeFrame + sizeof(struct IconFileModelAnimFrame)), AnimSequence->nbksp * sizeof(struct IconFileModelAnimFrameKeyData));
IconFileDataOffset = (void *)((unsigned int)IconFileDataOffset + 8 + sizeof(struct IconFileModelAnimFrameKeyData) * AnimSequence->nbksp);
}
}
// Texture loading.
TextureLength = size - ((unsigned int)IconFileDataOffset - (unsigned int)buffer);
if (ModelSectionHeader->attrib & ICON_MODEL_ATTR_RLE) {
IconModel->texture = memalign(64, 32768); // 2*TextureLength);
result = DecompressRLEData(IconModel->texture, 32768, IconFileDataOffset);
} else {
// if(TextureLength==32768){
if (TextureLength > 32768)
TextureLength = 32768;
IconModel->texture = memalign(64, 32768); // Should be 128x128x16-bit=32768
memcpy(IconModel->texture, IconFileDataOffset, TextureLength);
result = 0;
/* }
else{
printf("TextureLength: %d\n", TextureLength);
result=-1;
} */
}
} else
result = -1; // Unsupported version.
} else
result = EIO;
free(buffer);
}
} else
result = ENOENT;
return result;
}
void UnloadPS2IconModel(struct PS2IconModel *IconModel)
{
unsigned int i, vertex, sequence;
for (vertex = 0; vertex < IconModel->ModelSectionHeader.nbvtx; vertex++) {
if (IconModel->ModelVertices[vertex].vtx != NULL)
free(IconModel->ModelVertices[vertex].vtx);
}
if (IconModel->ModelVertices != NULL)
free(IconModel->ModelVertices);
for (sequence = 0; sequence < IconModel->ModelAnimSectionHeader.nbseq; sequence++) {
for (i = 0; i < IconModel->ModelAnimSequences[sequence].nbksp; i++) {
if (IconModel->ModelAnimSequences[sequence].frames[i].keys != NULL)
free(IconModel->ModelAnimSequences[sequence].frames[i].keys);
}
if (IconModel->ModelAnimSequences[sequence].frames != NULL)
free(IconModel->ModelAnimSequences[sequence].frames);
}
if (IconModel->ModelAnimSequences != NULL)
free(IconModel->ModelAnimSequences);
if (IconModel->texture != NULL)
free(IconModel->texture);
}