SpriteSheet2D.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "Log.h"
  26. #include "ResourceCache.h"
  27. #include "Serializer.h"
  28. #include "Sprite2D.h"
  29. #include "SpriteSheet2D.h"
  30. #include "Texture2D.h"
  31. #include "XMLFile.h"
  32. #include "DebugNew.h"
  33. namespace Urho3D
  34. {
  35. SpriteSheet2D::SpriteSheet2D(Context* context) : Resource(context)
  36. {
  37. }
  38. SpriteSheet2D::~SpriteSheet2D()
  39. {
  40. }
  41. void SpriteSheet2D::RegisterObject(Context* context)
  42. {
  43. context->RegisterFactory<SpriteSheet2D>();
  44. }
  45. bool SpriteSheet2D::Load(Deserializer& source)
  46. {
  47. spriteMapping_.Clear();
  48. SharedPtr<XMLFile> xmlFile(new XMLFile(context_));
  49. if(!xmlFile->Load(source))
  50. {
  51. LOGERROR("Could not load sprite sheet");
  52. return false;
  53. }
  54. SetMemoryUse(source.GetSize());
  55. XMLElement rootElem = xmlFile->GetRoot();
  56. if (!rootElem)
  57. {
  58. LOGERROR("Invalid sprite sheet");
  59. return false;
  60. }
  61. if (rootElem.GetName() == "SpriteSheet")
  62. {
  63. ResourceCache* cache = GetSubsystem<ResourceCache>();
  64. texture_ = cache->GetResource<Texture2D>(rootElem.GetAttribute("texture"));
  65. if (!texture_)
  66. {
  67. LOGERROR("Cound not load texture");
  68. return false;
  69. }
  70. XMLElement spriteElem = rootElem.GetChild("Sprite");
  71. while (spriteElem)
  72. {
  73. String name = spriteElem.GetAttribute("name");
  74. IntRect rectangle = spriteElem.GetIntRect("rectangle");
  75. Vector2 hotSpot(0.5f, 0.5f);
  76. if (spriteElem.HasAttribute("hotSpot"))
  77. hotSpot = spriteElem.GetVector2("hotSpot");
  78. DefineSprite(name, rectangle, hotSpot);
  79. spriteElem = spriteElem.GetNext("Sprite");
  80. }
  81. }
  82. // Sparrow Starling texture atlas
  83. else if (rootElem.GetName() == "TextureAtlas")
  84. {
  85. ResourceCache* cache = GetSubsystem<ResourceCache>();
  86. texture_ = cache->GetResource<Texture2D>(rootElem.GetAttribute("imagePath"));
  87. if (!texture_)
  88. {
  89. LOGERROR("Cound not load texture");
  90. return false;
  91. }
  92. XMLElement subTextureElem = rootElem.GetChild("SubTexture");
  93. while (subTextureElem)
  94. {
  95. String name = subTextureElem.GetAttribute("name");
  96. int x = subTextureElem.GetInt("x");
  97. int y = subTextureElem.GetInt("y");
  98. int width = subTextureElem.GetInt("width");
  99. int height = subTextureElem.GetInt("height");
  100. IntRect rectangle(x, y, x + width, y + height);
  101. Vector2 hotSpot(0.5f, 0.5f);
  102. if (subTextureElem.HasAttribute("frameWidth") && subTextureElem.HasAttribute("frameHeight"))
  103. {
  104. int frameX = subTextureElem.GetInt("frameX");
  105. int frameY = subTextureElem.GetInt("frameY");
  106. int frameWidth = subTextureElem.GetInt("frameWidth");
  107. int frameHeight = subTextureElem.GetInt("frameHeight");
  108. hotSpot.x_ = ((float)frameX + frameWidth / 2) / width;
  109. hotSpot.y_ = ((float)frameY + frameHeight / 2) / height;
  110. }
  111. DefineSprite(name, rectangle, hotSpot);
  112. subTextureElem = subTextureElem.GetNext("SubTexture");
  113. }
  114. }
  115. else
  116. {
  117. LOGERROR("Invalid sprite sheet file");
  118. return false;
  119. }
  120. return true;
  121. }
  122. bool SpriteSheet2D::Save(Serializer& dest) const
  123. {
  124. if (!texture_)
  125. return false;
  126. SharedPtr<XMLFile> xmlFile(new XMLFile(context_));
  127. XMLElement rootElem = xmlFile->CreateRoot("SpriteSheet");
  128. rootElem.SetAttribute("texture", texture_->GetName());
  129. for (HashMap<String, SharedPtr<Sprite2D> >::ConstIterator i = spriteMapping_.Begin(); i != spriteMapping_.End(); ++i)
  130. {
  131. XMLElement spriteElem = rootElem.CreateChild("Sprite");
  132. spriteElem.SetAttribute("name", i->first_);
  133. Sprite2D* sprite = i->second_;
  134. spriteElem.SetIntRect("rectangle", sprite->GetRectangle());
  135. spriteElem.SetVector2("hotSpot", sprite->GetHotSpot());
  136. }
  137. return xmlFile->Save(dest);
  138. }
  139. Sprite2D* SpriteSheet2D::GetSprite(const String& name) const
  140. {
  141. HashMap<String, SharedPtr<Sprite2D> >::ConstIterator i = spriteMapping_.Find(name);
  142. if (i == spriteMapping_.End())
  143. return 0;
  144. return i->second_;
  145. }
  146. void SpriteSheet2D::DefineSprite(const String& name, const IntRect& rectangle, const Vector2& hotSpot)
  147. {
  148. if (!texture_)
  149. return;
  150. if (GetSprite(name))
  151. return;
  152. SharedPtr<Sprite2D> sprite(new Sprite2D(context_));
  153. sprite->SetName(name);
  154. sprite->SetTexture(texture_);
  155. sprite->SetRectangle(rectangle);
  156. sprite->SetHotSpot(hotSpot);
  157. sprite->SetSpriteSheet(this);
  158. spriteMapping_[name] = sprite;
  159. }
  160. void SpriteSheet2D::UpdateSprite(const String& name, const IntRect& rectangle, const Vector2& hotSpot)
  161. {
  162. if (!texture_)
  163. return;
  164. Sprite2D* sprite = GetSprite(name);
  165. if (sprite)
  166. {
  167. sprite->SetRectangle(rectangle);
  168. sprite->SetHotSpot(hotSpot);
  169. }
  170. }
  171. }