Skip to content

Commit

Permalink
Added a texture cache
Browse files Browse the repository at this point in the history
Note this probably doesn't work properly yet since I haven't properly implemented shared management
of textures in their wrapper class.
  • Loading branch information
Vultraz committed Mar 18, 2018
1 parent da79179 commit 3e2046b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/image.cpp
Expand Up @@ -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 <SDL_image.h>
Expand Down Expand Up @@ -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_;

Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 4 additions & 1 deletion src/image.hpp
Expand Up @@ -22,6 +22,7 @@
#include <unordered_map>

class surface;
class texture;

///this module manages the cache of images. With an image name, you can get
///the surface corresponding to that image.
Expand Down Expand Up @@ -125,8 +126,8 @@ namespace image {

surface load_from_disk(const locator &loc);


typedef cache_type<surface> image_cache;
typedef cache_type<texture> texture_cache;
typedef cache_type<bool> bool_cache;

typedef std::map<t_translation::terrain_code, surface> mini_terrain_cache_map;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e2046b

Please sign in to comment.