diff options
author | flow <flowlnlnln@gmail.com> | 2022-08-30 18:06:13 -0300 |
---|---|---|
committer | flow <flowlnlnln@gmail.com> | 2022-09-03 13:37:22 -0300 |
commit | 3b13e692d29c96f99b2c153dd2c7933070eb8479 (patch) | |
tree | a0296e12519a146ef8545622453dff23b19b552c /launcher/minecraft/mod/ResourcePack.cpp | |
parent | 0331f5a1eb3e9fa21e89fc7fd56fdd4e87f29e44 (diff) | |
download | PrismLauncher-3b13e692d29c96f99b2c153dd2c7933070eb8479.tar.gz PrismLauncher-3b13e692d29c96f99b2c153dd2c7933070eb8479.tar.bz2 PrismLauncher-3b13e692d29c96f99b2c153dd2c7933070eb8479.zip |
feat: move resource pack images to QPixmapCache
This takes care of evicting entries when the cache gets too big for us,
so we can add new entries without much worries.
Signed-off-by: flow <flowlnlnln@gmail.com>
Diffstat (limited to 'launcher/minecraft/mod/ResourcePack.cpp')
-rw-r--r-- | launcher/minecraft/mod/ResourcePack.cpp | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/launcher/minecraft/mod/ResourcePack.cpp b/launcher/minecraft/mod/ResourcePack.cpp index 77bd98aa..cc8d23ce 100644 --- a/launcher/minecraft/mod/ResourcePack.cpp +++ b/launcher/minecraft/mod/ResourcePack.cpp @@ -6,6 +6,8 @@ #include "Version.h" +#include "minecraft/mod/tasks/LocalResourcePackParseTask.h" + // Values taken from: // https://minecraft.fandom.com/wiki/Tutorials/Creating_a_resource_pack#Formatting_pack.mcmeta static const QMap<int, std::pair<Version, Version>> s_pack_format_versions = { @@ -41,7 +43,29 @@ void ResourcePack::setImage(QImage new_image) Q_ASSERT(!new_image.isNull()); - m_pack_image = new_image; + if (m_pack_image_cache_key.key.isValid()) + QPixmapCache::remove(m_pack_image_cache_key.key); + + m_pack_image_cache_key.key = QPixmapCache::insert(QPixmap::fromImage(new_image)); + m_pack_image_cache_key.was_ever_used = true; +} + +QPixmap ResourcePack::image(QSize size) +{ + QPixmap cached_image; + if (QPixmapCache::find(m_pack_image_cache_key.key, &cached_image)) { + if (size.isNull()) + return cached_image; + return cached_image.scaled(size); + } + + // No valid image we can get + if (!m_pack_image_cache_key.was_ever_used) + return {}; + + // Imaged got evicted from the cache. Re-process it and retry. + ResourcePackUtils::process(*this); + return image(size); } std::pair<Version, Version> ResourcePack::compatibleVersions() const |