21 #ifndef GF_RESOURCE_MANAGER_H 22 #define GF_RESOURCE_MANAGER_H 25 #include <initializer_list> 30 #include "AssetManager.h" 32 #include "Portability.h" 36 #ifndef DOXYGEN_SHOULD_SKIP_THIS 55 using Loader = std::function<std::unique_ptr<T>(
const Path&)>;
63 : m_loader(
std::move(loader))
91 std::size_t h = boost::filesystem::hash_value(filename);
93 auto it = m_cache.find(h);
95 if (it != m_cache.end()) {
101 if (absolutePath.empty()) {
102 throw std::runtime_error(
"Path not found");
105 auto ptr = m_loader(absolutePath);
108 throw std::runtime_error(
"Resource not loaded");
111 auto inserted = m_cache.emplace(h, std::move(ptr));
113 if (inserted.second) {
114 return *inserted.first->second;
117 throw std::runtime_error(
"Resource not inserted in the cache");
122 std::map<std::size_t, std::unique_ptr<T>> m_cache;
152 return m_textures.getResource(*
this, path);
163 return m_fonts.getResource(*
this, path);
171 #ifndef DOXYGEN_SHOULD_SKIP_THIS 176 #endif // GF_RESOURCE_MANAGER_H Texture & getTexture(const Path &path)
Get a texture.
Definition: ResourceManager.h:151
Font & getFont(const Path &path)
Get a font.
Definition: ResourceManager.h:162
ResourceCache(Loader loader)
Constructor.
Definition: ResourceManager.h:62
An asset manager.
Definition: AssetManager.h:44
A texture for colored images.
Definition: Texture.h:309
T & getResource(AssetManager &assetManager, const Path &filename)
Get a resource.
Definition: ResourceManager.h:90
The namespace for gf classes.
Definition: Action.h:35
A character font.
Definition: Font.h:109
A resource manager.
Definition: ResourceManager.h:132
A generic cache for resources.
Definition: ResourceManager.h:50
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:44
std::function< std::unique_ptr< gf::Texture >(const Path &)> Loader
A resource loader.
Definition: ResourceManager.h:55
Path getAbsolutePath(const Path &relativePath) const
Search a file in the search directories.