Mercurial > might-and-magic-trilogy
diff Sprites.cpp @ 0:8b8875f5b359
Initial commit
author | Nomad |
---|---|
date | Fri, 05 Oct 2012 16:07:14 +0200 |
parents | |
children | 51a5b0dc3f30 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Sprites.cpp Fri Oct 05 16:07:14 2012 +0200 @@ -0,0 +1,733 @@ +#include <string.h> + +#include "Allocator.h" +#include "Sprites.h" +#include "PaletteManager.h" +#include "LOD.h" +#include "FrameTableInc.h" + +#include "mm7_data.h" + + + + +extern int (__cdecl *sprintfex)(char *a1, const char *a2, ...); + +struct SpriteFrameTable *pSpriteFrameTable; + + + + +//----- (0044D4D8) -------------------------------------------------------- +void SpriteFrameTable::ReleaseSFrames() +{ + SpriteFrameTable *v1; // esi@1 + + v1 = this; + if ( this->pSpriteSFrames ) + { + pAllocator->FreeChunk(this->pSpriteSFrames); + v1->pSpriteSFrames = 0; + } + v1->uNumSpriteFrames = 0; +} + +//----- (0044D4F6) -------------------------------------------------------- +void SpriteFrameTable::ResetSomeSpriteFlags() +{ + int v1; // esi@1 + signed int i; // edx@1 + char *v3; // eax@2 + + v1 = 0; + for ( i = 0; i < (signed int)this->uNumSpriteFrames; ++i ) + { + v3 = (char *)&this->pSpriteSFrames[v1].uFlags; + ++v1; + *v3 &= 0x7Fu; + } +} + +//----- (0044D513) -------------------------------------------------------- +void SpriteFrameTable::InitializeSprite(unsigned int uSpriteID) +{ + //SpriteFrameTable *v2; // esi@1 + unsigned int v3; // ebx@3 + //char *v4; // edx@3 + //int v5; // eax@3 + SpriteFrame *v6; // ecx@5 + int v7; // eax@5 + __int16 v8; // ax@6 + //signed int v9; // edx@6 + //int v10; // ecx@6 + signed int v11; // edi@10 + __int16 v12; // ax@16 + int v13; // ecx@16 + size_t v14; // eax@19 + signed int v15; // edi@19 + __int16 v16; // ax@27 + int v17; // ecx@27 + signed int v18; // edi@29 + SpriteFrame *v19; // eax@30 + __int16 v20; // ax@45 + int v21; // ecx@45 + const char *v22; // [sp-8h] [bp-50h]@12 + const char *v23; // [sp-8h] [bp-50h]@21 + const char *v24; // [sp-8h] [bp-50h]@37 + const char *v25; // [sp-4h] [bp-4Ch]@12 + char *v26; // [sp-4h] [bp-4Ch]@21 + const char *v27; // [sp-4h] [bp-4Ch]@37 + char v28[3]; // [sp+9h] [bp-3Fh]@19 + char Str; // [sp+Ch] [bp-3Ch]@19 + char Size[20]; // [sp+2Ch] [bp-1Ch]@15 + char Source[4]; // [sp+40h] [bp-8h]@19 + int v32; // [sp+44h] [bp-4h]@19 + unsigned int uSpriteIDa; // [sp+50h] [bp+8h]@4 + + //v2 = this; + if ( (signed int)uSpriteID <= (signed int)this->uNumSpriteFrames ) + { + if ( (uSpriteID & 0x80000000u) == 0 ) + { + v3 = uSpriteID; + + auto uFlags = pSpriteSFrames[uSpriteID].uFlags; + if (uFlags & 0x7F) + { + pSpriteSFrames[uSpriteID].uFlags |= 0x80; + + uSpriteIDa = 30 * uSpriteID + 12; + while ( 1 ) + { + pSpriteSFrames[v3].uPaletteIndex = pPaletteManager->LoadPalette(pSpriteSFrames[uSpriteID].uPaletteID); + v6 = &pSpriteSFrames[uSpriteID]; + v7 = v6->uFlags; + if ( v7 & 0x10 ) + { + v8 = pSprites_LOD->LoadSprite(v6->pTextureName, v6->uPaletteID); + + for (uint i = 0; i < 8; ++i) + pSpriteSFrames[v3].pHwSpriteIDs[i] = v8; + goto LABEL_46; + } + if ( v7 & 0x10000 ) + { + v11 = 0; + do + { + switch ( v11 ) + { + case 3: + case 4: + case 5: + v25 = pSpriteSFrames[v3].pTextureName; + v22 = "%s4"; + goto LABEL_15; + case 2: + case 6: + v25 = pSpriteSFrames[v3].pTextureName; + v22 = "%s2"; + goto LABEL_15; + case 0: + case 1: + case 7: + v25 = pSpriteSFrames[v3].pTextureName; + v22 = "%s0"; +LABEL_15: + sprintfex(Size, v22, v25); + break; + default: + break; + } + v12 = pSprites_LOD->LoadSprite(Size, pSpriteSFrames[v3].uPaletteID); + v13 = v11++ + uSpriteIDa; + *(short *)&pSpriteSFrames->pIconName[2 * v13] = v12; + } + while ( v11 < 8 ); + goto LABEL_46; + } + if ( !(v7 & 0x40) ) + break; + strcpy(Source, "stA"); + v32 = 0; + strcpy(&Str, v6->pTextureName); + v14 = strlen(&Str); + strcpy(&v28[v14], Source); + v15 = 0; + do + { + switch ( v15 ) + { + case 0: + v26 = pSpriteSFrames[v3].pTextureName; + v23 = "%s0"; + goto LABEL_26; + case 4: + v26 = &Str; + v23 = "%s4"; + goto LABEL_26; + case 3: + case 5: + v26 = &Str; + v23 = "%s3"; + goto LABEL_26; + case 2: + case 6: + v26 = pSpriteSFrames[v3].pTextureName; + v23 = "%s2"; + goto LABEL_26; + case 1: + case 7: + v26 = pSpriteSFrames[v3].pTextureName; + v23 = "%s1"; +LABEL_26: + sprintfex(Size, v23, v26); + break; + default: + break; + } + v16 = pSprites_LOD->LoadSprite(Size, pSpriteSFrames[v3].uPaletteID); + v17 = v15++ + uSpriteIDa; + *(short *)&pSpriteSFrames->pIconName[2 * v17] = v16; + } + while ( v15 < 8 ); +LABEL_46: + if ( !(pSpriteSFrames[v3].uFlags & 1) ) + return; + uSpriteIDa += 30; + ++v3; + } + v18 = 0; + while ( 1 ) + { + v19 = &pSpriteSFrames[v3]; + if ( !((256 << v18) & v19->uFlags) ) + { + sprintf(Size, "%s%i", v19->pTextureName, v18); + goto LABEL_45; + } + if ( v18 == 1 ) + { + v27 = v19->pTextureName; + v24 = "%s7"; + goto LABEL_42; + } + if ( v18 == 2 ) + { + v27 = v19->pTextureName; + v24 = "%s6"; + goto LABEL_42; + } + if ( v18 == 3 ) + { + v27 = v19->pTextureName; + v24 = "%s5"; + goto LABEL_42; + } + if ( v18 == 5 ) + { + v27 = v19->pTextureName; + v24 = "%s3"; + goto LABEL_42; + } + if ( v18 == 6 ) + break; + if ( v18 == 7 ) + { + v27 = v19->pTextureName; + v24 = "%s1"; +LABEL_42: + sprintf(Size, v24, v27); + } +LABEL_45: + v20 = pSprites_LOD->LoadSprite(Size, pSpriteSFrames[v3].uPaletteID); + v21 = v18++ + uSpriteIDa; + *(short *)&pSpriteSFrames->pIconName[2 * v21] = v20; + if ( v18 >= 8 ) + goto LABEL_46; + } + v27 = v19->pTextureName; + v24 = "%s2"; + goto LABEL_42; + } + } + } +} + +//----- (0044D813) -------------------------------------------------------- +unsigned int SpriteFrameTable::FastFindSprite(char *pSpriteName) +{ + SpriteFrameTable *v2; // esi@1 + int v3; // eax@1 + unsigned int result; // eax@2 + + v2 = this; + BinarySearch(0, this->uNumEFrames, pSpriteName); + v3 = v2->field_8; + if ( v3 < 0 ) + result = 0; + else + result = v2->pSpriteEFrames[v3]; + return result; +} + +//----- (0044D83A) -------------------------------------------------------- +void SpriteFrameTable::BinarySearch(int a2, int a3, const char *pSpriteName) +{ + int v4; // ebx@1 + SpriteFrameTable *v5; // edi@1 + int v6; // esi@2 + int v7; // eax@2 + int a2a; // [sp+14h] [bp+8h]@2 + + v4 = a2; + v5 = this; + while ( 1 ) + { + a2a = a3 - v4; + v6 = (a3 - v4) / 2 + v4; + v7 = _strcmpi(pSpriteName, v5->pSpritePFrames[v6]->pIconName); + if ( !v7 ) + v5->field_8 = v6; + if ( v4 == a3 ) + break; + if ( v7 >= 0 ) + { + if ( a2a <= 4 ) + { + while ( v4 < a3 ) + { + if ( !_strcmpi(pSpriteName, v5->pSpritePFrames[v4]->pIconName) ) + { +LABEL_19: + v5->field_8 = v4; + return; + } + ++v4; + } + break; + } + v4 += (a3 - v4) / 2; + } + else + { + if ( a2a <= 4 ) + { + while ( v4 < a3 ) + { + if ( !_strcmpi(pSpriteName, v5->pSpritePFrames[v4]->pIconName) ) + goto LABEL_19; + ++v4; + } + break; + } + a3 = (a3 - v4) / 2 + v4; + } + } + v5->field_8 = -1; +} + +//----- (0044D8D0) -------------------------------------------------------- +SpriteFrame *SpriteFrameTable::GetFrame(unsigned int uSpriteID, unsigned int uFrameID) +{ + SpriteFrame *v3; // edi@1 + SpriteFrame *v4; // ecx@1 + __int16 v5; // dx@2 + int v6; // edx@3 + unsigned int v7; // eax@3 + char *i; // ecx@3 + int v9; // esi@5 + SpriteFrame *result; // eax@6 + + v3 = this->pSpriteSFrames; + v4 = &v3[uSpriteID]; + if ( v4->uFlags & 1 && (v5 = v4->uAnimLength) != 0 ) + { + v6 = ((signed int)uFrameID >> 3) % v5; + v7 = uSpriteID; + for ( i = (char *)&v4->uAnimTime; ; i += 60 ) + { + v9 = *(short *)i; + if ( v6 <= v9 ) + break; + v6 -= v9; + ++v7; + } + result = &v3[v7]; + } + else + { + result = &v3[uSpriteID]; + } + return result; +} + +//----- (0044D91F) -------------------------------------------------------- +SpriteFrame *SpriteFrameTable::GetFrameBy_x(unsigned int uSpriteID, signed int a3) +{ + SpriteFrame *v3; // edi@1 + SpriteFrame *v4; // esi@1 + __int16 v5; // ax@2 + int v6; // ecx@3 + int v7; // edx@3 + unsigned int v8; // eax@3 + int v9; // ecx@3 + char *i; // edx@3 + int v11; // esi@5 + SpriteFrame *result; // eax@6 + + v3 = this->pSpriteSFrames; + v4 = &v3[uSpriteID]; + if ( v4->uFlags & 1 && (v5 = v4->uAnimLength) != 0 ) + { + v6 = v5; + v7 = a3 % v5; + v8 = uSpriteID; + v9 = v6 - v7; + for ( i = (char *)&v4->uAnimTime; ; i += 60 ) + { + v11 = *(short *)i; + if ( v9 <= v11 ) + break; + v9 -= v11; + ++v8; + } + result = &v3[v8]; + } + else + { + result = &v3[uSpriteID]; + } + return result; +} + +//----- (0044D96D) -------------------------------------------------------- +void SpriteFrameTable::ToFile() +{ + SpriteFrameTable *v1; // esi@1 + FILE *v2; // eax@1 + FILE *v3; // edi@1 + + v1 = this; + v2 = fopen("data\\dsft.bin", "wb"); + v3 = v2; + if ( !v2 ) + Abortf("Unable to save dsft.bin!"); + fwrite(v1, 4u, 1u, v2); + fwrite(&v1->uNumEFrames, 4u, 1u, v3); + fwrite(v1->pSpriteSFrames, 0x3Cu, v1->uNumSpriteFrames, v3); + fwrite(v1->pSpriteEFrames, 2u, v1->uNumEFrames, v3); + fclose(v3); +} + +//----- (0044D9D7) -------------------------------------------------------- +void SpriteFrameTable::FromFile(void *pSerialized) +{ + //v2 = this; + uNumSpriteFrames = *(int *)pSerialized; + uNumEFrames = *((int *)pSerialized + 1); + //memcpy(this, pSerialized, 4u); + //memcpy(&v2->field_4, (char *)pSerialized + 4, 4u); + pSpriteSFrames = (SpriteFrame *)pAllocator->AllocNamedChunk(pSpriteSFrames, 60 * uNumSpriteFrames, "S Frames"); + pSpriteEFrames = (__int16 *)pAllocator->AllocNamedChunk(pSpriteEFrames, 2 * uNumSpriteFrames, "E Frames"); + //v3 = pAllocator->AllocNamedChunk(pSpritePFrames, 4 * uNumSpriteFrames, "P Frames"); + //v4 = v2->uNumSpriteFrames; + pSpritePFrames = (SpriteFrame **)pAllocator->AllocNamedChunk(pSpritePFrames, 4 * uNumSpriteFrames, "P Frames"); + auto uSpriteFramesSize = 60 * uNumSpriteFrames; + memcpy(pSpriteSFrames, (char *)pSerialized + 8, uSpriteFramesSize); + memcpy(pSpriteEFrames, (char *)pSerialized + uSpriteFramesSize + 8, 2 * uNumEFrames); + for (uint i = 0; i < uNumSpriteFrames; ++i) + pSpritePFrames[i] = &pSpriteSFrames[pSpriteEFrames[i]]; +} + +//----- (0044DA92) -------------------------------------------------------- +bool SpriteFrameTable::FromFileTxt(const char *Args) +{ + SpriteFrameTable *v2; // ebx@1 + FILE *v3; // eax@1 + unsigned int v4; // esi@3 + signed int result; // eax@10 + FILE *v6; // ST18_4@11 + char *i; // eax@11 + const char *v8; // ST20_4@14 + __int16 v9; // ax@14 + const char *v10; // ST0C_4@14 + double v11; // st7@14 + int v12; // eax@14 + const char *v13; // ST04_4@14 + __int16 v14; // ax@14 + const char *v15; // ST00_4@14 + int v16; // eax@14 + int v17; // eax@17 + int v18; // eax@23 + int v19; // eax@24 + int v20; // eax@25 + int v21; // eax@28 + int v22; // eax@29 + int j; // edi@30 + const char *v24; // esi@31 + int v25; // eax@32 + int v26; // edx@53 + int v27; // ecx@54 + int v28; // eax@55 + signed int k; // edx@58 + SpriteFrame *v30; // ecx@59 + int v31; // esi@59 + int l; // eax@60 + signed int v33; // eax@65 + int v34; // edi@66 + int v35; // esi@66 + SpriteFrame **v36; // eax@69 + int v37; // ecx@69 + SpriteFrame *v38; // edx@69 + __int16 *v39; // eax@69 + int v40; // ecx@69 + char Buf[500]; // [sp+Ch] [bp-2F0h]@3 + FrameTableTxtLine v42; // [sp+200h] [bp-FCh]@4 + FrameTableTxtLine v43; // [sp+27Ch] [bp-80h]@4 + FILE *File; // [sp+2F8h] [bp-4h]@1 + unsigned int Argsa; // [sp+304h] [bp+8h]@3 + int Argsb; // [sp+304h] [bp+8h]@59 + FILE *Argsc; // [sp+304h] [bp+8h]@67 + + v2 = this; + ReleaseSFrames(); + v3 = fopen(Args, "r"); + File = v3; + if ( !v3 ) + Abortf("CSpriteFrameTable::load - Unable to open file: %s.", Args); + v4 = 0; + Argsa = 0; + if ( fgets(Buf, 490, v3) ) + { + do + { + *strchr(Buf, '\n') = 0; + memcpy(&v43, frame_table_txt_parser(Buf, &v42), sizeof(v43)); + if ( v43.field_0 && *v43.pProperties[0] != '/' ) + ++Argsa; + } + while ( fgets(Buf, 490, File) ); + v4 = Argsa; + } + v2->uNumSpriteFrames = v4; + v2->pSpriteSFrames = (SpriteFrame *)pAllocator->AllocNamedChunk(v2->pSpriteSFrames, 60 * v4, "S Frames"); + v2->pSpriteEFrames = (__int16 *)pAllocator->AllocNamedChunk( + v2->pSpriteEFrames, + 2 * v2->uNumSpriteFrames, + "E Frames"); + v2->pSpritePFrames = (SpriteFrame **)pAllocator->AllocNamedChunk( + v2->pSpritePFrames, + 4 * v2->uNumSpriteFrames, + "P Frames"); + if ( v2->pSpriteSFrames ) + { + v6 = File; + v2->uNumSpriteFrames = 0; + fseek(v6, 0, 0); + for ( i = fgets(Buf, 490, File); i; i = fgets(Buf, 490, File) ) + { + *strchr(Buf, 10) = 0; + memcpy(&v43, frame_table_txt_parser(Buf, &v42), sizeof(v43)); + if ( v43.field_0 && *v43.pProperties[0] != '/' ) + { + v8 = v43.pProperties[0]; + v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags = 0; + v2->pSpriteSFrames[v2->uNumSpriteFrames].uPaletteIndex = 0; + strcpy(v2->pSpriteSFrames[v2->uNumSpriteFrames].pIconName, v8); + strcpy(v2->pSpriteSFrames[v2->uNumSpriteFrames].pTextureName, v43.pProperties[1]); + v9 = atoi(v43.pProperties[3]); + v10 = v43.pProperties[4]; + v2->pSpriteSFrames[v2->uNumSpriteFrames].uPaletteID = v9; + v11 = atof(v10) * 65536.0; + v12 = abs((signed __int64)v11); + v13 = v43.pProperties[5]; + v2->pSpriteSFrames[v2->uNumSpriteFrames].scale = v12; + v14 = atoi(v13); + v15 = v43.pProperties[6]; + v2->pSpriteSFrames[v2->uNumSpriteFrames].uGlowRadius = v14; + v2->pSpriteSFrames[v2->uNumSpriteFrames].uAnimTime = atoi(v15); + v16 = (int)&v2->pSpriteSFrames[v2->uNumSpriteFrames]; + if ( *(short *)(v16 + 48) ) + *(int *)(v16 + 44) |= 2u; + if ( !_strcmpi(v43.pProperties[2], "new") ) + { + v17 = (int)&v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags; + *(int *)v17 |= 4u; + v2->pSpritePFrames[v2->uNumEFrames] = &v2->pSpriteSFrames[v2->uNumSpriteFrames]; + v2->pSpriteEFrames[v2->uNumEFrames++] = LOWORD(v2->uNumSpriteFrames); + } + if ( !_strcmpi(v43.pProperties[10], "1") ) + BYTE2(v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags) |= 4u; + if ( !_strcmpi(v43.pProperties[11], "1") ) + BYTE2(v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags) |= 2u; + if ( !_strcmpi(v43.pProperties[12], "1") ) + { + v18 = (int)&v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags; + *(int *)v18 |= 0x20u; + } + v19 = atoi(v43.pProperties[7]) - 1; + if ( v19 ) + { + v20 = v19 - 2; + if ( v20 ) + { + if ( v20 == 2 ) + BYTE1(v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags) |= 0xE0u; + } + else + { + v21 = (int)&v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags; + *(int *)v21 |= 0x1E000u; + } + } + else + { + v22 = (int)&v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags; + *(int *)v22 |= 0x10u; + } + for ( j = 13; j < v43.field_0; ++j ) + { + v24 = v43.pProperties[j]; + if ( _strcmpi(v43.pProperties[j], "Luminous") ) + { + if ( _strcmpi(v24, "Mirror0") ) + { + if ( _strcmpi(v24, "Mirror1") ) + { + if ( _strcmpi(v24, "Mirror2") ) + { + if ( _strcmpi(v24, "Mirror3") ) + { + if ( _strcmpi(v24, "Mirror4") ) + { + if ( _strcmpi(v24, "Mirror5") ) + { + if ( _strcmpi(v24, "Mirror6") ) + { + if ( !_strcmpi(v24, "Mirror7") ) + BYTE1(v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags) |= 0x80u; + } + else + { + BYTE1(v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags) |= 0x40u; + } + } + else + { + BYTE1(v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags) |= 0x20u; + } + } + else + { + BYTE1(v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags) |= 0x10u; + } + } + else + { + BYTE1(v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags) |= 8u; + } + } + else + { + BYTE1(v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags) |= 4u; + } + } + else + { + BYTE1(v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags) |= 2u; + } + } + else + { + BYTE1(v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags) |= 1u; + } + } + else + { + v25 = (int)&v2->pSpriteSFrames[v2->uNumSpriteFrames].uFlags; + *(int *)v25 |= 2u; + } + } + ++v2->uNumSpriteFrames; + } + } + fclose(File); + v26 = 0; + if ( (signed int)(v2->uNumSpriteFrames - 1) > 0 ) + { + v27 = 0; + do + { + v28 = (int)&v2->pSpriteSFrames[v27]; + if ( !(*(char *)(v28 + 104) & 4) ) + *(int *)(v28 + 44) |= 1u; + ++v26; + ++v27; + } + while ( v26 < (signed int)(v2->uNumSpriteFrames - 1) ); + } + for ( k = 0; k < (signed int)v2->uNumSpriteFrames; *(short *)(Argsb + 56) = v31 ) + { + v30 = v2->pSpriteSFrames; + Argsb = (int)&v30[k]; + v31 = *(short *)(Argsb + 54); + if ( *(char *)(Argsb + 44) & 1 ) + { + ++k; + for ( l = (int)&v30[k]; *(char *)(l + 44) & 1; l += 60 ) + { + v31 += *(short *)(l + 54); + ++k; + } + LOWORD(v31) = v30[k].uAnimTime + v31; + } + ++k; + } + v33 = v2->uNumEFrames; + if ( v33 > 0 ) + { + v34 = 0; + v35 = 0; + File = (FILE *)1; + do + { + Argsc = File; + if ( (signed int)File < v33 ) + { + do + { + if ( _strcmpi(v2->pSpritePFrames[(int)Argsc]->pIconName, v2->pSpritePFrames[v35]->pIconName) < 0 ) + { + v36 = v2->pSpritePFrames; + v37 = (int)&v36[(int)Argsc]; + v38 = *(SpriteFrame **)v37; + *(int *)v37 = (int)v36[v35]; + v2->pSpritePFrames[v35] = v38; + v39 = v2->pSpriteEFrames; + v40 = (int)&v39[(int)Argsc]; + LOWORD(v38) = *(short *)v40; + *(short *)v40 = v39[v34]; + v2->pSpriteEFrames[v34] = (signed __int16)v38; + } + Argsc = (FILE *)((char *)Argsc + 1); + } + while ( (signed int)Argsc < v2->uNumEFrames ); + } + File = (FILE *)((char *)File + 1); + v33 = v2->uNumEFrames; + ++v35; + ++v34; + } + while ( (signed int)((char *)File - 1) < v33 ); + } + result = 1; + } + else + { + MessageBoxW(nullptr, L"CSpriteFrameTable::load - Out of Memory!", nullptr, 0); + fclose(File); + result = 0; + } + return result; +} \ No newline at end of file