From 12441076c090b4d2680ea7fa15e57a0db646bc56 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 30 Mar 2013 13:38:25 +0100 Subject: [PATCH] Improved error handling and fallbacks. --- source/abstract/Sprite.cpp | 33 ++++++++++++++++++++++----------- source/util/ResourceManager.h | 11 ++++++++--- source/util/Yaml.h | 11 ++++++++--- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/source/abstract/Sprite.cpp b/source/abstract/Sprite.cpp index 50e65e0..8419dd4 100755 --- a/source/abstract/Sprite.cpp +++ b/source/abstract/Sprite.cpp @@ -29,11 +29,22 @@ Sprite::Sprite(const Data& data, const Yaml& config) : mMask(data.mask), mDelete(false) { std::string texture = config.get(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(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)); } - mTexture = ResourceManager::i().acquire(Loader::i() - .fromFile(texture)); 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(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); diff --git a/source/util/ResourceManager.h b/source/util/ResourceManager.h index 38d50a9..b045b8a 100644 --- a/source/util/ResourceManager.h +++ b/source/util/ResourceManager.h @@ -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 { +class ResourceManager : public thor::MultiResourceCache, + public Singleton { // Private functions. private: friend class Singleton; - ResourceManager() = default; + ResourceManager() { + setLoadingFailureStrategy(thor::Resources::ThrowException); + setReleaseStrategy(thor::Resources::AutoRelease); + }; }; #endif /* DG_RESOURCEMANAGER_H_ */ diff --git a/source/util/Yaml.h b/source/util/Yaml.h index 60eaf94..559c56e 100644 --- a/source/util/Yaml.h +++ b/source/util/Yaml.h @@ -15,6 +15,8 @@ #include +#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 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; } };