AssetManager.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "AssetManager.hxx"
  2. #include "Config.hxx"
  3. #include <algorithm>
  4. #include <array>
  5. #include <cassert>
  6. #include <filesystem>
  7. #include <SDL_image.h>
  8. AssetManager* AssetManager::instance_ = nullptr;
  9. AssetManager::AssetManager(SDLRenderer& renderer)
  10. :renderer_{renderer}
  11. {
  12. namespace fs = std::filesystem;
  13. assert(instance_==nullptr);
  14. auto const current_path = fs::current_path();
  15. std::array const asset_paths{
  16. current_path/"assets",
  17. current_path.parent_path()/"assets",
  18. current_path.parent_path()/"Resources"/"assets", // MacOS bundle
  19. INSTALLED_ASSETS_PATH,
  20. };
  21. auto const it = std::ranges::find_if(asset_paths, [](fs::path const& p){
  22. return fs::exists(p);
  23. });
  24. if (it == std::end(asset_paths)) {
  25. throw std::runtime_error("Assets directory not found.");
  26. }
  27. auto const asset_directory = *it;
  28. SDL_Log("Loading assets from %s.", asset_directory.c_str());
  29. total_assets_ = std::distance(fs::directory_iterator(asset_directory),
  30. fs::directory_iterator());
  31. loading_thread_ = std::thread{&AssetManager::load_assets, this, asset_directory};
  32. instance_ = this;
  33. }
  34. AssetManager::~AssetManager()
  35. {
  36. assert(instance_!=nullptr);
  37. if (loading_thread_.joinable()) {
  38. loading_thread_.join();
  39. }
  40. for (auto const& kv: surface_assets_) {
  41. SDL_FreeSurface(kv.second);
  42. }
  43. for (auto const& kv: texture_assets_) {
  44. SDL_DestroyTexture(kv.second);
  45. SDL_Log("Unloaded texture %s successfully.", kv.first.c_str());
  46. }
  47. for (auto const& kv: font_assets_) {
  48. TTF_CloseFont(kv.second);
  49. SDL_Log("Unloaded font %s successfully.", kv.first.c_str());
  50. }
  51. instance_ = nullptr;
  52. }
  53. void AssetManager::load_assets(std::filesystem::path const& asset_directory)
  54. {
  55. for (auto const& entry: std::filesystem::directory_iterator(asset_directory)) {
  56. auto const path = entry.path().string();
  57. auto const ext = entry.path().extension();
  58. auto const filename = entry.path().filename().string();
  59. if (ext==".png" || ext==".jpg") {
  60. auto const surface = IMG_Load(path.c_str());
  61. if (surface==nullptr) {
  62. throw SDLError{"Failed to load texture "+path+"."};
  63. }
  64. surface_assets_[filename] = surface;
  65. SDL_Log("Loaded texture %s successfully.", filename.c_str());
  66. }
  67. else if (ext==".ttf") {
  68. auto const font = TTF_OpenFont(path.c_str(), 42);
  69. if (font==nullptr) {
  70. throw SDLError{"Failed to load font "+path+"."};
  71. }
  72. font_assets_[filename] = font;
  73. SDL_Log("Loaded font %s successfully.", filename.c_str());
  74. }
  75. ++assets_loaded_;
  76. }
  77. }
  78. float AssetManager::get_progress() const
  79. {
  80. return static_cast<float>(assets_loaded_)/static_cast<float>(total_assets_);
  81. }
  82. SDL_Texture* AssetManager::get_texture_asset(std::string const& filepath)
  83. {
  84. auto const it = texture_assets_.find(filepath);
  85. if (it==std::end(texture_assets_)) {
  86. auto const surf_it = surface_assets_.find(filepath);
  87. if (surf_it==std::end(surface_assets_))
  88. return nullptr;
  89. auto const texture = SDL_CreateTextureFromSurface(renderer_, surf_it->second);
  90. texture_assets_[filepath] = texture;
  91. SDL_FreeSurface(surf_it->second);
  92. surface_assets_.erase(surf_it);
  93. return texture;
  94. }
  95. return it->second;
  96. }
  97. TTF_Font* AssetManager::get_font_asset(std::string const& filepath)
  98. {
  99. return font_assets_[filepath];
  100. }
  101. AssetManager& AssetManager::instance() noexcept
  102. {
  103. assert(instance_!=nullptr);
  104. return *instance_;
  105. }