AssetManager.cxx 3.2 KB

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