Gamedev Framework (gf)  0.7.0
A C++14 framework for 2D games
ResourceManager.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 <map>
26 #include <memory>
27 #include <stdexcept>
28 
29 #include "AssetManager.h"
30 #include "Font.h"
31 #include "Portability.h"
32 #include "Texture.h"
33 
34 namespace gf {
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 inline namespace v1 {
37 #endif
38 
48  template<typename T>
49  class ResourceCache {
50  public:
54  using Loader = std::function<std::unique_ptr<T>(const Path&)>;
55 
61  explicit ResourceCache(Loader loader)
62  : m_loader(std::move(loader))
63  {
64 
65  }
66 
70  ResourceCache(const ResourceCache&) = delete;
71 
75  ResourceCache& operator=(const ResourceCache&) = delete;
76 
89  T& getResource(AssetManager& assetManager, const Path& filename) {
90  std::size_t h = boost::filesystem::hash_value(filename);
91 
92  auto it = m_cache.find(h);
93 
94  if (it != m_cache.end()) {
95  return *it->second;
96  }
97 
98  Path absolutePath = assetManager.getAbsolutePath(filename);
99 
100  if (absolutePath.empty()) {
101  throw std::runtime_error("Path not found");
102  }
103 
104  auto ptr = m_loader(absolutePath);
105 
106  if (!ptr) {
107  throw std::runtime_error("Resource not loaded");
108  }
109 
110  auto inserted = m_cache.emplace(h, std::move(ptr));
111 
112  if (inserted.second) {
113  return *inserted.first->second;
114  }
115 
116  throw std::runtime_error("Resource not inserted in the cache");
117  }
118 
119  private:
120  Loader m_loader;
121  std::map<std::size_t, std::unique_ptr<T>> m_cache;
122  };
123 
124 
131  class GF_API ResourceManager : public AssetManager {
132  public:
136  ResourceManager();
137 
145  Texture& getTexture(const Path& path) {
146  return m_textures.getResource(*this, path);
147  }
148 
156  Font& getFont(const Path& path) {
157  return m_fonts.getResource(*this, path);
158  }
159 
160  private:
161  ResourceCache<Texture> m_textures;
162  ResourceCache<Font> m_fonts;
163  };
164 
165 #ifndef DOXYGEN_SHOULD_SKIP_THIS
166 }
167 #endif
168 }
169 
170 #endif // GF_RESOURCE_MANAGER_H
Texture & getTexture(const Path &path)
Get a texture.
Definition: ResourceManager.h:145
Font & getFont(const Path &path)
Get a font.
Definition: ResourceManager.h:156
ResourceCache(Loader loader)
Constructor.
Definition: ResourceManager.h:61
STL namespace.
An asset manager.
Definition: AssetManager.h:44
A texture for colored images.
Definition: Texture.h:339
T & getResource(AssetManager &assetManager, const Path &filename)
Get a resource.
Definition: ResourceManager.h:89
The namespace for gf classes.
Definition: Action.h:34
A character font.
Definition: Font.h:130
A resource manager.
Definition: ResourceManager.h:131
A generic cache for resources.
Definition: ResourceManager.h:49
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:41
std::function< std::unique_ptr< gf::Texture >(const Path &)> Loader
A resource loader.
Definition: ResourceManager.h:54
Path getAbsolutePath(const Path &relativePath) const
Search a file in the search directories.