mail[Wesnoth-commits] r44200 - 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 - 23:48:
Author: alink
Date: Thu Jul 15 23:48:20 2010
New Revision: 44200

URL: http://svn.gna.org/viewcvs/wesnoth?rev=44200&view=rev
Log:
Simpler structure for ToD variants.
Faster for standard cases and will be more flexible for special variants

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=44200&r1=44199&r2=44200&view=diff
==============================================================================
--- trunk/src/builder.cpp (original)
+++ trunk/src/builder.cpp Thu Jul 15 23:48:20 2010
@@ -80,21 +80,19 @@
        }
 
        foreach(const rule_image_rand& ri, images){
-               rule_image_variantlist::const_iterator tod_variant =
-                               ri->variants.find(tod);
+               foreach(const rule_image_variant& variant, ri->variants){
+                       if(!variant.tod.empty() && variant.tod != tod)
+                               continue;
                
-               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());
+                       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(variant.image);
+                       img_list.back().set_animation_time(ri.rand % 
img_list.back().get_animation_duration());
+
+                       break; // found a matching variant 
+               }
        }
 }
 
@@ -301,7 +299,6 @@
        // If not, this rule will not match.
        rule_imagelist::const_iterator image;
        constraint_set::const_iterator constraint;
-       rule_image_variantlist::const_iterator variant;
 
        for(constraint = rule.constraints.begin();
                        constraint != rule.constraints.end(); ++constraint) {
@@ -309,8 +306,8 @@
                                image != constraint->second.images.end();
                                ++image) {
 
-                       for(variant = image->variants.begin(); variant != 
image->variants.end(); ++variant) {
-                               std::string s = variant->second.image_string;
+                       foreach(const rule_image_variant& variant, 
image->variants) {
+                               std::string s = variant.image_string;
                                s = s.substr(0, s.find_first_of(",:~"));
                                // we already precached file existence in the 
constructor
                                // but only for filenames not using ".."
@@ -339,7 +336,6 @@
 {
        rule_imagelist::iterator image;
        constraint_set::iterator constraint;
-       rule_image_variantlist::iterator variant;
 
        for(constraint = rule.constraints.begin();
                        constraint != rule.constraints.end(); ++constraint) {
@@ -348,10 +344,10 @@
                                image != constraint->second.images.end();
                                ++image) {
 
-                       for(variant = image->variants.begin(); variant != 
image->variants.end(); ++variant) {
+                       foreach(rule_image_variant& variant, image->variants) 
{
 
                                animated<image::locator>::anim_description 
image_vector;
-                               std::vector<std::string> items = 
utils::parenthetical_split(variant->second.image_string,',');
+                               std::vector<std::string> items = 
utils::parenthetical_split(variant.image_string,',');
                                std::vector<std::string>::const_iterator itor 
= items.begin();
                                for(; itor != items.end(); ++itor) {
                                        const std::vector<std::string>& items 
= utils::split(*itor, ':');
@@ -382,8 +378,8 @@
 
                                animated<image::locator> th(image_vector);
 
-                               variant->second.image = th;
-                               variant->second.image.start_animation(0, 
true);
+                               variant.image = th;
+                               variant.image.start_animation(0, true);
                        }
                }
        }
@@ -489,10 +485,8 @@
 
 void terrain_builder::replace_token(terrain_builder::rule_image &image, 
const std::string &token, const std::string &replacement)
 {
-       rule_image_variantlist::iterator itor;
-
-       for(itor = image.variants.begin(); itor != image.variants.end(); 
++itor) {
-               replace_token(itor->second, token, replacement);
+       foreach(rule_image_variant& variant, image.variants) {
+               replace_token(variant, token, replacement);
        }
 }
 
@@ -611,18 +605,19 @@
 
                images.push_back(rule_image(layer, basex - dx, basey - dy, 
global, center_x, center_y));
 
-               // Adds the main (default) variant of the image, if present
-               images.back().variants.insert(std::make_pair("", 
rule_image_variant(name)));
-
                // Adds the other variants of the image
                foreach (const config &variant, img.child_range("variant"))
                {
                        const std::string &name = variant["name"];
                        const std::string &tod = variant["tod"];
 
-                       images.back().variants.insert(std::make_pair(tod, 
rule_image_variant(name)));
-
-               }
+                       
images.back().variants.push_back(rule_image_variant(name, tod));
+               }
+
+               // Adds the main (default) variant of the image at the end,
+               // (will be used only if previous variants don't match)
+               images.back().variants.push_back(rule_image_variant(name,""));
+
        }
 }
 

Modified: trunk/src/builder.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/builder.hpp?rev=44200&r1=44199&r2=44200&view=diff
==============================================================================
--- trunk/src/builder.hpp (original)
+++ trunk/src/builder.hpp Thu Jul 15 23:48:20 2010
@@ -143,9 +143,10 @@
         */
        struct rule_image_variant {
                /** Shorthand constructor for this structure */
-               rule_image_variant(const std::string &image_string) :
+               rule_image_variant(const std::string &image_string, const 
std::string& tod = "") :
                        image_string(image_string),
-                       image()
+                       image(),
+                       tod(tod)
                        {};
 
                /** A string representing either the filename for an image, or
@@ -172,13 +173,10 @@
                 * be returned to the user.
                 */
                animated<image::locator> image;
+
+               /** The Time of Day associated to this variant (if any)*/
+               std::string tod;
        };
-
-       /**
-        * A map associating a rule_image_variant to a string representing
-        * the time of day.
-        */
-       typedef std::map<std::string, rule_image_variant> 
rule_image_variantlist;
 
        /**
         * Each terrain_graphics rule is associated a set of images, which are
@@ -197,9 +195,8 @@
                 */
                int basex, basey;
 
-               /** A list of Time-Of-Day-related variants for this image
-                */
-               rule_image_variantlist variants;
+               /** A list of variants for this image */
+               std::vector<rule_image_variant> variants;
 
                /** Set to true if the image was defined as a child of the
                 * [terrain_graphics] tag, set to false if it was defined as a




Related Messages


Powered by MHonArc, Updated Fri Jul 16 00:00:11 2010