Working on assets

This commit is contained in:
2022-12-01 22:37:42 -08:00
parent 535e2b2dc5
commit ba9881e39d
18 changed files with 355 additions and 42 deletions

View File

@ -6,6 +6,7 @@
#pragma once
#include "dawnlibs.hpp"
#include "assert/assert.hpp"
#include "event/Event.hpp"
namespace Dawn {
class AssetManager;
@ -16,6 +17,7 @@ namespace Dawn {
std::string name;
uint8_t state = 0x00;
bool loaded = false;
Event<> eventLoaded;
/**
* Create an abstract Asset object.

View File

@ -15,26 +15,72 @@ void AssetManager::init() {
}
void AssetManager::update() {
auto it = this->assetsNotLoaded.begin();
while(it != this->assetsNotLoaded.end()) {
auto asset = it->second;
void AssetManager::queueLoad(std::vector<Asset*> assets) {
vectorAppend(&this->assetsToLoad, &assets);
}
void AssetManager::queueUnload(std::vector<Asset*> assets) {
vectorAppend(&this->assetsToUnload, &assets);
}
void AssetManager::queueSwap(
std::vector<Asset*> newAssets,
std::vector<Asset*> oldAssets
) {
std::vector<Asset*> unload;
std::vector<Asset*> load;
// Determine assets to unload.
auto itUnload = oldAssets.begin();
while(itUnload != oldAssets.end()) {
auto inNewAssets = std::find(newAssets.begin(), newAssets.end(), *itUnload);
if(inNewAssets == oldAssets.end()) unload.push_back(*itUnload);
++itUnload;
}
// Determine assets to load
auto itLoad = newAssets.begin();
while(itLoad != newAssets.end()) {
auto inOldAssets = std::find(oldAssets.begin(), oldAssets.end(), *itLoad);
if(inOldAssets == oldAssets.end()) load.push_back(*itLoad);
++itLoad;
}
this->queueUnload(unload);
this->queueLoad(load);
}
void AssetManager::syncTick() {
auto it = this->assetsToLoad.begin();
// auto it = this->assetsNotLoaded.begin();
while(it != this->assetsToLoad.end()) {
auto asset = *it;
if(asset->loaded) {
it = this->assetsNotLoaded.erase(it);
it = this->assetsToLoad.erase(it);
continue;
}
asset->updateSync();
asset->updateAsync();
asset->updateAsync();//TODO: Make Async
if(asset->loaded) {
it = this->assetsNotLoaded.erase(it);
this->assets[asset->name] = asset;
it = this->assetsToLoad.erase(it);
continue;
}
++it;
}
// auto it2 = this->assetsToUnload.begin();
// while(it2 != this->assetsToUnload.end()) {
// ++it2;
// }
}
void AssetManager::syncLoad() {
while(this->assetsToLoad.size() > 0) {
this->syncTick();
}
}
AssetManager::~AssetManager() {
@ -43,10 +89,4 @@ AssetManager::~AssetManager() {
delete it->second;
++it;
}
auto it2 = this->assetsNotLoaded.begin();
while(it2 != this->assetsNotLoaded.end()) {
delete it2->second;
++it2;
}
}

View File

@ -5,13 +5,15 @@
#pragma once
#include "Asset.hpp"
#include "util/array.hpp"
namespace Dawn {
class AssetManager {
private:
/** List of pointers to assets, mapped by their asset key. */
std::map<std::string, Asset*> assets;
std::map<std::string, Asset*> assetsNotLoaded;
std::vector<Asset*> assetsToLoad;
std::vector<Asset*> assetsToUnload;
public:
/**
@ -20,9 +22,41 @@ namespace Dawn {
void init();
/**
* Ticks the asset manager for a single frame, synchronously.
* Queue a loading of a list of assets. Does not actually begin loading.
*
* @param assets Assets to load.
*/
void update();
void queueLoad(std::vector<Asset*> assets);
/**
* Takes a list of lists to queue to unload. Does not immediately unload.
*
* @param assets Assets to unload.
*/
void queueUnload(std::vector<Asset*> assets);
/**
* Queues load and unload based on the difference between two sets of
* assets. This is mostly used to perform a scene change.
*
* @param newAssets New list of assets to maintain.
* @param oldAssets Old list of assets to no longer maintain.
*/
void queueSwap(
std::vector<Asset*> newAssets,
std::vector<Asset*> oldAssets
);
/**
* Ticks the asset manager, synchronously.
*/
void syncTick();
/**
* Loads everything that isn't loaded, blocks the current thread until
* that has finished.
*/
void syncLoad();
/**
* Creates and queue an asset to load.
@ -31,7 +65,7 @@ namespace Dawn {
* @return The asset element to be loaded.
*/
template<class T>
T * load(std::string name) {
T * get(std::string name) {
assertTrue(name.size() > 0);
auto existing = this->assets.find(name);
@ -40,7 +74,6 @@ namespace Dawn {
}
auto asset = new T(this, name);
this->assets[name] = asset;
this->assetsNotLoaded[name] = asset;
return asset;
}

View File

@ -24,6 +24,7 @@ void TextureAsset::updateSync() {
this->texture.buffer(this->colors);
this->state = 0x05;
this->loaded = true;
this->eventLoaded.invoke();
}
void TextureAsset::updateAsync() {

View File

@ -29,6 +29,7 @@ void TrueTypeAsset::updateSync() {
this->state = 0x05;
this->loaded = true;
this->eventLoaded.invoke();
}
void TrueTypeAsset::updateAsync() {