Sprite2D.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "../Core/Context.h"
  24. #include "../IO/Deserializer.h"
  25. #include "../Resource/ResourceCache.h"
  26. #include "../Atomic2D/Sprite2D.h"
  27. #include "../Atomic2D/SpriteSheet2D.h"
  28. #include "../Graphics/Texture2D.h"
  29. #include "../DebugNew.h"
  30. namespace Atomic
  31. {
  32. Sprite2D::Sprite2D(Context* context) :
  33. Resource(context),
  34. hotSpot_(0.5f, 0.5f),
  35. offset_(0, 0)
  36. {
  37. }
  38. Sprite2D::~Sprite2D()
  39. {
  40. }
  41. void Sprite2D::RegisterObject(Context* context)
  42. {
  43. context->RegisterFactory<Sprite2D>();
  44. }
  45. bool Sprite2D::BeginLoad(Deserializer& source)
  46. {
  47. if (GetName().Empty())
  48. SetName(source.GetName());
  49. // Reload
  50. if (texture_)
  51. loadTexture_ = texture_;
  52. else
  53. {
  54. loadTexture_ = new Texture2D(context_);
  55. loadTexture_->SetName(GetName());
  56. }
  57. // In case we're async loading, only call BeginLoad() for the texture (load image but do not upload to GPU)
  58. if (!loadTexture_->BeginLoad(source))
  59. {
  60. // Reload failed
  61. if (loadTexture_ == texture_)
  62. texture_.Reset();
  63. loadTexture_.Reset();
  64. return false;
  65. }
  66. return true;
  67. }
  68. bool Sprite2D::EndLoad()
  69. {
  70. // Finish loading of the texture in the main thread
  71. bool success = false;
  72. if (loadTexture_ && loadTexture_->EndLoad())
  73. {
  74. success = true;
  75. SetTexture(loadTexture_);
  76. if (texture_)
  77. SetRectangle(IntRect(0, 0, texture_->GetWidth(), texture_->GetHeight()));
  78. }
  79. else
  80. {
  81. // Reload failed
  82. if (loadTexture_ == texture_)
  83. texture_.Reset();
  84. }
  85. loadTexture_.Reset();
  86. return success;
  87. }
  88. void Sprite2D::SetTexture(Texture2D* texture)
  89. {
  90. texture_ = texture;
  91. }
  92. void Sprite2D::SetRectangle(const IntRect& rectangle)
  93. {
  94. rectangle_ = rectangle;
  95. }
  96. void Sprite2D::SetHotSpot(const Vector2& hotSpot)
  97. {
  98. hotSpot_ = hotSpot;
  99. }
  100. void Sprite2D::SetOffset(const IntVector2& offset)
  101. {
  102. offset_ = offset;
  103. }
  104. void Sprite2D::SetSpriteSheet(SpriteSheet2D* spriteSheet)
  105. {
  106. spriteSheet_ = spriteSheet;
  107. }
  108. ResourceRef Sprite2D::SaveToResourceRef(Sprite2D* sprite)
  109. {
  110. SpriteSheet2D* spriteSheet = 0;
  111. if (sprite)
  112. spriteSheet = sprite->GetSpriteSheet();
  113. if (!spriteSheet)
  114. return GetResourceRef(sprite, Sprite2D::GetTypeStatic());
  115. // Combine sprite sheet name and sprite name as resource name.
  116. return ResourceRef(spriteSheet->GetType(), spriteSheet->GetName() + "@" + sprite->GetName());
  117. }
  118. Sprite2D* Sprite2D::LoadFromResourceRef(Object* object, const ResourceRef& value)
  119. {
  120. if (!object)
  121. return 0;
  122. ResourceCache* cache = object->GetSubsystem<ResourceCache>();
  123. if (value.type_ == Sprite2D::GetTypeStatic())
  124. return cache->GetResource<Sprite2D>(value.name_);
  125. if (value.type_ == SpriteSheet2D::GetTypeStatic())
  126. {
  127. // value.name_ include sprite sheet name and sprite name.
  128. Vector<String> names = value.name_.Split('@');
  129. if (names.Size() != 2)
  130. return 0;
  131. const String& spriteSheetName = names[0];
  132. const String& spriteName = names[1];
  133. SpriteSheet2D* spriteSheet = cache->GetResource<SpriteSheet2D>(spriteSheetName);
  134. if (!spriteSheet)
  135. return 0;
  136. return spriteSheet->GetSprite(spriteName);
  137. }
  138. return 0;
  139. }
  140. }