Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Rubberduckycooly
GitHub Repository: Rubberduckycooly/RSDKv5-Decompilation
Path: blob/master/RSDKv5/RSDK/Graphics/Animation.cpp
1162 views
1
#include "RSDK/Core/RetroEngine.hpp"
2
3
using namespace RSDK;
4
5
#if RETRO_REV0U
6
#include "Legacy/AnimationLegacy.cpp"
7
#endif
8
9
SpriteAnimation RSDK::spriteAnimationList[SPRFILE_COUNT];
10
11
uint16 RSDK::LoadSpriteAnimation(const char *filePath, uint8 scope)
12
{
13
if (!scope || scope > SCOPE_STAGE)
14
return -1;
15
16
char fullFilePath[0x100];
17
sprintf_s(fullFilePath, sizeof(fullFilePath), "Data/Sprites/%s", filePath);
18
19
RETRO_HASH_MD5(hash);
20
GEN_HASH_MD5(filePath, hash);
21
22
for (int32 i = 0; i < SPRFILE_COUNT; ++i) {
23
if (HASH_MATCH_MD5(spriteAnimationList[i].hash, hash))
24
return i;
25
}
26
27
uint16 id = -1;
28
for (id = 0; id < SPRFILE_COUNT; ++id) {
29
if (spriteAnimationList[id].scope == SCOPE_NONE)
30
break;
31
}
32
33
if (id >= SPRFILE_COUNT)
34
return -1;
35
36
char nameBuffer[0x8][0x20];
37
uint8 sheetIDs[0x18];
38
sheetIDs[0] = 0;
39
40
FileInfo info;
41
InitFileInfo(&info);
42
if (LoadFile(&info, fullFilePath, FMODE_RB)) {
43
uint32 sig = ReadInt32(&info, false);
44
45
if (sig != RSDK_SIGNATURE_SPR) {
46
CloseFile(&info);
47
return -1;
48
}
49
50
SpriteAnimation *spr = &spriteAnimationList[id];
51
spr->scope = scope;
52
memcpy(spr->hash, hash, 4 * sizeof(uint32));
53
54
uint32 frameCount = ReadInt32(&info, false);
55
AllocateStorage((void **)&spr->frames, frameCount * sizeof(SpriteFrame), DATASET_STG, false);
56
57
uint8 sheetCount = ReadInt8(&info);
58
for (int32 s = 0; s < sheetCount; ++s) {
59
ReadString(&info, fullFilePath);
60
sheetIDs[s] = LoadSpriteSheet(fullFilePath, scope);
61
}
62
63
uint8 hitboxCount = ReadInt8(&info);
64
for (int32 h = 0; h < hitboxCount; ++h) {
65
ReadString(&info, nameBuffer[h]);
66
}
67
68
spr->animCount = ReadInt16(&info);
69
AllocateStorage((void **)&spr->animations, spr->animCount * sizeof(SpriteAnimationEntry), DATASET_STG, false);
70
71
int32 frameID = 0;
72
for (int32 a = 0; a < spr->animCount; ++a) {
73
SpriteAnimationEntry *animation = &spr->animations[a];
74
ReadString(&info, textBuffer);
75
GEN_HASH_MD5_BUFFER(textBuffer, animation->hash);
76
77
animation->frameCount = ReadInt16(&info);
78
animation->frameListOffset = frameID;
79
animation->animationSpeed = ReadInt16(&info);
80
animation->loopIndex = ReadInt8(&info);
81
animation->rotationStyle = ReadInt8(&info);
82
83
for (int32 f = 0; f < animation->frameCount; ++f) {
84
SpriteFrame *frame = &spr->frames[frameID++];
85
86
frame->sheetID = sheetIDs[ReadInt8(&info)];
87
frame->duration = ReadInt16(&info);
88
frame->unicodeChar = ReadInt16(&info);
89
frame->sprX = ReadInt16(&info);
90
frame->sprY = ReadInt16(&info);
91
frame->width = ReadInt16(&info);
92
frame->height = ReadInt16(&info);
93
frame->pivotX = ReadInt16(&info);
94
frame->pivotY = ReadInt16(&info);
95
96
frame->hitboxCount = hitboxCount;
97
for (int32 h = 0; h < hitboxCount; ++h) {
98
frame->hitboxes[h].left = ReadInt16(&info);
99
frame->hitboxes[h].top = ReadInt16(&info);
100
frame->hitboxes[h].right = ReadInt16(&info);
101
frame->hitboxes[h].bottom = ReadInt16(&info);
102
}
103
}
104
}
105
106
CloseFile(&info);
107
108
return id;
109
}
110
111
return -1;
112
}
113
114
uint16 RSDK::CreateSpriteAnimation(const char *filename, uint32 frameCount, uint32 animCount, uint8 scope)
115
{
116
if (!scope || scope > SCOPE_STAGE)
117
return -1;
118
119
char fullFilePath[0x100];
120
sprintf_s(fullFilePath, sizeof(fullFilePath), "Data/Sprites/%s", filename);
121
122
RETRO_HASH_MD5(hash);
123
GEN_HASH_MD5(filename, hash);
124
125
for (int32 i = 0; i < SPRFILE_COUNT; ++i) {
126
if (HASH_MATCH_MD5(spriteAnimationList[i].hash, hash)) {
127
return i;
128
}
129
}
130
131
uint16 id = -1;
132
for (id = 0; id < SPRFILE_COUNT; ++id) {
133
if (spriteAnimationList[id].scope == SCOPE_NONE)
134
break;
135
}
136
137
if (id >= SPRFILE_COUNT)
138
return -1;
139
140
SpriteAnimation *spr = &spriteAnimationList[id];
141
spr->scope = scope;
142
memcpy(spr->hash, hash, 4 * sizeof(uint32));
143
144
AllocateStorage((void **)&spr->frames, sizeof(SpriteFrame) * MIN(frameCount, SPRITEFRAME_COUNT), DATASET_STG, true);
145
AllocateStorage((void **)&spr->animations, sizeof(SpriteAnimationEntry) * MIN(animCount, SPRITEANIM_COUNT), DATASET_STG, true);
146
147
return id;
148
}
149
150
void RSDK::ProcessAnimation(Animator *animator)
151
{
152
if (!animator || !animator->frames)
153
return;
154
155
animator->timer += animator->speed;
156
157
if (animator->frames == (SpriteFrame *)1) { // model anim
158
while (animator->timer > animator->frameDuration) {
159
++animator->frameID;
160
161
animator->timer -= animator->frameDuration;
162
if (animator->frameID >= animator->frameCount)
163
animator->frameID = animator->loopIndex;
164
}
165
}
166
else { // sprite anim
167
while (animator->timer > animator->frameDuration) {
168
++animator->frameID;
169
170
animator->timer -= animator->frameDuration;
171
if (animator->frameID >= animator->frameCount)
172
animator->frameID = animator->loopIndex;
173
174
animator->frameDuration = animator->frames[animator->frameID].duration;
175
}
176
}
177
}
178
179
int32 RSDK::GetStringWidth(uint16 aniFrames, uint16 animID, String *string, int32 startIndex, int32 length, int32 spacing)
180
{
181
if (aniFrames >= SPRFILE_COUNT || !string || !string->chars)
182
return 0;
183
184
SpriteAnimation *spr = &spriteAnimationList[aniFrames];
185
if (animID < spr->animCount) {
186
SpriteAnimationEntry *anim = &spr->animations[animID];
187
188
startIndex = CLAMP(startIndex, 0, string->length - 1);
189
190
if (length <= 0 || length > string->length)
191
length = string->length;
192
193
int32 w = 0;
194
for (int32 c = startIndex; c < length; ++c) {
195
int32 charFrame = string->chars[c];
196
if (charFrame < anim->frameCount) {
197
w += spr->frames[anim->frameListOffset + charFrame].width;
198
if (c + 1 >= length)
199
return w;
200
201
w += spacing;
202
}
203
}
204
205
return w;
206
}
207
208
return 0;
209
}
210
211
void RSDK::SetSpriteString(uint16 aniFrames, uint16 animID, String *string)
212
{
213
if (aniFrames >= SPRFILE_COUNT || !string)
214
return;
215
216
SpriteAnimation *spr = &spriteAnimationList[aniFrames];
217
if (animID < spr->animCount) {
218
SpriteAnimationEntry *anim = &spr->animations[animID];
219
220
for (int32 c = 0; c < string->length; ++c) {
221
int32 unicodeChar = string->chars[c];
222
string->chars[c] = -1;
223
for (int32 f = 0; f < anim->frameCount; ++f) {
224
if (spr->frames[anim->frameListOffset + f].unicodeChar == unicodeChar) {
225
string->chars[c] = f;
226
break;
227
}
228
}
229
}
230
}
231
}
232
233