Working on assets
This commit is contained in:
@ -50,7 +50,6 @@ static inline void assertTrue(bool_t x) {}
|
||||
* Asserts a function as being deprecated.
|
||||
*/
|
||||
void assertDeprecated();
|
||||
|
||||
#else
|
||||
|
||||
#define assertTrue assert
|
||||
|
@ -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.
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -24,6 +24,7 @@ void TextureAsset::updateSync() {
|
||||
this->texture.buffer(this->colors);
|
||||
this->state = 0x05;
|
||||
this->loaded = true;
|
||||
this->eventLoaded.invoke();
|
||||
}
|
||||
|
||||
void TextureAsset::updateAsync() {
|
||||
|
@ -29,6 +29,7 @@ void TrueTypeAsset::updateSync() {
|
||||
|
||||
this->state = 0x05;
|
||||
this->loaded = true;
|
||||
this->eventLoaded.invoke();
|
||||
}
|
||||
|
||||
void TrueTypeAsset::updateAsync() {
|
||||
|
@ -6,6 +6,7 @@
|
||||
#pragma once
|
||||
#include "SceneItem.hpp"
|
||||
#include "event/Event.hpp"
|
||||
#include "asset/Asset.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class DawnGame;
|
||||
@ -41,6 +42,21 @@ namespace Dawn {
|
||||
*/
|
||||
SceneItem * createSceneItem();
|
||||
|
||||
/**
|
||||
* Returns the required assets for this scene. Assets required are meant
|
||||
* to be referenced back to the loader so that special loading actions can
|
||||
* be performed against them.
|
||||
*
|
||||
* @return List of assets required by this scene.
|
||||
*/
|
||||
virtual std::vector<Asset*> getRequiredAssets() = 0;
|
||||
|
||||
/**
|
||||
* Method to begin the actual staging of the scene, typically called after
|
||||
* the scene has finished loading.
|
||||
*/
|
||||
virtual void stage() = 0;
|
||||
|
||||
/**
|
||||
* Finds an existing component on the scene (Root Level Only) that has a
|
||||
* component matching the given component type. Returns nullptr if no item
|
||||
|
@ -11,4 +11,5 @@ target_sources(${DAWN_TARGET_NAME}
|
||||
UILabel.cpp
|
||||
UISprite.cpp
|
||||
UIEmpty.cpp
|
||||
UIGrid.cpp
|
||||
)
|
35
src/dawn/ui/UIGrid.cpp
Normal file
35
src/dawn/ui/UIGrid.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "UIGrid.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
UIGrid::UIGrid(UICanvas *canvas) : UIComponent(canvas) {
|
||||
}
|
||||
|
||||
void UIGrid::setSize(int32_t rows, int32_t columns) {
|
||||
this->gridArea.clear();
|
||||
|
||||
this->rows = rows;
|
||||
this->columns = columns;
|
||||
|
||||
int32_t i, l;
|
||||
l = rows * columns;
|
||||
for(i = 0; i < l; i++) {
|
||||
struct GridArea area;
|
||||
area.uiComponent = nullptr;
|
||||
this->gridArea.push_back(area);
|
||||
}
|
||||
}
|
||||
|
||||
void UIGrid::setItem(int32_t x, int32_t y, UIComponent *comp) {
|
||||
auto item = &this->gridArea[(y * this->rows) + x];
|
||||
|
||||
//Too lazy to support re setting. Need to mod setSize too
|
||||
assertNull(item->uiComponent);
|
||||
|
||||
item->uiComponent = comp;
|
||||
}
|
41
src/dawn/ui/UIGrid.hpp
Normal file
41
src/dawn/ui/UIGrid.hpp
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "UIComponent.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
enum GridAlign {
|
||||
ALIGN_GROW,
|
||||
ALIGN_CONTENT,
|
||||
ALIGN_FIXED
|
||||
};
|
||||
|
||||
struct GridArea {
|
||||
enum GridAlignment xAlign;
|
||||
enum GridAlignment yAlign;
|
||||
float_t width, height;
|
||||
float_t x, y;
|
||||
UIComponent *uiComponent;
|
||||
};
|
||||
|
||||
class UIGrid : public UIComponent {
|
||||
protected:
|
||||
void alignChildren();
|
||||
|
||||
public:
|
||||
glm::vec2 cellPadding;
|
||||
glm::vec2 cellMargin;
|
||||
int32_t rows = 1;
|
||||
int32_t columns = 1;
|
||||
std::vector<struct GridArea> gridArea;
|
||||
|
||||
UIGrid(UICanvas *canvas);
|
||||
|
||||
void setSize(int32_t rows, int32_t columns);
|
||||
|
||||
void setItem(int32_t row, int32_t col, UIComponent *comp);
|
||||
};
|
||||
}
|
22
src/dawn/util/array.hpp
Normal file
22
src/dawn/util/array.hpp
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
#include "assert/assert.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
template<typename T>
|
||||
void vectorAppend(std::vector<T> *list, std::vector<T> *append) {
|
||||
assertNotNull(list);
|
||||
assertNotNull(append);
|
||||
|
||||
auto it = append->begin();
|
||||
while(it != append->end()) {
|
||||
list->push_back(*it);
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user