mail[Wesnoth-commits] r24436 - in /trunk/src: unit_animation.cpp unit_frame.cpp unit_frame.hpp


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

Header


Content

Posted by jeremy . rosen on March 09, 2008 - 12:33:
Author: boucman
Date: Sun Mar  9 12:33:15 2008
New Revision: 24436

URL: http://svn.gna.org/viewcvs/wesnoth?rev=24436&view=rev
Log:
use named parameters to simplify unit_frame creation. crude_animation is next

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

Modified: trunk/src/unit_animation.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit_animation.cpp?rev=24436&r1=24435&r2=24436&view=diff
==============================================================================
--- trunk/src/unit_animation.cpp (original)
+++ trunk/src/unit_animation.cpp Sun Mar  9 12:33:15 2008
@@ -298,10 +298,11 @@
                }
        }
        if( animation_base.empty() )
-               
animation_base.push_back(unit_animation(0,unit_frame(default_image,600),"",unit_animation::DEFAULT_ANIM));
-
-       
animations.push_back(unit_animation(0,unit_frame(default_image,1),"_disabled_",0));
-       
animations.push_back(unit_animation(0,unit_frame(default_image,1,"","",display::rgb(255,255,255),"0.0~0.3:100,0.3~0.0:200"),"_disabled_selected_",0));
+               
animation_base.push_back(unit_animation(0,frame_builder().image(default_image).duration(600),"",unit_animation::DEFAULT_ANIM));
+
+       
animations.push_back(unit_animation(0,frame_builder().image(default_image).duration(1),"_disabled_",0));
+       
animations.push_back(unit_animation(0,frame_builder().image(default_image).duration(1).
+                                       
blend("0.0~0.3:100,0.3~0.0:200",display::rgb(255,255,255)),"_disabled_selected_",0));
        for(itor = animation_base.begin() ; itor != animation_base.end() ; 
itor++ ) {
                unit_animation tmp_anim = *itor;
                // provide all default anims
@@ -369,8 +370,9 @@
                tmp_anim.event_ = utils::split("death");
                animations.push_back(tmp_anim);
                animations.back().sub_anims_["_death_sound"] = 
crude_animation();
-               
animations.back().sub_anims_["_death_sound"].add_frame(1,unit_frame());
-               
animations.back().sub_anims_["_death_sound"].add_frame(1,unit_frame(image::locator(),1,"","",0,"","","","","",cfg["die_sound"]),true);
+               
animations.back().sub_anims_["_death_sound"].add_frame(1,frame_builder());
+               
animations.back().sub_anims_["_death_sound"].add_frame(1,frame_builder().sound(cfg["die_sound"]),true);
+
 
                tmp_anim = *itor;
                tmp_anim.unit_anim_.override(0,"1~0:150");
@@ -390,8 +392,8 @@
                tmp_anim.event_ = utils::split("healed");
                animations.push_back(tmp_anim);
                animations.back().sub_anims_["_healed_sound"] = 
crude_animation();
-               
animations.back().sub_anims_["_healed_sound"].add_frame(1,unit_frame());
-               
animations.back().sub_anims_["_healed_sound"].add_frame(1,unit_frame(image::locator(),1,"","",0,"","","","","","heal.wav"),true);
+               
animations.back().sub_anims_["_healed_sound"].add_frame(1,frame_builder());
+               
animations.back().sub_anims_["_healed_sound"].add_frame(1,frame_builder().sound("heal.wav"),true);
 
                tmp_anim = *itor;
                
tmp_anim.unit_anim_.override(0,"","0:30,0.5:30,0:30,0.5:30,0:30,0.5:30,0:30,0.5:30,0:30",display::rgb(0,255,0));
@@ -399,8 +401,8 @@
                tmp_anim.event_ = utils::split("poisoned");
                animations.push_back(tmp_anim);
                animations.back().sub_anims_["_poison_sound"] = 
crude_animation();
-               
animations.back().sub_anims_["_poison_sound"].add_frame(1,unit_frame());
-               
animations.back().sub_anims_["_poison_sound"].add_frame(1,unit_frame(image::locator(),1,"","",0,"","","","","","poison.ogg"),true);
+               
animations.back().sub_anims_["_poison_sound"].add_frame(1,frame_builder());
+               
animations.back().sub_anims_["_poison_sound"].add_frame(1,frame_builder().sound("poison.ogg"),true);
 
        }
 
@@ -467,8 +469,8 @@
                (**anim_itor)["value"]=(**anim_itor)["healing"];
                animations.push_back(unit_animation(**anim_itor));
                animations.back().sub_anims_["_healed_sound"] = 
crude_animation();
-               
animations.back().sub_anims_["_healed_sound"].add_frame(1,unit_frame());
-               
animations.back().sub_anims_["_healed_sound"].add_frame(1,unit_frame(image::locator(),1,"","",0,"","","","","","heal.wav"),true);
+               
animations.back().sub_anims_["_healed_sound"].add_frame(1,frame_builder());
+               
animations.back().sub_anims_["_healed_sound"].add_frame(1,frame_builder().sound("heal.wav"),true);
        }
        expanded_cfg = unit_animation::prepare_animation(cfg,"poison_anim");
        const config::child_list& poison_anims = 
expanded_cfg.get_children("poison_anim");
@@ -477,8 +479,8 @@
                (**anim_itor)["value"]=(**anim_itor)["damage"];
                animations.push_back(unit_animation(**anim_itor));
                animations.back().sub_anims_["_poison_sound"] = 
crude_animation();
-               
animations.back().sub_anims_["_poison_sound"].add_frame(1,unit_frame());
-               
animations.back().sub_anims_["_poison_sound"].add_frame(1,unit_frame(image::locator(),1,"","",0,"","","","","","poison.ogg"),true);
+               
animations.back().sub_anims_["_poison_sound"].add_frame(1,frame_builder());
+               
animations.back().sub_anims_["_poison_sound"].add_frame(1,frame_builder().sound("poison.ogg"),true);
        }
        expanded_cfg = unit_animation::prepare_animation(cfg,"movement_anim");
        const config::child_list& movement_anims = 
expanded_cfg.get_children("movement_anim");
@@ -498,14 +500,20 @@
                        (**anim_itor)["value"]=(**anim_itor)["damage"];
                        animations.push_back(unit_animation(**anim_itor));
                        if(atoi((**anim_itor)["value"].c_str()) != 0) {
-                               
animations.back().add_frame(100,unit_frame(animations.back().get_last_frame().image(),100,"1.0","",game_display::rgb(255,0,0),"0.5:50,0.0:50"));
+                               
animations.back().add_frame(100,frame_builder()
+                                                       
.image(animations.back().get_last_frame().image())
+                                                       .duration(100)
+                                                       
.blend("0.5:50,0.0:50",game_display::rgb(255,0,0)));
                        }
                } else {
                        (**anim_itor)["value"]="0";
-                       animations.push_back(unit_animation(**anim_itor)),
+                       animations.push_back(unit_animation(**anim_itor));
                        (**anim_itor)["value"]="";
-                       animations.push_back(unit_animation(**anim_itor)),
-                       
animations.back().add_frame(100,unit_frame(animations.back().get_last_frame().image(),100,"1.0","",game_display::rgb(255,0,0),"0.5:50,0.0:50"));
+                       animations.push_back(unit_animation(**anim_itor));
+                               
animations.back().add_frame(100,frame_builder()
+                                                       
.image(animations.back().get_last_frame().image())
+                                                       .duration(100)
+                                                       
.blend("0.5:50,0.0:50",game_display::rgb(255,0,0)));
                }
        }
        expanded_cfg = unit_animation::prepare_animation(cfg,"attack_anim");
@@ -532,11 +540,11 @@
                (**anim_itor)["apply_to"] ="death";
                animations.push_back(unit_animation(**anim_itor));
                image::locator image_loc = 
animations.back().get_last_frame().image();
-               
animations.back().add_frame(600,unit_frame(image_loc,600,"1~0:600"));
+               
animations.back().add_frame(600,frame_builder().image(image_loc).duration(600).highlight("1~0:600"));
                if(!cfg["die_sound"].empty()) {
                        animations.back().sub_anims_["_death_sound"] = 
crude_animation();
-                       
animations.back().sub_anims_["_death_sound"].add_frame(1,unit_frame());
-                       
animations.back().sub_anims_["_death_sound"].add_frame(1,unit_frame(image::locator(),1,"","",0,"","","","","",cfg["die_sound"]),true);
+                       
animations.back().sub_anims_["_death_sound"].add_frame(1,frame_builder());
+                       
animations.back().sub_anims_["_death_sound"].add_frame(1,frame_builder().sound(cfg["die_sound"]),true);
                }
        }
        // Always have a defensive animation
@@ -749,8 +757,8 @@
        unit_anim_.start_animation(start_time, src, dst, cycles);
        if(!text.empty()) {
                crude_animation crude_build;
-               crude_build.add_frame(1,unit_frame());
-               
crude_build.add_frame(1,unit_frame(image::locator(),1,"","",0,"","","","","","",text,text_color),true);
+               crude_build.add_frame(1,frame_builder());
+               
crude_build.add_frame(1,frame_builder().text(text,text_color),true);
                sub_anims_["_add_text"] = crude_build;
        }
        std::map<std::string,crude_animation>::iterator anim_itor 
=sub_anims_.begin();

Modified: trunk/src/unit_frame.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit_frame.cpp?rev=24436&r1=24435&r2=24436&view=diff
==============================================================================
--- trunk/src/unit_frame.cpp (original)
+++ trunk/src/unit_frame.cpp Sun Mar  9 12:33:15 2008
@@ -149,72 +149,88 @@
 #undef UNIT_FRAME_H_PART2
 #include "unit_frame.hpp"
 
-unit_frame::unit_frame() :
-       image_(), image_diagonal_(),halo_(), sound_(),
-       text_(""),text_color_(0),
-       halo_x_(), halo_y_(), duration_(0),
-       blend_with_(0),blend_ratio_(),
-       highlight_ratio_(""), offset_()
-{
-}
-
-unit_frame::unit_frame(const image::locator& image, int duration,
-               const std::string& highlight, const std::string& offset,
-               Uint32 blend_color, const std::string& blend_rate,
-               const std::string& in_halo, const std::string& halox, const 
std::string& haloy,
-               const image::locator & diag,const std::string & sound,
-               const std::string & text, const Uint32 text_color) :
-       image_(image),image_diagonal_(diag),
-       halo_(in_halo,duration),
-       sound_(sound),
-       text_(text), text_color_(text_color),
-       halo_x_(halox,duration),
-       halo_y_(haloy,duration),
-       duration_(duration),
-       blend_with_(blend_color), blend_ratio_(blend_rate,duration),
-       highlight_ratio_(highlight,duration),offset_(offset,duration)
-{
-       // let's decide of duration ourselves
-       duration_ = maximum<int>(duration_, highlight_ratio_.duration());
-       duration_ = maximum<int>(duration_, blend_ratio_.duration());
-       duration_ = maximum<int>(duration_, halo_.duration());
-       duration_ = maximum<int>(duration_, offset_.duration());
-}
+
 
 unit_frame::unit_frame(const config& cfg)
 {
-       image_ = image::locator(cfg["image"]);
-       image_diagonal_ = image::locator(cfg["image_diagonal"]);
-       sound_ = cfg["sound"];
-       text_ = cfg["text"];
+       internal_param_.image(image::locator(cfg["image"]));
+       internal_param_.image_diagonal(image::locator(cfg["image_diagonal"]));
+       internal_param_.sound(cfg["sound"]);
        std::vector<std::string> 
tmp_string_vect=utils::split(cfg["text_color"]);
        if(tmp_string_vect.size() ==3) {
-               text_color_ = 
display::rgb(atoi(tmp_string_vect[0].c_str()),atoi(tmp_string_vect[1].c_str()),atoi(tmp_string_vect[2].c_str()));
+       internal_param_.text(cfg["text"],
+                
display::rgb(atoi(tmp_string_vect[0].c_str()),atoi(tmp_string_vect[1].c_str()),atoi(tmp_string_vect[2].c_str())));
        } else {
-               text_color_ = 0;
+               internal_param_.text(cfg["text"],0);
        }
 
        if(!cfg["duration"].empty()) {
-               duration_ = atoi(cfg["duration"].c_str());
+               internal_param_.duration(atoi(cfg["duration"].c_str()));
        } else {
-               duration_ = atoi(cfg["end"].c_str()) - 
atoi(cfg["begin"].c_str());
-       }
-       halo_ = progressive_string(cfg["halo"],duration_);
-       halo_x_ = progressive_int(cfg["halo_x"],duration_);
-       halo_y_ = progressive_int(cfg["halo_y"],duration_);
+               internal_param_.duration(atoi(cfg["end"].c_str()) - 
atoi(cfg["begin"].c_str()));
+       }
+       internal_param_.halo(cfg["halo"],cfg["halo_x"],cfg["halo_y"]);
         tmp_string_vect=utils::split(cfg["blend_color"]);
        if(tmp_string_vect.size() ==3) {
-       blend_with_= 
display::rgb(atoi(tmp_string_vect[0].c_str()),atoi(tmp_string_vect[1].c_str()),atoi(tmp_string_vect[2].c_str()));
+               
internal_param_.blend(cfg["blend_ratio"],display::rgb(atoi(tmp_string_vect[0].c_str()),atoi(tmp_string_vect[1].c_str()),atoi(tmp_string_vect[2].c_str())));
        } else {
-               blend_with_ = 0;
-       }
-       blend_ratio_ = progressive_double(cfg["blend_ratio"],duration_);
-       highlight_ratio_ = progressive_double(cfg["alpha"],duration_);
-       offset_ = progressive_double(cfg["offset"],duration_);
-
-}
-
-bool unit_frame::does_not_change() const
+               internal_param_.blend(cfg["blend_ratio"],0);
+       }
+       internal_param_.highlight(cfg["alpha"]);
+       internal_param_.offset(cfg["offset"]);
+
+}
+
+frame_builder & frame_builder::image(const image::locator image )
+{
+       image_ = image;
+       return *this;
+}
+frame_builder & frame_builder::image_diagonal(const image::locator 
image_diagonal)
+{
+       image_diagonal_ = image_diagonal;
+       return *this;
+}
+frame_builder & frame_builder::sound(const std::string& sound)
+{
+       sound_=sound;
+       return *this;
+}
+frame_builder & frame_builder::text(const std::string& text,const  Uint32 
text_color)
+{
+       text_=text;
+       text_color_=text_color;
+       return *this;
+}
+frame_builder & frame_builder::halo(const std::string &halo, const 
std::string &halo_x, const std::string& halo_y)
+{
+       halo_ = progressive_string(halo,duration_);
+       halo_x_ = progressive_int(halo_x,duration_);
+       halo_y_ = progressive_int(halo_y,duration_);
+       return *this;
+}
+frame_builder & frame_builder::duration(const int duration)
+{
+       duration_= duration;
+       return *this;
+}
+frame_builder & frame_builder::blend(const std::string& blend_ratio,const 
Uint32 blend_color)
+{
+       blend_with_=blend_color;
+       blend_ratio_=progressive_double(blend_ratio,duration_);
+       return *this;
+}
+frame_builder & frame_builder::highlight(const std::string& highlight)
+{
+       highlight_ratio_=progressive_double(highlight,duration_);
+       return *this;
+}
+frame_builder & frame_builder::offset(const std::string& offset)
+{
+       offset_=progressive_double(offset);
+       return *this;
+}
+bool frame_builder::does_not_change() const
 {
        return halo_.does_not_change() &&
                halo_x_.does_not_change() &&
@@ -223,7 +239,7 @@
                highlight_ratio_.does_not_change() &&
                offset_.does_not_change();
 }
-bool unit_frame::need_update() const
+bool frame_builder::need_update() const
 {
        if(!halo_.does_not_change() ||
                        !halo_x_.does_not_change() ||

Modified: trunk/src/unit_frame.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/unit_frame.hpp?rev=24436&r1=24435&r2=24436&view=diff
==============================================================================
--- trunk/src/unit_frame.hpp (original)
+++ trunk/src/unit_frame.hpp Sun Mar  9 12:33:15 2008
@@ -58,33 +58,50 @@
 #ifndef UNIT_FRAME_H_PART2
 #define UNIT_FRAME_H_PART2
 
-//! Describe a unit's animation sequence.
-class unit_frame {
+//! keep most parameters in a separate class to simplify handling of large 
number of parameters
+class frame_builder {
        public:
-       // Constructors
-               unit_frame();
-               explicit unit_frame(const image::locator& image, int 
duration=0,
-                               const std::string& highlight="",const 
std::string& offset="",
-                               Uint32 blend_color = 0, const std::string& 
blend_rate = "",
-                               const std::string & in_halo = "",
-                               const std::string & halox = "",const 
std::string & haloy = "",
-                               const image::locator & diag ="",const 
std::string & sound = "",const std::string & text = "", const Uint32 
text_color=0);
-               explicit unit_frame(const config& cfg);
-               image::locator image() const { return image_ ;}
-               image::locator image_diagonal() const { return 
image_diagonal_ ; }
+               //! initial constructor
+               frame_builder():
+               image_(image::locator()),
+               image_diagonal_(image::locator()),
+               halo_(""),
+               sound_(""),
+               text_(""),
+               text_color_(0),
+               halo_x_(""),
+               halo_y_(""),
+               duration_(1),
+               blend_with_(0),
+               blend_ratio_(""),
+               highlight_ratio_(""),
+               offset_("") {};
+               //! allow easy chained modifications
+               frame_builder & image(const image::locator image );
+               frame_builder & image_diagonal(const image::locator 
image_diagonal);
+               frame_builder & sound(const std::string& sound);
+               frame_builder & text(const std::string& text,const  Uint32 
text_color);
+               frame_builder & halo(const std::string &halo, const 
std::string &halo_x, const std::string& halo_y);
+               frame_builder & duration(const int duration);
+               frame_builder & blend(const std::string& blend_ratio,const 
Uint32 blend_color);
+               frame_builder & highlight(const std::string& highlight);
+               frame_builder & offset(const std::string& offset);
+               //! getters for the different parameters
+               const image::locator image() const { return image_ ;}
+               const image::locator image_diagonal() const { return 
image_diagonal_ ; }
                const std::string &halo(int current_time,const std::string& 
default_val="") const
                        { return 
halo_.get_current_element(current_time,default_val); }
 
-               std::string sound() const { return sound_ ; };
-               std::pair<std::string,Uint32> text() const { return 
std::pair<std::string,Uint32>(text_,text_color_) ; };
-               int halo_x(int current_time,const int default_val=0) const { 
return halo_x_.get_current_element(current_time,default_val); }
-               int halo_y(int current_time,const int default_val=0) const { 
return halo_y_.get_current_element(current_time,default_val); }
-               int duration() const { return duration_; }
-               Uint32 blend_with(const Uint32 default_val) const { return 
blend_with_?blend_with_:default_val; }
-               double blend_ratio(int current_time,const double 
default_val=0.0) const
+               const std::string sound() const { return sound_ ; };
+               const std::pair<std::string,Uint32> text() const { return 
std::pair<std::string,Uint32>(text_,text_color_) ; };
+               const int halo_x(int current_time,const int default_val=0) 
const { return halo_x_.get_current_element(current_time,default_val); }
+               const int halo_y(int current_time,const int default_val=0) 
const { return halo_y_.get_current_element(current_time,default_val); }
+               const int duration() const { return duration_; }
+               const Uint32 blend_with(const Uint32 default_val) const { 
return blend_with_?blend_with_:default_val; }
+               const double blend_ratio(int current_time,const double 
default_val=0.0) const
                        { return 
blend_ratio_.get_current_element(current_time,default_val); }
 
-               fixed_t highlight_ratio(int current_time,double default_val 
=0.0) const
+               const fixed_t highlight_ratio(int current_time,double 
default_val =0.0) const
                        {  return 
ftofxp(highlight_ratio_.get_current_element(current_time,default_val)); }
 
                double offset(int current_time,double default_val =0.0) const
@@ -96,7 +113,6 @@
                image::locator image_;
                image::locator image_diagonal_;
                progressive_string halo_;
-
                std::string sound_;
                std::string text_;
                Uint32 text_color_;
@@ -108,5 +124,35 @@
                progressive_double highlight_ratio_;
                progressive_double offset_;
 };
+//! Describe a unit's animation sequence.
+class unit_frame{
+       public:
+               // Constructors
+               unit_frame(const frame_builder 
builder=frame_builder()):internal_param_(builder){};
+               explicit unit_frame(const config& cfg);
+
+               bool does_not_change() const { return 
internal_param_.does_not_change();}
+               bool need_update() const { return 
internal_param_.need_update();}
+               // Passing internal params
+               const image::locator image() const { return 
internal_param_.image() ;}
+               const image::locator image_diagonal() const { return 
internal_param_.image_diagonal() ; }
+               const std::string &halo(int current_time,const std::string& 
default_val="") const
+                       { return 
internal_param_.halo(current_time,default_val); }
+               const std::string sound() const { return 
internal_param_.sound() ; };
+               const std::pair<std::string,Uint32> text() const { return 
internal_param_.text(); };
+               const int halo_x(int current_time,const int default_val=0) 
const { return internal_param_.halo_x(current_time,default_val); }
+               const int halo_y(int current_time,const int default_val=0) 
const { return internal_param_.halo_y(current_time,default_val); }
+               const int duration() const { return 
internal_param_.duration(); }
+               const Uint32 blend_with(const Uint32 default_val) const { 
return internal_param_.blend_with(default_val); }
+               const double blend_ratio(int current_time,const double 
default_val=0.0) const
+                       { return 
internal_param_.blend_ratio(current_time,default_val); }
+               const fixed_t highlight_ratio(int current_time,double 
default_val =0.0) const
+                       {  return 
internal_param_.highlight_ratio(current_time,default_val); }
+               const double offset(int current_time,double default_val =0.0) 
const
+                       { return 
internal_param_.offset(current_time,default_val)  ; }
+
+       private:
+               frame_builder internal_param_;
+};
 
 #endif




Related Messages


Powered by MHonArc, Updated Sun Mar 09 13:41:11 2008