Author: alink
Date: Sun Apr 12 14:08:21 2009
New Revision: 34777

URL: http://svn.gna.org/viewcvs/wesnoth?rev=34777&view=rev
Log:
Simplify and optimize the in_hex caching by remove it from amimation code
and instead implement it like other image caches (but just storing bool)
This allow to load and scan images only when needed.
Also fix a tiny bug when using :refresh after modifiying sprite's size.

Modified:
    trunk/src/image.cpp
    trunk/src/image.hpp
    trunk/src/unit_frame.cpp
    trunk/src/unit_frame.hpp

Modified: trunk/src/image.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/image.cpp?rev=34777&r1=34776&r2=34777&view=diff
==============================================================================
--- trunk/src/image.cpp (original)
+++ trunk/src/image.cpp Sun Apr 12 14:08:21 2009
@@ -47,6 +47,9 @@
 image::image_cache 
images_,hexed_images_,scaled_to_hex_images_,scaled_to_zoom_,unmasked_images_;
 image::image_cache brightened_images_,semi_brightened_images_;
 
+// cache storing if each image fit in a hex
+image::bool_cache in_hex_info_;
+
 // const int cache_version_ = 0;
 
 std::map<image::locator,bool> image_existence_map;
@@ -93,6 +96,7 @@
        unmasked_images_.flush();
        brightened_images_.flush();
        semi_brightened_images_.flush();
+       in_hex_info_.flush();
        mini_terrain_cache.clear();
        mini_fogged_terrain_cache.clear();
        reversed_images_.clear();
@@ -946,14 +950,21 @@
 
 bool is_in_hex(const locator& i_locator)
 {
-       const surface mask(get_image(game_config::terrain_mask_image, 
UNSCALED));
-       const surface image(get_image(i_locator, UNSCALED));
-
-       bool res = in_mask_surface(image, mask);
-       //std::cout << "in_hex : " << i_locator.get_filename()
-       //              << " " << (res ? "yes" : "no") << "\n";
-
-       return res;
+       if(i_locator.in_cache(in_hex_info_)) {
+               return i_locator.locate_in_cache(in_hex_info_);
+       } else {
+               const surface mask(get_image(game_config::terrain_mask_image, 
UNSCALED));
+               const surface image(get_image(i_locator, UNSCALED));
+
+               bool res = in_mask_surface(image, mask);
+               
+               i_locator.add_to_cache(in_hex_info_, res);
+
+               //std::cout << "in_hex : " << i_locator.get_filename()
+               //              << " " << (res ? "yes" : "no") << "\n";
+
+               return res;
+       }
 }
 
 

Modified: trunk/src/image.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/image.hpp?rev=34777&r1=34776&r2=34777&view=diff
==============================================================================
--- trunk/src/image.hpp (original)
+++ trunk/src/image.hpp Sun Apr 12 14:08:21 2009
@@ -101,8 +101,6 @@
        {
        public:
                enum type { NONE, FILE, SUB_FILE };
-
-
        private:
                // Called by each constructor after actual construction to
                // initialize the index_ field
@@ -191,6 +189,13 @@
                void add_to_cache(cache_type<surface>& cache, const surface 
&image) const
                        { if(index_ != -1 ) cache.get_element(index_) = 
cache_item<surface>(image); cache.on_load(index_); }
 
+               bool in_cache(cache_type<bool>& cache) const
+                       { return index_ == -1 ? false : 
cache.get_element(index_).loaded; }
+               bool locate_in_cache(cache_type<bool>& cache) const
+                       { return index_ == -1 ? false : 
cache.get_element(index_).item; }
+               void add_to_cache(cache_type<bool>& cache, bool st) const
+                       { if(index_ != -1 ) cache.get_element(index_) = 
cache_item<bool>(st); cache.on_load(index_); }
+
                bool in_cache(cache_type<locator>& cache) const
                        { return index_ == -1 ? false : 
cache.get_element(index_).loaded; cache.on_load(index_); }
                locator locate_in_cache(cache_type<locator>& cache) const
@@ -213,6 +218,7 @@
 
        typedef cache_type<surface> image_cache;
        typedef cache_type<locator> locator_cache;
+       typedef cache_type<bool> bool_cache;
        typedef std::map<t_translation::t_terrain, surface> 
mini_terrain_cache_map;
        extern mini_terrain_cache_map mini_terrain_cache;
        extern mini_terrain_cache_map mini_fogged_terrain_cache;

Modified: trunk/src/unit_frame.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit_frame.cpp?rev=34777&r1=34776&r2=34777&view=diff
==============================================================================
--- trunk/src/unit_frame.cpp (original)
+++ trunk/src/unit_frame.cpp Sun Apr 12 14:08:21 2009
@@ -174,9 +174,7 @@
        submerge_(""),
        x_(""),
        y_(""),
-       drawing_layer_(""),
-       in_hex_(false),
-       diagonal_in_hex_(false)
+       drawing_layer_("")
 {
        
image(image::locator(cfg[frame_string+"image"]),cfg[frame_string+"image_mod"]);
        
image_diagonal(image::locator(cfg[frame_string+"image_diagonal"]),cfg[frame_string+"image_mod"]);
@@ -231,22 +229,18 @@
        result.x = x_.get_current_element(current_time);
        result.y = y_.get_current_element(current_time);
        result.drawing_layer = 
drawing_layer_.get_current_element(current_time,display::LAYER_UNIT_DEFAULT-display::LAYER_UNIT_FIRST);
-       result.in_hex = in_hex_;
-       result.diagonal_in_hex = diagonal_in_hex_;
        return result;
 }
 frame_builder & frame_builder::image(const image::locator image ,const 
std::string & image_mod)
 {
        image_ = image;
        image_mod_ = image_mod;
-       in_hex_ = is_in_hex(image);
        return *this;
 }
 frame_builder & frame_builder::image_diagonal(const image::locator 
image_diagonal,const std::string& image_mod)
 {
        image_diagonal_ = image_diagonal;
        image_mod_ = image_mod;
-       diagonal_in_hex_ = is_in_hex(image_diagonal);
        return *this;
 }
 frame_builder & frame_builder::sound(const std::string& sound)
@@ -476,11 +470,11 @@
        image::locator image_loc;
        if(direction != map_location::NORTH && direction != 
map_location::SOUTH) {
                image_loc = current_data.image_diagonal;
-               image_fit_hex = current_data.diagonal_in_hex;
+               image_fit_hex = image::is_in_hex(image_loc);
        }
        if(image_loc.is_void() || image_loc.get_filename() == "") { // invalid 
diag image, or not diagonal
                image_loc = current_data.image;
-               image_fit_hex = current_data.in_hex;
+               image_fit_hex = image::is_in_hex(image_loc);
        }
 
        // we always invalidate our own hex because we need to be called at 
redraw time even
@@ -541,18 +535,14 @@
 
        /** engine provides a default image to use for the unit when none is 
available */
        result.image = current_val.image.is_void() || 
current_val.image.get_filename() == ""?animation_val.image:current_val.image;
-       result.in_hex = current_val.image.is_void() || 
current_val.image.get_filename() == ""?animation_val.in_hex:current_val.in_hex;
        if(primary && ( result.image.is_void() || 
result.image.get_filename().empty())) {
                result.image = engine_val.image;
-               result.in_hex = engine_val.in_hex;
        }
 
        /** engine provides a default image to use for the unit when none is 
available */
        result.image_diagonal = current_val.image_diagonal.is_void() || 
current_val.image_diagonal.get_filename() == 
""?animation_val.image_diagonal:current_val.image_diagonal;
-       result.diagonal_in_hex = current_val.image_diagonal.is_void() || 
current_val.image_diagonal.get_filename() == 
""?animation_val.diagonal_in_hex:current_val.diagonal_in_hex;
        if(primary && ( result.image_diagonal.is_void() || 
result.image_diagonal.get_filename().empty())) {
                result.image_diagonal = engine_val.image_diagonal;
-               result.diagonal_in_hex = engine_val.diagonal_in_hex;
        }
 
        /** engine provides a string for "petrified" and "team color" 
modifications */
@@ -619,9 +609,7 @@
 #ifdef LOW_MEM
        if(primary) {
                result.image= engine_val.image;
-               result.in_hex = engine_val.in_hex;
                result.image_diagonal= engine_val.image;
-               result.diagonal_in_hex = engine_val.diagonal_in_hex;
        }
 #endif
        return result;

Modified: trunk/src/unit_frame.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit_frame.hpp?rev=34777&r1=34776&r2=34777&view=diff
==============================================================================
--- trunk/src/unit_frame.hpp (original)
+++ trunk/src/unit_frame.hpp Sun Apr 12 14:08:21 2009
@@ -85,9 +85,7 @@
        submerge(0.0),
        x(0),
        y(0),
-       drawing_layer(display::LAYER_UNIT_DEFAULT-display::LAYER_UNIT_FIRST),
-       in_hex(false),
-       diagonal_in_hex(false)
+       drawing_layer(display::LAYER_UNIT_DEFAULT-display::LAYER_UNIT_FIRST)
        {};
 
        image::locator image;
@@ -138,9 +136,7 @@
                submerge_(""),
                x_(""),
                y_(""),
-               
drawing_layer_(lexical_cast<std::string>(display::LAYER_UNIT_DEFAULT-display::LAYER_UNIT_FIRST)),
-               in_hex_(false),
-               diagonal_in_hex_(false)
+               
drawing_layer_(lexical_cast<std::string>(display::LAYER_UNIT_DEFAULT-display::LAYER_UNIT_FIRST))
        {};
                frame_builder(const config& cfg,const std::string &frame_string 
= "");
                /** allow easy chained modifications will raised assert if used 
after initialization */
@@ -184,8 +180,6 @@
                progressive_int x_;
                progressive_int y_;
                progressive_int drawing_layer_;
-               bool in_hex_;
-               bool diagonal_in_hex_;
 };
 
 /** Describe a unit's animation sequence. */


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to