sprite_manager.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. // 2023 Vankata453
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #include "sprite/sprite_manager.hpp"
  18. #include <optional>
  19. #include <sstream>
  20. #include "sprite/sprite.hpp"
  21. #include "util/file_system.hpp"
  22. #include "util/log.hpp"
  23. #include "util/reader_document.hpp"
  24. #include "util/reader_mapping.hpp"
  25. #include "util/string_util.hpp"
  26. std::unique_ptr<SpriteData> SpriteManager::s_dummy_sprite_data = nullptr;
  27. SpriteManager::SpriteManager() :
  28. m_sprites(),
  29. m_load_successful(false)
  30. {
  31. if (!s_dummy_sprite_data)
  32. s_dummy_sprite_data.reset(new SpriteData());
  33. }
  34. SpritePtr
  35. SpriteManager::create(const std::string& name)
  36. {
  37. Sprites::iterator i = m_sprites.find(name);
  38. SpriteData* data;
  39. if (i == m_sprites.end())
  40. {
  41. // Try loading the sprite file.
  42. try
  43. {
  44. data = load(name);
  45. }
  46. catch (const std::exception& err)
  47. {
  48. log_warning << "Error loading sprite '" << name << "', using dummy texture: " << err.what() << std::endl;
  49. m_load_successful = false;
  50. return SpritePtr(new Sprite(*s_dummy_sprite_data)); // Return a dummy sprite.
  51. }
  52. }
  53. else
  54. {
  55. data = i->second.get();
  56. }
  57. m_load_successful = true;
  58. return SpritePtr(new Sprite(*data));
  59. }
  60. SpriteData*
  61. SpriteManager::load(const std::string& filename)
  62. {
  63. std::unique_ptr<SpriteData> sprite_data;
  64. if (StringUtil::has_suffix(filename, ".sprite"))
  65. {
  66. std::optional<ReaderDocument> doc;
  67. try
  68. {
  69. doc = ReaderDocument::from_file(filename);
  70. }
  71. catch (const std::exception& err)
  72. {
  73. std::ostringstream msg;
  74. msg << "Parse error when trying to load sprite '" << filename
  75. << "': " << err.what();
  76. throw std::runtime_error(msg.str());
  77. }
  78. auto root = doc->get_root();
  79. if (root.get_name() != "supertux-sprite")
  80. {
  81. std::ostringstream msg;
  82. msg << "'" << filename << "' is not a supertux-sprite file";
  83. throw std::runtime_error(msg.str());
  84. }
  85. else
  86. {
  87. sprite_data = std::make_unique<SpriteData>(root.get_mapping());
  88. }
  89. }
  90. else
  91. {
  92. sprite_data = std::make_unique<SpriteData>(filename);
  93. }
  94. m_sprites[filename] = std::move(sprite_data);
  95. return m_sprites[filename].get();
  96. }
  97. /* EOF */