Gamedev Framework (gf)  0.1.0
A C++11 framework for 2D games
ResourceManager.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 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 
28 #include "AssetManager.h"
29 #include "Font.h"
30 #include "Portability.h"
31 #include "Texture.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
38  /**
39  * @ingroup game
40  * @brief A generic cache for ressources
41  *
42  * This function is a low-level class that is used in gf::ResourceManager.
43  * It is generic enough so that you can use it for your own purpose.
44  *
45  * @sa gf::ResourceManager
46  */
47  template<typename T>
49  public:
50  /**
51  * @brief A ressource loader
52  */
53  typedef std::function<std::unique_ptr<T>(const Path&)> Loader;
54 
55  /**
56  * @brief Constructor
57  *
58  * @param loader A ressource loader
59  */
60  explicit ResourceCache(Loader loader)
61  : m_loader(std::move(loader))
62  {
63 
64  }
65 
66  /**
67  * @brief Deleted copy constructor
68  */
69  ResourceCache(const ResourceCache&) = delete;
70 
71  /**
72  * @brief Deleted copy assignment
73  */
74  ResourceCache& operator=(const ResourceCache&) = delete;
75 
76  /**
77  * @brief Get a ressource
78  *
79  * If the ressource exists in the cache, it is returned immediately.
80  * Otherwise, it is searched thanks to an asset manager and put in the
81  * cache.
82  *
83  * @param assetManager An asset manager
84  * @param filename The filename of the ressource
85  * @return The ressource or `nullptr` if it has not been found
86  */
87  T *getResource(AssetManager& assetManager, const Path& filename) {
88  auto it = m_cache.find(filename);
89 
90  if (it != m_cache.end()) {
91  return it->second.get();
92  }
93 
94  Path absolutePath = assetManager.getAbsolutePath(filename);
95 
96  if (absolutePath.empty()) {
97  return nullptr;
98  }
99 
100  auto ptr = m_loader(absolutePath);
101 
102  if (!ptr) {
103  return nullptr;
104  }
105 
106  auto inserted = m_cache.emplace(filename, std::move(ptr));
107 
108  if (inserted.second) {
109  return inserted.first->second.get();
110  }
111 
112  return nullptr;
113  }
114 
115  private:
116  Loader m_loader;
117  std::map<Path, std::unique_ptr<T>> m_cache;
118  };
119 
120 
121  /**
122  * @ingroup game
123  * @brief A ressource manager
124  *
125  * @sa gf::ResourceCache
126  */
128  public:
129  /**
130  * @brief Default constructor
131  */
132  ResourceManager();
133 
134  /**
135  * @brief Get a texture
136  *
137  * @param path A path to the texture
138  * @return A texture or `nullptr` if it has not been found
139  */
140  Texture *getTexture(const Path& path) {
141  return m_textures.getResource(*this, path);
142  }
143 
144  /**
145  * @brief Get a font
146  *
147  * @param path A path to the font
148  * @return A font or `nullptr` if it has not been found
149  */
150  Font *getFont(const Path& path) {
151  return m_fonts.getResource(*this, path);
152  }
153 
154  private:
155  ResourceCache<Texture> m_textures;
156  ResourceCache<Font> m_fonts;
157  };
158 
159 #ifndef DOXYGEN_SHOULD_SKIP_THIS
160 }
161 #endif
162 }
163 
164 #endif // GF_RESOURCE_MANAGER_H
Path getAbsolutePath(const Path &relativePath) const
Search a file in the search directories.
Font * getFont(const Path &path)
Get a font.
Definition: ResourceManager.h:150
ResourceCache(Loader loader)
Constructor.
Definition: ResourceManager.h:60
An asset manager.
Definition: AssetManager.h:44
std::function< std::unique_ptr< T >const Path &)> Loader
A ressource loader.
Definition: ResourceManager.h:53
ResourceCache(const ResourceCache &)=delete
Deleted copy constructor.
A texture for colored images.
Definition: Texture.h:317
T * getResource(AssetManager &assetManager, const Path &filename)
Get a ressource.
Definition: ResourceManager.h:87
ResourceManager()
Default constructor.
Definition: Action.h:34
A character font.
Definition: Font.h:130
A ressource manager.
Definition: ResourceManager.h:127
A generic cache for ressources.
Definition: ResourceManager.h:48
Texture * getTexture(const Path &path)
Get a texture.
Definition: ResourceManager.h:140
ResourceCache & operator=(const ResourceCache &)=delete
Deleted copy assignment.
#define GF_API
Definition: Portability.h:35