Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
ResourceManager.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 Julien Bernard
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21#ifndef GF_RESOURCE_MANAGER_H
22#define GF_RESOURCE_MANAGER_H
23
24#include <functional>
25#include <future>
26#include <initializer_list>
27#include <map>
28#include <memory>
29#include <stdexcept>
30
31#include "AssetManager.h"
32#include "GraphicsApi.h"
33#include "Image.h"
34#include "Font.h"
35#include "Texture.h"
36#include "Window.h"
37
38namespace gf {
39#ifndef DOXYGEN_SHOULD_SKIP_THIS
40inline namespace v1 {
41#endif
42
52 template<typename T>
54 public:
58 using Loader = std::function<std::unique_ptr<T>(const Path&)>;
59
65 explicit ResourceCache(Loader loader)
66 : m_loader(std::move(loader))
67 {
68
69 }
70
74 ResourceCache(const ResourceCache&) = delete;
75
80
93 T& getResource(AssetManager& assets, Path filename) {
94 // try to find a known prefix
95 if (filename.is_absolute()) {
96 Path relativePath = assets.getRelativePath(filename);
97
98 if (!relativePath.empty()) {
99 filename = relativePath;
100 }
101 }
102
103 std::size_t h = std::filesystem::hash_value(filename);
104
105 auto it = m_cache.find(h);
106
107 if (it != m_cache.end()) {
108 return *it->second;
109 }
110
111 Path absolutePath = assets.getAbsolutePath(filename);
112
113 if (absolutePath.empty()) {
114 throw std::runtime_error("Path not found");
115 }
116
117 auto ptr = m_loader(absolutePath);
118
119 if (!ptr) {
120 throw std::runtime_error("Resource not loaded");
121 }
122
123 auto inserted = m_cache.emplace(h, std::move(ptr));
124
125 if (inserted.second) {
126 return *inserted.first->second;
127 }
128
129 throw std::runtime_error("Resource not inserted in the cache");
130 }
131
132 private:
133 Loader m_loader;
134 std::map<std::size_t, std::unique_ptr<T>> m_cache;
135 };
136
137
144 class GF_GRAPHICS_API ResourceManager : public AssetManager {
145 public:
150
154 ResourceManager(std::initializer_list<Path> paths);
155
156
164 Image& getImage(const Path& path) {
165 std::lock_guard<std::mutex> lock(m_mutex);
166 return m_images.getResource(*this, path);
167 }
168
176 Texture& getTexture(const Path& path) {
177 std::lock_guard<std::mutex> lock(m_mutex);
178 return m_textures.getResource(*this, path);
179 }
180
188 Font& getFont(const Path& path) {
189 std::lock_guard<std::mutex> lock(m_mutex);
190 return m_fonts.getResource(*this, path);
191 }
192
193 private:
194 ResourceCache<Image> m_images;
195 ResourceCache<Texture> m_textures;
196 ResourceCache<Font> m_fonts;
197 std::mutex m_mutex;
198 };
199
200#ifndef DOXYGEN_SHOULD_SKIP_THIS
201}
202#endif
203}
204
205#endif // GF_RESOURCE_MANAGER_H
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.