issue #1686:synchronize CCSprite.cpp

This commit is contained in:
minggo 2013-02-27 15:30:49 +08:00
parent f571d66ed7
commit 94076a5b9c
5 changed files with 120 additions and 109 deletions

View File

@ -1333,6 +1333,16 @@ void CCNodeRGBA::updateDisplayedOpacity(GLubyte parentOpacity)
}
}
bool CCNodeRGBA::isCascadeOpacityEnabled(void)
{
return _cascadeOpacityEnabled;
}
void CCNodeRGBA::setCascadeOpacityEnabled(bool cascadeOpacityEnabled)
{
_cascadeOpacityEnabled = cascadeOpacityEnabled;
}
const ccColor3B& CCNodeRGBA::getColor(void)
{
return _realColor;
@ -1379,4 +1389,14 @@ void CCNodeRGBA::updateDisplayedColor(const ccColor3B& parentColor)
}
}
bool CCNodeRGBA::isCascadeColorEnabled(void)
{
return _cascadeColorEnabled;
}
void CCNodeRGBA::setCascadeColorEnabled(bool cascadeColorEnabled)
{
_cascadeColorEnabled = cascadeColorEnabled;
}
NS_CC_END

View File

@ -1414,11 +1414,18 @@ public:
virtual GLubyte getDisplayedOpacity();
virtual void setOpacity(GLubyte opacity);
virtual void updateDisplayedOpacity(GLubyte parentOpacity);
virtual bool isCascadeOpacityEnabled(void);
virtual void setCascadeOpacityEnabled(bool cascadeOpacityEnabled);
virtual const ccColor3B& getColor(void);
virtual const ccColor3B& getDisplayedColor();
virtual void setColor(const ccColor3B& color);
virtual void updateDisplayedColor(const ccColor3B& parentColor);
virtual bool isCascadeColorEnabled(void);
virtual void setCascadeColorEnabled(bool cascadeColorEnabled);
virtual void setOpacityModifyRGB(bool bValue) = 0;
virtual bool isOpacityModifyRGB(void) = 0;
protected:
GLubyte _displayedOpacity;

View File

@ -106,16 +106,18 @@ public:
* whether or not color should be propagated to its children.
*/
virtual bool isCascadeColorEnabled(void) = 0;
virtual void setCascadeColorEnabled(bool cascadeColorEnabled) = 0;
/**
* recursive method that updates display color
*/
virtual void updateDisplayedColor(ccColor3B color) = 0;
virtual void updateDisplayedColor(const ccColor3B& color) = 0;
/**
* whether or not opacity should be propagated to its children.
*/
virtual bool isCascadeOpacityEnabled(void) = 0;
virtual void setCascadeOpacityEnabled(bool cascadeOpacityEnabled) = 0;
/**
* recursive method that updates the displayed opacity.

View File

@ -150,49 +150,54 @@ bool CCSprite::init(void)
// designated initializer
bool CCSprite::initWithTexture(CCTexture2D *pTexture, const CCRect& rect, bool rotated)
{
m_pobBatchNode = NULL;
// shader program
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
m_bRecursiveDirty = false;
setDirty(false);
m_bOpacityModifyRGB = true;
m_nOpacity = 255;
m_sColor = m_sColorUnmodified = ccWHITE;
m_sBlendFunc.src = CC_BLEND_SRC;
m_sBlendFunc.dst = CC_BLEND_DST;
m_bFlipX = m_bFlipY = false;
// default transform anchor: center
setAnchorPoint(ccp(0.5f, 0.5f));
// zwoptex default values
m_obOffsetPosition = CCPointZero;
m_bHasChildren = false;
// clean the Quad
memset(&m_sQuad, 0, sizeof(m_sQuad));
// Atlas: Color
ccColor4B tmpColor = { 255, 255, 255, 255 };
m_sQuad.bl.colors = tmpColor;
m_sQuad.br.colors = tmpColor;
m_sQuad.tl.colors = tmpColor;
m_sQuad.tr.colors = tmpColor;
// 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;
if (CCNodeRGBA::init())
{
m_pobBatchNode = NULL;
// shader program
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
m_bRecursiveDirty = false;
setDirty(false);
m_bOpacityModifyRGB = true;
m_sBlendFunc.src = CC_BLEND_SRC;
m_sBlendFunc.dst = CC_BLEND_DST;
m_bFlipX = m_bFlipY = false;
// default transform anchor: center
setAnchorPoint(ccp(0.5f, 0.5f));
// zwoptex default values
m_obOffsetPosition = CCPointZero;
m_bHasChildren = false;
// clean the Quad
memset(&m_sQuad, 0, sizeof(m_sQuad));
// Atlas: Color
ccColor4B tmpColor = { 255, 255, 255, 255 };
m_sQuad.bl.colors = tmpColor;
m_sQuad.br.colors = tmpColor;
m_sQuad.tl.colors = tmpColor;
m_sQuad.tr.colors = tmpColor;
// 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;
}
}
bool CCSprite::initWithTexture(CCTexture2D *pTexture, const CCRect& rect)
@ -887,7 +892,15 @@ bool CCSprite::isFlipY(void)
void CCSprite::updateColor(void)
{
ccColor4B color4 = { m_sColor.r, m_sColor.g, m_sColor.b, m_nOpacity };
ccColor4B color4 = { _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity };
// special opacity for premultiplied textures
if (m_bOpacityModifyRGB)
{
color4.r *= _displayedOpacity/255.0f;
color4.g *= _displayedOpacity/255.0f;
color4.b *= _displayedOpacity/255.0f;
}
m_sQuad.bl.colors = color4;
m_sQuad.br.colors = color4;
@ -915,46 +928,25 @@ void CCSprite::updateColor(void)
void CCSprite::setOpacity(GLubyte opacity)
{
m_nOpacity = opacity;
// special opacity for premultiplied textures
if (m_bOpacityModifyRGB)
{
setColor(m_sColorUnmodified);
}
CCNodeRGBA::setOpacity(opacity);
updateColor();
}
const ccColor3B& CCSprite::getColor(void)
{
if (m_bOpacityModifyRGB)
{
return m_sColorUnmodified;
}
return m_sColor;
}
void CCSprite::setColor(const ccColor3B& color3)
{
m_sColor = m_sColorUnmodified = color3;
if (m_bOpacityModifyRGB)
{
m_sColor.r = color3.r * m_nOpacity/255.0f;
m_sColor.g = color3.g * m_nOpacity/255.0f;
m_sColor.b = color3.b * m_nOpacity/255.0f;
}
CCNodeRGBA::setColor(color3);
updateColor();
}
void CCSprite::setOpacityModifyRGB(bool bValue)
void CCSprite::setOpacityModifyRGB(bool modify)
{
ccColor3B oldColor = m_sColor;
m_bOpacityModifyRGB = bValue;
m_sColor = oldColor;
if (m_bOpacityModifyRGB != modify)
{
m_bOpacityModifyRGB = modify;
updateColor();
}
}
bool CCSprite::isOpacityModifyRGB(void)
@ -962,6 +954,20 @@ bool CCSprite::isOpacityModifyRGB(void)
return m_bOpacityModifyRGB;
}
void CCSprite::updateDisplayedColor(const ccColor3B& parentColor)
{
CCNodeRGBA::updateDisplayedColor(parentColor);
updateColor();
}
void CCSprite::updateDisplayedOpacity(GLubyte opacity)
{
CCNodeRGBA::updateDisplayedOpacity(opacity);
updateColor();
}
// Frames
void CCSprite::setDisplayFrame(CCSpriteFrame *pNewFrame)

View File

@ -76,7 +76,7 @@ struct transformValues_;
*
* The default anchorPoint in CCSprite is (0.5, 0.5).
*/
class CC_DLL CCSprite : public CCNode, public CCTextureProtocol, public CCRGBAProtocol
class CC_DLL CCSprite : public CCNodeRGBA, public CCTextureProtocol
{
public:
/// @{
@ -253,33 +253,6 @@ public:
/// @} end of initializers
/// @{
/// @name Functions inherited from CCRGBAProtocol
inline virtual GLubyte getOpacity() { return m_nOpacity; }
/**
* Changes the opacity.
*
* @warning If the sprite's texture has premultiplied alpha then, the R, G and B channels will be modified.
*
* @param value Goes from 0 to 255, where 255 means fully opaque and 0 means fully transparent.
*/
virtual void setOpacity(GLubyte value);
virtual const ccColor3B& getColor();
virtual void setColor(const ccColor3B& value);
/**
* Changes the premultipliedAlphaOpacity property.
*
* Textures with premultiplied alpha will have this property by default on true.
* Otherwise the default value is false.
*
* @param bValue flase then opacity will be applied as: glColor(R,G,B,opacity);
* true then opacity will be applied as: glColor(opacity, opacity, opacity, opacity);
*/
virtual void setOpacityModifyRGB(bool bValue);
virtual bool isOpacityModifyRGB(void);
/// @} end of CCRGBAProtocol Inheritance
/// @{
/// @name Functions inherited from CCTextureProtocol
virtual void setTexture(CCTexture2D *texture);
@ -312,6 +285,16 @@ public:
virtual void setVisible(bool bVisible);
virtual void draw(void);
/// @}
/// @{
/// @name Functions inherited from CCNodeRGBA
virtual void setColor(const ccColor3B& color3);
virtual void updateDisplayedColor(const ccColor3B& parentColor);
virtual void setOpacity(GLubyte opacity);
virtual void setOpacityModifyRGB(bool modify);
virtual bool isOpacityModifyRGB(void);
virtual void updateDisplayedOpacity(GLubyte parentOpacity);
/// @}
/// @{
@ -540,18 +523,11 @@ protected:
ccV3F_C4B_T2F_Quad m_sQuad;
// opacity and RGB protocol
ccColor3B m_sColorUnmodified;
bool m_bOpacityModifyRGB;
// image is flipped
bool m_bFlipX; /// Whether the sprite is flipped horizaontally or not.
bool m_bFlipY; /// Whether the sprite is flipped vertically or not.
// opacity
GLubyte m_nOpacity; /// Goes from 0-255. 0 means fully tranparent and 255 means fully opaque.
// Color: conforms with CCRGBAProtocol protocol
ccColor3B m_sColor;
};