X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fguiscalingfilter.cpp;h=dc6219b60ba9b611f80d60786424283942a116b1;hb=617d94c8038e3ff035eaeef7ccdfa4f442feb873;hp=92dadeaec6c75351695b89df9dd7f700250cc245;hpb=6d61375cc72bad5c569d25c253adca4e3701dd27;p=dragonfireclient.git diff --git a/src/guiscalingfilter.cpp b/src/guiscalingfilter.cpp index 92dadeaec..dc6219b60 100644 --- a/src/guiscalingfilter.cpp +++ b/src/guiscalingfilter.cpp @@ -19,63 +19,66 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "guiscalingfilter.h" #include "imagefilters.h" #include "settings.h" -#include "main.h" // for g_settings #include "util/numeric.h" -#include +#include +#include "client/renderingengine.h" /* Maintain a static cache to store the images that correspond to textures * in a format that's manipulable by code. Some platforms exhibit issues * converting textures back into images repeatedly, and some don't even * allow it at all. */ -std::map imgCache; +std::map g_imgCache; /* Maintain a static cache of all pre-scaled textures. These need to be * cleared as well when the cached images. */ -std::map txrCache; +std::map g_txrCache; /* Manually insert an image into the cache, useful to avoid texture-to-image * conversion whenever we can intercept it. */ -void guiScalingCache(io::path key, video::IVideoDriver *driver, video::IImage *value) { +void guiScalingCache(io::path key, video::IVideoDriver *driver, video::IImage *value) +{ if (!g_settings->getBool("gui_scaling_filter")) return; video::IImage *copied = driver->createImage(value->getColorFormat(), value->getDimension()); value->copyTo(copied); - imgCache[key] = copied; + g_imgCache[key] = copied; } // Manually clear the cache, e.g. when switching to different worlds. -void guiScalingCacheClear(video::IVideoDriver *driver) { - for (std::map::iterator it = imgCache.begin(); - it != imgCache.end(); it++) { - if (it->second != NULL) - it->second->drop(); +void guiScalingCacheClear() +{ + for (auto &it : g_imgCache) { + if (it.second) + it.second->drop(); } - imgCache.clear(); - for (std::map::iterator it = txrCache.begin(); - it != txrCache.end(); it++) { - if (it->second != NULL) - driver->removeTexture(it->second); + g_imgCache.clear(); + for (auto &it : g_txrCache) { + if (it.second) + RenderingEngine::get_video_driver()->removeTexture(it.second); } - txrCache.clear(); + g_txrCache.clear(); } /* Get a cached, high-quality pre-scaled texture for display purposes. If the * texture is not already cached, attempt to create it. Returns a pre-scaled texture, * or the original texture if unable to pre-scale it. */ -video::ITexture *guiScalingResizeCached(video::IVideoDriver *driver, video::ITexture *src, - const core::rect &srcrect, const core::rect &destrect) { - +video::ITexture *guiScalingResizeCached(video::IVideoDriver *driver, + video::ITexture *src, const core::rect &srcrect, + const core::rect &destrect) +{ + if (src == NULL) + return src; if (!g_settings->getBool("gui_scaling_filter")) return src; // Calculate scaled texture name. char rectstr[200]; - sprintf(rectstr, "%d:%d:%d:%d:%d:%d", + snprintf(rectstr, sizeof(rectstr), "%d:%d:%d:%d:%d:%d", srcrect.UpperLeftCorner.X, srcrect.UpperLeftCorner.Y, srcrect.getWidth(), @@ -86,20 +89,20 @@ video::ITexture *guiScalingResizeCached(video::IVideoDriver *driver, video::ITex io::path scalename = origname + "@guiScalingFilter:" + rectstr; // Search for existing scaled texture. - video::ITexture *scaled = txrCache[scalename]; + video::ITexture *scaled = g_txrCache[scalename]; if (scaled) return scaled; // Try to find the texture converted to an image in the cache. // If the image was not found, try to extract it from the texture. - video::IImage* srcimg = imgCache[origname]; + video::IImage* srcimg = g_imgCache[origname]; if (srcimg == NULL) { if (!g_settings->getBool("gui_scaling_filter_txr2img")) return src; srcimg = driver->createImageFromData(src->getColorFormat(), src->getSize(), src->lock(), false); src->unlock(); - imgCache[origname] = srcimg; + g_imgCache[origname] = srcimg; } // Create a new destination image and scale the source into it. @@ -125,7 +128,7 @@ video::ITexture *guiScalingResizeCached(video::IVideoDriver *driver, video::ITex // Convert the scaled image back into a texture. scaled = driver->addTexture(scalename, destimg, NULL); destimg->drop(); - txrCache[scalename] = scaled; + g_txrCache[scalename] = scaled; return scaled; } @@ -133,8 +136,11 @@ video::ITexture *guiScalingResizeCached(video::IVideoDriver *driver, video::ITex /* Convenience wrapper for guiScalingResizeCached that accepts parameters that * are available at GUI imagebutton creation time. */ -video::ITexture *guiScalingImageButton(video::IVideoDriver *driver, video::ITexture *src, - s32 width, s32 height) { +video::ITexture *guiScalingImageButton(video::IVideoDriver *driver, + video::ITexture *src, s32 width, s32 height) +{ + if (src == NULL) + return src; return guiScalingResizeCached(driver, src, core::rect(0, 0, src->getSize().Width, src->getSize().Height), core::rect(0, 0, width, height)); @@ -146,10 +152,12 @@ video::ITexture *guiScalingImageButton(video::IVideoDriver *driver, video::IText void draw2DImageFilterScaled(video::IVideoDriver *driver, video::ITexture *txr, const core::rect &destrect, const core::rect &srcrect, const core::rect *cliprect, const video::SColor *const colors, - bool usealpha) { - + bool usealpha) +{ // Attempt to pre-scale image in software in high quality. video::ITexture *scaled = guiScalingResizeCached(driver, txr, srcrect, destrect); + if (scaled == NULL) + return; // Correct source rect based on scaled image. const core::rect mysrcrect = (scaled != txr)