// Copyright (c) 2022 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #pragma once #include "Asset.hpp" namespace Dawn { class AssetManager { private: /** List of pointers to assets, mapped by their asset key. */ std::map> assets; std::vector> assetsToLoad; std::vector> assetsToUnload; public: /** * Initializes this asset manager so it can begin accepting assets. */ void init(); /** * Updates the asset manager. */ void update(); /** * Queue a loading of a list of assets. Does not actually begin loading. * * @param assets Assets to load. */ void queueLoad(const std::vector> assets); /** * Queue a loading of a single asset. Does not actually begin loading. * * @param asset Asset to load. */ void queueLoad(const std::shared_ptr &asset); /** * Takes a list of lists to queue to unload. Does not immediately unload. * * @param assets Assets to unload. */ void queueUnload(const std::vector> &assets); /** * Takes a single asset to queue to unload. Does not immediately unload. * * @param assets Assets to unload. */ void queueUnload(const std::shared_ptr &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( const std::vector> &newAssets, const std::vector> &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. * * @param name Name of the asset to load. * @return The asset element to be loaded. */ template std::shared_ptr get(std::string name) { assertTrue( name.size() > 0, "AssetManager::get: name must be greater than 0" ); auto existing = this->assets.find(name); if(existing != this->assets.end()) { return dynamic_pointer_cast(existing->second); } auto asset = std::make_shared(name); this->assets[name] = asset; return asset; } /** * Both gets an asset, and puts it into the load queue. * * @param name Name of the asset to load. * @return The asset element to be loaded. */ template std::shared_ptr getAndLoad(std::string name) { auto asset = this->get(name); this->queueLoad(asset); return asset; } template void unload(std::shared_ptr asset) { assertUnreachable("AssetManager::unload: NOT IMPLEMENTED"); } void unload(std::string name) { assertUnreachable("AssetManager::unload: NOT IMPLEMENTED"); } /** * Dispose the asset manager, and all attached assets. */ ~AssetManager(); }; }