AssetManager.hpp 936 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #ifndef FREESHOP_ASSETMANAGER_HPP
  2. #define FREESHOP_ASSETMANAGER_HPP
  3. #include <cpp3ds/Audio/Sound.hpp>
  4. #include <cpp3ds/Audio/SoundBuffer.hpp>
  5. #include <cpp3ds/System/Err.hpp>
  6. #include <memory>
  7. #include <map>
  8. #include <assert.h>
  9. namespace FreeShop {
  10. template <class T>
  11. class AssetManager {
  12. public:
  13. static T& get(const std::string& filename)
  14. {
  15. static AssetManager<T> manager; // Yep, singleton
  16. auto item = manager.m_assets.find(filename);
  17. if (item == manager.m_assets.end())
  18. {
  19. std::unique_ptr<T> asset(new T);
  20. if (!asset->loadFromFile(filename))
  21. cpp3ds::err() << "Failed to load asset: " << filename << std::endl;
  22. manager.m_assets.insert(std::make_pair(filename, std::move(asset)));
  23. return get(filename);
  24. }
  25. return *item->second;
  26. }
  27. private:
  28. AssetManager<T>() {} // Empty constructor
  29. std::map<std::string, std::unique_ptr<T>> m_assets;
  30. };
  31. } // namespace FreeShop
  32. #endif // FREESHOP_ASSETMANAGER_HPP