CCSpriteFrame.cpp 7.1 KB

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