mirror of https://github.com/axmolengine/axmol.git
fixes Sprite::setTexture()
Sprite::setTexture(std::string&) works as expected. It sets the texture rect of the sprite
This commit is contained in:
parent
cd0ba6850c
commit
b87684c022
|
@ -145,7 +145,74 @@ Sprite* Sprite::create()
|
|||
|
||||
bool Sprite::init(void)
|
||||
{
|
||||
return initWithTexture(NULL, Rect::ZERO);
|
||||
return initWithTexture(NULL, Rect::ZERO );
|
||||
}
|
||||
|
||||
bool Sprite::initWithTexture(Texture2D *texture)
|
||||
{
|
||||
CCASSERT(texture != NULL, "Invalid texture for sprite");
|
||||
|
||||
Rect rect = Rect::ZERO;
|
||||
rect.size = texture->getContentSize();
|
||||
|
||||
return initWithTexture(texture, rect);
|
||||
}
|
||||
|
||||
bool Sprite::initWithTexture(Texture2D *texture, const Rect& rect)
|
||||
{
|
||||
return initWithTexture(texture, rect, false);
|
||||
}
|
||||
|
||||
bool Sprite::initWithFile(const std::string& filename)
|
||||
{
|
||||
CCASSERT(filename.size()>0, "Invalid filename for sprite");
|
||||
|
||||
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);
|
||||
if (texture)
|
||||
{
|
||||
Rect rect = Rect::ZERO;
|
||||
rect.size = texture->getContentSize();
|
||||
return initWithTexture(texture, rect);
|
||||
}
|
||||
|
||||
// don't release here.
|
||||
// when load texture failed, it's better to get a "transparent" sprite then a crashed program
|
||||
// this->release();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Sprite::initWithFile(const std::string &filename, const Rect& rect)
|
||||
{
|
||||
CCASSERT(filename.size()>0, "Invalid filename");
|
||||
|
||||
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);
|
||||
if (texture)
|
||||
{
|
||||
return initWithTexture(texture, rect);
|
||||
}
|
||||
|
||||
// don't release here.
|
||||
// when load texture failed, it's better to get a "transparent" sprite then a crashed program
|
||||
// this->release();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Sprite::initWithSpriteFrameName(const std::string& spriteFrameName)
|
||||
{
|
||||
CCASSERT(spriteFrameName.size() > 0, "Invalid spriteFrameName");
|
||||
|
||||
SpriteFrame *frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(spriteFrameName);
|
||||
return initWithSpriteFrame(frame);
|
||||
}
|
||||
|
||||
bool Sprite::initWithSpriteFrame(SpriteFrame *spriteFrame)
|
||||
{
|
||||
CCASSERT(spriteFrame != NULL, "");
|
||||
|
||||
bool bRet = initWithTexture(spriteFrame->getTexture(), spriteFrame->getRect());
|
||||
setSpriteFrame(spriteFrame);
|
||||
|
||||
return bRet;
|
||||
}
|
||||
|
||||
// designated initializer
|
||||
|
@ -200,73 +267,6 @@ bool Sprite::initWithTexture(Texture2D *texture, const Rect& rect, bool rotated)
|
|||
}
|
||||
}
|
||||
|
||||
bool Sprite::initWithTexture(Texture2D *texture, const Rect& rect)
|
||||
{
|
||||
return initWithTexture(texture, rect, false);
|
||||
}
|
||||
|
||||
bool Sprite::initWithTexture(Texture2D *texture)
|
||||
{
|
||||
CCASSERT(texture != NULL, "Invalid texture for sprite");
|
||||
|
||||
Rect rect = Rect::ZERO;
|
||||
rect.size = texture->getContentSize();
|
||||
|
||||
return initWithTexture(texture, rect);
|
||||
}
|
||||
|
||||
bool Sprite::initWithFile(const std::string& filename)
|
||||
{
|
||||
CCASSERT(filename.size()>0, "Invalid filename for sprite");
|
||||
|
||||
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);
|
||||
if (texture)
|
||||
{
|
||||
Rect rect = Rect::ZERO;
|
||||
rect.size = texture->getContentSize();
|
||||
return initWithTexture(texture, rect);
|
||||
}
|
||||
|
||||
// don't release here.
|
||||
// when load texture failed, it's better to get a "transparent" sprite then a crashed program
|
||||
// this->release();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Sprite::initWithFile(const std::string &filename, const Rect& rect)
|
||||
{
|
||||
CCASSERT(filename.size()>0, "Invalid filename");
|
||||
|
||||
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);
|
||||
if (texture)
|
||||
{
|
||||
return initWithTexture(texture, rect);
|
||||
}
|
||||
|
||||
// don't release here.
|
||||
// when load texture failed, it's better to get a "transparent" sprite then a crashed program
|
||||
// this->release();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Sprite::initWithSpriteFrame(SpriteFrame *spriteFrame)
|
||||
{
|
||||
CCASSERT(spriteFrame != NULL, "");
|
||||
|
||||
bool bRet = initWithTexture(spriteFrame->getTexture(), spriteFrame->getRect());
|
||||
setDisplayFrame(spriteFrame);
|
||||
|
||||
return bRet;
|
||||
}
|
||||
|
||||
bool Sprite::initWithSpriteFrameName(const std::string& spriteFrameName)
|
||||
{
|
||||
CCASSERT(spriteFrameName.size() > 0, "Invalid spriteFrameName");
|
||||
|
||||
SpriteFrame *frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(spriteFrameName);
|
||||
return initWithSpriteFrame(frame);
|
||||
}
|
||||
|
||||
// XXX: deprecated
|
||||
/*
|
||||
Sprite* Sprite::initWithCGImage(CGImageRef pImage)
|
||||
|
@ -304,11 +304,76 @@ Sprite::~Sprite(void)
|
|||
CC_SAFE_RELEASE(_texture);
|
||||
}
|
||||
|
||||
/*
|
||||
* Texture methods
|
||||
*/
|
||||
|
||||
/*
|
||||
* This array is the data of a white image with 2 by 2 dimension.
|
||||
* It's used for creating a default texture when sprite's texture is set to NULL.
|
||||
* Supposing codes as follows:
|
||||
*
|
||||
* auto sp = new Sprite();
|
||||
* sp->init(); // Texture was set to NULL, in order to make opacity and color to work correctly, we need to create a 2x2 white texture.
|
||||
*
|
||||
* The test is in "TestCpp/SpriteTest/Sprite without texture".
|
||||
*/
|
||||
static unsigned char cc_2x2_white_image[] = {
|
||||
// RGBA8888
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF
|
||||
};
|
||||
|
||||
#define CC_2x2_WHITE_IMAGE_KEY "/cc_2x2_white_image"
|
||||
|
||||
void Sprite::setTexture(const std::string &filename)
|
||||
{
|
||||
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(filename);
|
||||
setTexture(texture);
|
||||
|
||||
Rect rect = Rect::ZERO;
|
||||
rect.size = texture->getContentSize();
|
||||
setTextureRect(rect);
|
||||
}
|
||||
|
||||
void Sprite::setTexture(Texture2D *texture)
|
||||
{
|
||||
// If batchnode, then texture id should be the same
|
||||
CCASSERT(! _batchNode || texture->getName() == _batchNode->getTexture()->getName(), "CCSprite: Batched sprites should use the same texture as the batchnode");
|
||||
// accept texture==nil as argument
|
||||
CCASSERT( !texture || dynamic_cast<Texture2D*>(texture), "setTexture expects a Texture2D. Invalid argument");
|
||||
|
||||
if (texture == nullptr)
|
||||
{
|
||||
// Gets the texture by key firstly.
|
||||
texture = Director::getInstance()->getTextureCache()->getTextureForKey(CC_2x2_WHITE_IMAGE_KEY);
|
||||
|
||||
// If texture wasn't in cache, create it from RAW data.
|
||||
if (texture == nullptr)
|
||||
{
|
||||
Image* image = new Image();
|
||||
bool isOK = image->initWithRawData(cc_2x2_white_image, sizeof(cc_2x2_white_image), 2, 2, 8);
|
||||
CCASSERT(isOK, "The 2x2 empty texture was created unsuccessfully.");
|
||||
|
||||
texture = Director::getInstance()->getTextureCache()->addImage(image, CC_2x2_WHITE_IMAGE_KEY);
|
||||
CC_SAFE_RELEASE(image);
|
||||
}
|
||||
}
|
||||
|
||||
if (!_batchNode && _texture != texture)
|
||||
{
|
||||
CC_SAFE_RETAIN(texture);
|
||||
CC_SAFE_RELEASE(_texture);
|
||||
_texture = texture;
|
||||
updateBlendFunc();
|
||||
}
|
||||
}
|
||||
|
||||
Texture2D* Sprite::getTexture() const
|
||||
{
|
||||
return _texture;
|
||||
}
|
||||
|
||||
void Sprite::setTextureRect(const Rect& rect)
|
||||
|
@ -998,20 +1063,20 @@ void Sprite::setSpriteFrame(const std::string &spriteFrameName)
|
|||
setSpriteFrame(spriteFrame);
|
||||
}
|
||||
|
||||
void Sprite::setSpriteFrame(SpriteFrame *pNewFrame)
|
||||
void Sprite::setSpriteFrame(SpriteFrame *spriteFrame)
|
||||
{
|
||||
_unflippedOffsetPositionFromCenter = pNewFrame->getOffset();
|
||||
_unflippedOffsetPositionFromCenter = spriteFrame->getOffset();
|
||||
|
||||
Texture2D *pNewTexture = pNewFrame->getTexture();
|
||||
Texture2D *texture = spriteFrame->getTexture();
|
||||
// update texture before updating texture rect
|
||||
if (pNewTexture != _texture)
|
||||
if (texture != _texture)
|
||||
{
|
||||
setTexture(pNewTexture);
|
||||
setTexture(texture);
|
||||
}
|
||||
|
||||
// update rect
|
||||
_rectRotated = pNewFrame->isRotated();
|
||||
setTextureRect(pNewFrame->getRect(), _rectRotated, pNewFrame->getOriginalSize());
|
||||
_rectRotated = spriteFrame->isRotated();
|
||||
setTextureRect(spriteFrame->getRect(), _rectRotated, spriteFrame->getOriginalSize());
|
||||
}
|
||||
|
||||
void Sprite::setDisplayFrameWithAnimationName(const std::string& animationName, int frameIndex)
|
||||
|
@ -1026,7 +1091,7 @@ void Sprite::setDisplayFrameWithAnimationName(const std::string& animationName,
|
|||
|
||||
CCASSERT(frame, "CCSprite#setDisplayFrame. Invalid frame");
|
||||
|
||||
setDisplayFrame(frame->getSpriteFrame());
|
||||
setSpriteFrame(frame->getSpriteFrame());
|
||||
}
|
||||
|
||||
bool Sprite::isFrameDisplayed(SpriteFrame *frame) const
|
||||
|
@ -1038,7 +1103,7 @@ bool Sprite::isFrameDisplayed(SpriteFrame *frame) const
|
|||
frame->getOffset().equals(_unflippedOffsetPositionFromCenter));
|
||||
}
|
||||
|
||||
SpriteFrame* Sprite::getDisplayFrame()
|
||||
SpriteFrame* Sprite::getSpriteFrame() const
|
||||
{
|
||||
return SpriteFrame::createWithTexture(_texture,
|
||||
CC_RECT_POINTS_TO_PIXELS(_rect),
|
||||
|
@ -1099,62 +1164,4 @@ void Sprite::updateBlendFunc(void)
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This array is the data of a white image with 2 by 2 dimension.
|
||||
* It's used for creating a default texture when sprite's texture is set to NULL.
|
||||
* Supposing codes as follows:
|
||||
*
|
||||
* auto sp = new Sprite();
|
||||
* sp->init(); // Texture was set to NULL, in order to make opacity and color to work correctly, we need to create a 2x2 white texture.
|
||||
*
|
||||
* The test is in "TestCpp/SpriteTest/Sprite without texture".
|
||||
*/
|
||||
static unsigned char cc_2x2_white_image[] = {
|
||||
// RGBA8888
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF
|
||||
};
|
||||
|
||||
#define CC_2x2_WHITE_IMAGE_KEY "/cc_2x2_white_image"
|
||||
|
||||
void Sprite::setTexture(Texture2D *texture)
|
||||
{
|
||||
// If batchnode, then texture id should be the same
|
||||
CCASSERT(! _batchNode || texture->getName() == _batchNode->getTexture()->getName(), "CCSprite: Batched sprites should use the same texture as the batchnode");
|
||||
// accept texture==nil as argument
|
||||
CCASSERT( !texture || dynamic_cast<Texture2D*>(texture), "setTexture expects a Texture2D. Invalid argument");
|
||||
|
||||
if (NULL == texture)
|
||||
{
|
||||
// Gets the texture by key firstly.
|
||||
texture = Director::getInstance()->getTextureCache()->getTextureForKey(CC_2x2_WHITE_IMAGE_KEY);
|
||||
|
||||
// If texture wasn't in cache, create it from RAW data.
|
||||
if (NULL == texture)
|
||||
{
|
||||
Image* image = new Image();
|
||||
bool isOK = image->initWithRawData(cc_2x2_white_image, sizeof(cc_2x2_white_image), 2, 2, 8);
|
||||
CCASSERT(isOK, "The 2x2 empty texture was created unsuccessfully.");
|
||||
|
||||
texture = Director::getInstance()->getTextureCache()->addImage(image, CC_2x2_WHITE_IMAGE_KEY);
|
||||
CC_SAFE_RELEASE(image);
|
||||
}
|
||||
}
|
||||
|
||||
if (!_batchNode && _texture != texture)
|
||||
{
|
||||
CC_SAFE_RETAIN(texture);
|
||||
CC_SAFE_RELEASE(_texture);
|
||||
_texture = texture;
|
||||
updateBlendFunc();
|
||||
}
|
||||
}
|
||||
|
||||
Texture2D* Sprite::getTexture(void) const
|
||||
{
|
||||
return _texture;
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -194,9 +194,19 @@ public:
|
|||
/// @{
|
||||
/// @name Texture / Frame methods
|
||||
|
||||
/** Sets a new texture (from a filename) to the sprite */
|
||||
/** Sets a new texture (from a filename) to the sprite.
|
||||
It will call `setTextureRect()` with the texture's content size.
|
||||
*/
|
||||
virtual void setTexture(const std::string &filename );
|
||||
|
||||
/** Sets a new texture to the sprite.
|
||||
The Texture's rect is not changed.
|
||||
*/
|
||||
virtual void setTexture(Texture2D *texture) override;
|
||||
|
||||
/** returns the Texture2D object used by the sprite */
|
||||
virtual Texture2D* getTexture() const override;
|
||||
|
||||
/**
|
||||
* Updates the texture rect of the Sprite in points.
|
||||
* It will call setTextureRect(const Rect& rect, bool rotated, const Size& untrimmedSize) with \p rotated = false, and \p utrimmedSize = rect.size.
|
||||
|
@ -224,20 +234,21 @@ public:
|
|||
virtual void setSpriteFrame(const std::string &spriteFrameName);
|
||||
|
||||
/** @deprecated Use `setSpriteFrame()` instead. */
|
||||
virtual void setDisplayFrame(SpriteFrame *newFrame) CC_DEPRECATED_ATTRIBUTE { setSpriteFrame(newFrame); }
|
||||
CC_DEPRECATED_ATTRIBUTE virtual void setDisplayFrame(SpriteFrame *newFrame) { setSpriteFrame(newFrame); }
|
||||
|
||||
/**
|
||||
* Returns whether or not a SpriteFrame is being displayed
|
||||
*/
|
||||
virtual bool isFrameDisplayed(SpriteFrame *pFrame) const;
|
||||
|
||||
/** @deprecated Use getDisplayFrame() instead */
|
||||
CC_DEPRECATED_ATTRIBUTE virtual SpriteFrame* displayFrame() { return getDisplayFrame(); };
|
||||
|
||||
/**
|
||||
* Returns the current displayed frame.
|
||||
*/
|
||||
virtual SpriteFrame* getDisplayFrame();
|
||||
virtual SpriteFrame* getSpriteFrame() const;
|
||||
/** @deprecated Use `getSpriteFrame()` instead */
|
||||
CC_DEPRECATED_ATTRIBUTE virtual SpriteFrame* getDisplayFrame() const { return getSpriteFrame(); }
|
||||
/** @deprecated Use `getSpriteFrame()` instead */
|
||||
CC_DEPRECATED_ATTRIBUTE virtual SpriteFrame* displayFrame() const { return getSpriteFrame(); };
|
||||
|
||||
/// @} End of frames methods
|
||||
|
||||
|
@ -367,8 +378,6 @@ public:
|
|||
//
|
||||
/// @{
|
||||
/// @name Functions inherited from TextureProtocol
|
||||
virtual void setTexture(Texture2D *texture) override;
|
||||
virtual Texture2D* getTexture() const override;
|
||||
/**
|
||||
*@code
|
||||
*When this function bound into js or lua,the parameter will be changed
|
||||
|
|
Loading…
Reference in New Issue