update Assets container - template-based now

This commit is contained in:
MihailRis
2024-06-22 22:30:14 +03:00
parent 848d121099
commit d54b6b2e58
20 changed files with 68 additions and 143 deletions
+27 -28
View File
@@ -7,57 +7,56 @@
#include <memory>
#include <functional>
#include <unordered_map>
#include <typeindex>
#include <typeinfo>
#include <vector>
class Texture;
class Shader;
class Font;
class Atlas;
class Assets;
class UiDocument;
namespace audio {
class Sound;
}
namespace model {
struct Model;
}
namespace assetload {
/// @brief final work to do in the main thread
using postfunc = std::function<void(Assets*)>;
}
class Assets {
std::unordered_map<std::string, std::shared_ptr<Texture>> textures;
std::unordered_map<std::string, std::shared_ptr<Shader>> shaders;
std::unordered_map<std::string, std::shared_ptr<Font>> fonts;
std::unordered_map<std::string, std::shared_ptr<Atlas>> atlases;
std::unordered_map<std::string, std::shared_ptr<UiDocument>> layouts;
std::unordered_map<std::string, std::shared_ptr<audio::Sound>> sounds;
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;
public:
Assets() {}
Assets(const Assets&) = delete;
~Assets();
Texture* getTexture(const std::string& name) const;
void store(std::unique_ptr<Texture> texture, const std::string& name);
Shader* getShader(const std::string& name) const;
void store(std::unique_ptr<Shader> shader, const std::string& name);
Font* getFont(const std::string& name) const;
void store(std::unique_ptr<Font> font, const std::string& name);
Atlas* getAtlas(const std::string& name) const;
void store(std::unique_ptr<Atlas> atlas, const std::string& name);
audio::Sound* getSound(const std::string& name) const;
void store(std::unique_ptr<audio::Sound> sound, const std::string& name);
const std::vector<TextureAnimation>& getAnimations();
void store(const TextureAnimation& animation);
UiDocument* getLayout(const std::string& name) const;
void store(std::unique_ptr<UiDocument> layout, const std::string& name);
template<class T>
void store(std::unique_ptr<T> asset, const std::string& name) {
assets[typeid(T)][name].reset(asset.release());
}
template<class T>
T* get(const std::string& name) const {
const auto& mapIter = assets.find(typeid(T));
if (mapIter == assets.end()) {
return nullptr;
}
const auto& map = mapIter->second;
const auto& found = map.find(name);
if (found == map.end()) {
return nullptr;
}
return static_cast<T*>(found->second.get());
}
};
#endif // ASSETS_ASSETS_HPP_