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
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of SpriteFrame
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteFrame* SpriteFrame::create(const char* filename, const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteFrame *pSpriteFrame = new SpriteFrame();;
|
2012-12-06 18:51:33 +08:00
|
|
|
pSpriteFrame->initWithTextureFilename(filename, rect);
|
2012-04-19 14:35:52 +08:00
|
|
|
pSpriteFrame->autorelease();
|
|
|
|
|
|
|
|
return pSpriteFrame;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteFrame* SpriteFrame::createWithTexture(Texture2D *pobTexture, const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteFrame *pSpriteFrame = new SpriteFrame();;
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteFrame* SpriteFrame::createWithTexture(Texture2D* pobTexture, const Rect& rect, bool rotated, const Point& offset, const Size& originalSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteFrame *pSpriteFrame = new SpriteFrame();;
|
2012-04-19 14:35:52 +08:00
|
|
|
pSpriteFrame->initWithTexture(pobTexture, rect, rotated, offset, originalSize);
|
|
|
|
pSpriteFrame->autorelease();
|
|
|
|
|
|
|
|
return pSpriteFrame;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteFrame* SpriteFrame::create(const char* filename, const Rect& rect, bool rotated, const Point& offset, const Size& originalSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteFrame *pSpriteFrame = new SpriteFrame();;
|
2012-04-19 14:35:52 +08:00
|
|
|
pSpriteFrame->initWithTextureFilename(filename, rect, rotated, offset, originalSize);
|
|
|
|
pSpriteFrame->autorelease();
|
|
|
|
|
|
|
|
return pSpriteFrame;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool SpriteFrame::initWithTexture(Texture2D* pobTexture, const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Rect rectInPixels = CC_RECT_POINTS_TO_PIXELS(rect);
|
2013-07-12 14:47:36 +08:00
|
|
|
return initWithTexture(pobTexture, rectInPixels, false, Point::ZERO, rectInPixels.size);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool SpriteFrame::initWithTextureFilename(const char* filename, const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Rect rectInPixels = CC_RECT_POINTS_TO_PIXELS( rect );
|
2013-07-12 14:47:36 +08:00
|
|
|
return initWithTextureFilename(filename, rectInPixels, false, Point::ZERO, rectInPixels.size);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool SpriteFrame::initWithTexture(Texture2D* pobTexture, const Rect& rect, bool rotated, const Point& offset, const Size& originalSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool SpriteFrame::initWithTextureFilename(const char* filename, const Rect& rect, bool rotated, const Point& offset, const Size& originalSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteFrame::~SpriteFrame(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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-20 14:13:12 +08:00
|
|
|
SpriteFrame* SpriteFrame::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteFrame *copy = new SpriteFrame();
|
2013-06-19 06:06:53 +08:00
|
|
|
copy->initWithTextureFilename(_textureFilename.c_str(), _rectInPixels, _rotated, _offsetInPixels, _originalSizeInPixels);
|
|
|
|
copy->setTexture(_texture);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteFrame::setRect(const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteFrame::setRectInPixels(const Rect& rectInPixels)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const Point& SpriteFrame::getOffset(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _offset;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteFrame::setOffset(const Point& offsets)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const Point& SpriteFrame::getOffsetInPixels(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _offsetInPixels;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteFrame::setOffsetInPixels(const Point& offsetInPixels)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteFrame::setTexture(Texture2D * texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D* SpriteFrame::getTexture(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
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 ) {
|
2013-07-12 06:24:23 +08:00
|
|
|
return TextureCache::getInstance()->addImage(_textureFilename.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
// no texture or texture filename
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|