Removed static Yaml constants to avoid build failures on MingW,

updated yaml-cpp.
This commit is contained in:
Felix Ableitner 2013-09-17 18:59:13 +02:00
parent 7532554b02
commit 14749cb378
10 changed files with 83 additions and 68 deletions

View file

@ -9,12 +9,16 @@
#include "../abstract/Character.h" #include "../abstract/Character.h"
const Yaml Heal::CONFIG("res/yaml/heal.yaml"); const std::string Heal::CONFIG_NAME = "heal.yaml";
Heal::Heal() : Heal::Heal() :
Gadget(CONFIG.get("name", std::string()), CONFIG.get("cooldown", 0)), Heal(Yaml(CONFIG_NAME)) {
mHealedTotal(CONFIG.get("amount_healed", 0)), }
mTimePerPoint(sf::milliseconds(CONFIG.get("time_healing", 0))),
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) { mHealed(mHealedTotal + 1) {
} }

View file

@ -22,7 +22,10 @@ protected:
GadgetType getType() const override; GadgetType getType() const override;
private: private:
static const Yaml CONFIG; Heal(const Yaml& config);
private:
static const std::string CONFIG_NAME;
const int mHealedTotal; const int mHealedTotal;
const sf::Time mTimePerPoint; const sf::Time mTimePerPoint;

View file

@ -7,12 +7,16 @@
#include "HealthOrb.h" #include "HealthOrb.h"
const Yaml HealthOrb::CONFIG("res/yaml/health_orb.yaml"); const std::string HealthOrb::CONFIG_NAME = "health_orb.yaml";
HealthOrb::HealthOrb() : HealthOrb::HealthOrb() :
Item(CONFIG.get("size", Vector2i()), CONFIG.get("texture", std::string())), HealthOrb(Yaml(CONFIG_NAME)) {
mName(CONFIG.get("name", std::string())), }
mAmountHealed(CONFIG.get("amount_healed", 0)) {
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 int

View file

@ -19,7 +19,10 @@ public:
int getAmountHealed() const; int getAmountHealed() const;
private: private:
static const Yaml CONFIG; HealthOrb(const Yaml& config);
private:
static const std::string CONFIG_NAME;
const std::string mName; const std::string mName;
const int mAmountHealed; const int mAmountHealed;
}; };

View file

@ -12,17 +12,22 @@
#include "../effects/Bullet.h" #include "../effects/Bullet.h"
#include "../../World.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) : RingOfFire::RingOfFire(World& world) :
Gadget(CONFIG.get("name", std::string()), CONFIG.get("cooldown", 0)), RingOfFire(world, Yaml(CONFIG_NAME)) {
mWavesPerUse(CONFIG.get("waves_per_use", 0)), }
mAngleOffset(CONFIG.get("angle_offset", 0.0f)),
mBulletsPerWave(CONFIG.get("bullets_per_wave", 0)), RingOfFire::RingOfFire(World& world, const Yaml& config) :
mDelay(sf::milliseconds(CONFIG.get("delay", 0))), 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), mCurrentWave(mWavesPerUse + 1),
mWorld(world), mWorld(world),
mBullet(CONFIG.get("bullet", std::string())) { mBullet(config.get("bullet", std::string())) {
} }
void void

View file

@ -25,7 +25,10 @@ protected:
GadgetType getType() const override; GadgetType getType() const override;
private: private:
static const Yaml CONFIG; RingOfFire(World& world, const Yaml& config);
private:
static const std::string CONFIG_NAME;
const int mWavesPerUse; const int mWavesPerUse;
const int mAngleOffset; const int mAngleOffset;
const int mBulletsPerWave; const int mBulletsPerWave;

View file

@ -11,10 +11,14 @@
#include "../RotatingShield.h" #include "../RotatingShield.h"
#include "../../World.h" #include "../../World.h"
const Yaml Shield::CONFIG("res/yaml/rotating_shield.yaml"); const std::string Shield::CONFIG_NAME = "rotating_shield.yaml";
Shield::Shield() : 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 void

View file

@ -25,7 +25,10 @@ protected:
GadgetType getType() const override; GadgetType getType() const override;
private: private:
static const Yaml CONFIG; Shield(const Yaml& config);
private:
static const std::string CONFIG_NAME;
Character* mCharacter; Character* mCharacter;
std::shared_ptr<RotatingShield> mRotatingShield; std::shared_ptr<RotatingShield> mRotatingShield;
}; };

View file

@ -16,24 +16,10 @@ std::string Yaml::mFolder = "";
* set in setFolder(). * set in setFolder().
*/ */
Yaml::Yaml(const std::string& filename) : Yaml::Yaml(const std::string& filename) :
mFilename(mFolder+filename), mFilename(filename) {
mFile(mFilename) { mNode = YAML::LoadFile(mFolder + filename);
if (mFile.fail()) if (mNode.IsNull())
LOG_W("Failed to open YAML file: " << mFolder << filename); LOG_D("Failed to load config 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;
} }
/** /**

View file

@ -22,9 +22,6 @@
class Yaml { class Yaml {
public: public:
explicit Yaml(const std::string& filename); explicit Yaml(const std::string& filename);
~Yaml();
std::string getFilename() const;
static void setFolder(const std::string& folder); static void setFolder(const std::string& folder);
@ -33,28 +30,39 @@ public:
private: private:
static std::string mFolder; static std::string mFolder;
std::string mFilename; std::string mFilename;
std::ifstream mFile;
YAML::Node mNode; YAML::Node mNode;
}; };
/** /**
* Stream output operators to specialize Yaml::get for other types. * Template specializations to convert yaml parameters into custom types.
* Error handling is done in Yaml::get.
*/ */
namespace { namespace YAML {
void operator>>(const YAML::Node& node, Vector2i& vector) { template<>
node[0] >> vector.x; struct convert<Vector2f> {
node[1] >> vector.y; static bool decode(const Node& node, Vector2f& rhs) {
} if(!node.IsSequence() || node.size() != 2)
return false;
void operator>>(const YAML::Node& node, Vector2f& vector) { rhs.x = node[0].as<float>();
node[0] >> vector.x; rhs.y = node[1].as<float>();
node[1] >> vector.y; return true;
} }
}; };
template<>
struct convert<Vector2i> {
static bool decode(const Node& node, Vector2i& rhs) {
if(!node.IsSequence() || node.size() != 2)
return false;
rhs.x = node[0].as<int>();
rhs.y = node[1].as<int>();
return true;
}
};
}
/** /**
* Gets a value of a specified type by key. Returns default value on error. * Gets a value of a specified type by key. Returns default value on error.
* *
@ -64,18 +72,10 @@ namespace {
*/ */
template <typename T> template <typename T>
T Yaml::get(const std::string& key, const T& defaultValue) const { T Yaml::get(const std::string& key, const T& defaultValue) const {
try { if(mNode[key])
if (const YAML::Node* node = mNode.FindValue(key)) { return mNode[key].as<T>();
T value;
*node >> value;
return value;
}
else else
return defaultValue; return defaultValue;
}
catch(YAML::Exception&) {
return defaultValue;
}
}; };
#endif /* DG_YAML_H_ */ #endif /* DG_YAML_H_ */