Dawn/src/dawn/asset/AssetManager.cpp
2023-11-02 17:56:18 -05:00

100 lines
2.4 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) {
std::merge(
this->assetsToLoad.begin(), this->assetsToLoad.end(),
assets.begin(), assets.end(),
this->assetsToLoad.begin()
);
}
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;
std::merge(
this->assetsToUnload.begin(), this->assetsToUnload.end(),
assets.begin(), assets.end(),
this->assetsToUnload.begin()
);
}
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<Asset*> unload;
std::vector<Asset*> load;
// Determine assets to unload.
std::for_each(oldAssets.begin(), oldAssets.end(), [&](Asset *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(), [&](Asset *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();
}