Sunset old state system finally.

This commit is contained in:
2023-11-02 12:36:14 -05:00
parent 62d207d066
commit 239f7b2fb9
32 changed files with 212 additions and 420 deletions

View File

@ -13,8 +13,7 @@ namespace Dawn {
public:
const std::string name;
bool_t loaded = false;
Event<> eventLoaded;
StateEvent<> event2Loaded;
StateEvent<> eventLoaded;
/**
* Create an abstract Asset object.

View File

@ -15,7 +15,7 @@ void AssetManager::update() {
this->syncTick();
}
void AssetManager::queueLoad(std::vector<Asset*> assets) {
void AssetManager::queueLoad(const std::vector<Asset*> assets) {
std::merge(
this->assetsToLoad.begin(), this->assetsToLoad.end(),
assets.begin(), assets.end(),
@ -27,7 +27,7 @@ void AssetManager::queueLoad(Asset *asset) {
this->assetsToLoad.push_back(asset);
}
void AssetManager::queueUnload(std::vector<Asset*> assets) {
void AssetManager::queueUnload(const std::vector<Asset*> assets) {
std::cout <<
"Asset list was queued to unload, but is not yet implemented" <<
std::endl;
@ -46,60 +46,45 @@ void AssetManager::queueUnload(Asset *asset) {
}
void AssetManager::queueSwap(
std::vector<Asset*> newAssets,
std::vector<Asset*> oldAssets
const std::vector<Asset*> newAssets,
const 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 == newAssets.end()) unload.push_back(*itUnload);
++itUnload;
}
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
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;
}
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() {
auto it = this->assetsToLoad.begin();
while(it != this->assetsToLoad.end()) {
auto asset = *it;
std::erase_if(assetsToLoad, [&](auto &asset){
if(asset->loaded) {
it = this->assetsToLoad.erase(it);
asset->eventLoaded.invoke();
asset->event2Loaded.invoke();
continue;
return true;
}
asset->updateSync();
asset->updateAsync();//TODO: Make Async
if(asset->loaded) {
it = this->assetsToLoad.erase(it);
asset->eventLoaded.invoke();
asset->event2Loaded.invoke();
continue;
return true;
}
++it;
}
// auto it = this->assetsNotLoaded.begin();
// auto it2 = this->assetsToUnload.begin();
// while(it2 != this->assetsToUnload.end()) {
// ++it2;
// }
return asset->loaded;
});
}
void AssetManager::syncLoad() {
@ -109,9 +94,7 @@ void AssetManager::syncLoad() {
}
AssetManager::~AssetManager() {
auto it = this->assets.begin();
while(it != this->assets.end()) {
delete it->second;
++it;
}
std::for_each(this->assets.begin(), this->assets.end(), [&](auto &item){
delete item.second;
});
}

View File

@ -30,7 +30,13 @@ namespace Dawn {
*
* @param assets Assets to load.
*/
void queueLoad(std::vector<Asset*> assets);
void queueLoad(const std::vector<Asset*> assets);
/**
* Queue a loading of a single asset. Does not actually begin loading.
*
* @param assets Assets to load.
*/
void queueLoad(Asset *assets);
/**
@ -38,7 +44,13 @@ namespace Dawn {
*
* @param assets Assets to unload.
*/
void queueUnload(std::vector<Asset*> assets);
void queueUnload(const std::vector<Asset*> assets);
/**
* Takes a single asset to queue to unload. Does not immediately unload.
*
* @param assets Assets to unload.
*/
void queueUnload(Asset* assets);
/**
@ -49,8 +61,8 @@ namespace Dawn {
* @param oldAssets Old list of assets to no longer maintain.
*/
void queueSwap(
std::vector<Asset*> newAssets,
std::vector<Asset*> oldAssets
const std::vector<Asset*> newAssets,
const std::vector<Asset*> oldAssets
);
/**

View File

@ -7,15 +7,14 @@
using namespace Dawn;
IAssetLoader::IAssetLoader(std::string fileName) {
IAssetLoader::IAssetLoader(const std::string fileName) : fileName(fileName) {
assertTrue(
fileName.size() > 0,
"IAssetLoader::IAssetLoader: fileName must be greater than 0"
);
this->fileName = fileName;
}
size_t IAssetLoader::setPosition(size_t position) {
size_t IAssetLoader::setPosition(const size_t position) {
assertTrue(
position >= 0,
"IAssetLoader::setPosition: position must be greater than or equal to 0"

View File

@ -24,7 +24,7 @@ namespace Dawn {
*
* @param fileName File name of the asset that is to be loaded.
*/
IAssetLoader(std::string fileName);
IAssetLoader(const std::string fileName);
/**
* Platform-centric method to open a file buffer to an asset.
@ -43,7 +43,7 @@ namespace Dawn {
* @param size Length of the data buffer (How many bytes to read).
* @return The count of bytes read.
*/
virtual size_t read(uint8_t *buffer, size_t size) = 0;
virtual size_t read(uint8_t *buffer, const size_t size) = 0;
/**
* Get the size of the asset.
@ -57,7 +57,7 @@ namespace Dawn {
* @param n Count of bytes to progress the read head by.
* @return Count of bytes progressed.
*/
virtual size_t skip(size_t n) = 0;
virtual size_t skip(const size_t n) = 0;
/**
* Rewind the read head to the beginning of the file.
@ -70,7 +70,7 @@ namespace Dawn {
*
* @param absolutePosition Absolute position to set the read head to.
*/
size_t setPosition(size_t absolutePosition);
size_t setPosition(const size_t absolutePosition);
/**
* Returns the current position of the read head.
@ -89,13 +89,14 @@ namespace Dawn {
* @return The count of bytes read.
*/
template<class T>
size_t loadBufferedCallback(T *instance, bool_t (T::*callback)(uint8_t n)) {
size_t loadBufferedCallback(
std::function<bool_t(uint8_t n)> callback
) {
uint8_t buffer[1024];
size_t read, length;
int32_t i;
bool_t result;
assertNotNull(instance, "Instance cannot be null.");
assertNotNull(callback, "Callback cannot be null.");
// Open the buffer.
@ -107,7 +108,7 @@ namespace Dawn {
// Buffer from input
while((read = this->read(buffer, 1024)) != 0) {
for(i = 0; i < read; i++) {
result = ((*instance).*(callback))(buffer[i]);
result = callback(buffer[i]);
if(!result) {
length += i;
break;

View File

@ -64,7 +64,7 @@ void LanguageAsset::updateAsync() {
this->loaded = true;
}
std::string LanguageAsset::getValue(std::string key) {
std::string LanguageAsset::getValue(const std::string key) {
assertTrue(
this->state == LANGUAGE_ASSET_LOAD_STATE_READY_TO_READ,
"State must be LANGUAGE_ASSET_LOAD_STATE_READY_TO_READ"

View File

@ -31,6 +31,6 @@ namespace Dawn {
LanguageAsset(const std::string name);
void updateSync() override;
void updateAsync() override;
std::string getValue(std::string key);
std::string getValue(const std::string key);
};
}

View File

@ -142,13 +142,15 @@ void TrueTypeAsset::updateAsync() {
// Now we are at the first byte of the first style.
this->state = TRUE_TYPE_ASSET_STATE_ADJUSTING_OFFSETS;
auto itStyle = assetStyles.begin();
while(itStyle != assetStyles.end()) {
(*itStyle).dataOffset = styleListBegin;
styleListBegin += (*itStyle).dataSize;
styleListBegin += 1; // Vertical bar
++itStyle;
}
std::for_each(
assetStyles.begin(),
assetStyles.end(),
[&](struct TrueTypeAssetStyle &style){
style.dataOffset = styleListBegin;
styleListBegin += style.dataSize;
styleListBegin += 1;
}
);
// Init FreeType
this->state = TRUE_TYPE_ASSET_STATE_INIT_FREETYPE;
@ -160,7 +162,7 @@ void TrueTypeAsset::updateAsync() {
this->loaded = true;
}
usagelockid_t TrueTypeAsset::lock(struct TrueTypeFaceTextureStyle style) {
usagelockid_t TrueTypeAsset::lock(const struct TrueTypeFaceTextureStyle style) {
assertTrue(this->state == TRUE_TYPE_ASSET_STATE_READY, "Asset is not ready");
// Try and find an existing texture that matches this style
@ -214,22 +216,24 @@ usagelockid_t TrueTypeAsset::lock(struct TrueTypeFaceTextureStyle style) {
return lock;
}
TrueTypeFaceTexture * TrueTypeAsset::getTexture(usagelockid_t id) {
TrueTypeFaceTexture * TrueTypeAsset::getTexture(const usagelockid_t id) {
auto it = this->textureByLock.find(id);
assertTrue(it != this->textureByLock.end(), "Could not find texture");
return it->second;
}
void TrueTypeAsset::unlock(usagelockid_t id) {
void TrueTypeAsset::unlock(const usagelockid_t id) {
this->locks.removeLock(id);
}
TrueTypeAsset::~TrueTypeAsset() {
auto it = this->textures.begin();
while(it != this->textures.end()) {
delete (*it);
it++;
}
std::for_each(
this->textures.begin(),
this->textures.end(),
[](TrueTypeFaceTexture *texture){
delete texture;
}
);
FT_Done_FreeType(this->fontLibrary);
}

View File

@ -55,7 +55,7 @@ namespace Dawn {
* @param style Style to lock.
* @return A unique lock ID for this style.
*/
usagelockid_t lock(struct TrueTypeFaceTextureStyle style);
usagelockid_t lock(const struct TrueTypeFaceTextureStyle style);
/**
* Get a texture by a previous lock ID.
@ -63,14 +63,14 @@ namespace Dawn {
* @param lock Lock to get the texture of.
* @return Matching texture by this ID.
*/
TrueTypeFaceTexture * getTexture(usagelockid_t lock);
TrueTypeFaceTexture * getTexture(const usagelockid_t lock);
/**
* Releases a previously held font lock.
*
* @param lock Lock to release/unlock.
*/
void unlock(usagelockid_t lock);
void unlock(const usagelockid_t lock);
~TrueTypeAsset();
};