diff --git a/client/render/CAnimation.cpp b/client/render/CAnimation.cpp index d411ae81b..514540d79 100644 --- a/client/render/CAnimation.cpp +++ b/client/render/CAnimation.cpp @@ -28,7 +28,7 @@ bool CAnimation::loadFrame(size_t frame, size_t group) if(auto image = getImageImpl(frame, group, false)) return true; - std::shared_ptr image = GH.renderHandler().loadImage(source[group][frame], mode); + std::shared_ptr image = GH.renderHandler().loadImage(getImageLocator(frame, group), mode); if(image) { @@ -109,19 +109,7 @@ CAnimation::~CAnimation() = default; void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup) { - if(!source.count(sourceGroup)) - { - logAnim->error("Group %d missing in %s", sourceGroup, name.getName()); - return; - } - - if(source[sourceGroup].size() <= sourceFrame) - { - logAnim->error("Frame [%d %d] missing in %s", sourceGroup, sourceFrame, name.getName()); - return; - } - - ImageLocator clone(source[sourceGroup][sourceFrame]); + ImageLocator clone(getImageLocator(sourceFrame, sourceGroup)); source[targetGroup].push_back(clone); } @@ -179,8 +167,9 @@ void CAnimation::horizontalFlip(size_t frame, size_t group) // ignore - image not loaded } - ImageLocator & locator = source.at(group).at(frame); + auto locator = getImageLocator(frame, group); locator.horizontalFlip = !locator.horizontalFlip; + source[group][frame] = locator; } void CAnimation::verticalFlip(size_t frame, size_t group) @@ -194,8 +183,9 @@ void CAnimation::verticalFlip(size_t frame, size_t group) // ignore - image not loaded } - ImageLocator & locator = source.at(group).at(frame); + auto locator = getImageLocator(frame, group); locator.verticalFlip = !locator.verticalFlip; + source[group][frame] = locator; } void CAnimation::playerColored(PlayerColor player) @@ -213,3 +203,13 @@ void CAnimation::createFlippedGroup(const size_t sourceGroup, const size_t targe verticalFlip(frame, targetGroup); } } + +ImageLocator CAnimation::getImageLocator(size_t frame, size_t group) const +{ + const ImageLocator & locator = source.at(group).at(frame); + + if (!locator.empty()) + return locator; + + return ImageLocator(name, frame, group); +} diff --git a/client/render/CAnimation.h b/client/render/CAnimation.h index 13b96ed25..75c8d1566 100644 --- a/client/render/CAnimation.h +++ b/client/render/CAnimation.h @@ -50,6 +50,8 @@ private: void printError(size_t frame, size_t group, std::string type) const; std::shared_ptr getImageImpl(size_t frame, size_t group=0, bool verbose=true); + + ImageLocator getImageLocator(size_t frame, size_t group) const; public: CAnimation(const AnimationPath & Name, std::map > layout, EImageBlitMode mode); ~CAnimation(); diff --git a/client/render/ImageLocator.cpp b/client/render/ImageLocator.cpp index f1c94462e..250fd2f47 100644 --- a/client/render/ImageLocator.cpp +++ b/client/render/ImageLocator.cpp @@ -49,3 +49,8 @@ bool ImageLocator::operator<(const ImageLocator & other) const return verticalFlip < other.verticalFlip; return horizontalFlip < other.horizontalFlip; } + +bool ImageLocator::empty() const +{ + return !image.has_value() && !defFile.has_value(); +} diff --git a/client/render/ImageLocator.h b/client/render/ImageLocator.h index 13b6f1625..875b537c1 100644 --- a/client/render/ImageLocator.h +++ b/client/render/ImageLocator.h @@ -22,8 +22,10 @@ struct ImageLocator bool horizontalFlip = false; ImageLocator() = default; - ImageLocator(const JsonNode & config); - ImageLocator(const ImagePath & path); ImageLocator(const AnimationPath & path, int frame, int group); + explicit ImageLocator(const JsonNode & config); + explicit ImageLocator(const ImagePath & path); + bool operator < (const ImageLocator & other) const; + bool empty() const; }; diff --git a/client/renderSDL/RenderHandler.cpp b/client/renderSDL/RenderHandler.cpp index bfaf83ec1..ae932a16a 100644 --- a/client/renderSDL/RenderHandler.cpp +++ b/client/renderSDL/RenderHandler.cpp @@ -68,7 +68,7 @@ void RenderHandler::initFromJson(AnimationLayoutMap & source, const JsonNode & c JsonNode toAdd; JsonUtils::inherit(toAdd, base); toAdd["file"].String() = basepath + frame.String(); - source[groupID].push_back(toAdd); + source[groupID].push_back(ImageLocator(toAdd)); } } @@ -83,7 +83,7 @@ void RenderHandler::initFromJson(AnimationLayoutMap & source, const JsonNode & c JsonNode toAdd; JsonUtils::inherit(toAdd, base); toAdd["file"].String() = basepath + node["file"].String(); - source[group][frame] = toAdd; + source[group][frame] = ImageLocator(toAdd); } } @@ -141,10 +141,10 @@ std::shared_ptr RenderHandler::loadImageFromAnimationFileUncached(c if (frame >= layout.at(group).size()) return loadImageFromSingleFile(ImagePath::builtin("DEFAULT")); - const auto & config = layout.at(group).at(frame); - if (config.image) + const auto & locator = layout.at(group).at(frame); + if (locator.image) { - return loadImageImpl(ImageLocator(config)); + return loadImageImpl(locator); } else { @@ -168,7 +168,7 @@ std::shared_ptr RenderHandler::loadImageImpl(const ImageLocator & l std::shared_ptr result; - if (!locator.image) + if (locator.image) result = loadImageFromSingleFile(*locator.image); else if (locator.defFile) result = loadImageFromAnimationFile(*locator.defFile, locator.defFrame, locator.defGroup); @@ -228,7 +228,7 @@ void RenderHandler::addImageListEntries(const EntityService * service) if (index >= layout[group].size()) layout[group].resize(index + 1); - layout[group][index] = entry; + layout[group][index] = ImageLocator(entry); }); }); }