AssetManager.cxx 3.6 KB

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