// Copyright (c) 2022 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #pragma once #include "dawn.hpp" #include "asset/AssetLoader.hpp" namespace Dawn { class AssetManager final { private: std::vector> pendingAssetLoaders; std::vector> finishedAssetLoaders; /** * Returns an existing asset loader if it exists. * * @param filename The filename of the asset to get. * @return The asset loader if it exists, otherwise nullptr. */ template std::shared_ptr getExisting(const std::string filename) { auto existing = std::find_if( pendingAssetLoaders.begin(), pendingAssetLoaders.end(), [&](auto &loader) { return loader->name == filename; } ); if(existing == finishedAssetLoaders.end()) { existing = std::find_if( finishedAssetLoaders.begin(), finishedAssetLoaders.end(), [&](auto &loader) { return loader->name == filename; } ); if(existing == finishedAssetLoaders.end()) return nullptr; } return std::static_pointer_cast(*existing); } /** * Removes an existing asset loader if it exists. * * @param filename The filename of the asset to remove. */ void removeExisting(const std::string filename); public: /** * Initializes this asset manager so it can begin accepting assets. */ void init(); /** * Updates the asset manager. */ void update(); /** * Returns whether the asset manager has loaded all of the currently * managed assets. * * @return True if all assets have been loaded. */ 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. * * @param filename The filename of the asset to get. * @return The asset loader for the given asset. */ template std::shared_ptr get(const std::string filename) { auto existing = this->getExisting(filename); if(existing) return existing; std::shared_ptr loader = std::make_shared(filename); pendingAssetLoaders.push_back( std::static_pointer_cast(loader) ); return loader; } /** * Returns the asset loader for the given asset. * * @param filename The filename of the asset to get. * @param fontSize The font size to get the truetype asset of. * @return The asset loader for the given asset. */ // std::shared_ptr get( // const std::string filename, // const uint32_t fontSize // ) { // auto existing = this->getExisting(filename); // if(existing) { // // Check pointer hasn't gone stale, if it has remove it and create new. // auto texture = existing->getTexture(fontSize); // if(texture) return texture; // this->removeExisting(filename); // } // std::shared_ptr loader = std::make_shared( // filename // ); // pendingAssetLoaders.push_back(std::static_pointer_cast(loader)); // return loader->getTexture(fontSize); // } /** * Dispose the asset manager, and all attached assets. */ ~AssetManager(); }; }