2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2008-2011 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "textures/CCTextureCache.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCSpriteFrame.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
// implementation of CCSpriteFrame
|
|
|
|
|
2012-12-06 18:51:33 +08:00
|
|
|
CCSpriteFrame* CCSpriteFrame::create(const char* filename, const CCRect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCSpriteFrame *pSpriteFrame = new CCSpriteFrame();;
|
2012-12-06 18:51:33 +08:00
|
|
|
pSpriteFrame->initWithTextureFilename(filename, rect);
|
2012-04-19 14:35:52 +08:00
|
|
|
pSpriteFrame->autorelease();
|
|
|
|
|
|
|
|
return pSpriteFrame;
|
|
|
|
}
|
|
|
|
|
2012-12-06 18:51:33 +08:00
|
|
|
CCSpriteFrame* CCSpriteFrame::createWithTexture(CCTexture2D *pobTexture, const CCRect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCSpriteFrame *pSpriteFrame = new CCSpriteFrame();;
|
2012-12-06 18:51:33 +08:00
|
|
|
pSpriteFrame->initWithTexture(pobTexture, rect);
|
2012-04-19 14:35:52 +08:00
|
|
|
pSpriteFrame->autorelease();
|
2012-12-06 18:51:33 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pSpriteFrame;
|
|
|
|
}
|
|
|
|
|
2012-07-23 22:49:11 +08:00
|
|
|
CCSpriteFrame* CCSpriteFrame::createWithTexture(CCTexture2D* pobTexture, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCSpriteFrame *pSpriteFrame = new CCSpriteFrame();;
|
|
|
|
pSpriteFrame->initWithTexture(pobTexture, rect, rotated, offset, originalSize);
|
|
|
|
pSpriteFrame->autorelease();
|
|
|
|
|
|
|
|
return pSpriteFrame;
|
|
|
|
}
|
|
|
|
|
2012-06-27 14:21:29 +08:00
|
|
|
CCSpriteFrame* CCSpriteFrame::create(const char* filename, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCSpriteFrame *pSpriteFrame = new CCSpriteFrame();;
|
|
|
|
pSpriteFrame->initWithTextureFilename(filename, rect, rotated, offset, originalSize);
|
|
|
|
pSpriteFrame->autorelease();
|
|
|
|
|
|
|
|
return pSpriteFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCSpriteFrame::initWithTexture(CCTexture2D* pobTexture, const CCRect& rect)
|
|
|
|
{
|
|
|
|
CCRect rectInPixels = CC_RECT_POINTS_TO_PIXELS(rect);
|
|
|
|
return initWithTexture(pobTexture, rectInPixels, false, CCPointZero, rectInPixels.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCSpriteFrame::initWithTextureFilename(const char* filename, const CCRect& rect)
|
|
|
|
{
|
|
|
|
CCRect rectInPixels = CC_RECT_POINTS_TO_PIXELS( rect );
|
|
|
|
return initWithTextureFilename(filename, rectInPixels, false, CCPointZero, rectInPixels.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCSpriteFrame::initWithTexture(CCTexture2D* pobTexture, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_texture = pobTexture;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pobTexture)
|
|
|
|
{
|
|
|
|
pobTexture->retain();
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_rectInPixels = rect;
|
|
|
|
_rect = CC_RECT_PIXELS_TO_POINTS(rect);
|
|
|
|
_offsetInPixels = offset;
|
|
|
|
_offset = CC_POINT_PIXELS_TO_POINTS( _offsetInPixels );
|
|
|
|
_originalSizeInPixels = originalSize;
|
|
|
|
_originalSize = CC_SIZE_PIXELS_TO_POINTS( _originalSizeInPixels );
|
|
|
|
_rotated = rotated;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCSpriteFrame::initWithTextureFilename(const char* filename, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_texture = NULL;
|
|
|
|
_textureFilename = filename;
|
|
|
|
_rectInPixels = rect;
|
|
|
|
_rect = CC_RECT_PIXELS_TO_POINTS( rect );
|
|
|
|
_offsetInPixels = offset;
|
|
|
|
_offset = CC_POINT_PIXELS_TO_POINTS( _offsetInPixels );
|
|
|
|
_originalSizeInPixels = originalSize;
|
|
|
|
_originalSize = CC_SIZE_PIXELS_TO_POINTS( _originalSizeInPixels );
|
|
|
|
_rotated = rotated;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCSpriteFrame::~CCSpriteFrame(void)
|
|
|
|
{
|
|
|
|
CCLOGINFO("cocos2d: deallocing %p", this);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-19 06:06:53 +08:00
|
|
|
CCSpriteFrame* CCSpriteFrame::clone() const
|
|
|
|
{
|
|
|
|
// no copy constructor
|
|
|
|
CCSpriteFrame *copy = new CCSpriteFrame();
|
|
|
|
copy->initWithTextureFilename(_textureFilename.c_str(), _rectInPixels, _rotated, _offsetInPixels, _originalSizeInPixels);
|
|
|
|
copy->setTexture(_texture);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCObject* CCSpriteFrame::copyWithZone(CCZone *pZone)
|
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(pZone);
|
|
|
|
CCSpriteFrame *pCopy = new CCSpriteFrame();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy->initWithTextureFilename(_textureFilename.c_str(), _rectInPixels, _rotated, _offsetInPixels, _originalSizeInPixels);
|
|
|
|
pCopy->setTexture(_texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCSpriteFrame::setRect(const CCRect& rect)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_rect = rect;
|
|
|
|
_rectInPixels = CC_RECT_POINTS_TO_PIXELS(_rect);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCSpriteFrame::setRectInPixels(const CCRect& rectInPixels)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_rectInPixels = rectInPixels;
|
|
|
|
_rect = CC_RECT_PIXELS_TO_POINTS(rectInPixels);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const CCPoint& CCSpriteFrame::getOffset(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _offset;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCSpriteFrame::setOffset(const CCPoint& offsets)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_offset = offsets;
|
|
|
|
_offsetInPixels = CC_POINT_POINTS_TO_PIXELS( _offset );
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const CCPoint& CCSpriteFrame::getOffsetInPixels(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _offsetInPixels;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCSpriteFrame::setOffsetInPixels(const CCPoint& offsetInPixels)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_offsetInPixels = offsetInPixels;
|
|
|
|
_offset = CC_POINT_PIXELS_TO_POINTS( _offsetInPixels );
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCSpriteFrame::setTexture(CCTexture2D * texture)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _texture != texture ) {
|
|
|
|
CC_SAFE_RELEASE(_texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RETAIN(texture);
|
2013-06-15 14:03:30 +08:00
|
|
|
_texture = texture;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CCTexture2D* CCSpriteFrame::getTexture(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _texture ) {
|
|
|
|
return _texture;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _textureFilename.length() > 0 ) {
|
|
|
|
return CCTextureCache::sharedTextureCache()->addImage(_textureFilename.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
// no texture or texture filename
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|