Author: shadowmaster
Date: Sun Nov  2 20:57:29 2008
New Revision: 30546

URL: http://svn.gna.org/viewcvs/wesnoth?rev=30546&view=rev
Log:
* Rewrote the experimental WML write interface. Now it is finished
  and ready for real use.
* Sourcespecs may be built from WML config objects directly (reader
  interface).

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

Modified: trunk/src/soundsource.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/soundsource.cpp?rev=30546&r1=30545&r2=30546&view=diff
==============================================================================
--- trunk/src/soundsource.cpp (original)
+++ trunk/src/soundsource.cpp Sun Nov  2 20:57:29 2008
@@ -20,11 +20,15 @@
 #include "config.hpp"
 #include "display.hpp"
 #include "foreach.hpp"
+#include "log.hpp"
 #include "pathutils.hpp"
+#include "serialization/string_utils.hpp"
 #include "sound.hpp"
 #include "soundsource.hpp"
 #include "util.hpp"
 
+#define DEFAULT_CHANCE                         100
+#define DEFAULT_DELAY                          1000
 #define DEFAULT_FULL_RANGE                     3
 #define DEFAULT_FADE_RANGE                     14
 
@@ -107,6 +111,16 @@
                (*it).second->add_location(loc);
 }
 
+void manager::write_sourcespecs(config& cfg) const
+{
+       for(positional_source_const_iterator i = sources_.begin(); i != 
sources_.end(); ++i) {
+               assert(i->second);
+               
+               config& child = cfg.add_child("sound_source");
+               child["id"] = i->first;
+               i->second->write_config(child);
+       }
+}
 
 positional_source::positional_source(const sourcespec &spec) :
        last_played_(0),
@@ -202,33 +216,59 @@
        locations_.push_back(loc);
 }
 
-void sourcespec::write(config& cfg) const
-{
-       config& info = cfg.add_child("sound_source");
-       
-       info["id"] = this->id_;
-       info["sounds"] = this->files_;
-       info["delay"] = str_cast<int>(this->min_delay_);
-       info["chance"] = str_cast<int>(this->chance_);
-       info["check_fogged"] = this->check_fogged_ ? "yes" : "no";
-       
-       info["x"] = info["y"] = "";
+void positional_source::write_config(config& cfg) const
+{
+       cfg["sounds"] = this->files_;
+       cfg["delay"] = str_cast<unsigned int>(this->min_delay_);
+       cfg["chance"] = str_cast<unsigned int>(this->chance_);
+       cfg["check_fogged"] = this->check_fogged_ ? "yes" : "no";
+       
+       cfg["x"] = cfg["y"] = "";
        bool first_loc = true;
        
        foreach(const map_location& loc, this->locations_) {
                if(!first_loc) {
-                       info["x"] += ",";
-                       info["y"] += ",";
+                       cfg["x"] += ",";
+                       cfg["y"] += ",";
                } else {
                        first_loc = false;
                }
-               info["x"] += str_cast<int>(loc.x);
-               info["y"] += str_cast<int>(loc.y);
-       }
-       
-       info["loop"] = str_cast<int>(this->loops_);
-       info["full_range"] = str_cast<int>(this->range_);
-       info["fade_range"] = str_cast<int>(this->faderange_);
+               cfg["x"] += str_cast<unsigned int>(loc.x);
+               cfg["y"] += str_cast<unsigned int>(loc.y);
+       }
+       
+       cfg["loop"] = str_cast<unsigned int>(this->loops_);
+       cfg["full_range"] = str_cast<unsigned int>(this->range_);
+       cfg["fade_range"] = str_cast<unsigned int>(this->faderange_);
+}
+
+sourcespec::sourcespec(const config& cfg) :
+       id_(cfg["id"]),
+       files_(cfg["sounds"]),
+       min_delay_(lexical_cast_default<int>(cfg["delay"], DEFAULT_DELAY)),
+       chance_(lexical_cast_default<int>(cfg["chance"], DEFAULT_CHANCE)),
+       loops_(lexical_cast_default<int>(cfg["loop"], 0)),
+       range_(lexical_cast_default<int>(cfg["full_range"], 3)),
+       faderange_(lexical_cast_default<int>(cfg["fade_range"], 14)),
+       check_fogged_(utils::string_bool(cfg["check_fogged"], true)),
+       locations_()
+{
+       const std::vector<std::string>& vx = utils::split(cfg["x"]);
+       const std::vector<std::string>& vy = utils::split(cfg["y"]);
+       
+       if(vx.empty() || vy.empty()) {
+               lg::wml_error << "missing sound source locations";
+       }
+
+       if(vx.size() != vy.size()) {
+               lg::wml_error << "mismatched number of sound source location 
coordinates";
+       }
+       else {
+               for(unsigned int i = 0; i < std::min(vx.size(), vy.size()); 
++i) {
+                       map_location loc(lexical_cast<int>(vx[i]), 
lexical_cast<int>(vy[i]));
+                       locations_.push_back(loc);
+               }
+       }
 }
 
 } // namespace soundsource

Modified: trunk/src/soundsource.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/soundsource.hpp?rev=30546&r1=30545&r2=30546&view=diff
==============================================================================
--- trunk/src/soundsource.hpp (original)
+++ trunk/src/soundsource.hpp Sun Nov  2 20:57:29 2008
@@ -21,6 +21,7 @@
 #include "generic_event.hpp"
 #include "map_location.hpp"
 
+class config;
 class display;
 
 namespace soundsource {
@@ -68,18 +69,22 @@
        void replace_location(const map_location &oldloc, const map_location 
&newloc);
        
        int calculate_volume(const map_location &loc, const display &disp);
+       
+       /**
+        * Serializes attributes as WML config.
+        * @param cfg A reference to a [sound_source] tag object.
+        */
+       void write_config(config& cfg) const;
 };
 
 class manager : public events::observer {
 
        typedef std::map<std::string, positional_source *> 
positional_source_map;
-       typedef positional_source_map::iterator positional_source_iterator;
+       typedef positional_source_map::iterator            
positional_source_iterator;
+       typedef positional_source_map::const_iterator      
positional_source_const_iterator;
 
        positional_source_map sources_;
        const display &disp_;
-
-       // checks which sound sources are visible
-       void update_positions();
 
 public:
        manager(const display &disp);
@@ -94,6 +99,15 @@
        void update();
 
        void add_location(const std::string &id, const map_location &loc);
+       
+       // checks which sound sources are visible
+       void update_positions();
+       
+       /**
+        * Serializes information into cfg as new childs of key
+        * "sound_source", appendend to existing content.
+        */
+       void write_sourcespecs(config& cfg) const;
 };
 
 /**
@@ -117,7 +131,7 @@
        std::vector<map_location> locations_;
 
 public:
-       /** Constructor. */
+       /** Parameter-list constructor. */
        sourcespec(const std::string& id, const std::string& files, int 
min_delay, int chance) :
                id_(id),
                files_(files),
@@ -130,6 +144,9 @@
                locations_()
        {}
        
+       /** WML constructor. */
+       sourcespec(const config& cfg);
+       
        /**
         * Serializes information into cfg as a new (appended)
         * child of key "sound_source".


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

Reply via email to