add assetssetupfunc

This commit is contained in:
MihailRis 2024-07-01 16:32:54 +03:00
parent a72a37ccf7
commit 4a3487f17b
2 changed files with 40 additions and 0 deletions

View File

@ -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_

View File

@ -23,6 +23,7 @@
#include "graphics/core/ImageData.hpp"
#include "graphics/core/Shader.hpp"
#include "graphics/ui/GUI.hpp"
#include "objects/rigging.hpp"
#include "logic/EngineController.hpp"
#include "logic/CommandsInterpreter.hpp"
#include "logic/scripting/scripting.hpp"
@ -121,6 +122,7 @@ void Engine::loadControls() {
}
void Engine::onAssetsLoaded() {
assets->setup();
gui->onAssetsLoad(assets.get());
}
@ -243,9 +245,12 @@ void Engine::loadAssets() {
Shader::preprocessor->setPaths(resPaths.get());
auto new_assets = std::make_unique<Assets>();
new_assets->addSetupFunc(assets_setup<rigging::RigConfig>);
AssetsLoader loader(new_assets.get(), resPaths.get());
AssetsLoader::addDefaults(loader, content.get());
// no need
// correct log messages order is more useful
bool threading = false;
if (threading) {
auto task = loader.startTask([=](){});