// Copyright (c) 2022 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #pragma once #include "Asset.hpp" #include "util/array.hpp" #include "assets/LanguageAsset.hpp" #include "assets/TextureAsset.hpp" #include "assets/TilesetAsset.hpp" #include "assets/TrueTypeAsset.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(std::vector assets); void queueLoad(Asset *assets); /** * Takes a list of lists to queue to unload. Does not immediately unload. * * @param assets Assets to unload. */ void queueUnload(std::vector assets); void queueUnload(Asset* 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( std::vector newAssets, 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 T * get(std::string name) { assertTrue(name.size() > 0); auto existing = this->assets.find(name); if(existing != this->assets.end()) { return (T*)existing->second; } auto asset = new T(this, 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 T * getAndLoad(std::string name) { auto asset = this->get(name); this->queueLoad(asset); return asset; } template void unload(T *asset) { assertUnreachable(); } void unload(std::string name) { assertUnreachable(); } /** * Dispose the asset manager, and all attached assets. */ ~AssetManager(); }; }