Scopify assets

This commit is contained in:
2024-12-02 08:46:21 -06:00
parent 4dccd7d969
commit ac0f0e86c5
13 changed files with 78 additions and 46 deletions

View File

@ -8,6 +8,8 @@
using namespace Dawn; using namespace Dawn;
const std::string AssetLoader::ASSET_TYPE = "unknown";
AssetLoader::AssetLoader( AssetLoader::AssetLoader(
const std::shared_ptr<AssetManager> assetManager, const std::shared_ptr<AssetManager> assetManager,
const std::string name const std::string name

View File

@ -14,9 +14,10 @@ namespace Dawn {
std::weak_ptr<AssetManager> assetManager; std::weak_ptr<AssetManager> assetManager;
public: public:
const static std::string ASSET_TYPE;
const std::string name; const std::string name;
bool_t loaded = false; bool_t loaded = false;
/** /**
* Create an abstract Asset object. * Create an abstract Asset object.
@ -42,6 +43,13 @@ namespace Dawn {
*/ */
virtual void updateAsync() = 0; virtual void updateAsync() = 0;
/**
* Returns the asset type.
*
* @return The asset type.
*/
virtual std::string getAssetType() const = 0;
/** /**
* Returns the asset manager. * Returns the asset manager.
* *

View File

@ -27,38 +27,33 @@ void AssetManager::update() {
} }
} }
void AssetManager::removeExisting(const std::string &filename) { // Disabled because it does not respect scopes
auto existing = std::find_if( // void AssetManager::removeExisting(const std::string &filename) {
pendingAssetLoaders.begin(), pendingAssetLoaders.end(), // auto existing = std::find_if(
[&](auto &loader) { // pendingAssetLoaders.begin(), pendingAssetLoaders.end(),
return loader->name == filename; // [&](auto &loader) {
} // return loader->name == filename;
); // }
if(existing != pendingAssetLoaders.end()) { // );
pendingAssetLoaders.erase(existing); // if(existing != pendingAssetLoaders.end()) {
} // pendingAssetLoaders.erase(existing);
// }
existing = std::find_if( // existing = std::find_if(
finishedAssetLoaders.begin(), finishedAssetLoaders.end(), // finishedAssetLoaders.begin(), finishedAssetLoaders.end(),
[&](auto &loader) { // [&](auto &loader) {
return loader->name == filename; // return loader->name == filename;
} // }
); // );
if(existing != finishedAssetLoaders.end()) { // if(existing != finishedAssetLoaders.end()) {
finishedAssetLoaders.erase(existing); // finishedAssetLoaders.erase(existing);
} // }
} // }
bool_t AssetManager::isEverythingLoaded() { bool_t AssetManager::isEverythingLoaded() {
return pendingAssetLoaders.size() == 0; return pendingAssetLoaders.size() == 0;
} }
bool_t AssetManager::isLoaded(const std::string &filename) {
auto existing = this->getExisting<AssetLoader>(filename);
if(existing) return existing->loaded;
return false;
}
AssetManager::~AssetManager() { AssetManager::~AssetManager() {
} }

View File

@ -25,7 +25,10 @@ namespace Dawn {
pendingAssetLoaders.begin(), pendingAssetLoaders.begin(),
pendingAssetLoaders.end(), pendingAssetLoaders.end(),
[&](auto &loader) { [&](auto &loader) {
return loader->name == filename; return (
loader->name == filename &&
loader->getAssetType() == T::ASSET_TYPE
);
} }
); );
@ -33,7 +36,10 @@ namespace Dawn {
existing = std::find_if( existing = std::find_if(
finishedAssetLoaders.begin(), finishedAssetLoaders.end(), finishedAssetLoaders.begin(), finishedAssetLoaders.end(),
[&](auto &loader) { [&](auto &loader) {
return loader->name == filename; return (
loader->name == filename &&
loader->getAssetType() == T::ASSET_TYPE
);
} }
); );
@ -69,14 +75,6 @@ namespace Dawn {
*/ */
bool_t isEverythingLoaded(); bool_t isEverythingLoaded();
/**
* Returns whether the asset manager has loaded the given asset.
*
* @param filename The filename of the asset to check.
* @return True if the asset has been loaded.
*/
bool_t isLoaded(const std::string &filename);
/** /**
* Returns the asset loader for the given asset. * Returns the asset loader for the given asset.
* *

View File

@ -7,6 +7,8 @@
using namespace Dawn; using namespace Dawn;
const std::string JSONLoader::ASSET_TYPE = "json";
JSONLoader::JSONLoader( JSONLoader::JSONLoader(
const std::shared_ptr<AssetManager> assetManager, const std::shared_ptr<AssetManager> assetManager,
const std::string name const std::string name
@ -30,6 +32,10 @@ void JSONLoader::updateAsync() {
void JSONLoader::updateSync() { void JSONLoader::updateSync() {
} }
std::string JSONLoader::getAssetType() const {
return JSONLoader::ASSET_TYPE;
}
JSONLoader::~JSONLoader() { JSONLoader::~JSONLoader() {
} }

View File

@ -23,6 +23,8 @@ namespace Dawn {
enum JSONLoaderState state; enum JSONLoaderState state;
public: public:
const static std::string ASSET_TYPE;
json data; json data;
JSONLoader( JSONLoader(
@ -31,6 +33,7 @@ namespace Dawn {
); );
void updateSync() override; void updateSync() override;
void updateAsync() override; void updateAsync() override;
std::string getAssetType() const override;
~JSONLoader(); ~JSONLoader();
}; };
} }

View File

@ -7,6 +7,8 @@
using namespace Dawn; using namespace Dawn;
const std::string SceneLoader::ASSET_TYPE = "scene";
SceneLoader::SceneLoader( SceneLoader::SceneLoader(
const std::shared_ptr<AssetManager> assetManager, const std::shared_ptr<AssetManager> assetManager,
const std::string name const std::string name
@ -35,6 +37,10 @@ void SceneLoader::updateSync() {
} }
std::string SceneLoader::getAssetType() const {
return SceneLoader::ASSET_TYPE;
}
std::shared_ptr<Scene> SceneLoader::getScene() { std::shared_ptr<Scene> SceneLoader::getScene() {
return scene; return scene;
} }

View File

@ -21,12 +21,15 @@ namespace Dawn {
std::shared_ptr<Scene> scene; std::shared_ptr<Scene> scene;
public: public:
const static std::string ASSET_TYPE;
SceneLoader( SceneLoader(
const std::shared_ptr<AssetManager> assetManager, const std::shared_ptr<AssetManager> assetManager,
const std::string name const std::string name
); );
void updateSync() override; void updateSync() override;
void updateAsync() override; void updateAsync() override;
std::string getAssetType() const override;
/** /**
* Returns the Scene that was loaded, or nullptr if not loaded. * Returns the Scene that was loaded, or nullptr if not loaded.

View File

@ -8,6 +8,8 @@
using namespace Dawn; using namespace Dawn;
const std::string TextureLoader::ASSET_TYPE = "texture";
TextureLoader::TextureLoader( TextureLoader::TextureLoader(
const std::shared_ptr<AssetManager> assetManager, const std::shared_ptr<AssetManager> assetManager,
const std::string name const std::string name
@ -137,6 +139,10 @@ void TextureLoader::updateSync() {
this->loaded = true; this->loaded = true;
} }
std::string TextureLoader::getAssetType() const {
return TextureLoader::ASSET_TYPE;
}
std::shared_ptr<Texture> TextureLoader::getTexture() { std::shared_ptr<Texture> TextureLoader::getTexture() {
return this->texture; return this->texture;
} }

View File

@ -33,6 +33,8 @@ namespace Dawn {
std::shared_ptr<Texture> texture; std::shared_ptr<Texture> texture;
public: public:
const static std::string ASSET_TYPE;
TextureLoader( TextureLoader(
const std::shared_ptr<AssetManager> assetManager, const std::shared_ptr<AssetManager> assetManager,
const std::string name const std::string name
@ -40,6 +42,7 @@ namespace Dawn {
void updateSync() override; void updateSync() override;
void updateAsync() override; void updateAsync() override;
std::string getAssetType() const override;
/** /**
* Get the texture asset. * Get the texture asset.

View File

@ -8,6 +8,8 @@
using namespace Dawn; using namespace Dawn;
const std::string TrueTypeLoader::ASSET_TYPE = "ttf";
TrueTypeLoader::TrueTypeLoader( TrueTypeLoader::TrueTypeLoader(
const std::shared_ptr<AssetManager> assetManager, const std::shared_ptr<AssetManager> assetManager,
const std::string name const std::string name
@ -65,6 +67,10 @@ void TrueTypeLoader::updateAsync() {
state = TrueTypeLoaderState::ASYNC_DONE; state = TrueTypeLoaderState::ASYNC_DONE;
} }
std::string TrueTypeLoader::getAssetType() const {
return TrueTypeLoader::ASSET_TYPE;
}
std::shared_ptr<TrueTypeTexture> TrueTypeLoader::getTexture( std::shared_ptr<TrueTypeTexture> TrueTypeLoader::getTexture(
const uint32_t fontSize const uint32_t fontSize
) { ) {

View File

@ -27,6 +27,8 @@ namespace Dawn {
uint8_t *buffer = nullptr; uint8_t *buffer = nullptr;
public: public:
const static std::string ASSET_TYPE;
TrueTypeLoader( TrueTypeLoader(
const std::shared_ptr<AssetManager> assetManager, const std::shared_ptr<AssetManager> assetManager,
const std::string name const std::string name
@ -34,6 +36,7 @@ namespace Dawn {
void updateSync() override; void updateSync() override;
void updateAsync() override; void updateAsync() override;
std::string getAssetType() const override;
/** /**
* Returns the texture for the given font size. * Returns the texture for the given font size.

View File

@ -38,15 +38,8 @@ void IGame::init() {
this->initManagers(); this->initManagers();
auto initialScene = this->getInitialScene();
auto sceneLoader = this->assetManager->get<SceneLoader>("test_scene.json"); nextFrameScene = std::make_shared<Scene>(selfAsGame, initialScene);
while(!sceneLoader->loaded) {
this->assetManager->update();
}
nextFrameScene = sceneLoader->getScene();
// auto initialScene = this->getInitialScene();
// nextFrameScene = std::make_shared<Scene>(selfAsGame, initialScene);
} }
void IGame::deinit() { void IGame::deinit() {