Merged ResourceManager into Loader.

This commit is contained in:
Felix Ableitner 2013-09-02 18:35:36 +02:00
parent fdf20428b0
commit 8c5d3a32cf
4 changed files with 21 additions and 36 deletions

View file

@ -13,7 +13,6 @@
#include "sprites/Enemy.h"
#include "sprites/Player.h"
#include "util/Loader.h"
#include "util/ResourceManager.h"
#include "util/Yaml.h"
/**
@ -29,8 +28,7 @@ Game::Game(tgui::Window& window) :
initPlayer();
mCrosshairTexture = ResourceManager::i().acquire(Loader::i()
.fromFile<sf::Texture>("crosshair.png"));
mCrosshairTexture = Loader::i().fromFile<sf::Texture>("crosshair.png");
mCrosshair.setTexture(*mCrosshairTexture, true);
mWindow.setMouseCursorVisible(false);

View file

@ -11,7 +11,6 @@
#include "../../util/Loader.h"
#include "../../util/Log.h"
#include "../../util/ResourceManager.h"
Sprite::Sprite(const Vector2f& position, Category category,
unsigned short mask, const Vector2f& size,
@ -151,8 +150,7 @@ Sprite::setPosition(const Vector2f& position) {
void
Sprite::setTexture(const std::string& texture) {
try {
mTexture = ResourceManager::i().acquire(Loader::i()
.fromFile<sf::Texture>(texture));
mTexture = Loader::i().fromFile<sf::Texture>(texture);
mShape.setTexture(&*mTexture, false);
}
catch (thor::ResourceLoadingException&) {

View file

@ -37,6 +37,9 @@
* @endcode
*/
class Loader : public Singleton<Loader> {
private:
class ResourceManager;
public:
/**
* Sets the general resource folder path.
@ -52,11 +55,14 @@ public:
}
/**
* Loads a resource from a file, looking in the specific resource folder for this type.
* cquires a resource key by filename, then loads the resource from a
* file, looking in the specific resource folder for this type.
*/
template <typename T> thor::ResourceKey<T>
template <typename T> std::shared_ptr<T>
fromFile(const std::string& file) {
return static_cast<SpecificLoader<T>* >(getLoader<T>().get())->fromFile(mFolder, file);
auto resourceKey = static_cast<SpecificLoader<T>* >(getLoader<T>().get())
->fromFile(mFolder, file);
return mResourceManager.acquire(resourceKey);
}
private:
@ -117,9 +123,19 @@ private:
(typeid(T), std::unique_ptr<LoaderBase>(new SpecificLoader<T>))).first).second;
};
private:
class ResourceManager : public thor::MultiResourceCache {
public:
ResourceManager() {
setLoadingFailureStrategy(thor::Resources::ThrowException);
setReleaseStrategy(thor::Resources::AutoRelease);
};
};
private:
std::string mFolder;
std::map<std::type_index, std::unique_ptr<LoaderBase> > mLoaders;
ResourceManager mResourceManager;
};
#endif /* DG_LOADER_H_ */

View file

@ -1,27 +0,0 @@
/*
* ResourceManager.h
*
* Created on: 22.07.2012
* Author: Felix
*/
#ifndef DG_RESOURCEMANAGER_H_
#define DG_RESOURCEMANAGER_H_
#include "Singleton.h"
/**
* Loads and manages all resources by providing Singleton access to
* Thor ResourceManager.
*/
class ResourceManager : public thor::MultiResourceCache,
public Singleton<ResourceManager> {
private:
friend class Singleton<ResourceManager>;
ResourceManager() {
setLoadingFailureStrategy(thor::Resources::ThrowException);
setReleaseStrategy(thor::Resources::AutoRelease);
};
};
#endif /* DG_RESOURCEMANAGER_H_ */