Author: alink
Date: Fri May  8 02:06:52 2009
New Revision: 35477

URL: http://svn.gna.org/viewcvs/wesnoth?rev=35477&view=rev
Log:
backport r34777 to 1.6: better in_hex optimization
More efficient and less intrusive than the first version ported to 1.6.1
(both for code and loading behavior)

Modified:
    branches/1.6/changelog
    branches/1.6/src/image.cpp
    branches/1.6/src/image.hpp
    branches/1.6/src/unit_frame.cpp
    branches/1.6/src/unit_frame.hpp

Modified: branches/1.6/changelog
URL: 
http://svn.gna.org/viewcvs/wesnoth/branches/1.6/changelog?rev=35477&r1=35476&r2=35477&view=diff
==============================================================================
--- branches/1.6/changelog (original)
+++ branches/1.6/changelog Fri May  8 02:06:52 2009
@@ -31,6 +31,8 @@
    * Improved the sorting of the XP and traits columns in the unit list dialog
      (part of bug #13360)
    * Enabled hinting for texts displayed by Pango/Cairo (bug #13399)
+   * Improve the 1.6.1 framerate optimization about images fiting in a hex
+     (load and scan images only when needed)
 
 Version 1.6.1:
   * Campaigns:

Modified: branches/1.6/src/image.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/branches/1.6/src/image.cpp?rev=35477&r1=35476&r2=35477&view=diff
==============================================================================
--- branches/1.6/src/image.cpp (original)
+++ branches/1.6/src/image.cpp Fri May  8 02:06:52 2009
@@ -46,6 +46,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;
@@ -92,6 +95,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();
@@ -911,16 +915,22 @@
 
 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;
+       }
+}
 
 surface reverse_image(const surface& surf)
 {

Modified: branches/1.6/src/image.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/branches/1.6/src/image.hpp?rev=35477&r1=35476&r2=35477&view=diff
==============================================================================
--- branches/1.6/src/image.hpp (original)
+++ branches/1.6/src/image.hpp Fri May  8 02:06:52 2009
@@ -176,6 +176,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
@@ -196,6 +203,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: branches/1.6/src/unit_frame.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/branches/1.6/src/unit_frame.cpp?rev=35477&r1=35476&r2=35477&view=diff
==============================================================================
--- branches/1.6/src/unit_frame.cpp (original)
+++ branches/1.6/src/unit_frame.cpp Fri May  8 02:06:52 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"]);
@@ -232,22 +230,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);
        }
 
        surface image;
@@ -546,18 +540,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 "stoned" and "team color" 
modifications */
@@ -624,9 +614,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: branches/1.6/src/unit_frame.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/branches/1.6/src/unit_frame.hpp?rev=35477&r1=35476&r2=35477&view=diff
==============================================================================
--- branches/1.6/src/unit_frame.hpp (original)
+++ branches/1.6/src/unit_frame.hpp Fri May  8 02:06:52 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