diff --git a/cocos/2d/CCSpriteFrame.cpp b/cocos/2d/CCSpriteFrame.cpp index 51d02c4d73..f52c56e48c 100644 --- a/cocos/2d/CCSpriteFrame.cpp +++ b/cocos/2d/CCSpriteFrame.cpp @@ -69,6 +69,13 @@ SpriteFrame* SpriteFrame::create(const std::string& filename, const Rect& rect, return spriteFrame; } +SpriteFrame::SpriteFrame(void) +: _rotated(false) +, _texture(nullptr) +{ + +} + bool SpriteFrame::initWithTexture(Texture2D* texture, const Rect& rect) { Rect rectInPixels = CC_RECT_POINTS_TO_PIXELS(rect); diff --git a/cocos/2d/CCSpriteFrame.h b/cocos/2d/CCSpriteFrame.h index 9a534fbcdc..bf214ff653 100644 --- a/cocos/2d/CCSpriteFrame.h +++ b/cocos/2d/CCSpriteFrame.h @@ -75,34 +75,6 @@ public: The originalSize is the size in points of the frame before being trimmed. */ static SpriteFrame* createWithTexture(Texture2D* pobTexture, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize); - /** - * @js NA - * @lua NA - */ - virtual ~SpriteFrame(void); - - /** Initializes a SpriteFrame with a texture, rect in points. - It is assumed that the frame was not trimmed. - */ - bool initWithTexture(Texture2D* pobTexture, const Rect& rect); - - /** Initializes a SpriteFrame with a texture filename, rect in points; - It is assumed that the frame was not trimmed. - */ - bool initWithTextureFilename(const std::string& filename, const Rect& rect); - - /** Initializes a SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels. - The originalSize is the size in points of the frame before being trimmed. - */ - bool initWithTexture(Texture2D* pobTexture, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize); - - /** Initializes a SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels. - The originalSize is the size in pixels of the frame before being trimmed. - - @since v1.1 - */ - bool initWithTextureFilename(const std::string& filename, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize); - // attributes inline const Rect& getRectInPixels() const { return _rectInPixels; } @@ -141,6 +113,39 @@ public: // Overrides virtual SpriteFrame *clone() const override; + +CC_CONSTRUCTOR_ACCESS: + /** + * @lua NA + */ + SpriteFrame(void); + + /** + * @lua NA + */ + virtual ~SpriteFrame(void); + + /** Initializes a SpriteFrame with a texture, rect in points. + It is assumed that the frame was not trimmed. + */ + bool initWithTexture(Texture2D* pobTexture, const Rect& rect); + + /** Initializes a SpriteFrame with a texture filename, rect in points; + It is assumed that the frame was not trimmed. + */ + bool initWithTextureFilename(const std::string& filename, const Rect& rect); + + /** Initializes a SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels. + The originalSize is the size in points of the frame before being trimmed. + */ + bool initWithTexture(Texture2D* pobTexture, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize); + + /** Initializes a SpriteFrame with a texture, rect, rotated, offset and originalSize in pixels. + The originalSize is the size in pixels of the frame before being trimmed. + + @since v1.1 + */ + bool initWithTextureFilename(const std::string& filename, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize); protected: Vec2 _offset; diff --git a/cocos/2d/CCSpriteFrameCache.cpp b/cocos/2d/CCSpriteFrameCache.cpp index 66b06f76c0..94fc0fe559 100644 --- a/cocos/2d/CCSpriteFrameCache.cpp +++ b/cocos/2d/CCSpriteFrameCache.cpp @@ -132,13 +132,12 @@ void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Textu ow = abs(ow); oh = abs(oh); // create frame - spriteFrame = new SpriteFrame(); - spriteFrame->initWithTexture(texture, - Rect(x, y, w, h), - false, - Vec2(ox, oy), - Size((float)ow, (float)oh) - ); + spriteFrame = SpriteFrame::createWithTexture(texture, + Rect(x, y, w, h), + false, + Vec2(ox, oy), + Size((float)ow, (float)oh) + ); } else if(format == 1 || format == 2) { @@ -155,13 +154,12 @@ void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Textu Size sourceSize = SizeFromString(frameDict["sourceSize"].asString()); // create frame - spriteFrame = new SpriteFrame(); - spriteFrame->initWithTexture(texture, - frame, - rotated, - offset, - sourceSize - ); + spriteFrame = SpriteFrame::createWithTexture(texture, + frame, + rotated, + offset, + sourceSize + ); } else if (format == 3) { @@ -186,17 +184,15 @@ void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Textu } // create frame - spriteFrame = new SpriteFrame(); - spriteFrame->initWithTexture(texture, - Rect(textureRect.origin.x, textureRect.origin.y, spriteSize.width, spriteSize.height), - textureRotated, - spriteOffset, - spriteSourceSize); + spriteFrame = SpriteFrame::createWithTexture(texture, + Rect(textureRect.origin.x, textureRect.origin.y, spriteSize.width, spriteSize.height), + textureRotated, + spriteOffset, + spriteSourceSize); } // add sprite frame _spriteFrames.insert(spriteFrameName, spriteFrame); - spriteFrame->release(); } }