sprite_manager.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "sprite/sprite_manager.hpp"
  17. #include "sprite/sprite.hpp"
  18. #include "util/file_system.hpp"
  19. #include "util/reader_document.hpp"
  20. #include "util/reader_mapping.hpp"
  21. #include <sstream>
  22. #include <stdexcept>
  23. SpriteManager::SpriteManager() :
  24. sprites()
  25. {
  26. }
  27. SpriteManager::~SpriteManager()
  28. {
  29. }
  30. SpritePtr
  31. SpriteManager::create(const std::string& name)
  32. {
  33. Sprites::iterator i = sprites.find(name);
  34. SpriteData* data;
  35. if(i == sprites.end()) {
  36. // try loading the spritefile
  37. data = load(name);
  38. if(data == NULL) {
  39. std::stringstream msg;
  40. msg << "Sprite '" << name << "' not found.";
  41. throw std::runtime_error(msg.str());
  42. }
  43. } else {
  44. data = i->second.get();
  45. }
  46. return SpritePtr(new Sprite(*data));
  47. }
  48. SpriteData*
  49. SpriteManager::load(const std::string& filename)
  50. {
  51. ReaderDocument doc;
  52. try {
  53. if(filename.size() >= 7 && filename.compare(filename.size() - 7, 7, ".sprite") == 0) {
  54. // Sprite file
  55. doc = ReaderDocument::parse(filename);
  56. } else {
  57. // Load image file directly
  58. std::stringstream lisptext;
  59. lisptext << "(supertux-sprite (action "
  60. << "(name \"default\") "
  61. << "(images \"" << FileSystem::basename(filename) << "\")))";
  62. doc = ReaderDocument::parse(lisptext);
  63. }
  64. } catch(const std::exception& e) {
  65. std::ostringstream msg;
  66. msg << "Parse error when trying to load sprite '" << filename
  67. << "': " << e.what() << "\n";
  68. throw std::runtime_error(msg.str());
  69. }
  70. auto root = doc.get_root();
  71. if(root.get_name() != "supertux-sprite") {
  72. std::ostringstream msg;
  73. msg << "'" << filename << "' is not a supertux-sprite file";
  74. throw std::runtime_error(msg.str());
  75. } else {
  76. std::unique_ptr<SpriteData> data (
  77. new SpriteData(root.get_mapping(), FileSystem::dirname(filename)) );
  78. sprites[filename] = std::move(data);
  79. return sprites[filename].get();
  80. }
  81. }
  82. /* EOF */