mail[Wesnoth-commits] r44164 - in /trunk/src: builder.cpp builder.hpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by alinktomine on July 15, 2010 - 00:00:
Author: alink
Date: Thu Jul 15 00:00:56 2010
New Revision: 44164

URL: http://svn.gna.org/viewcvs/wesnoth?rev=44164&view=rev
Log:
Clean and optimize code sorting terrain layer.

Modified:
    trunk/src/builder.cpp
    trunk/src/builder.hpp

Modified: trunk/src/builder.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/builder.cpp?rev=44164&r1=44163&r2=44164&view=diff
==============================================================================
--- trunk/src/builder.cpp (original)
+++ trunk/src/builder.cpp Thu Jul 15 00:00:56 2010
@@ -44,10 +44,6 @@
  * considered foreground.
  */
 static const int UNITPOS = 36 + 18;
-/** The allowed interval for the base-y position. The possible values are 
from
- * -BASE_Y_INTERVAL to BASE_Y_INTERVAL-1
- */
-static const int BASE_Y_INTERVAL = 100000;
 
 terrain_builder::building_ruleset terrain_builder::building_rules_;
 const config* terrain_builder::rules_cfg_ = NULL;
@@ -67,38 +63,38 @@
        images(),
        images_foreground(),
        images_background(),
-       last_tod("invalid_tod")
+       last_tod("invalid_tod"),
+       sorted_images(false)
 {}
 
-void terrain_builder::tile::add_image_to_cache(const std::string &tod, 
ordered_ri_list::const_iterator itor)
-{
-       rule_image_variantlist::const_iterator tod_variant =
-               itor->second.second->variants.find(tod);
-
-       if(tod_variant == itor->second.second->variants.end())
-               tod_variant = itor->second.second->variants.find("");
-
-       if(tod_variant != itor->second.second->variants.end()) {
-               int layer = itor->second.second->layer;
-               int basey = itor->second.second->basey;
-
-               bool is_background = layer < 0 || (layer == 0 && basey < 
UNITPOS);
-               imagelist& img_list =
-                               is_background ? images_background : 
images_foreground;
-       
-               img_list.push_back(tod_variant->second.image);
-               img_list.back().set_animation_time(itor->second.first % 
img_list.back().get_animation_duration());
-       }
-}
-
-void terrain_builder::tile::rebuild_cache(const std::string &tod)
+void terrain_builder::tile::rebuild_cache(const std::string& tod)
 {
        images_background.clear();
        images_foreground.clear();
 
-       ordered_ri_list::const_iterator itor;
-       for(itor = images.begin(); itor != images.end(); ++itor) {
-               add_image_to_cache(tod, itor);
+       if(!sorted_images){
+               //sort images by their layer (and basey)
+               //but use stable to keep the insertion order in equal cases
+               std::stable_sort(images.begin(), images.end());
+               sorted_images = true;
+       }
+
+       foreach(const rule_image_rand& ri, images){
+               rule_image_variantlist::const_iterator tod_variant =
+                               ri->variants.find(tod);
+               
+               if(tod_variant == ri->variants.end())
+                       tod_variant = ri->variants.find("");
+
+               if(tod_variant == ri->variants.end())
+                       continue;
+                       
+               bool is_background = ri->layer < 0 || (ri->layer == 0 && 
ri->basey < UNITPOS);
+
+               imagelist& img_list = is_background ? images_background : 
images_foreground;
+
+               img_list.push_back(tod_variant->second.image);
+               img_list.back().set_animation_time(ri.rand % 
img_list.back().get_animation_duration());
        }
 }
 
@@ -961,11 +957,7 @@
                tile& btile = tile_map_[tloc];
 
                foreach(const rule_image& img, constraint->second.images) {
-                       // We want to order the images by layer first and 
base-y second,
-                       // so we sort by layer*BASE_Y_INTERVAL + 
BASE_Y_INTERVAL/2 + basey
-                       // Thus, allowed values for basey are from -50000 to 
49999
-                       int order = img.layer*BASE_Y_INTERVAL + 
BASE_Y_INTERVAL/2 + img.basey;
-                       btile.images.insert(std::make_pair(order, 
std::make_pair(rand_seed, &img)));
+                       btile.images.push_back(tile::rule_image_rand(&img, 
rand_seed));
                }
 
                // Sets flags

Modified: trunk/src/builder.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/builder.hpp?rev=44164&r1=44163&r2=44164&view=diff
==============================================================================
--- trunk/src/builder.hpp (original)
+++ trunk/src/builder.hpp Thu Jul 15 00:00:56 2010
@@ -256,22 +256,8 @@
         */
        struct tile
        {
-               /** An ordered rule_image list */
-               typedef std::multimap<int,std::pair<unsigned int, const 
rule_image*> > ordered_ri_list;
-
                /** Contructor for the tile() structure */
                tile();
-
-               /** Adds an image, extracted from an ordered rule_image list,
-                * to the background or foreground image cache.
-                *
-                * @param tod    The current time-of-day, to select between
-                *               images presenting several variants.
-                * @param itor   An iterator pointing to the rule_image where
-                *               to extract the image we wish to add to the
-                *               cache.
-                */
-               void add_image_to_cache(const std::string &tod, 
ordered_ri_list::const_iterator itor);
 
                /** Rebuilds the whole image cache, for a given time-of-day.
                 * Must be called when the time-of-day has changed,
@@ -287,10 +273,23 @@
                /** The list of flags present in this tile */
                std::set<std::string> flags;
 
-               /** The list of images associated to this tile, ordered by
-                * their layer first and base-y position second.
-                */
-               ordered_ri_list images;
+               /** Represent a rule_image applied with a random seed.*/
+               struct rule_image_rand{
+                       rule_image_rand(const rule_image* r_i, unsigned int 
rnd) : ri(r_i), rand(rnd) {}
+
+                       const rule_image* operator->() const {return ri;}
+                       /** sort by layer first then by basey */
+                       bool operator<(const rule_image_rand& o) const {
+                               return ri->layer < o.ri->layer ||
+                                               (ri->layer == o.ri->layer && 
ri->basey < o.ri->basey);}
+
+                       const rule_image* ri;
+                       unsigned int rand;
+               };
+
+               /** The list of rule_images and random seeds associated to 
this tile.
+                */
+               std::vector<rule_image_rand> images;
 
                /** The list of images which are in front of the unit sprites,
                 * attached to this tile. This member is considered a cache:
@@ -307,6 +306,8 @@
                 */
                std::string last_tod;
 
+               /** Indicates if 'images' is sorted */
+               bool sorted_images;
        };
 
 private:




Related Messages


Powered by MHonArc, Updated Thu Jul 15 00:20:04 2010