Gamedev Framework (gf)
0.6.0
A C++11 framework for 2D games
include
gf
ResourceManager.h
1
/*
2
* Gamedev Framework (gf)
3
* Copyright (C) 2016-2017 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
39
/**
40
* @ingroup game
41
* @brief A generic cache for resources
42
*
43
* This function is a low-level class that is used in gf::ResourceManager.
44
* It is generic enough so that you can use it for your own purpose.
45
*
46
* @sa gf::ResourceManager
47
*/
48
template
<
typename
T>
49
class
ResourceCache
{
50
public
:
51
/**
52
* @brief A resource loader
53
*/
54
using
Loader
=
std
::
function
<
std
::
unique_ptr
<
T
>(
const
Path
&)>;
55
56
/**
57
* @brief Constructor
58
*
59
* @param loader A resource loader
60
*/
61
explicit
ResourceCache
(Loader loader)
62
:
m_loader
(
std
::
move
(
loader
))
63
{
64
65
}
66
67
/**
68
* @brief Deleted copy constructor
69
*/
70
ResourceCache
(
const
ResourceCache
&) =
delete
;
71
72
/**
73
* @brief Deleted copy assignment
74
*/
75
ResourceCache
&
operator
=(
const
ResourceCache
&) =
delete
;
76
77
/**
78
* @brief Get a resource
79
*
80
* If the resource exists in the cache, it is returned immediately.
81
* Otherwise, it is searched thanks to an asset manager and put in the
82
* cache. If the resource is not found, an exception is thrown.
83
*
84
* @param assetManager An asset manager
85
* @param filename The filename of the resource
86
* @return A reference to the resource
87
* @throw std::runtime_error If the resource is not found
88
*/
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
125
/**
126
* @ingroup game
127
* @brief A resource manager
128
*
129
* @sa gf::ResourceCache
130
*/
131
class
GF_API
ResourceManager
:
public
AssetManager
{
132
public
:
133
/**
134
* @brief Default constructor
135
*/
136
ResourceManager
();
137
138
/**
139
* @brief Get a texture
140
*
141
* @param path A path to the texture
142
* @return A reference to the texture
143
* @throw std::runtime_error If the texture is not found
144
*/
145
Texture
&
getTexture
(
const
Path& path) {
146
return
m_textures.getResource(*
this
, path);
147
}
148
149
/**
150
* @brief Get a font
151
*
152
* @param path A path to the font
153
* @return A reference to the font
154
* @throw std::runtime_error If the font is not found
155
*/
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
gf::ResourceManager::getTexture
Texture & getTexture(const Path &path)
Get a texture.
Definition:
ResourceManager.h:145
gf::ResourceManager::getFont
Font & getFont(const Path &path)
Get a font.
Definition:
ResourceManager.h:156
gf::ResourceCache::ResourceCache
ResourceCache(Loader loader)
Constructor.
Definition:
ResourceManager.h:61
gf::AssetManager
An asset manager.
Definition:
AssetManager.h:44
gf::ResourceCache::ResourceCache
ResourceCache(const ResourceCache &)=delete
Deleted copy constructor.
gf::Texture
A texture for colored images.
Definition:
Texture.h:339
gf::ResourceCache::getResource
T & getResource(AssetManager &assetManager, const Path &filename)
Get a resource.
Definition:
ResourceManager.h:89
gf::ResourceManager::ResourceManager
ResourceManager()
Default constructor.
gf
The namespace for gf classes.
Definition:
Action.h:34
gf::Font
A character font.
Definition:
Font.h:130
gf::ResourceManager
A resource manager.
Definition:
ResourceManager.h:131
gf::ResourceCache
A generic cache for resources.
Definition:
ResourceManager.h:49
gf::ResourceCache::operator=
ResourceCache & operator=(const ResourceCache &)=delete
Deleted copy assignment.
GF_API
#define GF_API
Definition:
Portability.h:35
gf::AssetManager::getAbsolutePath
Path getAbsolutePath(const Path &relativePath) const
Search a file in the search directories.
Generated by
1.8.13