2012-04-19 14:35:52 +08:00
/****************************************************************************
2012-06-08 14:11:48 +08:00
Copyright ( c ) 2010 - 2012 cocos2d - x . org
2012-04-19 14:35:52 +08:00
Copyright ( c ) 2008 - 2010 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 .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "CCSpriteBatchNode.h"
# include "CCAnimation.h"
# include "CCAnimationCache.h"
# include "ccConfig.h"
# include "CCSprite.h"
# include "CCSpriteFrame.h"
# include "CCSpriteFrameCache.h"
2012-06-19 16:20:46 +08:00
# include "textures/CCTextureCache.h"
2012-11-16 14:23:14 +08:00
# include "draw_nodes/CCDrawingPrimitives.h"
2012-06-19 16:20:46 +08:00
# include "shaders/CCShaderCache.h"
# include "shaders/ccGLStateCache.h"
# include "shaders/CCGLProgram.h"
2012-04-19 14:35:52 +08:00
# include "CCDirector.h"
2012-06-19 16:20:46 +08:00
# include "support/CCPointExtension.h"
# include "cocoa/CCGeometry.h"
# include "textures/CCTexture2D.h"
# include "cocoa/CCAffineTransform.h"
2012-04-19 14:35:52 +08:00
# include "support/TransformUtils.h"
# include "support/CCProfiling.h"
// external
# include "kazmath/GL/matrix.h"
# include <string.h>
using namespace std ;
NS_CC_BEGIN
# if CC_SPRITEBATCHNODE_RENDER_SUBPIXEL
# define RENDER_IN_SUBPIXEL
# else
2012-11-14 18:05:15 +08:00
# define RENDER_IN_SUBPIXEL(__ARGS__) (ceil(__ARGS__))
2012-04-19 14:35:52 +08:00
# endif
2012-07-23 22:49:11 +08:00
CCSprite * CCSprite : : createWithTexture ( CCTexture2D * pTexture )
2012-04-19 14:35:52 +08:00
{
CCSprite * pobSprite = new CCSprite ( ) ;
if ( pobSprite & & pobSprite - > initWithTexture ( pTexture ) )
{
pobSprite - > autorelease ( ) ;
return pobSprite ;
}
CC_SAFE_DELETE ( pobSprite ) ;
return NULL ;
}
2012-07-23 22:49:11 +08:00
CCSprite * CCSprite : : createWithTexture ( CCTexture2D * pTexture , const CCRect & rect )
2012-04-19 14:35:52 +08:00
{
CCSprite * pobSprite = new CCSprite ( ) ;
if ( pobSprite & & pobSprite - > initWithTexture ( pTexture , rect ) )
{
pobSprite - > autorelease ( ) ;
return pobSprite ;
}
CC_SAFE_DELETE ( pobSprite ) ;
return NULL ;
}
2012-06-14 15:13:16 +08:00
CCSprite * CCSprite : : create ( const char * pszFileName )
2012-04-19 14:35:52 +08:00
{
CCSprite * pobSprite = new CCSprite ( ) ;
if ( pobSprite & & pobSprite - > initWithFile ( pszFileName ) )
{
pobSprite - > autorelease ( ) ;
return pobSprite ;
}
CC_SAFE_DELETE ( pobSprite ) ;
return NULL ;
}
2012-06-14 15:13:16 +08:00
CCSprite * CCSprite : : create ( const char * pszFileName , const CCRect & rect )
2012-04-19 14:35:52 +08:00
{
CCSprite * pobSprite = new CCSprite ( ) ;
if ( pobSprite & & pobSprite - > initWithFile ( pszFileName , rect ) )
{
pobSprite - > autorelease ( ) ;
return pobSprite ;
}
CC_SAFE_DELETE ( pobSprite ) ;
return NULL ;
}
2012-07-23 22:49:11 +08:00
CCSprite * CCSprite : : createWithSpriteFrame ( CCSpriteFrame * pSpriteFrame )
2012-04-19 14:35:52 +08:00
{
CCSprite * pobSprite = new CCSprite ( ) ;
2012-09-04 11:16:59 +08:00
if ( pSpriteFrame & & pobSprite & & pobSprite - > initWithSpriteFrame ( pSpriteFrame ) )
2012-04-19 14:35:52 +08:00
{
pobSprite - > autorelease ( ) ;
return pobSprite ;
}
CC_SAFE_DELETE ( pobSprite ) ;
return NULL ;
}
2012-06-14 15:13:16 +08:00
CCSprite * CCSprite : : createWithSpriteFrameName ( const char * pszSpriteFrameName )
2012-04-19 14:35:52 +08:00
{
CCSpriteFrame * pFrame = CCSpriteFrameCache : : sharedSpriteFrameCache ( ) - > spriteFrameByName ( pszSpriteFrameName ) ;
2012-09-04 11:16:59 +08:00
# if COCOS2D_DEBUG > 0
2012-04-19 14:35:52 +08:00
char msg [ 256 ] = { 0 } ;
sprintf ( msg , " Invalid spriteFrameName: %s " , pszSpriteFrameName ) ;
CCAssert ( pFrame ! = NULL , msg ) ;
2012-09-04 11:16:59 +08:00
# endif
2012-07-23 22:49:11 +08:00
return createWithSpriteFrame ( pFrame ) ;
2012-04-19 14:35:52 +08:00
}
2012-06-14 15:13:16 +08:00
CCSprite * CCSprite : : create ( )
2012-04-19 14:35:52 +08:00
{
CCSprite * pSprite = new CCSprite ( ) ;
if ( pSprite & & pSprite - > init ( ) )
{
pSprite - > autorelease ( ) ;
2012-06-02 06:11:42 +08:00
return pSprite ;
2012-04-19 14:35:52 +08:00
}
2012-06-02 06:11:42 +08:00
CC_SAFE_DELETE ( pSprite ) ;
return NULL ;
2012-04-19 14:35:52 +08:00
}
bool CCSprite : : init ( void )
{
return initWithTexture ( NULL , CCRectZero ) ;
}
// designated initializer
bool CCSprite : : initWithTexture ( CCTexture2D * pTexture , const CCRect & rect , bool rotated )
{
2013-02-27 15:30:49 +08:00
if ( CCNodeRGBA : : init ( ) )
{
2013-06-15 14:03:30 +08:00
_batchNode = NULL ;
2013-02-27 15:30:49 +08:00
2013-06-15 14:03:30 +08:00
_recursiveDirty = false ;
2013-02-27 15:30:49 +08:00
setDirty ( false ) ;
2013-06-15 14:03:30 +08:00
_opacityModifyRGB = true ;
2013-02-27 15:30:49 +08:00
2013-06-15 14:03:30 +08:00
_blendFunc . src = CC_BLEND_SRC ;
_blendFunc . dst = CC_BLEND_DST ;
2013-02-27 15:30:49 +08:00
2013-06-15 14:03:30 +08:00
_flipX = _flipY = false ;
2013-02-27 15:30:49 +08:00
// default transform anchor: center
setAnchorPoint ( ccp ( 0.5f , 0.5f ) ) ;
// zwoptex default values
2013-06-15 14:03:30 +08:00
_offsetPosition = CCPointZero ;
2013-02-27 15:30:49 +08:00
2013-06-15 14:03:30 +08:00
_hasChildren = false ;
2013-02-27 15:30:49 +08:00
// clean the Quad
2013-06-15 14:03:30 +08:00
memset ( & _quad , 0 , sizeof ( _quad ) ) ;
2013-02-27 15:30:49 +08:00
// Atlas: Color
ccColor4B tmpColor = { 255 , 255 , 255 , 255 } ;
2013-06-15 14:03:30 +08:00
_quad . bl . colors = tmpColor ;
_quad . br . colors = tmpColor ;
_quad . tl . colors = tmpColor ;
_quad . tr . colors = tmpColor ;
2013-02-27 15:30:49 +08:00
// update texture (calls updateBlendFunc)
setTexture ( pTexture ) ;
setTextureRect ( rect , rotated , rect . size ) ;
// by default use "Self Render".
// if the sprite is added to a batchnode, then it will automatically switch to "batchnode Render"
setBatchNode ( NULL ) ;
return true ;
}
else
{
return false ;
}
2012-04-19 14:35:52 +08:00
}
bool CCSprite : : initWithTexture ( CCTexture2D * pTexture , const CCRect & rect )
{
return initWithTexture ( pTexture , rect , false ) ;
}
bool CCSprite : : initWithTexture ( CCTexture2D * pTexture )
{
CCAssert ( pTexture ! = NULL , " Invalid texture for sprite " ) ;
CCRect rect = CCRectZero ;
rect . size = pTexture - > getContentSize ( ) ;
return initWithTexture ( pTexture , rect ) ;
}
bool CCSprite : : initWithFile ( const char * pszFilename )
{
CCAssert ( pszFilename ! = NULL , " Invalid filename for sprite " ) ;
CCTexture2D * pTexture = CCTextureCache : : sharedTextureCache ( ) - > addImage ( pszFilename ) ;
if ( pTexture )
{
CCRect rect = CCRectZero ;
rect . size = pTexture - > getContentSize ( ) ;
return initWithTexture ( pTexture , 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 CCSprite : : initWithFile ( const char * pszFilename , const CCRect & rect )
{
CCAssert ( pszFilename ! = NULL , " " ) ;
CCTexture2D * pTexture = CCTextureCache : : sharedTextureCache ( ) - > addImage ( pszFilename ) ;
if ( pTexture )
{
return initWithTexture ( pTexture , 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 CCSprite : : initWithSpriteFrame ( CCSpriteFrame * pSpriteFrame )
{
CCAssert ( pSpriteFrame ! = NULL , " " ) ;
bool bRet = initWithTexture ( pSpriteFrame - > getTexture ( ) , pSpriteFrame - > getRect ( ) ) ;
setDisplayFrame ( pSpriteFrame ) ;
return bRet ;
}
bool CCSprite : : initWithSpriteFrameName ( const char * pszSpriteFrameName )
{
CCAssert ( pszSpriteFrameName ! = NULL , " " ) ;
CCSpriteFrame * pFrame = CCSpriteFrameCache : : sharedSpriteFrameCache ( ) - > spriteFrameByName ( pszSpriteFrameName ) ;
return initWithSpriteFrame ( pFrame ) ;
}
// XXX: deprecated
/*
CCSprite * CCSprite : : initWithCGImage ( CGImageRef pImage )
{
// todo
2012-09-17 15:02:24 +08:00
// because it is deprecated, so we do not implement it
2012-04-19 14:35:52 +08:00
return NULL ;
}
*/
/*
CCSprite * CCSprite : : initWithCGImage ( CGImageRef pImage , const char * pszKey )
{
CCAssert ( pImage ! = NULL ) ;
// XXX: possible bug. See issue #349. New API should be added
CCTexture2D * pTexture = CCTextureCache : : sharedTextureCache ( ) - > addCGImage ( pImage , pszKey ) ;
2013-01-14 16:06:18 +08:00
const CCSize & size = pTexture - > getContentSize ( ) ;
2012-04-19 14:35:52 +08:00
CCRect rect = CCRectMake ( 0 , 0 , size . width , size . height ) ;
return initWithTexture ( texture , rect ) ;
}
*/
CCSprite : : CCSprite ( void )
2013-06-15 14:03:30 +08:00
: _shouldBeHidden ( false ) ,
_texture ( NULL )
2012-04-19 14:35:52 +08:00
{
}
CCSprite : : ~ CCSprite ( void )
{
2013-06-15 14:03:30 +08:00
CC_SAFE_RELEASE ( _texture ) ;
2012-04-19 14:35:52 +08:00
}
void CCSprite : : setTextureRect ( const CCRect & rect )
{
setTextureRect ( rect , false , rect . size ) ;
}
void CCSprite : : setTextureRect ( const CCRect & rect , bool rotated , const CCSize & untrimmedSize )
{
2013-06-15 14:03:30 +08:00
_rectRotated = rotated ;
2012-04-19 14:35:52 +08:00
setContentSize ( untrimmedSize ) ;
setVertexRect ( rect ) ;
setTextureCoords ( rect ) ;
2013-06-15 14:03:30 +08:00
CCPoint relativeOffset = _unflippedOffsetPositionFromCenter ;
2012-04-19 14:35:52 +08:00
// issue #732
2013-06-15 14:03:30 +08:00
if ( _flipX )
2012-04-19 14:35:52 +08:00
{
relativeOffset . x = - relativeOffset . x ;
}
2013-06-15 14:03:30 +08:00
if ( _flipY )
2012-04-19 14:35:52 +08:00
{
relativeOffset . y = - relativeOffset . y ;
}
2013-06-15 14:03:30 +08:00
_offsetPosition . x = relativeOffset . x + ( _contentSize . width - _rect . size . width ) / 2 ;
_offsetPosition . y = relativeOffset . y + ( _contentSize . height - _rect . size . height ) / 2 ;
2012-04-19 14:35:52 +08:00
// rendering using batch node
2013-06-15 14:03:30 +08:00
if ( _batchNode )
2012-04-19 14:35:52 +08:00
{
// update dirty_, don't update recursiveDirty_
setDirty ( true ) ;
}
else
{
// self rendering
// Atlas: Vertex
2013-06-15 14:03:30 +08:00
float x1 = 0 + _offsetPosition . x ;
float y1 = 0 + _offsetPosition . y ;
float x2 = x1 + _rect . size . width ;
float y2 = y1 + _rect . size . height ;
2012-04-19 14:35:52 +08:00
// Don't update Z.
2013-06-15 14:03:30 +08:00
_quad . bl . vertices = vertex3 ( x1 , y1 , 0 ) ;
_quad . br . vertices = vertex3 ( x2 , y1 , 0 ) ;
_quad . tl . vertices = vertex3 ( x1 , y2 , 0 ) ;
_quad . tr . vertices = vertex3 ( x2 , y2 , 0 ) ;
2012-04-19 14:35:52 +08:00
}
}
// override this method to generate "double scale" sprites
void CCSprite : : setVertexRect ( const CCRect & rect )
{
2013-06-15 14:03:30 +08:00
_rect = rect ;
2012-04-19 14:35:52 +08:00
}
void CCSprite : : setTextureCoords ( CCRect rect )
{
rect = CC_RECT_POINTS_TO_PIXELS ( rect ) ;
2013-06-15 14:03:30 +08:00
CCTexture2D * tex = _batchNode ? _textureAtlas - > getTexture ( ) : _texture ;
2012-04-19 14:35:52 +08:00
if ( ! tex )
{
return ;
}
float atlasWidth = ( float ) tex - > getPixelsWide ( ) ;
float atlasHeight = ( float ) tex - > getPixelsHigh ( ) ;
float left , right , top , bottom ;
2013-06-15 14:03:30 +08:00
if ( _rectRotated )
2012-04-19 14:35:52 +08:00
{
# if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
left = ( 2 * rect . origin . x + 1 ) / ( 2 * atlasWidth ) ;
right = left + ( rect . size . height * 2 - 2 ) / ( 2 * atlasWidth ) ;
top = ( 2 * rect . origin . y + 1 ) / ( 2 * atlasHeight ) ;
bottom = top + ( rect . size . width * 2 - 2 ) / ( 2 * atlasHeight ) ;
# else
left = rect . origin . x / atlasWidth ;
2012-06-08 14:11:48 +08:00
right = ( rect . origin . x + rect . size . height ) / atlasWidth ;
2012-04-19 14:35:52 +08:00
top = rect . origin . y / atlasHeight ;
2012-06-08 14:11:48 +08:00
bottom = ( rect . origin . y + rect . size . width ) / atlasHeight ;
2012-04-19 14:35:52 +08:00
# endif // CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
2013-06-15 14:03:30 +08:00
if ( _flipX )
2012-04-19 14:35:52 +08:00
{
CC_SWAP ( top , bottom , float ) ;
}
2013-06-15 14:03:30 +08:00
if ( _flipY )
2012-04-19 14:35:52 +08:00
{
CC_SWAP ( left , right , float ) ;
}
2013-06-15 14:03:30 +08:00
_quad . bl . texCoords . u = left ;
_quad . bl . texCoords . v = top ;
_quad . br . texCoords . u = left ;
_quad . br . texCoords . v = bottom ;
_quad . tl . texCoords . u = right ;
_quad . tl . texCoords . v = top ;
_quad . tr . texCoords . u = right ;
_quad . tr . texCoords . v = bottom ;
2012-04-19 14:35:52 +08:00
}
else
{
# if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
left = ( 2 * rect . origin . x + 1 ) / ( 2 * atlasWidth ) ;
right = left + ( rect . size . width * 2 - 2 ) / ( 2 * atlasWidth ) ;
top = ( 2 * rect . origin . y + 1 ) / ( 2 * atlasHeight ) ;
bottom = top + ( rect . size . height * 2 - 2 ) / ( 2 * atlasHeight ) ;
# else
left = rect . origin . x / atlasWidth ;
2012-06-08 14:11:48 +08:00
right = ( rect . origin . x + rect . size . width ) / atlasWidth ;
2012-04-19 14:35:52 +08:00
top = rect . origin . y / atlasHeight ;
2012-06-08 14:11:48 +08:00
bottom = ( rect . origin . y + rect . size . height ) / atlasHeight ;
2012-04-19 14:35:52 +08:00
# endif // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
2013-06-15 14:03:30 +08:00
if ( _flipX )
2012-04-19 14:35:52 +08:00
{
CC_SWAP ( left , right , float ) ;
}
2013-06-15 14:03:30 +08:00
if ( _flipY )
2012-04-19 14:35:52 +08:00
{
CC_SWAP ( top , bottom , float ) ;
}
2013-06-15 14:03:30 +08:00
_quad . bl . texCoords . u = left ;
_quad . bl . texCoords . v = bottom ;
_quad . br . texCoords . u = right ;
_quad . br . texCoords . v = bottom ;
_quad . tl . texCoords . u = left ;
_quad . tl . texCoords . v = top ;
_quad . tr . texCoords . u = right ;
_quad . tr . texCoords . v = top ;
2012-04-19 14:35:52 +08:00
}
}
void CCSprite : : updateTransform ( void )
{
2013-06-15 14:03:30 +08:00
CCAssert ( _batchNode , " updateTransform is only valid when CCSprite is being rendered using an CCSpriteBatchNode " ) ;
2012-04-19 14:35:52 +08:00
2012-09-17 15:02:24 +08:00
// recalculate matrix only if it is dirty
2012-04-19 14:35:52 +08:00
if ( isDirty ( ) ) {
// If it is not visible, or one of its ancestors is not visible, then do nothing:
2013-06-15 14:03:30 +08:00
if ( ! _visible | | ( _parent & & _parent ! = _batchNode & & ( ( CCSprite * ) _parent ) - > _shouldBeHidden ) )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
_quad . br . vertices = _quad . tl . vertices = _quad . tr . vertices = _quad . bl . vertices = vertex3 ( 0 , 0 , 0 ) ;
_shouldBeHidden = true ;
2012-04-19 14:35:52 +08:00
}
else
{
2013-06-15 14:03:30 +08:00
_shouldBeHidden = false ;
2012-04-19 14:35:52 +08:00
2013-06-15 14:03:30 +08:00
if ( ! _parent | | _parent = = _batchNode )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
_transformToBatch = nodeToParentTransform ( ) ;
2012-04-19 14:35:52 +08:00
}
else
{
2013-06-15 14:03:30 +08:00
CCAssert ( dynamic_cast < CCSprite * > ( _parent ) , " Logic error in CCSprite. Parent must be a CCSprite " ) ;
_transformToBatch = CCAffineTransformConcat ( nodeToParentTransform ( ) , ( ( CCSprite * ) _parent ) - > _transformToBatch ) ;
2012-04-19 14:35:52 +08:00
}
//
// calculate the Quad based on the Affine Matrix
//
2013-06-15 14:03:30 +08:00
CCSize size = _rect . size ;
2012-04-19 14:35:52 +08:00
2013-06-15 14:03:30 +08:00
float x1 = _offsetPosition . x ;
float y1 = _offsetPosition . y ;
2012-04-19 14:35:52 +08:00
float x2 = x1 + size . width ;
float y2 = y1 + size . height ;
2013-06-15 14:03:30 +08:00
float x = _transformToBatch . tx ;
float y = _transformToBatch . ty ;
2012-04-19 14:35:52 +08:00
2013-06-15 14:03:30 +08:00
float cr = _transformToBatch . a ;
float sr = _transformToBatch . b ;
float cr2 = _transformToBatch . d ;
float sr2 = - _transformToBatch . c ;
2012-04-19 14:35:52 +08:00
float ax = x1 * cr - y1 * sr2 + x ;
float ay = x1 * sr + y1 * cr2 + y ;
float bx = x2 * cr - y1 * sr2 + x ;
float by = x2 * sr + y1 * cr2 + y ;
float cx = x2 * cr - y2 * sr2 + x ;
float cy = x2 * sr + y2 * cr2 + y ;
float dx = x1 * cr - y2 * sr2 + x ;
float dy = x1 * sr + y2 * cr2 + y ;
2013-06-15 14:03:30 +08:00
_quad . bl . vertices = vertex3 ( RENDER_IN_SUBPIXEL ( ax ) , RENDER_IN_SUBPIXEL ( ay ) , _vertexZ ) ;
_quad . br . vertices = vertex3 ( RENDER_IN_SUBPIXEL ( bx ) , RENDER_IN_SUBPIXEL ( by ) , _vertexZ ) ;
_quad . tl . vertices = vertex3 ( RENDER_IN_SUBPIXEL ( dx ) , RENDER_IN_SUBPIXEL ( dy ) , _vertexZ ) ;
_quad . tr . vertices = vertex3 ( RENDER_IN_SUBPIXEL ( cx ) , RENDER_IN_SUBPIXEL ( cy ) , _vertexZ ) ;
2012-04-19 14:35:52 +08:00
}
2012-10-19 23:38:58 +08:00
// MARMALADE CHANGE: ADDED CHECK FOR NULL, TO PERMIT SPRITES WITH NO BATCH NODE / TEXTURE ATLAS
2013-06-15 14:03:30 +08:00
if ( _textureAtlas )
2012-11-12 10:20:04 +08:00
{
2013-06-15 14:03:30 +08:00
_textureAtlas - > updateQuad ( & _quad , _atlasIndex ) ;
2012-11-12 10:20:04 +08:00
}
2013-06-15 14:03:30 +08:00
_recursiveDirty = false ;
2012-04-19 14:35:52 +08:00
setDirty ( false ) ;
}
2012-11-14 18:05:15 +08:00
// MARMALADE CHANGED
// recursively iterate over children
2013-06-15 14:03:30 +08:00
/* if( _hasChildren )
2012-11-14 18:05:15 +08:00
{
// MARMALADE: CHANGED TO USE CCNode*
// NOTE THAT WE HAVE ALSO DEFINED virtual CCNode::updateTransform()
2013-06-15 14:03:30 +08:00
arrayMakeObjectsPerformSelector ( _children , updateTransform , CCSprite * ) ;
2012-11-14 18:05:15 +08:00
} */
2012-10-30 10:24:28 +08:00
CCNode : : updateTransform ( ) ;
2012-04-19 14:35:52 +08:00
# if CC_SPRITE_DEBUG_DRAW
// draw bounding box
CCPoint vertices [ 4 ] = {
2013-06-15 14:03:30 +08:00
ccp ( _quad . bl . vertices . x , _quad . bl . vertices . y ) ,
ccp ( _quad . br . vertices . x , _quad . br . vertices . y ) ,
ccp ( _quad . tr . vertices . x , _quad . tr . vertices . y ) ,
ccp ( _quad . tl . vertices . x , _quad . tl . vertices . y ) ,
2012-04-19 14:35:52 +08:00
} ;
ccDrawPoly ( vertices , 4 , true ) ;
# endif // CC_SPRITE_DEBUG_DRAW
}
// draw
void CCSprite : : draw ( void )
{
CC_PROFILER_START_CATEGORY ( kCCProfilerCategorySprite , " CCSprite - draw " ) ;
2013-06-15 14:03:30 +08:00
CCAssert ( ! _batchNode , " If CCSprite is being rendered by CCSpriteBatchNode, CCSprite#draw SHOULD NOT be called " ) ;
2012-04-19 14:35:52 +08:00
CC_NODE_DRAW_SETUP ( ) ;
2013-06-15 14:03:30 +08:00
ccGLBlendFunc ( _blendFunc . src , _blendFunc . dst ) ;
2012-04-19 14:35:52 +08:00
2013-06-15 14:03:30 +08:00
if ( _texture ! = NULL )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
ccGLBindTexture2D ( _texture - > getName ( ) ) ;
2013-06-06 14:41:22 +08:00
ccGLEnableVertexAttribs ( kCCVertexAttribFlag_PosColorTex ) ;
2012-04-19 14:35:52 +08:00
}
else
{
ccGLBindTexture2D ( 0 ) ;
2013-06-06 14:41:22 +08:00
ccGLEnableVertexAttribs ( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color ) ;
2012-04-19 14:35:52 +08:00
}
2013-06-15 14:03:30 +08:00
# define kQuadSize sizeof(_quad.bl)
2013-04-05 13:20:59 +08:00
# ifdef EMSCRIPTEN
long offset = 0 ;
2013-06-15 14:03:30 +08:00
setGLBufferData ( & _quad , 4 * kQuadSize , 0 ) ;
2013-04-05 13:20:59 +08:00
# else
2013-06-15 14:03:30 +08:00
long offset = ( long ) & _quad ;
2013-04-05 13:20:59 +08:00
# endif // EMSCRIPTEN
2012-04-19 14:35:52 +08:00
// vertex
int diff = offsetof ( ccV3F_C4B_T2F , vertices ) ;
glVertexAttribPointer ( kCCVertexAttrib_Position , 3 , GL_FLOAT , GL_FALSE , kQuadSize , ( void * ) ( offset + diff ) ) ;
2013-06-15 14:03:30 +08:00
if ( _texture ! = NULL )
2013-06-06 14:41:22 +08:00
{
// texCoods
diff = offsetof ( ccV3F_C4B_T2F , texCoords ) ;
glVertexAttribPointer ( kCCVertexAttrib_TexCoords , 2 , GL_FLOAT , GL_FALSE , kQuadSize , ( void * ) ( offset + diff ) ) ;
}
2012-04-19 14:35:52 +08:00
// color
diff = offsetof ( ccV3F_C4B_T2F , colors ) ;
glVertexAttribPointer ( kCCVertexAttrib_Color , 4 , GL_UNSIGNED_BYTE , GL_TRUE , kQuadSize , ( void * ) ( offset + diff ) ) ;
glDrawArrays ( GL_TRIANGLE_STRIP , 0 , 4 ) ;
CHECK_GL_ERROR_DEBUG ( ) ;
# if CC_SPRITE_DEBUG_DRAW == 1
// draw bounding box
CCPoint vertices [ 4 ] = {
2013-06-15 14:03:30 +08:00
ccp ( _quad . tl . vertices . x , _quad . tl . vertices . y ) ,
ccp ( _quad . bl . vertices . x , _quad . bl . vertices . y ) ,
ccp ( _quad . br . vertices . x , _quad . br . vertices . y ) ,
ccp ( _quad . tr . vertices . x , _quad . tr . vertices . y ) ,
2012-04-19 14:35:52 +08:00
} ;
ccDrawPoly ( vertices , 4 , true ) ;
# elif CC_SPRITE_DEBUG_DRAW == 2
// draw texture box
CCSize s = this - > getTextureRect ( ) . size ;
CCPoint offsetPix = this - > getOffsetPosition ( ) ;
CCPoint vertices [ 4 ] = {
ccp ( offsetPix . x , offsetPix . y ) , ccp ( offsetPix . x + s . width , offsetPix . y ) ,
ccp ( offsetPix . x + s . width , offsetPix . y + s . height ) , ccp ( offsetPix . x , offsetPix . y + s . height )
} ;
ccDrawPoly ( vertices , 4 , true ) ;
# endif // CC_SPRITE_DEBUG_DRAW
CC_INCREMENT_GL_DRAWS ( 1 ) ;
CC_PROFILER_STOP_CATEGORY ( kCCProfilerCategorySprite , " CCSprite - draw " ) ;
}
// CCNode overrides
void CCSprite : : addChild ( CCNode * pChild )
{
CCNode : : addChild ( pChild ) ;
}
void CCSprite : : addChild ( CCNode * pChild , int zOrder )
{
CCNode : : addChild ( pChild , zOrder ) ;
}
void CCSprite : : addChild ( CCNode * pChild , int zOrder , int tag )
{
CCAssert ( pChild ! = NULL , " Argument must be non-NULL " ) ;
2013-06-15 14:03:30 +08:00
if ( _batchNode )
2012-04-19 14:35:52 +08:00
{
CCSprite * pChildSprite = dynamic_cast < CCSprite * > ( pChild ) ;
CCAssert ( pChildSprite , " CCSprite only supports CCSprites as children when using CCSpriteBatchNode " ) ;
2013-06-15 14:03:30 +08:00
CCAssert ( pChildSprite - > getTexture ( ) - > getName ( ) = = _textureAtlas - > getTexture ( ) - > getName ( ) , " " ) ;
2012-04-19 14:35:52 +08:00
//put it in descendants array of batch node
2013-06-15 14:03:30 +08:00
_batchNode - > appendChild ( pChildSprite ) ;
2012-04-19 14:35:52 +08:00
2013-06-15 14:03:30 +08:00
if ( ! _reorderChildDirty )
2012-04-19 14:35:52 +08:00
{
setReorderChildDirtyRecursively ( ) ;
}
}
//CCNode already sets isReorderChildDirty_ so this needs to be after batchNode check
CCNode : : addChild ( pChild , zOrder , tag ) ;
2013-06-15 14:03:30 +08:00
_hasChildren = true ;
2012-04-19 14:35:52 +08:00
}
void CCSprite : : reorderChild ( CCNode * pChild , int zOrder )
{
CCAssert ( pChild ! = NULL , " " ) ;
2013-06-15 14:03:30 +08:00
CCAssert ( _children - > containsObject ( pChild ) , " " ) ;
2012-04-19 14:35:52 +08:00
if ( zOrder = = pChild - > getZOrder ( ) )
{
return ;
}
2013-06-15 14:03:30 +08:00
if ( _batchNode & & ! _reorderChildDirty )
2012-04-19 14:35:52 +08:00
{
setReorderChildDirtyRecursively ( ) ;
2013-06-15 14:03:30 +08:00
_batchNode - > reorderBatch ( true ) ;
2012-04-19 14:35:52 +08:00
}
CCNode : : reorderChild ( pChild , zOrder ) ;
}
void CCSprite : : removeChild ( CCNode * pChild , bool bCleanup )
{
2013-06-15 14:03:30 +08:00
if ( _batchNode )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
_batchNode - > removeSpriteFromAtlas ( ( CCSprite * ) ( pChild ) ) ;
2012-04-19 14:35:52 +08:00
}
CCNode : : removeChild ( pChild , bCleanup ) ;
}
void CCSprite : : removeAllChildrenWithCleanup ( bool bCleanup )
{
2013-06-15 14:03:30 +08:00
if ( _batchNode )
2012-04-19 14:35:52 +08:00
{
CCObject * pObject = NULL ;
2013-06-15 14:03:30 +08:00
CCARRAY_FOREACH ( _children , pObject )
2012-04-19 14:35:52 +08:00
{
CCSprite * pChild = dynamic_cast < CCSprite * > ( pObject ) ;
if ( pChild )
{
2013-06-15 14:03:30 +08:00
_batchNode - > removeSpriteFromAtlas ( pChild ) ;
2012-04-19 14:35:52 +08:00
}
}
}
CCNode : : removeAllChildrenWithCleanup ( bCleanup ) ;
2013-06-15 14:03:30 +08:00
_hasChildren = false ;
2012-04-19 14:35:52 +08:00
}
void CCSprite : : sortAllChildren ( )
{
2013-06-15 14:03:30 +08:00
if ( _reorderChildDirty )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
int i = 0 , j = 0 , length = _children - > data - > num ;
CCNode * * x = ( CCNode * * ) _children - > data - > arr ;
2012-04-19 14:35:52 +08:00
CCNode * tempItem = NULL ;
// insertion sort
for ( i = 1 ; i < length ; i + + )
{
tempItem = x [ i ] ;
j = i - 1 ;
//continue moving element downwards while zOrder is smaller or when zOrder is the same but orderOfArrival is smaller
while ( j > = 0 & & ( tempItem - > getZOrder ( ) < x [ j ] - > getZOrder ( ) | | ( tempItem - > getZOrder ( ) = = x [ j ] - > getZOrder ( ) & & tempItem - > getOrderOfArrival ( ) < x [ j ] - > getOrderOfArrival ( ) ) ) )
{
x [ j + 1 ] = x [ j ] ;
j = j - 1 ;
}
x [ j + 1 ] = tempItem ;
}
2013-06-15 14:03:30 +08:00
if ( _batchNode )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
arrayMakeObjectsPerformSelector ( _children , sortAllChildren , CCSprite * ) ;
2012-04-19 14:35:52 +08:00
}
2013-06-15 14:03:30 +08:00
_reorderChildDirty = false ;
2012-04-19 14:35:52 +08:00
}
}
//
// CCNode property overloads
// used only when parent is CCSpriteBatchNode
//
void CCSprite : : setReorderChildDirtyRecursively ( void )
{
//only set parents flag the first time
2013-06-15 14:03:30 +08:00
if ( ! _reorderChildDirty )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
_reorderChildDirty = true ;
CCNode * pNode = ( CCNode * ) _parent ;
while ( pNode & & pNode ! = _batchNode )
2012-04-19 14:35:52 +08:00
{
( ( CCSprite * ) pNode ) - > setReorderChildDirtyRecursively ( ) ;
pNode = pNode - > getParent ( ) ;
}
}
}
void CCSprite : : setDirtyRecursively ( bool bValue )
{
2013-06-15 14:03:30 +08:00
_recursiveDirty = bValue ;
2012-04-19 14:35:52 +08:00
setDirty ( bValue ) ;
// recursively set dirty
2013-06-15 14:03:30 +08:00
if ( _hasChildren )
2012-04-19 14:35:52 +08:00
{
CCObject * pObject = NULL ;
2013-06-15 14:03:30 +08:00
CCARRAY_FOREACH ( _children , pObject )
2012-04-19 14:35:52 +08:00
{
CCSprite * pChild = dynamic_cast < CCSprite * > ( pObject ) ;
if ( pChild )
{
pChild - > setDirtyRecursively ( true ) ;
}
}
}
}
// XXX HACK: optimization
# define SET_DIRTY_RECURSIVELY() { \
2013-06-15 14:03:30 +08:00
if ( _batchNode & & ! _recursiveDirty ) { \
_recursiveDirty = true ; \
2012-04-19 14:35:52 +08:00
setDirty ( true ) ; \
2013-06-15 14:03:30 +08:00
if ( _hasChildren ) \
2012-04-19 14:35:52 +08:00
setDirtyRecursively ( true ) ; \
} \
}
void CCSprite : : setPosition ( const CCPoint & pos )
{
CCNode : : setPosition ( pos ) ;
SET_DIRTY_RECURSIVELY ( ) ;
}
void CCSprite : : setRotation ( float fRotation )
{
CCNode : : setRotation ( fRotation ) ;
SET_DIRTY_RECURSIVELY ( ) ;
}
2012-11-14 18:05:15 +08:00
void CCSprite : : setRotationX ( float fRotationX )
{
CCNode : : setRotationX ( fRotationX ) ;
SET_DIRTY_RECURSIVELY ( ) ;
}
void CCSprite : : setRotationY ( float fRotationY )
{
CCNode : : setRotationY ( fRotationY ) ;
SET_DIRTY_RECURSIVELY ( ) ;
}
2012-04-19 14:35:52 +08:00
void CCSprite : : setSkewX ( float sx )
{
CCNode : : setSkewX ( sx ) ;
SET_DIRTY_RECURSIVELY ( ) ;
}
void CCSprite : : setSkewY ( float sy )
{
CCNode : : setSkewY ( sy ) ;
SET_DIRTY_RECURSIVELY ( ) ;
}
void CCSprite : : setScaleX ( float fScaleX )
{
CCNode : : setScaleX ( fScaleX ) ;
SET_DIRTY_RECURSIVELY ( ) ;
}
void CCSprite : : setScaleY ( float fScaleY )
{
CCNode : : setScaleY ( fScaleY ) ;
SET_DIRTY_RECURSIVELY ( ) ;
}
void CCSprite : : setScale ( float fScale )
{
CCNode : : setScale ( fScale ) ;
SET_DIRTY_RECURSIVELY ( ) ;
}
void CCSprite : : setVertexZ ( float fVertexZ )
{
CCNode : : setVertexZ ( fVertexZ ) ;
SET_DIRTY_RECURSIVELY ( ) ;
}
void CCSprite : : setAnchorPoint ( const CCPoint & anchor )
{
CCNode : : setAnchorPoint ( anchor ) ;
SET_DIRTY_RECURSIVELY ( ) ;
}
2012-06-15 15:10:40 +08:00
void CCSprite : : ignoreAnchorPointForPosition ( bool value )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
CCAssert ( ! _batchNode , " ignoreAnchorPointForPosition is invalid in CCSprite " ) ;
2012-06-15 15:10:40 +08:00
CCNode : : ignoreAnchorPointForPosition ( value ) ;
2012-04-19 14:35:52 +08:00
}
2012-06-15 16:47:30 +08:00
void CCSprite : : setVisible ( bool bVisible )
2012-04-19 14:35:52 +08:00
{
2012-06-15 15:10:40 +08:00
CCNode : : setVisible ( bVisible ) ;
2012-04-19 14:35:52 +08:00
SET_DIRTY_RECURSIVELY ( ) ;
}
void CCSprite : : setFlipX ( bool bFlipX )
{
2013-06-15 14:03:30 +08:00
if ( _flipX ! = bFlipX )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
_flipX = bFlipX ;
setTextureRect ( _rect , _rectRotated , _contentSize ) ;
2012-04-19 14:35:52 +08:00
}
}
bool CCSprite : : isFlipX ( void )
{
2013-06-15 14:03:30 +08:00
return _flipX ;
2012-04-19 14:35:52 +08:00
}
void CCSprite : : setFlipY ( bool bFlipY )
{
2013-06-15 14:03:30 +08:00
if ( _flipY ! = bFlipY )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
_flipY = bFlipY ;
setTextureRect ( _rect , _rectRotated , _contentSize ) ;
2012-04-19 14:35:52 +08:00
}
}
bool CCSprite : : isFlipY ( void )
{
2013-06-15 14:03:30 +08:00
return _flipY ;
2012-04-19 14:35:52 +08:00
}
//
// RGBA protocol
//
void CCSprite : : updateColor ( void )
{
2013-02-27 15:30:49 +08:00
ccColor4B color4 = { _displayedColor . r , _displayedColor . g , _displayedColor . b , _displayedOpacity } ;
// special opacity for premultiplied textures
2013-06-15 14:03:30 +08:00
if ( _opacityModifyRGB )
2013-02-27 15:30:49 +08:00
{
color4 . r * = _displayedOpacity / 255.0f ;
color4 . g * = _displayedOpacity / 255.0f ;
color4 . b * = _displayedOpacity / 255.0f ;
}
2012-04-19 14:35:52 +08:00
2013-06-15 14:03:30 +08:00
_quad . bl . colors = color4 ;
_quad . br . colors = color4 ;
_quad . tl . colors = color4 ;
_quad . tr . colors = color4 ;
2012-04-19 14:35:52 +08:00
// renders using batch node
2013-06-15 14:03:30 +08:00
if ( _batchNode )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
if ( _atlasIndex ! = CCSpriteIndexNotInitialized )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
_textureAtlas - > updateQuad ( & _quad , _atlasIndex ) ;
2012-04-19 14:35:52 +08:00
}
else
{
// no need to set it recursively
// update dirty_, don't update recursiveDirty_
setDirty ( true ) ;
}
}
// self render
// do nothing
}
void CCSprite : : setOpacity ( GLubyte opacity )
{
2013-02-27 15:30:49 +08:00
CCNodeRGBA : : setOpacity ( opacity ) ;
2012-04-19 14:35:52 +08:00
updateColor ( ) ;
}
2013-02-27 15:30:49 +08:00
void CCSprite : : setColor ( const ccColor3B & color3 )
2012-04-19 14:35:52 +08:00
{
2013-02-27 15:30:49 +08:00
CCNodeRGBA : : setColor ( color3 ) ;
2012-04-19 14:35:52 +08:00
2013-02-27 15:30:49 +08:00
updateColor ( ) ;
2012-04-19 14:35:52 +08:00
}
2013-02-27 15:30:49 +08:00
void CCSprite : : setOpacityModifyRGB ( bool modify )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
if ( _opacityModifyRGB ! = modify )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
_opacityModifyRGB = modify ;
2013-02-27 15:30:49 +08:00
updateColor ( ) ;
2012-04-19 14:35:52 +08:00
}
2013-02-27 15:30:49 +08:00
}
2012-04-19 14:35:52 +08:00
2013-02-27 15:30:49 +08:00
bool CCSprite : : isOpacityModifyRGB ( void )
{
2013-06-15 14:03:30 +08:00
return _opacityModifyRGB ;
2012-04-19 14:35:52 +08:00
}
2013-02-27 15:30:49 +08:00
void CCSprite : : updateDisplayedColor ( const ccColor3B & parentColor )
2012-04-19 14:35:52 +08:00
{
2013-02-27 15:30:49 +08:00
CCNodeRGBA : : updateDisplayedColor ( parentColor ) ;
updateColor ( ) ;
2012-04-19 14:35:52 +08:00
}
2013-02-27 15:30:49 +08:00
void CCSprite : : updateDisplayedOpacity ( GLubyte opacity )
2012-04-19 14:35:52 +08:00
{
2013-02-27 15:30:49 +08:00
CCNodeRGBA : : updateDisplayedOpacity ( opacity ) ;
updateColor ( ) ;
2012-04-19 14:35:52 +08:00
}
// Frames
void CCSprite : : setDisplayFrame ( CCSpriteFrame * pNewFrame )
{
2013-06-15 14:03:30 +08:00
_unflippedOffsetPositionFromCenter = pNewFrame - > getOffset ( ) ;
2012-04-19 14:35:52 +08:00
CCTexture2D * pNewTexture = pNewFrame - > getTexture ( ) ;
// update texture before updating texture rect
2013-06-15 14:03:30 +08:00
if ( pNewTexture ! = _texture )
2012-04-19 14:35:52 +08:00
{
setTexture ( pNewTexture ) ;
}
// update rect
2013-06-15 14:03:30 +08:00
_rectRotated = pNewFrame - > isRotated ( ) ;
setTextureRect ( pNewFrame - > getRect ( ) , _rectRotated , pNewFrame - > getOriginalSize ( ) ) ;
2012-04-19 14:35:52 +08:00
}
void CCSprite : : setDisplayFrameWithAnimationName ( const char * animationName , int frameIndex )
{
CCAssert ( animationName , " CCSprite#setDisplayFrameWithAnimationName. animationName must not be NULL " ) ;
CCAnimation * a = CCAnimationCache : : sharedAnimationCache ( ) - > animationByName ( animationName ) ;
CCAssert ( a , " CCSprite#setDisplayFrameWithAnimationName: Frame not found " ) ;
CCAnimationFrame * frame = ( CCAnimationFrame * ) a - > getFrames ( ) - > objectAtIndex ( frameIndex ) ;
CCAssert ( frame , " CCSprite#setDisplayFrame. Invalid frame " ) ;
setDisplayFrame ( frame - > getSpriteFrame ( ) ) ;
}
bool CCSprite : : isFrameDisplayed ( CCSpriteFrame * pFrame )
{
CCRect r = pFrame - > getRect ( ) ;
2013-06-15 14:03:30 +08:00
return ( r . equals ( _rect ) & &
pFrame - > getTexture ( ) - > getName ( ) = = _texture - > getName ( ) & &
pFrame - > getOffset ( ) . equals ( _unflippedOffsetPositionFromCenter ) ) ;
2012-04-19 14:35:52 +08:00
}
CCSpriteFrame * CCSprite : : displayFrame ( void )
{
2013-06-15 14:03:30 +08:00
return CCSpriteFrame : : createWithTexture ( _texture ,
CC_RECT_POINTS_TO_PIXELS ( _rect ) ,
_rectRotated ,
CC_POINT_POINTS_TO_PIXELS ( _unflippedOffsetPositionFromCenter ) ,
CC_SIZE_POINTS_TO_PIXELS ( _contentSize ) ) ;
2012-04-19 14:35:52 +08:00
}
CCSpriteBatchNode * CCSprite : : getBatchNode ( void )
{
2013-06-15 14:03:30 +08:00
return _batchNode ;
2012-04-19 14:35:52 +08:00
}
void CCSprite : : setBatchNode ( CCSpriteBatchNode * pobSpriteBatchNode )
{
2013-06-15 14:03:30 +08:00
_batchNode = pobSpriteBatchNode ; // weak reference
2012-04-19 14:35:52 +08:00
// self render
2013-06-15 14:03:30 +08:00
if ( ! _batchNode ) {
_atlasIndex = CCSpriteIndexNotInitialized ;
2012-04-19 14:35:52 +08:00
setTextureAtlas ( NULL ) ;
2013-06-15 14:03:30 +08:00
_recursiveDirty = false ;
2012-04-19 14:35:52 +08:00
setDirty ( false ) ;
2013-06-15 14:03:30 +08:00
float x1 = _offsetPosition . x ;
float y1 = _offsetPosition . y ;
float x2 = x1 + _rect . size . width ;
float y2 = y1 + _rect . size . height ;
_quad . bl . vertices = vertex3 ( x1 , y1 , 0 ) ;
_quad . br . vertices = vertex3 ( x2 , y1 , 0 ) ;
_quad . tl . vertices = vertex3 ( x1 , y2 , 0 ) ;
_quad . tr . vertices = vertex3 ( x2 , y2 , 0 ) ;
2012-04-19 14:35:52 +08:00
} else {
// using batch
2013-06-15 14:03:30 +08:00
_transformToBatch = CCAffineTransformIdentity ;
setTextureAtlas ( _batchNode - > getTextureAtlas ( ) ) ; // weak ref
2012-04-19 14:35:52 +08:00
}
}
// Texture protocol
void CCSprite : : updateBlendFunc ( void )
{
2013-06-15 14:03:30 +08:00
CCAssert ( ! _batchNode , " CCSprite: updateBlendFunc doesn't work when the sprite is rendered using a CCSpriteBatchNode " ) ;
2012-04-19 14:35:52 +08:00
// it is possible to have an untextured sprite
2013-06-15 14:03:30 +08:00
if ( ! _texture | | ! _texture - > hasPremultipliedAlpha ( ) )
2012-04-19 14:35:52 +08:00
{
2013-06-15 14:03:30 +08:00
_blendFunc . src = GL_SRC_ALPHA ;
_blendFunc . dst = GL_ONE_MINUS_SRC_ALPHA ;
2012-06-15 15:10:40 +08:00
setOpacityModifyRGB ( false ) ;
2012-04-19 14:35:52 +08:00
}
else
{
2013-06-15 14:03:30 +08:00
_blendFunc . src = CC_BLEND_SRC ;
_blendFunc . dst = CC_BLEND_DST ;
2012-06-15 15:10:40 +08:00
setOpacityModifyRGB ( true ) ;
2012-04-19 14:35:52 +08:00
}
}
void CCSprite : : setTexture ( CCTexture2D * texture )
{
2012-06-08 14:11:48 +08:00
// If batchnode, then texture id should be the same
2013-06-15 14:03:30 +08:00
CCAssert ( ! _batchNode | | texture - > getName ( ) = = _batchNode - > getTexture ( ) - > getName ( ) , " CCSprite: Batched sprites should use the same texture as the batchnode " ) ;
2012-04-19 14:35:52 +08:00
// accept texture==nil as argument
CCAssert ( ! texture | | dynamic_cast < CCTexture2D * > ( texture ) , " setTexture expects a CCTexture2D. Invalid argument " ) ;
2013-06-06 14:22:09 +08:00
// shader program
if ( texture )
{
setShaderProgram ( CCShaderCache : : sharedShaderCache ( ) - > programForKey ( kCCShader_PositionTextureColor ) ) ;
}
else
{
setShaderProgram ( CCShaderCache : : sharedShaderCache ( ) - > programForKey ( kCCShader_PositionColor ) ) ;
}
2013-06-15 14:03:30 +08:00
if ( ! _batchNode & & _texture ! = texture )
2012-04-19 14:35:52 +08:00
{
CC_SAFE_RETAIN ( texture ) ;
2013-06-15 14:03:30 +08:00
CC_SAFE_RELEASE ( _texture ) ;
_texture = texture ;
2012-04-19 14:35:52 +08:00
updateBlendFunc ( ) ;
}
}
CCTexture2D * CCSprite : : getTexture ( void )
{
2013-06-15 14:03:30 +08:00
return _texture ;
2012-04-19 14:35:52 +08:00
}
NS_CC_END