Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
ResourceManager.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 "Image.h"
33 #include "Font.h"
34 #include "Portability.h"
35 #include "Texture.h"
36 #include "Window.h"
37 
38 namespace gf {
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40 inline namespace v1 {
41 #endif
42 
52  template<typename T>
53  class ResourceCache {
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 
79  ResourceCache& operator=(const ResourceCache&) = delete;
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 = boost::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_API ResourceManager : public AssetManager {
145  public:
149  ResourceManager();
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
Texture & getTexture(const Path &path)
Get a texture.
Definition: ResourceManager.h:176
Font & getFont(const Path &path)
Get a font.
Definition: ResourceManager.h:188
ResourceCache(Loader loader)
Constructor.
Definition: ResourceManager.h:65
STL namespace.
Path getRelativePath(const Path &absolutePath) const
Search a file in the search directories.
Image & getImage(const Path &path)
Get an image.
Definition: ResourceManager.h:164
An asset manager.
Definition: AssetManager.h:44
A texture for colored images.
Definition: Texture.h:309
Class for loading, manipulating and saving images.
Definition: Image.h:80
The namespace for gf classes.
Definition: Action.h:35
A character font.
Definition: Font.h:109
A resource manager.
Definition: ResourceManager.h:144
A generic cache for resources.
Definition: ResourceManager.h:53
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:44
std::function< std::unique_ptr< gf::Image >(const Path &)> Loader
A resource loader.
Definition: ResourceManager.h:58
Path getAbsolutePath(const Path &relativePath) const
Search a file in the search directories.
T & getResource(AssetManager &assets, Path filename)
Get a resource.
Definition: ResourceManager.h:93