cleaned things up a bit, looks good to start implementing the rpg mechs
This commit is contained in:
@ -8,8 +8,8 @@
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
AssetLoader::AssetLoader(const std::string &name) : name(name) {
|
||||
assertTrue(name.size() > 0, "Asset::Asset: Name cannot be empty");
|
||||
AssetLoader::AssetLoader(const std::string name) : name(name) {
|
||||
assertTrue(name.size() > 0, "Name cannot be empty");
|
||||
}
|
||||
|
||||
AssetLoader::~AssetLoader() {
|
||||
|
@ -17,7 +17,7 @@ namespace Dawn {
|
||||
*
|
||||
* @param name Name of the asset.
|
||||
*/
|
||||
AssetLoader(const std::string &name);
|
||||
AssetLoader(const std::string name);
|
||||
|
||||
/**
|
||||
* Virtual function that will be called by the asset manager on a
|
||||
|
@ -17,6 +17,7 @@ void AssetManager::update() {
|
||||
auto loader = *itPending;
|
||||
loader->updateSync();
|
||||
loader->updateAsync();
|
||||
loader->updateSync();
|
||||
if(loader->loaded) {
|
||||
finishedAssetLoaders.push_back(loader);
|
||||
itPending = pendingAssetLoaders.erase(itPending);
|
||||
|
@ -22,13 +22,14 @@ namespace Dawn {
|
||||
template<class T>
|
||||
std::shared_ptr<T> getExisting(const std::string &filename) {
|
||||
auto existing = std::find_if(
|
||||
pendingAssetLoaders.begin(), pendingAssetLoaders.end(),
|
||||
pendingAssetLoaders.begin(),
|
||||
pendingAssetLoaders.end(),
|
||||
[&](auto &loader) {
|
||||
return loader->name == filename;
|
||||
}
|
||||
);
|
||||
|
||||
if(existing == finishedAssetLoaders.end()) {
|
||||
if(existing == pendingAssetLoaders.end()) {
|
||||
existing = std::find_if(
|
||||
finishedAssetLoaders.begin(), finishedAssetLoaders.end(),
|
||||
[&](auto &loader) {
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
JSONLoader::JSONLoader(const std::string &name) :
|
||||
JSONLoader::JSONLoader(const std::string name) :
|
||||
AssetLoader(name),
|
||||
loader(name),
|
||||
state(JSONLoaderState::INITIAL)
|
||||
|
@ -25,7 +25,7 @@ namespace Dawn {
|
||||
public:
|
||||
json data;
|
||||
|
||||
JSONLoader(const std::string &name);
|
||||
JSONLoader(const std::string name);
|
||||
void updateSync() override;
|
||||
void updateAsync() override;
|
||||
~JSONLoader();
|
||||
|
@ -8,13 +8,12 @@
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
TextureLoader::TextureLoader(const std::string &name) :
|
||||
TextureLoader::TextureLoader(const std::string name) :
|
||||
AssetLoader(name),
|
||||
loader(name + ".texture"),
|
||||
loader(name),
|
||||
state(TextureLoaderLoadState::INITIAL)
|
||||
{
|
||||
sharedTexture = std::make_shared<Texture>();
|
||||
weakTexture = sharedTexture;
|
||||
texture = std::make_shared<Texture>();
|
||||
}
|
||||
|
||||
void TextureLoader::updateAsync() {
|
||||
@ -114,34 +113,35 @@ void TextureLoader::updateSync() {
|
||||
if(this->state != TextureLoaderLoadState::ASYNC_DONE) return;
|
||||
this->state = TextureLoaderLoadState::SYNC_LOADING;
|
||||
|
||||
assertNotNull(this->sharedTexture, "Texture is null!");
|
||||
assertNotNull(this->texture, "Texture is null!");
|
||||
assertNotNull(this->data, "Texture data is null!");
|
||||
|
||||
// Setup Texture
|
||||
this->sharedTexture->setSize(
|
||||
this->texture->setSize(
|
||||
this->width,
|
||||
this->height,
|
||||
this->format,
|
||||
TextureDataFormat::UNSIGNED_BYTE
|
||||
);
|
||||
this->sharedTexture->buffer(this->data);
|
||||
this->texture->buffer(this->data);
|
||||
|
||||
// Free data buffer
|
||||
delete[] this->data;
|
||||
this->data = nullptr;
|
||||
|
||||
// Leat go of the held pointer
|
||||
this->sharedTexture = nullptr;
|
||||
|
||||
// Hand off and call done
|
||||
this->state = TextureLoaderLoadState::SYNC_DONE;
|
||||
this->loaded = true;
|
||||
}
|
||||
|
||||
std::shared_ptr<Texture> TextureLoader::getTexture() {
|
||||
return this->texture;
|
||||
}
|
||||
|
||||
TextureLoader::~TextureLoader() {
|
||||
if(this->data != nullptr) {
|
||||
delete[] this->data;
|
||||
this->data = nullptr;
|
||||
}
|
||||
this->sharedTexture = nullptr;
|
||||
this->texture = nullptr;
|
||||
}
|
@ -30,22 +30,27 @@ namespace Dawn {
|
||||
enum TextureWrapMode wrapY;
|
||||
enum TextureFilterMode filterMin;
|
||||
enum TextureFilterMode filterMag;
|
||||
std::shared_ptr<Texture> texture;
|
||||
|
||||
public:
|
||||
std::shared_ptr<Texture> sharedTexture;
|
||||
std::weak_ptr<Texture> weakTexture;
|
||||
|
||||
/**
|
||||
* Constructs a texture asset loader. You should instead use the parent
|
||||
* asset managers' abstracted load method
|
||||
*
|
||||
* @param name File name asset to load, omitting the extension.
|
||||
*/
|
||||
TextureLoader(const std::string &name);
|
||||
TextureLoader(const std::string name);
|
||||
|
||||
void updateSync() override;
|
||||
void updateAsync() override;
|
||||
|
||||
/**
|
||||
* Get the texture asset.
|
||||
*
|
||||
* @return Texture asset.
|
||||
*/
|
||||
std::shared_ptr<Texture> getTexture();
|
||||
|
||||
/**
|
||||
* Dispose / Cleanup the texture asset. Will also dispose the underlying
|
||||
* texture itself.
|
||||
|
@ -8,9 +8,9 @@
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
TrueTypeLoader::TrueTypeLoader(const std::string &name) :
|
||||
TrueTypeLoader::TrueTypeLoader(const std::string name) :
|
||||
AssetLoader(name),
|
||||
loader(name + ".ttf")
|
||||
loader(name)
|
||||
{
|
||||
// Init the font.
|
||||
auto ret = FT_Init_FreeType(&fontLibrary);
|
||||
|
@ -33,7 +33,7 @@ namespace Dawn {
|
||||
*
|
||||
* @param name File name asset to load, omitting the extension.
|
||||
*/
|
||||
TrueTypeLoader(const std::string &name);
|
||||
TrueTypeLoader(const std::string name);
|
||||
|
||||
void updateSync() override;
|
||||
void updateAsync() override;
|
||||
|
@ -49,6 +49,13 @@ void SceneComponent::dispose() {
|
||||
this->item.reset();
|
||||
}
|
||||
|
||||
bool_t SceneComponent::isInitialized() {
|
||||
return Flag::isOn<uint_fast8_t>(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_INIT
|
||||
);
|
||||
}
|
||||
|
||||
std::shared_ptr<SceneItem> SceneComponent::getItem() {
|
||||
return this->item.lock();
|
||||
}
|
||||
|
@ -47,6 +47,13 @@ namespace Dawn {
|
||||
*/
|
||||
void dispose();
|
||||
|
||||
/**
|
||||
* Returns whether this scene component is initialized.
|
||||
*
|
||||
* @return Whether this scene component is initialized.
|
||||
*/
|
||||
bool_t isInitialized();
|
||||
|
||||
/**
|
||||
* Returns the scene item that this scene component belongs to.
|
||||
*
|
||||
|
@ -25,8 +25,28 @@ std::shared_ptr<Scene> SceneItem::getScene() {
|
||||
|
||||
void SceneItem::init() {
|
||||
auto sharedThis = shared_from_this();
|
||||
for(auto &component : components) {
|
||||
component->init(sharedThis);
|
||||
|
||||
// Loop until all components initialized...
|
||||
while(true) {
|
||||
// Create copy of the components, components may chose to add more components
|
||||
// but those sub components will not be initialized at this time.
|
||||
auto components = this->components;
|
||||
for(auto &component : components) {
|
||||
if(component->isInitialized()) continue;
|
||||
component->init(sharedThis);
|
||||
}
|
||||
|
||||
// If they are all initalized we are fine.
|
||||
components = this->components;
|
||||
bool_t allInitialized = std::all_of(
|
||||
components.begin(),
|
||||
components.end(),
|
||||
[](auto &component) {
|
||||
return component->isInitialized();
|
||||
}
|
||||
);
|
||||
|
||||
if(allInitialized) break;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user