Timeout and Interval events

This commit is contained in:
2023-11-25 23:57:07 -06:00
parent 94941b1c14
commit 3920dd34a3
15 changed files with 418 additions and 10 deletions

View File

@ -27,13 +27,45 @@ void AssetManager::update() {
}
}
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);
}
}
template<>
std::shared_ptr<Texture> AssetManager::get<Texture>(
const std::string filename
) {
// TODO: Check for existing loader
auto existing = this->getExisting<TextureLoader>(filename);
if(existing) {
std::cout << "Found existing texture loader for " << filename << std::endl;
// Check pointer hasn't gone stale, if it has remove it and create new.
auto texture = existing->weakTexture.lock();
if(texture) return texture;
std::cout << "Texture loader for " << filename << " has gone stale." << std::endl;
this->removeExisting(filename);
}
// Create new loader
std::shared_ptr<TextureLoader> loader = std::make_shared<TextureLoader>(
filename
);

View File

@ -13,6 +13,42 @@ namespace Dawn {
std::vector<std::shared_ptr<AssetLoader>> pendingAssetLoaders;
std::vector<std::shared_ptr<AssetLoader>> finishedAssetLoaders;
/**
* Returns an existing asset loader if it exists.
*
* @param filename The filename of the asset to get.
* @return The asset loader if it exists, otherwise nullptr.
*/
template<class T>
std::shared_ptr<T> getExisting(const std::string filename) {
auto existing = std::find_if(
pendingAssetLoaders.begin(), pendingAssetLoaders.end(),
[&](auto &loader) {
return loader->name == filename;
}
);
if(existing == finishedAssetLoaders.end()) {
existing = std::find_if(
finishedAssetLoaders.begin(), finishedAssetLoaders.end(),
[&](auto &loader) {
return loader->name == filename;
}
);
if(existing == finishedAssetLoaders.end()) return nullptr;
}
return std::static_pointer_cast<T>(*existing);
}
/**
* Removes an existing asset loader if it exists.
*
* @param filename The filename of the asset to remove.
*/
void removeExisting(const std::string filename);
public:
/**
* Initializes this asset manager so it can begin accepting assets.