Sprite2D.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // Copyright (c) 2008-2015 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 "../Core/Context.h"
  24. #include "../Graphics/Texture2D.h"
  25. #include "../IO/Deserializer.h"
  26. #include "../Resource/ResourceCache.h"
  27. #include "../Atomic2D/Drawable2D.h"
  28. #include "../Atomic2D/Sprite2D.h"
  29. #include "../Atomic2D/SpriteSheet2D.h"
  30. #include "../DebugNew.h"
  31. namespace Atomic
  32. {
  33. Sprite2D::Sprite2D(Context* context) :
  34. Resource(context),
  35. hotSpot_(0.5f, 0.5f),
  36. offset_(0, 0)
  37. {
  38. }
  39. Sprite2D::~Sprite2D()
  40. {
  41. }
  42. void Sprite2D::RegisterObject(Context* context)
  43. {
  44. context->RegisterFactory<Sprite2D>();
  45. }
  46. bool Sprite2D::BeginLoad(Deserializer& source)
  47. {
  48. if (GetName().Empty())
  49. SetName(source.GetName());
  50. // Reload
  51. if (texture_)
  52. loadTexture_ = texture_;
  53. else
  54. {
  55. loadTexture_ = new Texture2D(context_);
  56. loadTexture_->SetName(GetName());
  57. }
  58. // In case we're async loading, only call BeginLoad() for the texture (load image but do not upload to GPU)
  59. if (!loadTexture_->BeginLoad(source))
  60. {
  61. // Reload failed
  62. if (loadTexture_ == texture_)
  63. texture_.Reset();
  64. loadTexture_.Reset();
  65. return false;
  66. }
  67. return true;
  68. }
  69. bool Sprite2D::EndLoad()
  70. {
  71. // Finish loading of the texture in the main thread
  72. bool success = false;
  73. if (loadTexture_ && loadTexture_->EndLoad())
  74. {
  75. success = true;
  76. SetTexture(loadTexture_);
  77. if (texture_)
  78. SetRectangle(IntRect(0, 0, texture_->GetWidth(), texture_->GetHeight()));
  79. }
  80. else
  81. {
  82. // Reload failed
  83. if (loadTexture_ == texture_)
  84. texture_.Reset();
  85. }
  86. loadTexture_.Reset();
  87. return success;
  88. }
  89. void Sprite2D::SetTexture(Texture2D* texture)
  90. {
  91. texture_ = texture;
  92. }
  93. void Sprite2D::SetRectangle(const IntRect& rectangle)
  94. {
  95. rectangle_ = rectangle;
  96. }
  97. void Sprite2D::SetHotSpot(const Vector2& hotSpot)
  98. {
  99. hotSpot_ = hotSpot;
  100. }
  101. void Sprite2D::SetOffset(const IntVector2& offset)
  102. {
  103. offset_ = offset;
  104. }
  105. void Sprite2D::SetSpriteSheet(SpriteSheet2D* spriteSheet)
  106. {
  107. spriteSheet_ = spriteSheet;
  108. }
  109. bool Sprite2D::GetDrawRectangle(Rect& rect, bool flipX, bool flipY) const
  110. {
  111. return GetDrawRectangle(rect, hotSpot_, flipX, flipY);
  112. }
  113. bool Sprite2D::GetDrawRectangle(Rect& rect, const Vector2& hotSpot, bool flipX, bool flipY) const
  114. {
  115. if (rectangle_.Width() == 0 || rectangle_.Height() == 0)
  116. return false;
  117. float width = (float)rectangle_.Width() * PIXEL_SIZE;
  118. float height = (float)rectangle_.Height() * PIXEL_SIZE;
  119. float hotSpotX = flipX ? (1.0f - hotSpot.x_) : hotSpot.x_;
  120. float hotSpotY = flipY ? (1.0f - hotSpot.y_) : hotSpot.y_;
  121. #ifdef ATOMIC_OPENGL
  122. rect.min_.x_ = -width * hotSpotX;
  123. rect.max_.x_ = width * (1.0f - hotSpotX);
  124. rect.min_.y_ = -height * hotSpotY;
  125. rect.max_.y_ = height * (1.0f - hotSpotY);
  126. #else
  127. const float halfPixelOffset = 0.5f * PIXEL_SIZE;
  128. rect.min_.x_ = -width * hotSpotX + halfPixelOffset;
  129. rect.max_.x_ = width * (1.0f - hotSpotX) + halfPixelOffset;
  130. rect.min_.y_ = -height * hotSpotY + halfPixelOffset;
  131. rect.max_.y_ = height * (1.0f - hotSpotY) + halfPixelOffset;
  132. #endif
  133. return true;
  134. }
  135. bool Sprite2D::GetTextureRectangle(Rect& rect, bool flipX, bool flipY) const
  136. {
  137. if (!texture_)
  138. return false;
  139. float invWidth = 1.0f / (float)texture_->GetWidth();
  140. float invHeight = 1.0f / (float)texture_->GetHeight();
  141. rect.min_.x_ = rectangle_.left_ * invWidth;
  142. rect.max_.x_ = rectangle_.right_ * invWidth;
  143. rect.min_.y_ = rectangle_.bottom_ * invHeight;
  144. rect.max_.y_ = rectangle_.top_ * invHeight;
  145. if (flipX)
  146. Swap(rect.min_.x_, rect.max_.x_);
  147. if (flipY)
  148. Swap(rect.min_.y_, rect.max_.y_);
  149. return true;
  150. }
  151. ResourceRef Sprite2D::SaveToResourceRef(Sprite2D* sprite)
  152. {
  153. SpriteSheet2D* spriteSheet = 0;
  154. if (sprite)
  155. spriteSheet = sprite->GetSpriteSheet();
  156. if (!spriteSheet)
  157. return GetResourceRef(sprite, Sprite2D::GetTypeStatic());
  158. // Combine sprite sheet name and sprite name as resource name.
  159. return ResourceRef(spriteSheet->GetType(), spriteSheet->GetName() + "@" + sprite->GetName());
  160. }
  161. Sprite2D* Sprite2D::LoadFromResourceRef(Object* object, const ResourceRef& value)
  162. {
  163. if (!object)
  164. return 0;
  165. ResourceCache* cache = object->GetSubsystem<ResourceCache>();
  166. if (value.type_ == Sprite2D::GetTypeStatic())
  167. return cache->GetResource<Sprite2D>(value.name_);
  168. if (value.type_ == SpriteSheet2D::GetTypeStatic())
  169. {
  170. // value.name_ include sprite sheet name and sprite name.
  171. Vector<String> names = value.name_.Split('@');
  172. if (names.Size() != 2)
  173. return 0;
  174. const String& spriteSheetName = names[0];
  175. const String& spriteName = names[1];
  176. SpriteSheet2D* spriteSheet = cache->GetResource<SpriteSheet2D>(spriteSheetName);
  177. if (!spriteSheet)
  178. return 0;
  179. return spriteSheet->GetSprite(spriteName);
  180. }
  181. return 0;
  182. }
  183. }