Path: blob/master/RSDKv5/RSDK/Graphics/Animation.cpp
1162 views
#include "RSDK/Core/RetroEngine.hpp"12using namespace RSDK;34#if RETRO_REV0U5#include "Legacy/AnimationLegacy.cpp"6#endif78SpriteAnimation RSDK::spriteAnimationList[SPRFILE_COUNT];910uint16 RSDK::LoadSpriteAnimation(const char *filePath, uint8 scope)11{12if (!scope || scope > SCOPE_STAGE)13return -1;1415char fullFilePath[0x100];16sprintf_s(fullFilePath, sizeof(fullFilePath), "Data/Sprites/%s", filePath);1718RETRO_HASH_MD5(hash);19GEN_HASH_MD5(filePath, hash);2021for (int32 i = 0; i < SPRFILE_COUNT; ++i) {22if (HASH_MATCH_MD5(spriteAnimationList[i].hash, hash))23return i;24}2526uint16 id = -1;27for (id = 0; id < SPRFILE_COUNT; ++id) {28if (spriteAnimationList[id].scope == SCOPE_NONE)29break;30}3132if (id >= SPRFILE_COUNT)33return -1;3435char nameBuffer[0x8][0x20];36uint8 sheetIDs[0x18];37sheetIDs[0] = 0;3839FileInfo info;40InitFileInfo(&info);41if (LoadFile(&info, fullFilePath, FMODE_RB)) {42uint32 sig = ReadInt32(&info, false);4344if (sig != RSDK_SIGNATURE_SPR) {45CloseFile(&info);46return -1;47}4849SpriteAnimation *spr = &spriteAnimationList[id];50spr->scope = scope;51memcpy(spr->hash, hash, 4 * sizeof(uint32));5253uint32 frameCount = ReadInt32(&info, false);54AllocateStorage((void **)&spr->frames, frameCount * sizeof(SpriteFrame), DATASET_STG, false);5556uint8 sheetCount = ReadInt8(&info);57for (int32 s = 0; s < sheetCount; ++s) {58ReadString(&info, fullFilePath);59sheetIDs[s] = LoadSpriteSheet(fullFilePath, scope);60}6162uint8 hitboxCount = ReadInt8(&info);63for (int32 h = 0; h < hitboxCount; ++h) {64ReadString(&info, nameBuffer[h]);65}6667spr->animCount = ReadInt16(&info);68AllocateStorage((void **)&spr->animations, spr->animCount * sizeof(SpriteAnimationEntry), DATASET_STG, false);6970int32 frameID = 0;71for (int32 a = 0; a < spr->animCount; ++a) {72SpriteAnimationEntry *animation = &spr->animations[a];73ReadString(&info, textBuffer);74GEN_HASH_MD5_BUFFER(textBuffer, animation->hash);7576animation->frameCount = ReadInt16(&info);77animation->frameListOffset = frameID;78animation->animationSpeed = ReadInt16(&info);79animation->loopIndex = ReadInt8(&info);80animation->rotationStyle = ReadInt8(&info);8182for (int32 f = 0; f < animation->frameCount; ++f) {83SpriteFrame *frame = &spr->frames[frameID++];8485frame->sheetID = sheetIDs[ReadInt8(&info)];86frame->duration = ReadInt16(&info);87frame->unicodeChar = ReadInt16(&info);88frame->sprX = ReadInt16(&info);89frame->sprY = ReadInt16(&info);90frame->width = ReadInt16(&info);91frame->height = ReadInt16(&info);92frame->pivotX = ReadInt16(&info);93frame->pivotY = ReadInt16(&info);9495frame->hitboxCount = hitboxCount;96for (int32 h = 0; h < hitboxCount; ++h) {97frame->hitboxes[h].left = ReadInt16(&info);98frame->hitboxes[h].top = ReadInt16(&info);99frame->hitboxes[h].right = ReadInt16(&info);100frame->hitboxes[h].bottom = ReadInt16(&info);101}102}103}104105CloseFile(&info);106107return id;108}109110return -1;111}112113uint16 RSDK::CreateSpriteAnimation(const char *filename, uint32 frameCount, uint32 animCount, uint8 scope)114{115if (!scope || scope > SCOPE_STAGE)116return -1;117118char fullFilePath[0x100];119sprintf_s(fullFilePath, sizeof(fullFilePath), "Data/Sprites/%s", filename);120121RETRO_HASH_MD5(hash);122GEN_HASH_MD5(filename, hash);123124for (int32 i = 0; i < SPRFILE_COUNT; ++i) {125if (HASH_MATCH_MD5(spriteAnimationList[i].hash, hash)) {126return i;127}128}129130uint16 id = -1;131for (id = 0; id < SPRFILE_COUNT; ++id) {132if (spriteAnimationList[id].scope == SCOPE_NONE)133break;134}135136if (id >= SPRFILE_COUNT)137return -1;138139SpriteAnimation *spr = &spriteAnimationList[id];140spr->scope = scope;141memcpy(spr->hash, hash, 4 * sizeof(uint32));142143AllocateStorage((void **)&spr->frames, sizeof(SpriteFrame) * MIN(frameCount, SPRITEFRAME_COUNT), DATASET_STG, true);144AllocateStorage((void **)&spr->animations, sizeof(SpriteAnimationEntry) * MIN(animCount, SPRITEANIM_COUNT), DATASET_STG, true);145146return id;147}148149void RSDK::ProcessAnimation(Animator *animator)150{151if (!animator || !animator->frames)152return;153154animator->timer += animator->speed;155156if (animator->frames == (SpriteFrame *)1) { // model anim157while (animator->timer > animator->frameDuration) {158++animator->frameID;159160animator->timer -= animator->frameDuration;161if (animator->frameID >= animator->frameCount)162animator->frameID = animator->loopIndex;163}164}165else { // sprite anim166while (animator->timer > animator->frameDuration) {167++animator->frameID;168169animator->timer -= animator->frameDuration;170if (animator->frameID >= animator->frameCount)171animator->frameID = animator->loopIndex;172173animator->frameDuration = animator->frames[animator->frameID].duration;174}175}176}177178int32 RSDK::GetStringWidth(uint16 aniFrames, uint16 animID, String *string, int32 startIndex, int32 length, int32 spacing)179{180if (aniFrames >= SPRFILE_COUNT || !string || !string->chars)181return 0;182183SpriteAnimation *spr = &spriteAnimationList[aniFrames];184if (animID < spr->animCount) {185SpriteAnimationEntry *anim = &spr->animations[animID];186187startIndex = CLAMP(startIndex, 0, string->length - 1);188189if (length <= 0 || length > string->length)190length = string->length;191192int32 w = 0;193for (int32 c = startIndex; c < length; ++c) {194int32 charFrame = string->chars[c];195if (charFrame < anim->frameCount) {196w += spr->frames[anim->frameListOffset + charFrame].width;197if (c + 1 >= length)198return w;199200w += spacing;201}202}203204return w;205}206207return 0;208}209210void RSDK::SetSpriteString(uint16 aniFrames, uint16 animID, String *string)211{212if (aniFrames >= SPRFILE_COUNT || !string)213return;214215SpriteAnimation *spr = &spriteAnimationList[aniFrames];216if (animID < spr->animCount) {217SpriteAnimationEntry *anim = &spr->animations[animID];218219for (int32 c = 0; c < string->length; ++c) {220int32 unicodeChar = string->chars[c];221string->chars[c] = -1;222for (int32 f = 0; f < anim->frameCount; ++f) {223if (spr->frames[anim->frameListOffset + f].unicodeChar == unicodeChar) {224string->chars[c] = f;225break;226}227}228}229}230}231232233