1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

Try to use lazy loading for animations

This commit is contained in:
Ivan Savenko
2024-06-04 16:24:23 +00:00
parent a8a330f39f
commit c1ebb6b0e3
2 changed files with 14 additions and 22 deletions

View File

@@ -25,7 +25,7 @@ bool CAnimation::loadFrame(size_t frame, size_t group)
return false; return false;
} }
if(auto image = getImage(frame, group, false)) if(auto image = getImageImpl(frame, group, false))
{ {
return true; return true;
} }
@@ -122,18 +122,13 @@ void CAnimation::printError(size_t frame, size_t group, std::string type) const
CAnimation::CAnimation(const AnimationPath & Name, std::map<size_t, std::vector <JsonNode> > layout): CAnimation::CAnimation(const AnimationPath & Name, std::map<size_t, std::vector <JsonNode> > layout):
name(boost::starts_with(Name.getName(), "SPRITES") ? Name : Name.addPrefix("SPRITES/")), name(boost::starts_with(Name.getName(), "SPRITES") ? Name : Name.addPrefix("SPRITES/")),
source(layout), source(layout)
preloaded(false)
{ {
if(source.empty()) if(source.empty())
logAnim->error("Animation %s failed to load", Name.getOriginalName()); logAnim->error("Animation %s failed to load", Name.getOriginalName());
} }
CAnimation::CAnimation(): CAnimation::CAnimation() = default;
preloaded(false)
{
}
CAnimation::~CAnimation() = default; CAnimation::~CAnimation() = default;
void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup) void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFrame, const size_t targetGroup)
@@ -161,11 +156,6 @@ void CAnimation::duplicateImage(const size_t sourceGroup, const size_t sourceFra
} }
source[targetGroup].push_back(clone); source[targetGroup].push_back(clone);
size_t index = source[targetGroup].size() - 1;
if(preloaded)
load(index, targetGroup);
} }
void CAnimation::setCustom(std::string filename, size_t frame, size_t group) void CAnimation::setCustom(std::string filename, size_t frame, size_t group)
@@ -176,7 +166,14 @@ void CAnimation::setCustom(std::string filename, size_t frame, size_t group)
//FIXME: update image if already loaded //FIXME: update image if already loaded
} }
std::shared_ptr<IImage> CAnimation::getImage(size_t frame, size_t group, bool verbose) const std::shared_ptr<IImage> CAnimation::getImage(size_t frame, size_t group, bool verbose)
{
if (!loadFrame(frame, group))
return nullptr;
return getImageImpl(frame, group, verbose);
}
std::shared_ptr<IImage> CAnimation::getImageImpl(size_t frame, size_t group, bool verbose)
{ {
auto groupIter = images.find(group); auto groupIter = images.find(group);
if (groupIter != images.end()) if (groupIter != images.end())
@@ -207,11 +204,7 @@ void CAnimation::unload()
void CAnimation::preload() void CAnimation::preload()
{ {
if(!preloaded) // TODO: remove
{
preloaded = true;
load();
}
} }
void CAnimation::loadGroup(size_t group) void CAnimation::loadGroup(size_t group)

View File

@@ -33,8 +33,6 @@ private:
//animation file name //animation file name
AnimationPath name; AnimationPath name;
bool preloaded;
//loader, will be called by load(), require opened def file for loading from it. Returns true if image is loaded //loader, will be called by load(), require opened def file for loading from it. Returns true if image is loaded
bool loadFrame(size_t frame, size_t group); bool loadFrame(size_t frame, size_t group);
@@ -44,6 +42,7 @@ private:
//to get rid of copy-pasting error message :] //to get rid of copy-pasting error message :]
void printError(size_t frame, size_t group, std::string type) const; void printError(size_t frame, size_t group, std::string type) const;
std::shared_ptr<IImage> getImageImpl(size_t frame, size_t group=0, bool verbose=true);
public: public:
CAnimation(const AnimationPath & Name, std::map<size_t, std::vector <JsonNode> > layout); CAnimation(const AnimationPath & Name, std::map<size_t, std::vector <JsonNode> > layout);
CAnimation(); CAnimation();
@@ -56,7 +55,7 @@ public:
//add custom surface to the selected position. //add custom surface to the selected position.
void setCustom(std::string filename, size_t frame, size_t group=0); void setCustom(std::string filename, size_t frame, size_t group=0);
std::shared_ptr<IImage> getImage(size_t frame, size_t group=0, bool verbose=true) const; std::shared_ptr<IImage> getImage(size_t frame, size_t group=0, bool verbose=true);
void exportBitmaps(const boost::filesystem::path & path) const; void exportBitmaps(const boost::filesystem::path & path) const;