hx3d  1
2D/3D Simple Game Framework
assets.inl.hpp
1 /*
2  Asset management.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 template <class Asset, class... Args>
22 void AssetManager::create(std::string name, Args... args) {
23  auto& type = typeid(Asset);
24  if (_assets.find(type) == _assets.end()) {
25  _assets[type] = std::map<std::string, Ptr<Resource>>();
26  }
27 
28  _assets[type][name] = std::make_shared<Asset>(args...);
29 }
30 
31 template <class Asset>
32 void AssetManager::add(std::string name, Ptr<Asset> asset) {
33  auto& type = typeid(Asset);
34  if (_assets.find(type) == _assets.end()) {
35  _assets[type] = std::map<std::string, Ptr<Resource>>();
36  }
37 
38  _assets[type][name] = asset;
39 }
40 
41 template <class Asset>
42 Ptr<Asset> AssetManager::get(std::string name) {
43  auto& type = typeid(Asset);
44  if (_assets.find(type) == _assets.end()) {
45  _assets[type] = std::map<std::string, Ptr<Resource>>();
46  }
47 
48  if (_assets[type].find(name) == _assets[type].end()) {
49  Log.Error("Asset `%s` unknown.", name.c_str());
50  return nullptr;
51  }
52 
53  return std::dynamic_pointer_cast<Asset>(_assets[type][name]);
54 }