From 2b7da8fcb48ac9461b6adf2a6d199379e655fd26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boldizs=C3=A1r=20Lipka?= Date: Sun, 8 Jun 2014 13:16:53 +0200 Subject: [PATCH] Move load_from_disk out of image::locator. --- src/image.cpp | 130 +++++++++++++++++++++++++------------------------- src/image.hpp | 8 +--- 2 files changed, 68 insertions(+), 70 deletions(-) diff --git a/src/image.cpp b/src/image.cpp index 0cffc29c1741..a238307009d4 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -60,6 +60,8 @@ struct cache_item bool loaded; }; + + namespace image { template @@ -450,11 +452,11 @@ static void add_localized_overlay (const std::string& ovr_file, surface &orig_su sdl_blit(ovr_surf, 0, orig_surf, &area); } -surface locator::load_image_file() const +static surface load_image_file(const image::locator &loc) { surface res; - std::string location = get_binary_file_location("images", val_.filename_); + std::string location = get_binary_file_location("images", loc.get_filename()); { @@ -475,15 +477,67 @@ surface locator::load_image_file() const } } - if (res.null() && !val_.filename_.empty()) { - ERR_DP << "could not open image '" << val_.filename_ << "'" << std::endl; - if (game_config::debug && val_.filename_ != game_config::images::missing) + if (res.null() && !loc.get_filename().empty()) { + ERR_DP << "could not open image '" << loc.get_filename() << "'" << std::endl; + if (game_config::debug && loc.get_filename() != game_config::images::missing) return get_image(game_config::images::missing, UNSCALED); } return res; } +static surface load_image_sub_file(const image::locator &loc) +{ + surface surf = get_image(loc.get_filename(), UNSCALED); + if(surf == NULL) + return NULL; + + modification_queue mods = modification::decode(loc.get_modifications()); + + while(!mods.empty()) { + modification* mod = mods.top(); + mods.pop(); + + try { + surf = (*mod)(surf); + } catch(const image::modification::texception& e) { + ERR_CFG << "Failed to apply a modification to an image:\n" + << "Image: " << loc.get_filename() << ".\n" + << "Modifications: " << loc.get_modifications() << ".\n" + << "Error: " << e.message; + } + delete mod; + } + + if(loc.get_loc().valid()) { + SDL_Rect srcrect = sdl::create_rect( + ((tile_size*3) / 4) * loc.get_loc().x + , tile_size * loc.get_loc().y + (tile_size / 2) * (loc.get_loc().x % 2) + , tile_size + , tile_size); + + if(loc.get_center_x() >= 0 && loc.get_center_y() >= 0){ + srcrect.x += surf->w/2 - loc.get_center_x(); + srcrect.y += surf->h/2 - loc.get_center_y(); + } + + // cut and hex mask, but also check and cache if empty result + surface cut(cut_surface(surf, srcrect)); + bool is_empty = false; + surf = mask_surface(cut, get_hexmask(), &is_empty); + // discard empty images to free memory + if(is_empty) { + // Safe because those images are only used by terrain rendering + // and it filters them out. + // A safer and more general way would be to keep only one copy of it + surf = NULL; + } + loc.add_to_cache(is_empty_hex_, is_empty); + } + + return surf; +} + //small utility function to store an int from (-256,254) to an signed char static signed char col_to_uchar(int i) { return static_cast(std::min(127, std::max(-128, i/2))); @@ -544,70 +598,18 @@ static surface apply_light(surface surf, const light_string& ls){ return light_surface(surf, lightmap); } -surface locator::load_image_sub_file() const -{ - surface surf = get_image(val_.filename_, UNSCALED); - if(surf == NULL) - return NULL; - - modification_queue mods = modification::decode(val_.modifications_); - - while(!mods.empty()) { - modification* mod = mods.top(); - mods.pop(); - - try { - surf = (*mod)(surf); - } catch(const image::modification::texception& e) { - ERR_CFG << "Failed to apply a modification to an image:\n" - << "Image: " << val_.filename_ << ".\n" - << "Modifications: " << val_.modifications_ << ".\n" - << "Error: " << e.message; - } - delete mod; - } - - if(val_.loc_.valid()) { - SDL_Rect srcrect = sdl::create_rect( - ((tile_size*3) / 4) * val_.loc_.x - , tile_size * val_.loc_.y + (tile_size / 2) * (val_.loc_.x % 2) - , tile_size - , tile_size); - - if(val_.center_x_ >= 0 && val_.center_y_>= 0){ - srcrect.x += surf->w/2 - val_.center_x_; - srcrect.y += surf->h/2 - val_.center_y_; - } - - // cut and hex mask, but also check and cache if empty result - surface cut(cut_surface(surf, srcrect)); - bool is_empty = false; - surf = mask_surface(cut, get_hexmask(), &is_empty); - // discard empty images to free memory - if(is_empty) { - // Safe because those images are only used by terrain rendering - // and it filters them out. - // A safer and more general way would be to keep only one copy of it - surf = NULL; - } - add_to_cache(is_empty_hex_, is_empty); - } - - return surf; -} - bool locator::file_exists() const { return !get_binary_file_location("images", val_.filename_).empty(); } -surface locator::load_from_disk() const +surface load_from_disk(const locator &loc) { - switch(val_.type_) { - case FILE: - return load_image_file(); - case SUB_FILE: - return load_image_sub_file(); + switch(loc.get_type()) { + case locator::FILE: + return load_image_file(loc); + case locator::SUB_FILE: + return load_image_sub_file(loc); default: return surface(NULL); } @@ -848,7 +850,7 @@ surface get_image(const image::locator& i_locator, TYPE type) switch(type) { case UNSCALED: // If type is unscaled, directly load the image from the disk. - res = i_locator.load_from_disk(); + res = load_from_disk(i_locator); break; case TOD_COLORED: res = get_tod_colored(i_locator); diff --git a/src/image.hpp b/src/image.hpp index f96a42129bf6..7f8890c8686f 100644 --- a/src/image.hpp +++ b/src/image.hpp @@ -108,9 +108,6 @@ namespace image { */ bool file_exists() const; - // loads the image it is pointing to from the disk - surface load_from_disk() const; - template bool in_cache(cache_type &cache) const; template @@ -122,13 +119,12 @@ namespace image { private: - surface load_image_file() const; - surface load_image_sub_file() const; - int index_; value val_; }; + surface load_from_disk(const locator &loc); + size_t hash_value(const locator::value&);