Dawn/src/dawn/asset/AssetManager.cpp

92 lines
2.3 KiB
C++

// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "AssetManager.hpp"
using namespace Dawn;
void AssetManager::init() {
}
void AssetManager::update() {
this->syncTick();
}
void AssetManager::queueLoad(const std::vector<std::shared_ptr<Asset>> assets) {
assetsToLoad.insert(this->assetsToLoad.end(), assets.begin(), assets.end());
}
void AssetManager::queueLoad(const std::shared_ptr<Asset> &asset) {
this->assetsToLoad.push_back(asset);
}
void AssetManager::queueUnload(
const std::vector<std::shared_ptr<Asset>> &assets
) {
std::cout <<
"Asset list was queued to unload, but is not yet implemented" <<
std::endl;
assetsToUnload.insert(assetsToUnload.end(), assets.begin(), assets.end());
}
void AssetManager::queueUnload(const std::shared_ptr<Asset> &asset) {
std::cout <<
"Asset was queued to unload, but is not yet implemented" <<
std::endl;
this->assetsToUnload.push_back(asset);
}
void AssetManager::queueSwap(
const std::vector<std::shared_ptr<Asset>> &newAssets,
const std::vector<std::shared_ptr<Asset>> &oldAssets
) {
std::vector<std::shared_ptr<Asset>> unload;
std::vector<std::shared_ptr<Asset>> load;
// Determine assets to unload.
std::for_each(oldAssets.begin(), oldAssets.end(), [&](const auto &asset){
auto it = std::find(newAssets.begin(), newAssets.end(), asset);
if(it == newAssets.end()) unload.push_back(asset);
});
// Determine assets to load
std::for_each(newAssets.begin(), newAssets.end(), [&](const auto &asset){
auto it = std::find(oldAssets.begin(), oldAssets.end(), asset);
if(it == oldAssets.end()) load.push_back(asset);
});
this->queueUnload(unload);
this->queueLoad(load);
}
void AssetManager::syncTick() {
std::erase_if(assetsToLoad, [&](auto &asset){
if(asset->loaded) {
asset->eventLoaded.invoke();
return true;
}
asset->updateSync();
asset->updateAsync();//TODO: Make Async
if(asset->loaded) {
asset->eventLoaded.invoke();
return true;
}
return asset->loaded;
});
}
void AssetManager::syncLoad() {
while(this->assetsToLoad.size() > 0) {
this->syncTick();
}
}
AssetManager::~AssetManager() {
this->assets.clear();
}