From ddb9c8301ab2ab4547a9a9cb1d849d9e287fe1cb Mon Sep 17 00:00:00 2001 From: "Ignacio R. Morelle" Date: Sun, 2 Nov 2008 19:57:29 +0000 Subject: [PATCH] [[WML engine improvements]] * 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). --- src/soundsource.cpp | 72 +++++++++++++++++++++++++++++++++++---------- src/soundsource.hpp | 27 +++++++++++++---- 2 files changed, 78 insertions(+), 21 deletions(-) diff --git a/src/soundsource.cpp b/src/soundsource.cpp index 6af16db98c0..29e40144bb2 100644 --- a/src/soundsource.cpp +++ b/src/soundsource.cpp @@ -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 @@ void manager::add_location(const std::string &id, const map_location &loc) (*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 @@ void positional_source::add_location(const map_location &loc) locations_.push_back(loc); } -void sourcespec::write(config& cfg) const +void positional_source::write_config(config& cfg) const { - config& info = cfg.add_child("sound_source"); + cfg["sounds"] = this->files_; + cfg["delay"] = str_cast(this->min_delay_); + cfg["chance"] = str_cast(this->chance_); + cfg["check_fogged"] = this->check_fogged_ ? "yes" : "no"; - info["id"] = this->id_; - info["sounds"] = this->files_; - info["delay"] = str_cast(this->min_delay_); - info["chance"] = str_cast(this->chance_); - info["check_fogged"] = this->check_fogged_ ? "yes" : "no"; - - info["x"] = info["y"] = ""; + 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(loc.x); - info["y"] += str_cast(loc.y); + cfg["x"] += str_cast(loc.x); + cfg["y"] += str_cast(loc.y); } - info["loop"] = str_cast(this->loops_); - info["full_range"] = str_cast(this->range_); - info["fade_range"] = str_cast(this->faderange_); + cfg["loop"] = str_cast(this->loops_); + cfg["full_range"] = str_cast(this->range_); + cfg["fade_range"] = str_cast(this->faderange_); +} + +sourcespec::sourcespec(const config& cfg) : + id_(cfg["id"]), + files_(cfg["sounds"]), + min_delay_(lexical_cast_default(cfg["delay"], DEFAULT_DELAY)), + chance_(lexical_cast_default(cfg["chance"], DEFAULT_CHANCE)), + loops_(lexical_cast_default(cfg["loop"], 0)), + range_(lexical_cast_default(cfg["full_range"], 3)), + faderange_(lexical_cast_default(cfg["fade_range"], 14)), + check_fogged_(utils::string_bool(cfg["check_fogged"], true)), + locations_() +{ + const std::vector& vx = utils::split(cfg["x"]); + const std::vector& 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(vx[i]), lexical_cast(vy[i])); + locations_.push_back(loc); + } + } } } // namespace soundsource diff --git a/src/soundsource.hpp b/src/soundsource.hpp index 31c3ff73b39..dd97c753671 100644 --- a/src/soundsource.hpp +++ b/src/soundsource.hpp @@ -21,6 +21,7 @@ #include "generic_event.hpp" #include "map_location.hpp" +class config; class display; namespace soundsource { @@ -68,19 +69,23 @@ public: 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 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); ~manager(); @@ -94,6 +99,15 @@ public: 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 @@ class sourcespec std::vector 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 @@ public: locations_() {} + /** WML constructor. */ + sourcespec(const config& cfg); + /** * Serializes information into cfg as a new (appended) * child of key "sound_source".