CCSpriteFrame.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /****************************************************************************
  2. Copyright (c) 2008-2011 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #include "renderer/CCTextureCache.h"
  25. #include "2d/CCSpriteFrame.h"
  26. #include "base/CCDirector.h"
  27. #include "platform/CCFileUtils.h"
  28. NS_CC_BEGIN
  29. // implementation of SpriteFrame
  30. SpriteFrame* SpriteFrame::create(const std::string& filename, const Rect& rect)
  31. {
  32. SpriteFrame *spriteFrame = new (std::nothrow) SpriteFrame();
  33. spriteFrame->initWithTextureFilename(filename, rect);
  34. spriteFrame->autorelease();
  35. return spriteFrame;
  36. }
  37. SpriteFrame* SpriteFrame::createWithTexture(Texture2D *texture, const Rect& rect)
  38. {
  39. SpriteFrame *spriteFrame = new (std::nothrow) SpriteFrame();
  40. spriteFrame->initWithTexture(texture, rect);
  41. spriteFrame->autorelease();
  42. return spriteFrame;
  43. }
  44. SpriteFrame* SpriteFrame::createWithTexture(Texture2D* texture, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize)
  45. {
  46. SpriteFrame *spriteFrame = new (std::nothrow) SpriteFrame();
  47. if (spriteFrame && spriteFrame->initWithTexture(texture, rect, rotated, offset, originalSize)) {
  48. spriteFrame->autorelease();
  49. return spriteFrame;
  50. }
  51. delete spriteFrame;
  52. return nullptr;
  53. }
  54. SpriteFrame* SpriteFrame::create(const std::string& filename, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize)
  55. {
  56. SpriteFrame *spriteFrame = new (std::nothrow) SpriteFrame();
  57. if (spriteFrame && spriteFrame->initWithTextureFilename(filename, rect, rotated, offset, originalSize)) {
  58. spriteFrame->autorelease();
  59. return spriteFrame;
  60. }
  61. delete spriteFrame;
  62. return nullptr;
  63. }
  64. SpriteFrame::SpriteFrame()
  65. : _rotated(false)
  66. , _texture(nullptr)
  67. {
  68. }
  69. bool SpriteFrame::initWithTexture(Texture2D* texture, const Rect& rect)
  70. {
  71. Rect rectInPixels = CC_RECT_POINTS_TO_PIXELS(rect);
  72. return initWithTexture(texture, rectInPixels, false, Vec2::ZERO, rectInPixels.size);
  73. }
  74. bool SpriteFrame::initWithTextureFilename(const std::string& filename, const Rect& rect)
  75. {
  76. Rect rectInPixels = CC_RECT_POINTS_TO_PIXELS( rect );
  77. return initWithTextureFilename(filename, rectInPixels, false, Vec2::ZERO, rectInPixels.size);
  78. }
  79. bool SpriteFrame::initWithTexture(Texture2D* texture, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize)
  80. {
  81. _texture = texture;
  82. if (texture)
  83. {
  84. texture->retain();
  85. }
  86. _rectInPixels = rect;
  87. _rect = CC_RECT_PIXELS_TO_POINTS(rect);
  88. _offsetInPixels = offset;
  89. _offset = CC_POINT_PIXELS_TO_POINTS( _offsetInPixels );
  90. _originalSizeInPixels = originalSize;
  91. _originalSize = CC_SIZE_PIXELS_TO_POINTS( _originalSizeInPixels );
  92. _rotated = rotated;
  93. _anchorPoint = Vec2(NAN, NAN);
  94. _centerRect = Rect(NAN, NAN, NAN, NAN);
  95. return true;
  96. }
  97. bool SpriteFrame::initWithTextureFilename(const std::string& filename, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize)
  98. {
  99. if (FileUtils::getInstance()->isFileExist(filename)) {
  100. _texture = nullptr;
  101. _textureFilename = filename;
  102. _rectInPixels = rect;
  103. _rect = CC_RECT_PIXELS_TO_POINTS( rect );
  104. _offsetInPixels = offset;
  105. _offset = CC_POINT_PIXELS_TO_POINTS( _offsetInPixels );
  106. _originalSizeInPixels = originalSize;
  107. _originalSize = CC_SIZE_PIXELS_TO_POINTS( _originalSizeInPixels );
  108. _rotated = rotated;
  109. _anchorPoint = Vec2(NAN, NAN);
  110. _centerRect = Rect(NAN, NAN, NAN, NAN);
  111. return true;
  112. }
  113. return false;
  114. }
  115. SpriteFrame::~SpriteFrame()
  116. {
  117. CCLOGINFO("deallocing SpriteFrame: %p", this);
  118. CC_SAFE_RELEASE(_texture);
  119. }
  120. SpriteFrame* SpriteFrame::clone() const
  121. {
  122. // no copy constructor
  123. SpriteFrame *copy = new (std::nothrow) SpriteFrame();
  124. copy->initWithTexture(_texture, _rectInPixels, _rotated, _offsetInPixels, _originalSizeInPixels);
  125. copy->setPolygonInfo(_polygonInfo);
  126. copy->autorelease();
  127. return copy;
  128. }
  129. void SpriteFrame::setRect(const Rect& rect)
  130. {
  131. _rect = rect;
  132. _rectInPixels = CC_RECT_POINTS_TO_PIXELS(_rect);
  133. }
  134. void SpriteFrame::setRectInPixels(const Rect& rectInPixels)
  135. {
  136. _rectInPixels = rectInPixels;
  137. _rect = CC_RECT_PIXELS_TO_POINTS(rectInPixels);
  138. }
  139. void SpriteFrame::setCenterRectInPixels(const Rect& centerRect)
  140. {
  141. _centerRect = CC_RECT_PIXELS_TO_POINTS(centerRect);
  142. }
  143. bool SpriteFrame::hasCenterRect() const
  144. {
  145. return !std::isnan(_centerRect.origin.x);
  146. }
  147. const Vec2& SpriteFrame::getOffset() const
  148. {
  149. return _offset;
  150. }
  151. void SpriteFrame::setOffset(const Vec2& offsets)
  152. {
  153. _offset = offsets;
  154. _offsetInPixels = CC_POINT_POINTS_TO_PIXELS( _offset );
  155. }
  156. const Vec2& SpriteFrame::getOffsetInPixels() const
  157. {
  158. return _offsetInPixels;
  159. }
  160. void SpriteFrame::setOffsetInPixels(const Vec2& offsetInPixels)
  161. {
  162. _offsetInPixels = offsetInPixels;
  163. _offset = CC_POINT_PIXELS_TO_POINTS( _offsetInPixels );
  164. }
  165. const Vec2& SpriteFrame::getAnchorPoint() const
  166. {
  167. return _anchorPoint;
  168. }
  169. void SpriteFrame::setAnchorPoint(const Vec2& anchorPoint)
  170. {
  171. _anchorPoint = anchorPoint;
  172. }
  173. bool SpriteFrame::hasAnchorPoint() const
  174. {
  175. return !std::isnan(_anchorPoint.x);
  176. }
  177. void SpriteFrame::setTexture(Texture2D * texture)
  178. {
  179. if( _texture != texture ) {
  180. CC_SAFE_RELEASE(_texture);
  181. CC_SAFE_RETAIN(texture);
  182. _texture = texture;
  183. }
  184. }
  185. Texture2D* SpriteFrame::getTexture()
  186. {
  187. if( _texture ) {
  188. return _texture;
  189. }
  190. if( !_textureFilename.empty()) {
  191. return Director::getInstance()->getTextureCache()->addImage(_textureFilename);
  192. }
  193. // no texture or texture filename
  194. return nullptr;
  195. }
  196. void SpriteFrame::setPolygonInfo(const PolygonInfo &polygonInfo)
  197. {
  198. _polygonInfo = polygonInfo;
  199. }
  200. const PolygonInfo& SpriteFrame::getPolygonInfo() const
  201. {
  202. return _polygonInfo;
  203. }
  204. bool SpriteFrame::hasPolygonInfo() const
  205. {
  206. return _polygonInfo.triangles.vertCount != 0;
  207. }
  208. NS_CC_END