From 14749cb378a25aa4f26a1dfc3759f319250e949a Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 17 Sep 2013 18:59:13 +0200 Subject: [PATCH] Removed static Yaml constants to avoid build failures on MingW, updated yaml-cpp. --- src/sprites/items/Heal.cpp | 12 ++++--- src/sprites/items/Heal.h | 5 ++- src/sprites/items/HealthOrb.cpp | 12 ++++--- src/sprites/items/HealthOrb.h | 5 ++- src/sprites/items/RingOfFire.cpp | 23 ++++++++------ src/sprites/items/RingOfFire.h | 5 ++- src/sprites/items/Shield.cpp | 8 +++-- src/sprites/items/Shield.h | 5 ++- src/util/Yaml.cpp | 22 +++---------- src/util/Yaml.h | 54 ++++++++++++++++---------------- 10 files changed, 83 insertions(+), 68 deletions(-) diff --git a/src/sprites/items/Heal.cpp b/src/sprites/items/Heal.cpp index 5ec04ab..9f210fe 100644 --- a/src/sprites/items/Heal.cpp +++ b/src/sprites/items/Heal.cpp @@ -9,12 +9,16 @@ #include "../abstract/Character.h" -const Yaml Heal::CONFIG("res/yaml/heal.yaml"); +const std::string Heal::CONFIG_NAME = "heal.yaml"; Heal::Heal() : - Gadget(CONFIG.get("name", std::string()), CONFIG.get("cooldown", 0)), - mHealedTotal(CONFIG.get("amount_healed", 0)), - mTimePerPoint(sf::milliseconds(CONFIG.get("time_healing", 0))), + Heal(Yaml(CONFIG_NAME)) { +} + +Heal::Heal(const Yaml& config) : + Gadget(config.get("name", std::string()), config.get("cooldown", 0)), + mHealedTotal(config.get("amount_healed", 0)), + mTimePerPoint(sf::milliseconds(config.get("time_healing", 0))), mHealed(mHealedTotal + 1) { } diff --git a/src/sprites/items/Heal.h b/src/sprites/items/Heal.h index fb87f22..ef8f69d 100644 --- a/src/sprites/items/Heal.h +++ b/src/sprites/items/Heal.h @@ -22,7 +22,10 @@ protected: GadgetType getType() const override; private: - static const Yaml CONFIG; + Heal(const Yaml& config); + +private: + static const std::string CONFIG_NAME; const int mHealedTotal; const sf::Time mTimePerPoint; diff --git a/src/sprites/items/HealthOrb.cpp b/src/sprites/items/HealthOrb.cpp index a184f59..8f97b86 100644 --- a/src/sprites/items/HealthOrb.cpp +++ b/src/sprites/items/HealthOrb.cpp @@ -7,12 +7,16 @@ #include "HealthOrb.h" -const Yaml HealthOrb::CONFIG("res/yaml/health_orb.yaml"); +const std::string HealthOrb::CONFIG_NAME = "health_orb.yaml"; HealthOrb::HealthOrb() : - Item(CONFIG.get("size", Vector2i()), CONFIG.get("texture", std::string())), - mName(CONFIG.get("name", std::string())), - mAmountHealed(CONFIG.get("amount_healed", 0)) { + HealthOrb(Yaml(CONFIG_NAME)) { +} + +HealthOrb::HealthOrb(const Yaml& config) : + Item(config.get("size", Vector2i()), config.get("texture", std::string())), + mName(config.get("name", std::string())), + mAmountHealed(config.get("amount_healed", 0)) { } int diff --git a/src/sprites/items/HealthOrb.h b/src/sprites/items/HealthOrb.h index a0e9dae..492cdaf 100644 --- a/src/sprites/items/HealthOrb.h +++ b/src/sprites/items/HealthOrb.h @@ -19,7 +19,10 @@ public: int getAmountHealed() const; private: - static const Yaml CONFIG; + HealthOrb(const Yaml& config); + +private: + static const std::string CONFIG_NAME; const std::string mName; const int mAmountHealed; }; diff --git a/src/sprites/items/RingOfFire.cpp b/src/sprites/items/RingOfFire.cpp index 403b497..9b2fac6 100644 --- a/src/sprites/items/RingOfFire.cpp +++ b/src/sprites/items/RingOfFire.cpp @@ -12,17 +12,22 @@ #include "../effects/Bullet.h" #include "../../World.h" -const Yaml RingOfFire::CONFIG("res/yaml/ring_of_fire.yaml"); +const std::string RingOfFire::CONFIG_NAME = "ring_of_fire.yaml"; RingOfFire::RingOfFire(World& world) : - Gadget(CONFIG.get("name", std::string()), CONFIG.get("cooldown", 0)), - mWavesPerUse(CONFIG.get("waves_per_use", 0)), - mAngleOffset(CONFIG.get("angle_offset", 0.0f)), - mBulletsPerWave(CONFIG.get("bullets_per_wave", 0)), - mDelay(sf::milliseconds(CONFIG.get("delay", 0))), - mCurrentWave(mWavesPerUse + 1), - mWorld(world), - mBullet(CONFIG.get("bullet", std::string())) { + RingOfFire(world, Yaml(CONFIG_NAME)) { +} + +RingOfFire::RingOfFire(World& world, const Yaml& config) : + Gadget(config.get("name", std::string()), config.get("cooldown", 0)), + mWavesPerUse(config.get("waves_per_use", 0)), + mAngleOffset(config.get("angle_offset", 0.0f)), + mBulletsPerWave(config.get("bullets_per_wave", 0)), + mDelay(sf::milliseconds(config.get("delay", 0))), + mCurrentWave(mWavesPerUse + 1), + mWorld(world), + mBullet(config.get("bullet", std::string())) { + } void diff --git a/src/sprites/items/RingOfFire.h b/src/sprites/items/RingOfFire.h index 3ff52e5..9f1c55f 100644 --- a/src/sprites/items/RingOfFire.h +++ b/src/sprites/items/RingOfFire.h @@ -25,7 +25,10 @@ protected: GadgetType getType() const override; private: - static const Yaml CONFIG; + RingOfFire(World& world, const Yaml& config); + +private: + static const std::string CONFIG_NAME; const int mWavesPerUse; const int mAngleOffset; const int mBulletsPerWave; diff --git a/src/sprites/items/Shield.cpp b/src/sprites/items/Shield.cpp index 5eab3bc..79966f4 100644 --- a/src/sprites/items/Shield.cpp +++ b/src/sprites/items/Shield.cpp @@ -11,10 +11,14 @@ #include "../RotatingShield.h" #include "../../World.h" -const Yaml Shield::CONFIG("res/yaml/rotating_shield.yaml"); +const std::string Shield::CONFIG_NAME = "rotating_shield.yaml"; Shield::Shield() : - Gadget(CONFIG.get("name", std::string()), CONFIG.get("cooldown", 0)) { + Shield(Yaml(CONFIG_NAME)) { +} + +Shield::Shield(const Yaml& config) : + Gadget(config.get("name", std::string()), config.get("cooldown", 0)) { } void diff --git a/src/sprites/items/Shield.h b/src/sprites/items/Shield.h index d5f59da..2722b50 100644 --- a/src/sprites/items/Shield.h +++ b/src/sprites/items/Shield.h @@ -25,7 +25,10 @@ protected: GadgetType getType() const override; private: - static const Yaml CONFIG; + Shield(const Yaml& config); + +private: + static const std::string CONFIG_NAME; Character* mCharacter; std::shared_ptr mRotatingShield; }; diff --git a/src/util/Yaml.cpp b/src/util/Yaml.cpp index f04b4c4..d01b37d 100644 --- a/src/util/Yaml.cpp +++ b/src/util/Yaml.cpp @@ -16,24 +16,10 @@ std::string Yaml::mFolder = ""; * set in setFolder(). */ Yaml::Yaml(const std::string& filename) : - mFilename(mFolder+filename), - mFile(mFilename) { - if (mFile.fail()) - LOG_W("Failed to open YAML file: " << mFolder << filename); - YAML::Parser parser(mFile); - parser.GetNextDocument(mNode); -} - -Yaml::~Yaml() { - mFile.close(); -} - -/** - * Return path and name of the file opened in this object. - */ -std::string -Yaml::getFilename() const { - return mFilename; + mFilename(filename) { + mNode = YAML::LoadFile(mFolder + filename); + if (mNode.IsNull()) + LOG_D("Failed to load config file " << mFolder << filename); } /** diff --git a/src/util/Yaml.h b/src/util/Yaml.h index fe401e7..cb011da 100644 --- a/src/util/Yaml.h +++ b/src/util/Yaml.h @@ -22,9 +22,6 @@ class Yaml { public: explicit Yaml(const std::string& filename); - ~Yaml(); - - std::string getFilename() const; static void setFolder(const std::string& folder); @@ -33,28 +30,39 @@ public: private: static std::string mFolder; - std::string mFilename; - std::ifstream mFile; YAML::Node mNode; }; /** - * Stream output operators to specialize Yaml::get for other types. - * Error handling is done in Yaml::get. + * Template specializations to convert yaml parameters into custom types. */ -namespace { - void operator>>(const YAML::Node& node, Vector2i& vector) { - node[0] >> vector.x; - node[1] >> vector.y; - } +namespace YAML { +template<> +struct convert { + static bool decode(const Node& node, Vector2f& rhs) { + if(!node.IsSequence() || node.size() != 2) + return false; - void operator>>(const YAML::Node& node, Vector2f& vector) { - node[0] >> vector.x; - node[1] >> vector.y; + rhs.x = node[0].as(); + rhs.y = node[1].as(); + return true; } }; +template<> +struct convert { + static bool decode(const Node& node, Vector2i& rhs) { + if(!node.IsSequence() || node.size() != 2) + return false; + + rhs.x = node[0].as(); + rhs.y = node[1].as(); + return true; + } +}; +} + /** * Gets a value of a specified type by key. Returns default value on error. * @@ -64,18 +72,10 @@ namespace { */ template T Yaml::get(const std::string& key, const T& defaultValue) const { - try { - if (const YAML::Node* node = mNode.FindValue(key)) { - T value; - *node >> value; - return value; - } - else - return defaultValue; - } - catch(YAML::Exception&) { - return defaultValue; - } + if(mNode[key]) + return mNode[key].as(); + else + return defaultValue; }; #endif /* DG_YAML_H_ */