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
-75
View File
@@ -1,72 +1,8 @@
#include "Assets.hpp"
#include "../audio/audio.hpp"
#include "../graphics/core/Texture.hpp"
#include "../graphics/core/Shader.hpp"
#include "../graphics/core/Atlas.hpp"
#include "../graphics/core/Font.hpp"
#include "../frontend/UiDocument.hpp"
#include "../logic/scripting/scripting.hpp"
Assets::~Assets() {
}
Texture* Assets::getTexture(const std::string& name) const {
auto found = textures.find(name);
if (found == textures.end())
return nullptr;
return found->second.get();
}
void Assets::store(std::unique_ptr<Texture> texture, const std::string& name){
textures.emplace(name, std::move(texture));
}
Shader* Assets::getShader(const std::string& name) const{
auto found = shaders.find(name);
if (found == shaders.end())
return nullptr;
return found->second.get();
}
void Assets::store(std::unique_ptr<Shader> shader, const std::string& name){
shaders.emplace(name, std::move(shader));
}
Font* Assets::getFont(const std::string& name) const {
auto found = fonts.find(name);
if (found == fonts.end())
return nullptr;
return found->second.get();
}
void Assets::store(std::unique_ptr<Font> font, const std::string& name){
fonts.emplace(name, std::move(font));
}
Atlas* Assets::getAtlas(const std::string& name) const {
auto found = atlases.find(name);
if (found == atlases.end())
return nullptr;
return found->second.get();
}
void Assets::store(std::unique_ptr<Atlas> atlas, const std::string& name){
atlases.emplace(name, std::move(atlas));
}
audio::Sound* Assets::getSound(const std::string& name) const {
auto found = sounds.find(name);
if (found == sounds.end())
return nullptr;
return found->second.get();
}
void Assets::store(std::unique_ptr<audio::Sound> sound, const std::string& name) {
sounds.emplace(name, std::move(sound));
}
const std::vector<TextureAnimation>& Assets::getAnimations() {
return animations;
}
@@ -74,14 +10,3 @@ const std::vector<TextureAnimation>& Assets::getAnimations() {
void Assets::store(const TextureAnimation& animation) {
animations.emplace_back(animation);
}
UiDocument* Assets::getLayout(const std::string& name) const {
auto found = layouts.find(name);
if (found == layouts.end())
return nullptr;
return found->second.get();
}
void Assets::store(std::unique_ptr<UiDocument> layout, const std::string& name) {
layouts[name] = std::shared_ptr<UiDocument>(std::move(layout));
}
+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_
+1 -1
View File
@@ -212,7 +212,7 @@ bool AssetsLoader::loadExternalTexture(
const std::string& name,
const std::vector<std::filesystem::path>& alternatives
) {
if (assets->getTexture(name) != nullptr) {
if (assets->get<Texture>(name) != nullptr) {
return true;
}
for (auto& path : alternatives) {