SpriteSheet2D.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "PListFile.h"
  28. #include "ResourceCache.h"
  29. #include "Serializer.h"
  30. #include "Sprite2D.h"
  31. #include "SpriteSheet2D.h"
  32. #include "Texture2D.h"
  33. #include "XMLFile.h"
  34. #include "DebugNew.h"
  35. namespace Urho3D
  36. {
  37. SpriteSheet2D::SpriteSheet2D(Context* context) :
  38. Resource(context)
  39. {
  40. }
  41. SpriteSheet2D::~SpriteSheet2D()
  42. {
  43. }
  44. void SpriteSheet2D::RegisterObject(Context* context)
  45. {
  46. context->RegisterFactory<SpriteSheet2D>();
  47. }
  48. bool SpriteSheet2D::BeginLoad(Deserializer& source)
  49. {
  50. if (GetName().Empty())
  51. SetName(source.GetName());
  52. loadTextureName_.Clear();
  53. spriteMapping_.Clear();
  54. String extension = GetExtension(source.GetName());
  55. if (extension == ".plist")
  56. return BeginLoadFromPListFile(source);
  57. if (extension == ".xml")
  58. return BeginLoadFromXMLFile(source);
  59. LOGERROR("Unsupported file type");
  60. return false;
  61. }
  62. bool SpriteSheet2D::EndLoad()
  63. {
  64. if (loadPListFile_)
  65. return EndLoadFromPListFile();
  66. if (loadXMLFile_)
  67. return EndLoadFromXMLFile();
  68. return false;
  69. }
  70. Sprite2D* SpriteSheet2D::GetSprite(const String& name) const
  71. {
  72. HashMap<String, SharedPtr<Sprite2D> >::ConstIterator i = spriteMapping_.Find(name);
  73. if (i == spriteMapping_.End())
  74. return 0;
  75. return i->second_;
  76. }
  77. void SpriteSheet2D::DefineSprite(const String& name, const IntRect& rectangle, const Vector2& hotSpot, const IntVector2& offset)
  78. {
  79. if (!texture_)
  80. return;
  81. if (GetSprite(name))
  82. return;
  83. SharedPtr<Sprite2D> sprite(new Sprite2D(context_));
  84. sprite->SetName(name);
  85. sprite->SetTexture(texture_);
  86. sprite->SetRectangle(rectangle);
  87. sprite->SetHotSpot(hotSpot);
  88. sprite->SetOffset(offset);
  89. sprite->SetSpriteSheet(this);
  90. spriteMapping_[name] = sprite;
  91. }
  92. bool SpriteSheet2D::BeginLoadFromPListFile(Deserializer& source)
  93. {
  94. loadPListFile_ = new PListFile(context_);
  95. if (!loadPListFile_->Load(source))
  96. {
  97. LOGERROR("Could not load sprite sheet");
  98. loadPListFile_.Reset();
  99. return false;
  100. }
  101. SetMemoryUse(source.GetSize());
  102. const PListValueMap& root = loadPListFile_->GetRoot();
  103. const PListValueMap& metadata = root["metadata"].GetValueMap();
  104. const String& textureFileName = metadata["realTextureFileName"].GetString();
  105. // If we're async loading, request the texture now. Finish during EndLoad().
  106. loadTextureName_ = GetParentPath(GetName()) + textureFileName;
  107. if (GetAsyncLoadState() == ASYNC_LOADING)
  108. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(loadTextureName_, true, this);
  109. return true;
  110. }
  111. bool SpriteSheet2D::EndLoadFromPListFile()
  112. {
  113. ResourceCache* cache = GetSubsystem<ResourceCache>();
  114. texture_ = cache->GetResource<Texture2D>(loadTextureName_);
  115. if (!texture_)
  116. {
  117. LOGERROR("Could not load texture " + loadTextureName_);
  118. loadXMLFile_.Reset();
  119. loadTextureName_.Clear();
  120. return false;
  121. }
  122. const PListValueMap& root = loadPListFile_->GetRoot();
  123. const PListValueMap& frames = root["frames"].GetValueMap();
  124. for (PListValueMap::ConstIterator i = frames.Begin(); i != frames.End(); ++i)
  125. {
  126. String name = i->first_.Split('.')[0];
  127. const PListValueMap& frameInfo = i->second_.GetValueMap();
  128. if (frameInfo["rotated"].GetBool())
  129. {
  130. LOGWARNING("Rotated sprite is not support now");
  131. continue;
  132. }
  133. IntRect rectangle = frameInfo["frame"].GetIntRect();
  134. Vector2 hotSpot(0.5f, 0.5f);
  135. IntVector2 offset(0, 0);
  136. IntRect sourceColorRect = frameInfo["sourceColorRect"].GetIntRect();
  137. if (sourceColorRect.left_ != 0 && sourceColorRect.top_ != 0)
  138. {
  139. offset.x_ = -sourceColorRect.left_;
  140. offset.y_ = -sourceColorRect.top_;
  141. IntVector2 sourceSize = frameInfo["sourceSize"].GetIntVector2();
  142. hotSpot.x_ = ((float)offset.x_ + sourceSize.x_ / 2) / rectangle.Width();
  143. hotSpot.y_ = 1.0f - ((float)offset.y_ + sourceSize.y_/ 2) / rectangle.Height();
  144. }
  145. DefineSprite(name, rectangle, hotSpot, offset);
  146. }
  147. loadXMLFile_.Reset();
  148. loadTextureName_.Clear();
  149. return true;
  150. }
  151. bool SpriteSheet2D::BeginLoadFromXMLFile(Deserializer& source)
  152. {
  153. loadXMLFile_ = new XMLFile(context_);
  154. if (!loadXMLFile_->Load(source))
  155. {
  156. LOGERROR("Could not load sprite sheet");
  157. loadXMLFile_.Reset();
  158. return false;
  159. }
  160. SetMemoryUse(source.GetSize());
  161. XMLElement rootElem = loadXMLFile_->GetRoot("TextureAtlas");
  162. if (!rootElem)
  163. {
  164. LOGERROR("Invalid sprite sheet");
  165. loadXMLFile_.Reset();
  166. return false;
  167. }
  168. // If we're async loading, request the texture now. Finish during EndLoad().
  169. loadTextureName_ = GetParentPath(GetName()) + rootElem.GetAttribute("imagePath");
  170. if (GetAsyncLoadState() == ASYNC_LOADING)
  171. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(loadTextureName_, true, this);
  172. return true;
  173. }
  174. bool SpriteSheet2D::EndLoadFromXMLFile()
  175. {
  176. ResourceCache* cache = GetSubsystem<ResourceCache>();
  177. texture_ = cache->GetResource<Texture2D>(loadTextureName_);
  178. if (!texture_)
  179. {
  180. LOGERROR("Could not load texture " + loadTextureName_);
  181. loadXMLFile_.Reset();
  182. loadTextureName_.Clear();
  183. return false;
  184. }
  185. XMLElement rootElem = loadXMLFile_->GetRoot("TextureAtlas");
  186. XMLElement subTextureElem = rootElem.GetChild("SubTexture");
  187. while (subTextureElem)
  188. {
  189. String name = subTextureElem.GetAttribute("name");
  190. int x = subTextureElem.GetInt("x");
  191. int y = subTextureElem.GetInt("y");
  192. int width = subTextureElem.GetInt("width");
  193. int height = subTextureElem.GetInt("height");
  194. IntRect rectangle(x, y, x + width, y + height);
  195. Vector2 hotSpot(0.5f, 0.5f);
  196. IntVector2 offset(0, 0);
  197. if (subTextureElem.HasAttribute("frameWidth") && subTextureElem.HasAttribute("frameHeight"))
  198. {
  199. offset.x_ = subTextureElem.GetInt("frameX");
  200. offset.y_ = subTextureElem.GetInt("frameY");
  201. int frameWidth = subTextureElem.GetInt("frameWidth");
  202. int frameHeight = subTextureElem.GetInt("frameHeight");
  203. hotSpot.x_ = ((float)offset.x_ + frameWidth / 2) / width;
  204. hotSpot.y_ = 1.0f - ((float)offset.y_ + frameHeight / 2) / height;
  205. }
  206. DefineSprite(name, rectangle, hotSpot, offset);
  207. subTextureElem = subTextureElem.GetNext("SubTexture");
  208. }
  209. loadXMLFile_.Reset();
  210. loadTextureName_.Clear();
  211. return true;
  212. }
  213. }