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;
const std::string AssetLoader::ASSET_TYPE = "unknown";
AssetLoader::AssetLoader(
const std::shared_ptr<AssetManager> assetManager,
const std::string name

View File

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

View File

@ -27,38 +27,33 @@ void AssetManager::update() {
}
}
void AssetManager::removeExisting(const std::string &filename) {
auto existing = std::find_if(
pendingAssetLoaders.begin(), pendingAssetLoaders.end(),
[&](auto &loader) {
return loader->name == filename;
}
);
if(existing != pendingAssetLoaders.end()) {
pendingAssetLoaders.erase(existing);
}
// Disabled because it does not respect scopes
// void AssetManager::removeExisting(const std::string &filename) {
// auto existing = std::find_if(
// pendingAssetLoaders.begin(), pendingAssetLoaders.end(),
// [&](auto &loader) {
// return loader->name == filename;
// }
// );
// if(existing != pendingAssetLoaders.end()) {
// pendingAssetLoaders.erase(existing);
// }
existing = std::find_if(
finishedAssetLoaders.begin(), finishedAssetLoaders.end(),
[&](auto &loader) {
return loader->name == filename;
}
);
if(existing != finishedAssetLoaders.end()) {
finishedAssetLoaders.erase(existing);
}
}
// existing = std::find_if(
// finishedAssetLoaders.begin(), finishedAssetLoaders.end(),
// [&](auto &loader) {
// return loader->name == filename;
// }
// );
// if(existing != finishedAssetLoaders.end()) {
// finishedAssetLoaders.erase(existing);
// }
// }
bool_t AssetManager::isEverythingLoaded() {
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() {
}

View File

@ -25,7 +25,10 @@ namespace Dawn {
pendingAssetLoaders.begin(),
pendingAssetLoaders.end(),
[&](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(
finishedAssetLoaders.begin(), finishedAssetLoaders.end(),
[&](auto &loader) {
return loader->name == filename;
return (
loader->name == filename &&
loader->getAssetType() == T::ASSET_TYPE
);
}
);
@ -69,14 +75,6 @@ namespace Dawn {
*/
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.
*

View File

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

View File

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

View File

@ -7,6 +7,8 @@
using namespace Dawn;
const std::string SceneLoader::ASSET_TYPE = "scene";
SceneLoader::SceneLoader(
const std::shared_ptr<AssetManager> assetManager,
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() {
return scene;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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