21#ifndef GF_RESOURCE_MANAGER_H
22#define GF_RESOURCE_MANAGER_H
26#include <initializer_list>
31#include "AssetManager.h"
32#include "GraphicsApi.h"
39#ifndef DOXYGEN_SHOULD_SKIP_THIS
58 using Loader = std::function<std::unique_ptr<T>(
const Path&)>;
66 : m_loader(std::move(loader))
95 if (filename.is_absolute()) {
98 if (!relativePath.empty()) {
99 filename = relativePath;
103 std::size_t h = std::filesystem::hash_value(filename);
105 auto it = m_cache.find(h);
107 if (it != m_cache.end()) {
113 if (absolutePath.empty()) {
114 throw std::runtime_error(
"Path not found");
117 auto ptr = m_loader(absolutePath);
120 throw std::runtime_error(
"Resource not loaded");
123 auto inserted = m_cache.emplace(h, std::move(ptr));
125 if (inserted.second) {
126 return *inserted.first->second;
129 throw std::runtime_error(
"Resource not inserted in the cache");
134 std::map<std::size_t, std::unique_ptr<T>> m_cache;
165 std::lock_guard<std::mutex> lock(m_mutex);
166 return m_images.getResource(*
this, path);
177 std::lock_guard<std::mutex> lock(m_mutex);
178 return m_textures.getResource(*
this, path);
189 std::lock_guard<std::mutex> lock(m_mutex);
190 return m_fonts.getResource(*
this, path);
200#ifndef DOXYGEN_SHOULD_SKIP_THIS
An asset manager.
Definition: AssetManager.h:44
Path getAbsolutePath(const Path &relativePath) const
Search a file in the search directories.
Path getRelativePath(const Path &absolutePath) const
Search a file in the search directories.
A character font.
Definition: Font.h:109
Class for loading, manipulating and saving images.
Definition: Image.h:81
A generic cache for resources.
Definition: ResourceManager.h:53
ResourceCache & operator=(const ResourceCache &)=delete
Deleted copy assignment.
T & getResource(AssetManager &assets, Path filename)
Get a resource.
Definition: ResourceManager.h:93
ResourceCache(Loader loader)
Constructor.
Definition: ResourceManager.h:65
std::function< std::unique_ptr< T >(const Path &)> Loader
A resource loader.
Definition: ResourceManager.h:58
ResourceCache(const ResourceCache &)=delete
Deleted copy constructor.
A resource manager.
Definition: ResourceManager.h:144
ResourceManager()
Default constructor.
Font & getFont(const Path &path)
Get a font.
Definition: ResourceManager.h:188
Image & getImage(const Path &path)
Get an image.
Definition: ResourceManager.h:164
ResourceManager(std::initializer_list< Path > paths)
Constructor with a list of search directories.
Texture & getTexture(const Path &path)
Get a texture.
Definition: ResourceManager.h:176
A texture for colored images.
Definition: Texture.h:313
std::filesystem::path Path
A path in the filesystem.
Definition: Path.h:40
The namespace for gf classes.