Dawn/src/dawn/asset/AssetManager.cpp

86 lines
2.2 KiB
C++

// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "AssetManager.hpp"
#include "loaders/TextureLoader.hpp"
#include "loaders/TrueTypeLoader.hpp"
using namespace Dawn;
void AssetManager::init() {
}
void AssetManager::update() {
auto itPending = pendingAssetLoaders.begin();
while(itPending != pendingAssetLoaders.end()) {
auto loader = *itPending;
loader->updateSync();
loader->updateAsync();
if(loader->loaded) {
finishedAssetLoaders.push_back(loader);
itPending = pendingAssetLoaders.erase(itPending);
} else {
itPending++;
}
}
}
void AssetManager::removeExisting(const std::string filename) {
auto existing = std::find_if(
pendingAssetLoaders.begin(), pendingAssetLoaders.end(),
[&](auto &loader) {
return loader->name == filename;
}
);
if(existing != pendingAssetLoaders.end()) {
pendingAssetLoaders.erase(existing);
}
existing = std::find_if(
finishedAssetLoaders.begin(), finishedAssetLoaders.end(),
[&](auto &loader) {
return loader->name == filename;
}
);
if(existing != finishedAssetLoaders.end()) {
finishedAssetLoaders.erase(existing);
}
}
bool_t AssetManager::isEverythingLoaded() {
return pendingAssetLoaders.size() == 0;
}
bool_t AssetManager::isLoaded(const std::string filename) {
auto existing = this->getExisting<AssetLoader>(filename);
if(existing) return existing->loaded;
return false;
}
template<>
std::shared_ptr<TrueTypeTexture> AssetManager::get<TrueTypeTexture>(
const std::string filename,
const uint32_t fontSize
) {
auto existing = this->getExisting<TrueTypeLoader>(filename);
if(existing) {
// Check pointer hasn't gone stale, if it has remove it and create new.
auto texture = existing->getTexture(fontSize);
if(texture) return texture;
this->removeExisting(filename);
}
std::shared_ptr<TrueTypeLoader> loader = std::make_shared<TrueTypeLoader>(
filename
);
pendingAssetLoaders.push_back(std::static_pointer_cast<AssetLoader>(loader));
return loader->getTexture(fontSize);
}
AssetManager::~AssetManager() {
}