cache NetworkedAnimator drawables

could use a LruCache later
This commit is contained in:
Kae 2023-06-26 21:39:22 +10:00
parent 14ef69c0f6
commit f2fedb0c87
3 changed files with 46 additions and 28 deletions

View File

@ -174,36 +174,38 @@ ImageOperation imageOperationFromString(StringView string) {
char ch = *ptr;
if (ch == '=' || ch == ';' || ptr == end) {
char* c = which ? a : b;
if (hexLen != 0) {
char* c = which ? a : b;
if (hexLen == 3) {
nibbleDecode(hexPtr, 3, c, 4);
c[0] |= (c[0] << 4);
c[1] |= (c[1] << 4);
c[2] |= (c[2] << 4);
c[3] = 255;
}
else if (hexLen == 4) {
nibbleDecode(hexPtr, 4, c, 4);
c[0] |= (c[0] << 4);
c[1] |= (c[1] << 4);
c[2] |= (c[2] << 4);
c[3] |= (c[3] << 4);
}
else if (hexLen == 6) {
hexDecode(hexPtr, 6, c, 4);
c[3] = 255;
}
else if (hexLen == 8) {
hexDecode(hexPtr, 8, c, 4);
}
else
throw ImageOperationException(strf("Improper size for hex string '%s' in imageOperationFromString", StringView(hexPtr, hexLen)), false);
if (hexLen == 3) {
nibbleDecode(hexPtr, 3, c, 4);
c[0] |= (c[0] << 4);
c[1] |= (c[1] << 4);
c[2] |= (c[2] << 4);
c[3] = 255;
}
else if (hexLen == 4) {
nibbleDecode(hexPtr, 4, c, 4);
c[0] |= (c[0] << 4);
c[1] |= (c[1] << 4);
c[2] |= (c[2] << 4);
c[3] |= (c[3] << 4);
}
else if (hexLen == 6) {
hexDecode(hexPtr, 6, c, 4);
c[3] = 255;
}
else if (hexLen == 8) {
hexDecode(hexPtr, 8, c, 4);
}
else
throw ImageOperationException(strf("Improper size for hex string '%s' in imageOperationFromString", StringView(hexPtr, hexLen)), false);
if (which = !which)
operation.colorReplaceMap[*(Vec4B*)&a] = *(Vec4B*)&b;
if (which = !which)
operation.colorReplaceMap[*(Vec4B*)&a] = *(Vec4B*)&b;
hexLen = 0;
hexLen = 0;
}
}
else if (!hexLen++)
hexPtr = ptr;

View File

@ -255,6 +255,7 @@ NetworkedAnimator& NetworkedAnimator::operator=(NetworkedAnimator&& animator) {
m_animationRate = move(animator.m_animationRate);
m_globalTags = move(animator.m_globalTags);
m_partTags = move(animator.m_partTags);
m_cachedPartDrawables = move(animator.m_cachedPartDrawables);
setupNetStates();
@ -278,6 +279,7 @@ NetworkedAnimator& NetworkedAnimator::operator=(NetworkedAnimator const& animato
m_animationRate = animator.m_animationRate;
m_globalTags = animator.m_globalTags;
m_partTags = animator.m_partTags;
m_cachedPartDrawables = animator.m_cachedPartDrawables;
setupNetStates();
@ -638,7 +640,20 @@ List<pair<Drawable, float>> NetworkedAnimator::drawablesWithZLevel(Vec2F const&
if (usedImage[0] != '/')
relativeImage = AssetPath::relativeTo(m_relativePath, usedImage);
auto drawable = Drawable::makeImage(!relativeImage.empty() ? relativeImage : usedImage, 1.0f / TilePixels, centered, Vec2F());
size_t hash = hashOf(relativeImage);
auto find = m_cachedPartDrawables.find(partName);
bool fail = find == m_cachedPartDrawables.end() || find->second.first != hash;
if (fail) {
Drawable drawable = Drawable::makeImage(!relativeImage.empty() ? relativeImage : usedImage, 1.0f / TilePixels, centered, Vec2F());
if (find == m_cachedPartDrawables.end())
find = m_cachedPartDrawables.emplace(partName, std::pair{ hash, move(drawable) }).first;
else {
find->second.first = hash;
find->second.second = move(drawable);
}
}
Drawable drawable = find->second.second;
auto& imagePart = drawable.imagePart();
for (Directives const& directives : baseProcessingDirectives)
imagePart.addDirectives(directives);

View File

@ -324,6 +324,7 @@ private:
NetElementHashMap<String, String> m_globalTags;
StableStringMap<NetElementHashMap<String, String>> m_partTags;
mutable StringMap<std::pair<size_t, Drawable>> m_cachedPartDrawables;
};
}