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"
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) {
}

View file

@ -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;

View file

@ -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

View file

@ -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;
};

View file

@ -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))),
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())) {
mBullet(config.get("bullet", std::string())) {
}
void

View file

@ -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;

View file

@ -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

View file

@ -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<RotatingShield> mRotatingShield;
};

View file

@ -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);
}
/**

View file

@ -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<Vector2f> {
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<float>();
rhs.y = node[1].as<float>();
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.
*
@ -64,18 +72,10 @@ namespace {
*/
template <typename T>
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;
}
if(mNode[key])
return mNode[key].as<T>();
else
return defaultValue;
}
catch(YAML::Exception&) {
return defaultValue;
}
};
#endif /* DG_YAML_H_ */