Skip to content

Commit

Permalink
Move load_from_disk out of image::locator.
Browse files Browse the repository at this point in the history
  • Loading branch information
lipk committed Jun 10, 2014
1 parent b39601d commit 2b7da8f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 70 deletions.
130 changes: 66 additions & 64 deletions src/image.cpp
Expand Up @@ -60,6 +60,8 @@ struct cache_item
bool loaded;
};



namespace image {

template<typename T>
Expand Down Expand Up @@ -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());


{
Expand All @@ -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<signed char>(std::min<int>(127, std::max<int>(-128, i/2)));
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions src/image.hpp
Expand Up @@ -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 <typename T>
bool in_cache(cache_type<T> &cache) const;
template <typename T>
Expand All @@ -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&);


Expand Down

0 comments on commit 2b7da8f

Please sign in to comment.