SpriteSheet2D.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "Deserializer.h"
  25. #include "FileSystem.h"
  26. #include "Log.h"
  27. #include "ResourceCache.h"
  28. #include "Serializer.h"
  29. #include "Sprite2D.h"
  30. #include "SpriteSheet2D.h"
  31. #include "Texture2D.h"
  32. #include "XMLFile.h"
  33. #include "DebugNew.h"
  34. namespace Urho3D
  35. {
  36. SpriteSheet2D::SpriteSheet2D(Context* context) :
  37. Resource(context)
  38. {
  39. }
  40. SpriteSheet2D::~SpriteSheet2D()
  41. {
  42. }
  43. void SpriteSheet2D::RegisterObject(Context* context)
  44. {
  45. context->RegisterFactory<SpriteSheet2D>();
  46. }
  47. bool SpriteSheet2D::Load(Deserializer& source)
  48. {
  49. spriteMapping_.Clear();
  50. SharedPtr<XMLFile> xmlFile(new XMLFile(context_));
  51. if(!xmlFile->Load(source))
  52. {
  53. LOGERROR("Could not load sprite sheet");
  54. return false;
  55. }
  56. SetMemoryUse(source.GetSize());
  57. XMLElement rootElem = xmlFile->GetRoot("TextureAtlas");
  58. if (!rootElem)
  59. {
  60. LOGERROR("Invalid sprite sheet");
  61. return false;
  62. }
  63. String textureFileName = rootElem.GetAttribute("imagePath");
  64. ResourceCache* cache = GetSubsystem<ResourceCache>();
  65. texture_ = cache->GetResource<Texture2D>(GetParentPath(GetName()) + textureFileName);
  66. if (!texture_)
  67. {
  68. LOGERROR("Could not load texture " + GetParentPath(GetName()) + textureFileName);
  69. return false;
  70. }
  71. XMLElement subTextureElem = rootElem.GetChild("SubTexture");
  72. while (subTextureElem)
  73. {
  74. String name = subTextureElem.GetAttribute("name");
  75. int x = subTextureElem.GetInt("x");
  76. int y = subTextureElem.GetInt("y");
  77. int width = subTextureElem.GetInt("width");
  78. int height = subTextureElem.GetInt("height");
  79. IntRect rectangle(x, y, x + width, y + height);
  80. Vector2 hotSpot(0.5f, 0.5f);
  81. IntVector2 offset(0, 0);
  82. if (subTextureElem.HasAttribute("frameWidth") && subTextureElem.HasAttribute("frameHeight"))
  83. {
  84. offset.x_ = subTextureElem.GetInt("frameX");
  85. offset.y_ = subTextureElem.GetInt("frameY");
  86. int frameWidth = subTextureElem.GetInt("frameWidth");
  87. int frameHeight = subTextureElem.GetInt("frameHeight");
  88. hotSpot.x_ = ((float)offset.x_ + frameWidth / 2) / width;
  89. hotSpot.y_ = 1.0f - ((float)offset.y_ + frameHeight / 2) / height;
  90. }
  91. DefineSprite(name, rectangle, hotSpot, offset);
  92. subTextureElem = subTextureElem.GetNext("SubTexture");
  93. }
  94. return true;
  95. }
  96. Sprite2D* SpriteSheet2D::GetSprite(const String& name) const
  97. {
  98. HashMap<String, SharedPtr<Sprite2D> >::ConstIterator i = spriteMapping_.Find(name);
  99. if (i == spriteMapping_.End())
  100. return 0;
  101. return i->second_;
  102. }
  103. void SpriteSheet2D::DefineSprite(const String& name, const IntRect& rectangle, const Vector2& hotSpot, const IntVector2& offset)
  104. {
  105. if (!texture_)
  106. return;
  107. if (GetSprite(name))
  108. return;
  109. SharedPtr<Sprite2D> sprite(new Sprite2D(context_));
  110. sprite->SetName(name);
  111. sprite->SetTexture(texture_);
  112. sprite->SetRectangle(rectangle);
  113. sprite->SetHotSpot(hotSpot);
  114. sprite->SetOffset(offset);
  115. sprite->SetSpriteSheet(this);
  116. spriteMapping_[name] = sprite;
  117. }
  118. }