diff options
| author | EllipticEllipsis <73679967+EllipticEllipsis@users.noreply.github.com> | 2021-09-13 18:19:34 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-13 13:19:34 -0400 |
| commit | 3c00dcb30f33b763e215aebfa8ebdf96a8c3ee4d (patch) | |
| tree | c397a7442330e580a827854f1421e8ce97a8dbdb /ZAPD/ZRoom | |
| parent | 4d051c0fa036d245027a211bc7fd6a4afdce207f (diff) | |
Texture Animation extraction from objects (#180)
* Initial files
* Add resource header, first go at ExtractFromXML
* Now prints plenty to terminal, but not to file
* Main array now declared in file
* Erroring, but need to checkout master to test
* Not erroring any more, hooray
* TextureScrollingParams working, missing pointer
* Scrolling works
* Cycling params should work completely now
* Type 2 works, type 4 crashes
* Fixed iterator bound, works for Type 4 too now
* Correct a couple more EnvColor things
* Add null checks to declarations
* Switch to Seg2Offset, make TextureAnimationEntry an ordinary struct, delete some obsolete functions
* Remove Unknown (throws exception at construction instead)
* Move GetResourceType down
* Begin rewrite of SetAnimatedMaterialList
* Remove old structs in SetAnimatedMaterialList
* Fix infinite loop
* Tested and working for MM, objects and scenes
* Add warnings for undeclared textures (MM OK)
* Make `__PRETTY_FUNCTION__` work on GCC/Clang, at least
* Remove address from GetDefault name, add fancy errors
* Format (also added some extensive documentation last time)
* More formatting
* Fiddle with errors a bit more
* Fix pointer offsets and alignment
* Format, add comment to SetAnimatedMaterialList
* Add to docs
* Review
* Add [[maybe_unused]]
* uint to get rid of warnings
* makefile
* Fix most warnings
* Add default to shut GCC up
* Format
Diffstat (limited to 'ZAPD/ZRoom')
| -rw-r--r-- | ZAPD/ZRoom/Commands/SetAnimatedMaterialList.cpp | 377 | ||||
| -rw-r--r-- | ZAPD/ZRoom/Commands/SetAnimatedMaterialList.h | 101 |
2 files changed, 15 insertions, 463 deletions
diff --git a/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.cpp b/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.cpp index 0de9e21..b1b3ed0 100644 --- a/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.cpp +++ b/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.cpp @@ -1,3 +1,8 @@ +/** + * File: SetAnimatedMaterialList.cpp + * Description: Defines a class SetAnimatedMaterialList to enable ZRoom to declare + * ZTextureAnimations, using that ZResource to do the work. + */ #include "SetAnimatedMaterialList.h" #include "Globals.h" @@ -5,111 +10,25 @@ #include "Utils/StringHelper.h" #include "ZFile.h" #include "ZRoom/ZRoom.h" +#include "ZTextureAnimation.h" -SetAnimatedMaterialList::SetAnimatedMaterialList(ZFile* nParent) : ZRoomCommand(nParent) +SetAnimatedMaterialList::SetAnimatedMaterialList(ZFile* nParent) + : ZRoomCommand(nParent), textureAnimation(nParent) { } void SetAnimatedMaterialList::ParseRawData() { ZRoomCommand::ParseRawData(); - int32_t currentPtr = segmentOffset; - bool keepGoing = true; - - do - { - AnimatedMaterial lastTexture(parent->GetRawData(), currentPtr); - keepGoing = (lastTexture.segment != 0) && (lastTexture.segment > -1); - currentPtr += 8; - textures.push_back(lastTexture); - } while (keepGoing); + textureAnimation.SetRawDataIndex(segmentOffset); + textureAnimation.ParseRawData(); } void SetAnimatedMaterialList::DeclareReferences(const std::string& prefix) { - std::string nameStr = - StringHelper::Sprintf("%sAnimatedMaterialList0x%06X", prefix.c_str(), segmentOffset); - - for (auto& texture : textures) - { - size_t declSize = 0; - std::string declTypeName = ""; - std::string declName = StringHelper::Sprintf("%sAnimatedMaterialParams0x%06X", - prefix.c_str(), texture.segmentOffset); - std::string declaration = ""; - size_t index = 0; - - switch (texture.type) - { - case 0: - case 1: - for (const auto& param : texture.params) - { - declaration += param->GenerateSourceCode(zRoom, texture.segmentOffset); - - if (index < texture.params.size() - 1) - declaration += "\n"; - - index++; - } - - declSize = texture.params.size() * 4; - declTypeName = "AnimatedMatTexScrollParams"; - - parent->AddDeclarationArray(texture.segmentOffset, DeclarationAlignment::Align4, - declSize, declTypeName, declName, texture.params.size(), - declaration); - break; - case 2: - case 3: - case 4: - declSize = texture.params.at(0)->GetParamsSize(); - declTypeName = "AnimatedMatColorParams"; - declaration = texture.params.at(0)->GenerateSourceCode(zRoom, texture.segmentOffset); - - parent->AddDeclaration(texture.segmentOffset, DeclarationAlignment::Align4, declSize, - declTypeName, declName, - StringHelper::Sprintf("\n\t%s\n", declaration.c_str())); - break; - case 5: - declSize = texture.params.at(0)->GetParamsSize(); - declTypeName = "AnimatedMatTexCycleParams"; - declaration = texture.params.at(0)->GenerateSourceCode(zRoom, texture.segmentOffset); - - parent->AddDeclaration(texture.segmentOffset, DeclarationAlignment::Align4, declSize, - declTypeName, declName, - StringHelper::Sprintf("\n\t%s\n", declaration.c_str())); - break; - case 6: - continue; - - default: - throw std::runtime_error( - StringHelper::Sprintf("Error in SetAnimatedMaterialList::DeclareReferences (%s)\n" - "\t Unknown texture.type: %i\n", - nameStr.c_str(), texture.type)); - } - } - - if (!textures.empty()) - { - std::string declaration = ""; - - for (size_t i = 0; i < textures.size(); i++) - { - std::string textureName = parent->GetDeclarationPtrName(textures.at(i).segmentAddress); - - declaration += StringHelper::Sprintf("\t{ %2i, %2i, %s },", textures.at(i).segment, - textures.at(i).type, textureName.c_str()); - - if (i + 1 < textures.size()) - declaration += "\n"; - } - - parent->AddDeclarationArray(segmentOffset, DeclarationAlignment::Align4, - DeclarationPadding::Pad16, textures.size() * 8, - "AnimatedMaterial", nameStr, textures.size(), declaration); - } + textureAnimation.SetName(textureAnimation.GetDefaultName(prefix.c_str())); + textureAnimation.DeclareReferences(prefix); + textureAnimation.GetSourceOutputCode(prefix); } std::string SetAnimatedMaterialList::GetBodySourceCode() const @@ -127,273 +46,3 @@ RoomCommand SetAnimatedMaterialList::GetRoomCommand() const { return RoomCommand::SetAnimatedMaterialList; } - -AnimatedMaterial::AnimatedMaterial(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex) - : segment(rawData.at(rawDataIndex)), type(BitConverter::ToInt16BE(rawData, rawDataIndex + 2)) -{ - segmentAddress = BitConverter::ToInt32BE(rawData, rawDataIndex + 4); - segmentOffset = GETSEGOFFSET(segmentAddress); - - switch (type) - { - case 0: - params.push_back(std::make_shared<ScrollingTexture>(rawData, segmentOffset)); - break; - case 1: - params.push_back(std::make_shared<ScrollingTexture>(rawData, segmentOffset)); - params.push_back(std::make_shared<ScrollingTexture>(rawData, segmentOffset + 4)); - break; - case 2: - case 3: - case 4: - params.push_back(std::make_shared<FlashingTexture>(rawData, segmentOffset, type)); - break; - case 5: - params.push_back(std::make_shared<AnimatedMatTexCycleParams>(rawData, segmentOffset)); - break; - case 6: // Some terminator when there are no animated textures? - break; - } -} - -ScrollingTexture::ScrollingTexture(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex) - : xStep(rawData.at(rawDataIndex + 0)), yStep(rawData.at(rawDataIndex + 1)), - width(rawData.at(rawDataIndex + 2)), height(rawData.at(rawDataIndex + 3)) -{ -} - -std::string ScrollingTexture::GenerateSourceCode([[maybe_unused]] ZRoom* zRoom, - [[maybe_unused]] uint32_t baseAddress) -{ - return StringHelper::Sprintf(" { %i, %i, 0x%02X, 0x%02X },", xStep, yStep, width, height); -} - -size_t ScrollingTexture::GetParamsSize() -{ - return 4; -} - -F3DPrimColor::F3DPrimColor(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex) - : r(rawData.at(rawDataIndex + 0)), g(rawData.at(rawDataIndex + 1)), - b(rawData.at(rawDataIndex + 2)), a(rawData.at(rawDataIndex + 3)), - lodFrac(rawData.at(rawDataIndex + 4)) -{ -} - -FlashingTextureEnvColor::FlashingTextureEnvColor(const std::vector<uint8_t>& rawData, - uint32_t rawDataIndex) - : r(rawData.at(rawDataIndex + 0)), g(rawData.at(rawDataIndex + 1)), - b(rawData.at(rawDataIndex + 2)), a(rawData.at(rawDataIndex + 3)) -{ -} - -FlashingTexture::FlashingTexture(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex, - int32_t type) - : cycleLength(BitConverter::ToUInt16BE(rawData, rawDataIndex + 0)), - numKeyFrames(BitConverter::ToUInt16BE(rawData, rawDataIndex + 2)) -{ - uint16_t length = (type == 2) ? cycleLength : numKeyFrames; - - primColorSegmentAddr = BitConverter::ToInt32BE(rawData, rawDataIndex + 4); - envColorSegmentAddr = BitConverter::ToInt32BE(rawData, rawDataIndex + 8); - keyFrameSegmentAddr = BitConverter::ToInt32BE(rawData, rawDataIndex + 12); - - primColorSegmentOffset = GETSEGOFFSET(primColorSegmentAddr); - envColorSegmentOffset = GETSEGOFFSET(envColorSegmentAddr); - keyFrameSegmentOffset = GETSEGOFFSET(keyFrameSegmentAddr); - - int32_t currentPtr = primColorSegmentOffset; - for (uint16_t i = 0; i < length; i++) - { - primColors.push_back(F3DPrimColor(rawData, currentPtr)); - currentPtr += 5; - } - - currentPtr = envColorSegmentOffset; - for (uint16_t i = 0; i < length; i++) - { - envColors.push_back(FlashingTextureEnvColor(rawData, currentPtr)); - currentPtr += 4; - } - - currentPtr = keyFrameSegmentOffset; - for (uint16_t i = 0; i < length; i++) - { - keyFrames.push_back(BitConverter::ToUInt16BE(rawData, currentPtr)); - currentPtr += 2; - } -} - -std::string FlashingTexture::GenerateSourceCode(ZRoom* zRoom, [[maybe_unused]] uint32_t baseAddress) -{ - if (primColorSegmentOffset != 0) - { - std::string declaration = ""; - size_t index = 0; - - for (F3DPrimColor& color : primColors) - { - declaration += StringHelper::Sprintf(" { 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X },", - color.r, color.g, color.b, color.a, color.lodFrac); - - if (index < primColors.size() - 1) - declaration += "\n"; - - index++; - } - - std::string primColorName = StringHelper::Sprintf( - "%sAnimatedMaterialPrimColor_%06X", zRoom->GetName().c_str(), primColorSegmentOffset); - zRoom->parent->AddDeclarationArray(primColorSegmentOffset, DeclarationAlignment::Align4, - primColors.size() * 5, "F3DPrimColor", primColorName, - primColors.size(), declaration); - } - - if (envColorSegmentOffset != 0) - { - std::string declaration = ""; - size_t index = 0; - - for (FlashingTextureEnvColor& color : envColors) - { - declaration += StringHelper::Sprintf(" { 0x%02X, 0x%02X, 0x%02X, 0x%02X },", color.r, - color.g, color.b, color.a); - - if (index < envColors.size() - 1) - declaration += "\n"; - - index++; - } - - std::string envColorName = StringHelper::Sprintf( - "%sAnimatedMaterialEnvColors0x%06X", zRoom->GetName().c_str(), envColorSegmentOffset); - zRoom->parent->AddDeclarationArray(envColorSegmentOffset, DeclarationAlignment::Align4, - envColors.size() * 4, "F3DEnvColor", envColorName, - envColors.size(), declaration); - } - - if (keyFrameSegmentOffset != 0) - { - std::string declaration = ""; - size_t index = 0; - - for (uint16_t keyFrame : keyFrames) - { - declaration += StringHelper::Sprintf(" 0x%02X,", keyFrame); - - if (index < keyFrames.size() - 1) - declaration += "\n"; - - index++; - } - - zRoom->parent->AddDeclarationArray( - keyFrameSegmentOffset, DeclarationAlignment::Align4, keyFrames.size() * 2, "u16", - StringHelper::Sprintf("%sAnimatedMaterialKeyFrames0x%06X", zRoom->GetName().c_str(), - keyFrameSegmentOffset), - keyFrames.size(), declaration); - } - - std::string primName = zRoom->parent->GetDeclarationPtrName(primColorSegmentAddr); - std::string envName = zRoom->parent->GetDeclarationPtrName(envColorSegmentAddr); - std::string keyName = zRoom->parent->GetDeclarationPtrName(keyFrameSegmentAddr); - - return StringHelper::Sprintf("%i, %i, %s, %s, %s", cycleLength, numKeyFrames, primName.c_str(), - envName.c_str(), keyName.c_str()); -} - -size_t FlashingTexture::GetParamsSize() -{ - return 16; -} - -AnimatedMatTexCycleParams::AnimatedMatTexCycleParams(const std::vector<uint8_t>& rawData, - uint32_t rawDataIndex) - : cycleLength(BitConverter::ToUInt16BE(rawData, rawDataIndex + 0)) -{ - textureSegmentOffsetsSegmentAddress = BitConverter::ToInt32BE(rawData, rawDataIndex + 4); - textureIndicesSegmentAddress = BitConverter::ToInt32BE(rawData, rawDataIndex + 8); - - textureSegmentOffsetsSegmentOffset = GETSEGOFFSET(textureSegmentOffsetsSegmentAddress); - textureIndicesSegmentOffset = GETSEGOFFSET(textureIndicesSegmentAddress); - - int32_t currentPtr = textureIndicesSegmentOffset; - int32_t maxIndex = 0; - - for (uint16_t i = 0; i < cycleLength; i++) - { - uint8_t newIndex = rawData.at(currentPtr); - textureIndices.push_back(newIndex); - currentPtr++; - if (newIndex > maxIndex) - maxIndex = newIndex; - } - - currentPtr = textureSegmentOffsetsSegmentOffset; - for (int32_t i = 0; i < maxIndex + 1; i++) - { - textureSegmentOffsets.push_back(GETSEGOFFSET(BitConverter::ToInt32BE(rawData, currentPtr))); - currentPtr += 4; - } -} - -std::string AnimatedMatTexCycleParams::GenerateSourceCode(ZRoom* zRoom, - [[maybe_unused]] uint32_t baseAddress) -{ - if (textureSegmentOffsetsSegmentOffset != 0) - { - std::string declaration = ""; - size_t index = 0; - - for (uint32_t offset : textureSegmentOffsets) - { - declaration += - StringHelper::Sprintf(" %sTex_%06X,", zRoom->GetName().c_str(), offset); - - if (index < textureSegmentOffsets.size() - 1) - declaration += "\n"; - - index++; - } - - zRoom->parent->AddDeclarationArray( - textureSegmentOffsetsSegmentOffset, DeclarationAlignment::Align4, - textureSegmentOffsets.size() * 4, "u64*", - StringHelper::Sprintf("%sAnimatedMaterialTexSegOffsets0x%06X", zRoom->GetName().c_str(), - textureSegmentOffsetsSegmentOffset), - textureSegmentOffsets.size(), declaration); - } - - if (textureIndicesSegmentOffset != 0) - { - std::string declaration = ""; - size_t index = 0; - - for (uint8_t textureIndex : textureIndices) - { - declaration += StringHelper::Sprintf(" 0x%02X,", textureIndex); - - if (index < textureIndices.size() - 1) - declaration += "\n"; - - index++; - } - - zRoom->parent->AddDeclarationArray( - textureIndicesSegmentOffset, DeclarationAlignment::Align4, textureIndices.size(), "u8", - StringHelper::Sprintf("%sAnimatedMaterialTexIndices0x%06X", zRoom->GetName().c_str(), - textureIndicesSegmentOffset), - textureIndices.size(), declaration); - } - - std::string segmName = - zRoom->parent->GetDeclarationPtrName(textureSegmentOffsetsSegmentAddress); - std::string indexesName = zRoom->parent->GetDeclarationPtrName(textureIndicesSegmentAddress); - - return StringHelper::Sprintf("%i, %s, %s", cycleLength, segmName.c_str(), indexesName.c_str()); -} - -size_t AnimatedMatTexCycleParams::GetParamsSize() -{ - return 12; -} diff --git a/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.h b/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.h index e48ae42..fecb9f2 100644 --- a/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.h +++ b/ZAPD/ZRoom/Commands/SetAnimatedMaterialList.h @@ -2,104 +2,7 @@ #include <memory> #include "ZRoom/ZRoomCommand.h" - -// TODO move into header and add all types -class AnimatedTextureParams -{ -public: - virtual ~AnimatedTextureParams() = default; - virtual std::string GenerateSourceCode(ZRoom* zRoom, uint32_t baseAddress) = 0; - virtual size_t GetParamsSize() = 0; -}; - -class ScrollingTexture : public AnimatedTextureParams -{ -public: - ScrollingTexture(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex); - std::string GenerateSourceCode(ZRoom* zRoom, uint32_t baseAddress) override; - size_t GetParamsSize() override; - - int8_t xStep; - int8_t yStep; - uint8_t width; - uint8_t height; -}; - -class F3DPrimColor -{ -public: - F3DPrimColor(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex); - - uint8_t r; - uint8_t g; - uint8_t b; - uint8_t a; - uint8_t lodFrac; -}; - -class FlashingTextureEnvColor -{ -public: - FlashingTextureEnvColor(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex); - - uint8_t r; - uint8_t g; - uint8_t b; - uint8_t a; -}; - -class FlashingTexture : public AnimatedTextureParams -{ -public: - FlashingTexture(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex, int32_t type); - std::string GenerateSourceCode(ZRoom* zRoom, uint32_t baseAddress) override; - size_t GetParamsSize() override; - - uint16_t cycleLength; - uint16_t numKeyFrames; - - segptr_t primColorSegmentAddr; - segptr_t envColorSegmentAddr; - segptr_t keyFrameSegmentAddr; - - uint32_t primColorSegmentOffset; - uint32_t envColorSegmentOffset; - uint32_t keyFrameSegmentOffset; - - std::vector<F3DPrimColor> primColors; - std::vector<FlashingTextureEnvColor> envColors; - std::vector<uint16_t> keyFrames; -}; - -class AnimatedMatTexCycleParams : public AnimatedTextureParams -{ -public: - AnimatedMatTexCycleParams(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex); - std::string GenerateSourceCode(ZRoom* zRoom, uint32_t baseAddress) override; - size_t GetParamsSize() override; - - uint16_t cycleLength; - - segptr_t textureSegmentOffsetsSegmentAddress; - segptr_t textureIndicesSegmentAddress; - uint32_t textureSegmentOffsetsSegmentOffset; - uint32_t textureIndicesSegmentOffset; - - std::vector<uint32_t> textureSegmentOffsets; - std::vector<uint8_t> textureIndices; -}; - -class AnimatedMaterial -{ -public: - AnimatedMaterial(const std::vector<uint8_t>& rawData, uint32_t rawDataIndex); - - int8_t segment; - int16_t type; - segptr_t segmentAddress; - uint32_t segmentOffset; - std::vector<std::shared_ptr<AnimatedTextureParams>> params; -}; +#include "ZTextureAnimation.h" class SetAnimatedMaterialList : public ZRoomCommand { @@ -115,5 +18,5 @@ public: std::string GetCommandCName() const override; private: - std::vector<AnimatedMaterial> textures; + ZTextureAnimation textureAnimation; }; |
