From 3b54fd5d1814941822041d87020429361766ab2c Mon Sep 17 00:00:00 2001 From: Ashton <38200084+Alphalaneous@users.noreply.github.com> Date: Fri, 8 Mar 2024 06:02:22 -0500 Subject: [PATCH] Less MenuLayer lag --- src/utils/Utils.h | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/src/utils/Utils.h b/src/utils/Utils.h index 8bf50af..b3736da 100644 --- a/src/utils/Utils.h +++ b/src/utils/Utils.h @@ -5,25 +5,54 @@ using namespace geode::prelude; +struct ImageCache{ + std::string filePath; + CCImage* img; + CCTexture2D* texture; +}; class Utils { public: + inline static std::vector imageCache; + static void generateTexture(std::string filePath, GLenum target){ - CCImage img; - img.initWithImageFile(filePath.c_str()); + ImageCache imgCache; + + bool found = false; + + for(ImageCache i : imageCache){ + if(i.filePath == filePath){ + imgCache = i; + found = true; + break; + } + } + + if(!found){ + CCImage* img = new CCImage; + img->initWithImageFile(filePath.c_str()); + img->retain(); + + CCTexture2D* texture = new CCTexture2D; + texture->initWithImage(img); + texture->retain(); - CCTexture2D texture; - texture.initWithImage(&img); + imgCache.filePath = filePath; + imgCache.img = img; + imgCache.texture = texture; + + imageCache.push_back(imgCache); + } - int pixelsWide = texture.getPixelsWide(); - int pixelsHigh = texture.getPixelsHigh(); + int pixelsWide = imgCache.texture->getPixelsWide(); + int pixelsHigh = imgCache.texture->getPixelsHigh(); - CCTexture2DPixelFormat pixelFormat = texture.getPixelFormat(); + CCTexture2DPixelFormat pixelFormat = imgCache.texture->getPixelFormat(); - auto data = img.getData(); + auto data = imgCache.img->getData(); switch(pixelFormat) {