add assetssetupfunc
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <typeindex>
|
||||
@@ -18,11 +19,17 @@ namespace assetload {
|
||||
using postfunc = std::function<void(Assets*)>;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void assets_setup(const Assets*);
|
||||
|
||||
using assetssetupfunc = std::function<void(const Assets*)>;
|
||||
|
||||
class Assets {
|
||||
std::vector<TextureAnimation> animations;
|
||||
|
||||
using assets_map = std::unordered_map<std::string, std::shared_ptr<void>>;
|
||||
std::unordered_map<std::type_index, assets_map> assets;
|
||||
std::vector<assetssetupfunc> setupFuncs;
|
||||
public:
|
||||
Assets() {}
|
||||
Assets(const Assets&) = delete;
|
||||
@@ -49,6 +56,34 @@ public:
|
||||
}
|
||||
return static_cast<T*>(found->second.get());
|
||||
}
|
||||
|
||||
template<class T>
|
||||
std::optional<const assets_map*> getMap() const {
|
||||
const auto& mapIter = assets.find(typeid(T));
|
||||
if (mapIter == assets.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return &mapIter->second;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
for (auto& setupFunc : setupFuncs) {
|
||||
setupFunc(this);
|
||||
}
|
||||
}
|
||||
|
||||
void addSetupFunc(assetssetupfunc setupfunc) {
|
||||
setupFuncs.push_back(setupfunc);
|
||||
}
|
||||
};
|
||||
|
||||
template<class T>
|
||||
void assets_setup(const Assets* assets) {
|
||||
if (auto mapPtr = assets->getMap<T>()) {
|
||||
for (const auto& entry : **mapPtr) {
|
||||
static_cast<T*>(entry.second.get())->setup(assets);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // ASSETS_ASSETS_HPP_
|
||||
|
||||
Reference in New Issue
Block a user