diff --git a/src/image.cpp b/src/image.cpp index e594fbed944d..59e408d45602 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -32,6 +32,7 @@ #include "serialization/base64.hpp" #include "serialization/string_utils.hpp" #include "sdl/rect.hpp" +#include "sdl/texture.hpp" #include "utils/general.hpp" #include @@ -161,6 +162,8 @@ image::locator::locator_finder_t locator_finder; image::image_cache images_, scaled_to_zoom_, hexed_images_, scaled_to_hex_images_, tod_colored_images_, brightened_images_; +image::texture_cache textures_; + // cache storing if each image fit in a hex image::bool_cache in_hex_info_; @@ -1090,6 +1093,51 @@ surface get_image(const image::locator& i_locator, TYPE type) #endif //_OPENMP i_locator.add_to_cache(*imap, res); + // Add the raw image to the texture cache. + if(type == UNSCALED && res != nullptr) { +#ifdef _OPENMP +#pragma omp critical(texture_cache) +#endif //_OPENMP + i_locator.add_to_cache(textures_, texture(res)); + } + + return res; +} + +texture get_texture(const image::locator& i_locator) +{ + // Returned the cached texture if applicable. If the image is already cached, the corresponding + // unscaled/unmodified surface has already been loaded from disk. + texture res; + + if(i_locator.is_void()) { + return res; + } + + bool in_cache; + +#ifdef _OPENMP +#pragma omp critical(texture_cache) +#endif //_OPENMP + in_cache = i_locator.in_cache(textures_); + + if(in_cache) { +#ifdef _OPENMP +#pragma omp critical(texture_cache) +#endif //_OPENMP + res = i_locator.locate_in_cache(textures_); + return res; + } + + // Else, no texture was cached. Fetch the surface from disk, which adds the corresponding texture + // to the texture cache. We ignore the returned surface and fetch the cached surface to avoid + // creating a new texture in memory for the same surface twice. + get_image(i_locator); +#ifdef _OPENMP +#pragma omp critical(texture_cache) +#endif //_OPENMP + res = i_locator.locate_in_cache(textures_); + return res; } diff --git a/src/image.hpp b/src/image.hpp index 053c54be179c..41e41fe47674 100644 --- a/src/image.hpp +++ b/src/image.hpp @@ -22,6 +22,7 @@ #include class surface; +class texture; ///this module manages the cache of images. With an image name, you can get ///the surface corresponding to that image. @@ -125,8 +126,8 @@ namespace image { surface load_from_disk(const locator &loc); - typedef cache_type image_cache; + typedef cache_type texture_cache; typedef cache_type bool_cache; typedef std::map mini_terrain_cache_map; @@ -193,6 +194,8 @@ namespace image { ///function to get the surface corresponding to an image. surface get_image(const locator& i_locator, TYPE type=UNSCALED); + texture get_texture(const image::locator& i_locator); + ///function to get the surface corresponding to an image. ///after applying the lightmap encoded in ls ///type should be HEXED or SCALED_TO_HEX