Improved error handling and fallbacks.
This commit is contained in:
parent
0c29dac282
commit
12441076c0
3 changed files with 38 additions and 17 deletions
|
@ -29,11 +29,22 @@ Sprite::Sprite(const Data& data, const Yaml& config) :
|
|||
mMask(data.mask),
|
||||
mDelete(false) {
|
||||
std::string texture = config.get<std::string>(KEY_TEXTURE, "");
|
||||
if (texture == "") {
|
||||
LOG_E("Failed to read texture from YAML file " << config.getFilename());
|
||||
}
|
||||
if (texture != "") {
|
||||
try {
|
||||
mTexture = ResourceManager::i().acquire(Loader::i()
|
||||
.fromFile<sf::Texture>(texture));
|
||||
mShape.shape->setTexture(&*mTexture, false);
|
||||
}
|
||||
catch (thor::ResourceLoadingException&) {
|
||||
LOG_W("Failed to load texture " << texture << ", coloring red.");
|
||||
mShape.shape->setFillColor(sf::Color(255, 0, 0));
|
||||
}
|
||||
}
|
||||
else {
|
||||
LOG_W("Failed to read texture file name from YAML file " <<
|
||||
config.getFilename() << ", coloring red.");
|
||||
mShape.shape->setFillColor(sf::Color(255, 0, 0));
|
||||
}
|
||||
|
||||
float radius = config.get(KEY_RADIUS, 0.0f);
|
||||
sf::Vector2f size = config.get(KEY_SIZE, sf::Vector2f());
|
||||
|
@ -41,8 +52,13 @@ Sprite::Sprite(const Data& data, const Yaml& config) :
|
|||
mShape.type = Shape::Type::CIRCLE;
|
||||
mShape.shape = std::unique_ptr<sf::Shape>(new sf::CircleShape(radius));
|
||||
mShape.shape->setOrigin(radius, radius);
|
||||
mShape.shape->setTextureRect(sf::IntRect(sf::Vector2i(0, 0), sf::Vector2i(radius * 2, radius * 2)));
|
||||
|
||||
mShape.shape->setTextureRect(sf::IntRect(sf::Vector2i(0, 0),
|
||||
sf::Vector2i(radius * 2, radius * 2)));
|
||||
}
|
||||
else if (size == sf::Vector2f()) {
|
||||
LOG_E("Failed to read size or radius from " << config.getFilename() <<
|
||||
", using texture size.");
|
||||
size = sf::Vector2f(mTexture->getSize());
|
||||
}
|
||||
else if (size != sf::Vector2f()) {
|
||||
mShape.type = Shape::Type::RECTANGLE;
|
||||
|
@ -50,11 +66,6 @@ Sprite::Sprite(const Data& data, const Yaml& config) :
|
|||
mShape.shape->setOrigin(size / 2.0f);
|
||||
mShape.shape->setTextureRect(sf::IntRect(sf::Vector2i(0, 0), sf::Vector2i(size)));
|
||||
}
|
||||
else {
|
||||
LOG_E("Could not read size or radius from " << config.getFilename());
|
||||
}
|
||||
|
||||
mShape.shape->setTexture(&*mTexture, false);
|
||||
|
||||
setPosition(data.position);
|
||||
setDirection(data.direction);
|
||||
|
|
|
@ -11,13 +11,18 @@
|
|||
#include "../abstract/Singleton.h"
|
||||
|
||||
/**
|
||||
* Loads and manages all resources by providing Singleton access to Thor ResourceManager.
|
||||
* Loads and manages all resources by providing Singleton access to
|
||||
* Thor ResourceManager.
|
||||
*/
|
||||
class ResourceManager : public thor::MultiResourceCache, public Singleton<ResourceManager> {
|
||||
class ResourceManager : public thor::MultiResourceCache,
|
||||
public Singleton<ResourceManager> {
|
||||
// Private functions.
|
||||
private:
|
||||
friend class Singleton<ResourceManager>;
|
||||
ResourceManager() = default;
|
||||
ResourceManager() {
|
||||
setLoadingFailureStrategy(thor::Resources::ThrowException);
|
||||
setReleaseStrategy(thor::Resources::AutoRelease);
|
||||
};
|
||||
};
|
||||
|
||||
#endif /* DG_RESOURCEMANAGER_H_ */
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
|
||||
#include <SFML/System.hpp>
|
||||
|
||||
#include "Log.h"
|
||||
|
||||
/**
|
||||
* Interface to a YAML file.
|
||||
*/
|
||||
|
@ -42,6 +44,7 @@ private:
|
|||
|
||||
/**
|
||||
* Stream output operators to specialize Yaml::get for other types.
|
||||
* Error handling is done in Yaml::get.
|
||||
*/
|
||||
namespace {
|
||||
void operator>>(const YAML::Node& node, sf::Vector2i& vector) {
|
||||
|
@ -56,7 +59,7 @@ namespace {
|
|||
};
|
||||
|
||||
/**
|
||||
* Gets a value of a specified type by key. Throws exception if key not found.
|
||||
* Gets a value of a specified type by key. Returns default value on error.
|
||||
*
|
||||
* @param key The string by which to select the return value.
|
||||
* @tparam T The type of the return value.
|
||||
|
@ -64,12 +67,14 @@ namespace {
|
|||
*/
|
||||
template <typename T>
|
||||
T Yaml::get(const std::string& key, const T& defaultValue) const {
|
||||
if (const YAML::Node* node = mNode.FindValue(key)) {
|
||||
try {
|
||||
const YAML::Node* node = mNode.FindValue(key);
|
||||
T value;
|
||||
*node >> value;
|
||||
return value;
|
||||
}
|
||||
else {
|
||||
catch (YAML::InvalidScalar&) {
|
||||
LOG_W("Failed to get key " << key << " from " << mFolder << mFilename);
|
||||
return defaultValue;
|
||||
}
|
||||
};
|
||||
|
|
Reference in a new issue