From 56037a49394043c1c6f3fa05e933b112503159bc Mon Sep 17 00:00:00 2001 From: Yongqin Liu Date: Tue, 28 May 2013 10:23:47 +0800 Subject: [PATCH 1/5] CCTextureAtlas::updateQuad modify for the memcpy hot spot change the place that call CCTextureAtlas::updateQuad method to update the quads with pointer reference directly. Signed-off-by: Yongqin Liu --- cocos2dx/label_nodes/CCLabelAtlas.cpp | 61 +++++++-------- cocos2dx/sprite_nodes/CCSprite.cpp | 75 ++++++++++++++++--- cocos2dx/sprite_nodes/CCSprite.h | 21 ++++++ cocos2dx/textures/CCTextureAtlas.h | 6 ++ .../tilemap_parallax_nodes/CCTileMapAtlas.cpp | 56 +++++++------- 5 files changed, 153 insertions(+), 66 deletions(-) diff --git a/cocos2dx/label_nodes/CCLabelAtlas.cpp b/cocos2dx/label_nodes/CCLabelAtlas.cpp index c30901ae28..69cc1fff91 100644 --- a/cocos2dx/label_nodes/CCLabelAtlas.cpp +++ b/cocos2dx/label_nodes/CCLabelAtlas.cpp @@ -116,8 +116,6 @@ void CCLabelAtlas::updateAtlasValues() { unsigned int n = m_sString.length(); - ccV3F_C4B_T2F_Quad quad; - const unsigned char *s = (unsigned char*)m_sString.c_str(); CCTexture2D *texture = m_pTextureAtlas->getTexture(); @@ -125,13 +123,13 @@ void CCLabelAtlas::updateAtlasValues() float textureHigh = (float) texture->getPixelsHigh(); float itemWidthInPixels = m_uItemWidth * CC_CONTENT_SCALE_FACTOR(); float itemHeightInPixels = m_uItemHeight * CC_CONTENT_SCALE_FACTOR(); - if (m_bIgnoreContentScaleFactor) { itemWidthInPixels = m_uItemWidth; itemHeightInPixels = m_uItemHeight; } - + + ccV3F_C4B_T2F_Quad* quads = m_pTextureAtlas->getQuads(); for(unsigned int i = 0; i < n; i++) { unsigned char a = s[i] - m_uMapStartChar; float row = (float) (a % m_uItemsPerRow); @@ -150,34 +148,37 @@ void CCLabelAtlas::updateAtlasValues() float bottom = top + itemHeightInPixels / textureHigh; #endif // ! CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL - quad.tl.texCoords.u = left; - quad.tl.texCoords.v = top; - quad.tr.texCoords.u = right; - quad.tr.texCoords.v = top; - quad.bl.texCoords.u = left; - quad.bl.texCoords.v = bottom; - quad.br.texCoords.u = right; - quad.br.texCoords.v = bottom; + quads[i].tl.texCoords.u = left; + quads[i].tl.texCoords.v = top; + quads[i].tr.texCoords.u = right; + quads[i].tr.texCoords.v = top; + quads[i].bl.texCoords.u = left; + quads[i].bl.texCoords.v = bottom; + quads[i].br.texCoords.u = right; + quads[i].br.texCoords.v = bottom; - quad.bl.vertices.x = (float) (i * m_uItemWidth); - quad.bl.vertices.y = 0; - quad.bl.vertices.z = 0.0f; - quad.br.vertices.x = (float)(i * m_uItemWidth + m_uItemWidth); - quad.br.vertices.y = 0; - quad.br.vertices.z = 0.0f; - quad.tl.vertices.x = (float)(i * m_uItemWidth); - quad.tl.vertices.y = (float)(m_uItemHeight); - quad.tl.vertices.z = 0.0f; - quad.tr.vertices.x = (float)(i * m_uItemWidth + m_uItemWidth); - quad.tr.vertices.y = (float)(m_uItemHeight); - quad.tr.vertices.z = 0.0f; - + quads[i].bl.vertices.x = (float) (i * m_uItemWidth); + quads[i].bl.vertices.y = 0; + quads[i].bl.vertices.z = 0.0f; + quads[i].br.vertices.x = (float)(i * m_uItemWidth + m_uItemWidth); + quads[i].br.vertices.y = 0; + quads[i].br.vertices.z = 0.0f; + quads[i].tl.vertices.x = (float)(i * m_uItemWidth); + quads[i].tl.vertices.y = (float)(m_uItemHeight); + quads[i].tl.vertices.z = 0.0f; + quads[i].tr.vertices.x = (float)(i * m_uItemWidth + m_uItemWidth); + quads[i].tr.vertices.y = (float)(m_uItemHeight); + quads[i].tr.vertices.z = 0.0f; ccColor4B c = { _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity }; - quad.tl.colors = c; - quad.tr.colors = c; - quad.bl.colors = c; - quad.br.colors = c; - m_pTextureAtlas->updateQuad(&quad, i); + quads[i].tl.colors = c; + quads[i].tr.colors = c; + quads[i].bl.colors = c; + quads[i].br.colors = c; + } + m_pTextureAtlas->setDirty(true); + unsigned int totalQuads = m_pTextureAtlas->getTotalQuads(); + if (n > totalQuads) { + m_pTextureAtlas->increaseTotalQuadsWith(n - totalQuads); } } diff --git a/cocos2dx/sprite_nodes/CCSprite.cpp b/cocos2dx/sprite_nodes/CCSprite.cpp index b0dea7ca5a..7069a14d9c 100644 --- a/cocos2dx/sprite_nodes/CCSprite.cpp +++ b/cocos2dx/sprite_nodes/CCSprite.cpp @@ -158,6 +158,11 @@ bool CCSprite::initWithTexture(CCTexture2D *pTexture, const CCRect& rect, bool r m_bRecursiveDirty = false; setDirty(false); + + //set them to dirty here to make it updated for the first time + setQuadTexCoordsDirty(true); + setQuadColorsDirty(true); + setQuadVerticlesDirty(true); m_bOpacityModifyRGB = true; @@ -354,6 +359,7 @@ void CCSprite::setTextureRect(const CCRect& rect, bool rotated, const CCSize& un m_sQuad.br.vertices = vertex3(x2, y1, 0); m_sQuad.tl.vertices = vertex3(x1, y2, 0); m_sQuad.tr.vertices = vertex3(x2, y2, 0); + setQuadVerticlesDirty(true); } } @@ -444,19 +450,37 @@ void CCSprite::setTextureCoords(CCRect rect) m_sQuad.tr.texCoords.u = right; m_sQuad.tr.texCoords.v = top; } + setQuadTexCoordsDirty(true); } void CCSprite::updateTransform(void) { + ccV3F_C4B_T2F_Quad* quad = NULL; + unsigned int totalQuads = 0; + bool useRefForUpdateQuad = false; + bool bDirtyQuad = isQuadColorsDirty() || isQuadTexCoordsDirty(); + if (m_pobTextureAtlas){ + totalQuads = m_pobTextureAtlas->getTotalQuads(); + useRefForUpdateQuad = (totalQuads >= m_uAtlasIndex + 1) && (!bDirtyQuad); + } CCAssert(m_pobBatchNode, "updateTransform is only valid when CCSprite is being rendered using an CCSpriteBatchNode"); // recalculate matrix only if it is dirty if( isDirty() ) { + if (m_pobTextureAtlas) + { + if (useRefForUpdateQuad){ + quad = &((m_pobTextureAtlas->getQuads())[m_uAtlasIndex]); + } + } // If it is not visible, or one of its ancestors is not visible, then do nothing: if( !m_bVisible || ( m_pParent && m_pParent != m_pobBatchNode && ((CCSprite*)m_pParent)->m_bShouldBeHidden) ) { m_sQuad.br.vertices = m_sQuad.tl.vertices = m_sQuad.tr.vertices = m_sQuad.bl.vertices = vertex3(0,0,0); + if ( useRefForUpdateQuad){ + quad->br.vertices = quad->tl.vertices = quad->tr.vertices = quad->bl.vertices = vertex3(0,0,0); + } m_bShouldBeHidden = true; } else @@ -507,14 +531,24 @@ void CCSprite::updateTransform(void) m_sQuad.br.vertices = vertex3( RENDER_IN_SUBPIXEL(bx), RENDER_IN_SUBPIXEL(by), m_fVertexZ ); m_sQuad.tl.vertices = vertex3( RENDER_IN_SUBPIXEL(dx), RENDER_IN_SUBPIXEL(dy), m_fVertexZ ); m_sQuad.tr.vertices = vertex3( RENDER_IN_SUBPIXEL(cx), RENDER_IN_SUBPIXEL(cy), m_fVertexZ ); + if ( useRefForUpdateQuad){ + quad->br.vertices = quad->tl.vertices = quad->tr.vertices = quad->bl.vertices = vertex3( RENDER_IN_SUBPIXEL(cx), RENDER_IN_SUBPIXEL(cy), m_fVertexZ ); + } } // MARMALADE CHANGE: ADDED CHECK FOR NULL, TO PERMIT SPRITES WITH NO BATCH NODE / TEXTURE ATLAS if (m_pobTextureAtlas) - { - m_pobTextureAtlas->updateQuad(&m_sQuad, m_uAtlasIndex); + { + if ( useRefForUpdateQuad){ + m_pobTextureAtlas->setDirty(true); + }else{ + m_pobTextureAtlas->updateQuad(&m_sQuad, m_uAtlasIndex); + setQuadColorsDirty(false); + setQuadTexCoordsDirty(false); + } + setQuadVerticlesDirty(false); } - + m_bRecursiveDirty = false; setDirty(false); } @@ -897,14 +931,22 @@ bool CCSprite::isFlipY(void) void CCSprite::updateColor(void) { + ccV3F_C4B_T2F_Quad* quad = NULL; + bool bDirtyQuad = isQuadVerticlesDirty() || isQuadTexCoordsDirty(); + unsigned int totalQuads = 0; + bool useRefForUpdateQuad = false; + if (m_uAtlasIndex != CCSpriteIndexNotInitialized){ + totalQuads = m_pobTextureAtlas->getTotalQuads(); + useRefForUpdateQuad = ( totalQuads >= m_uAtlasIndex + 1) && (!bDirtyQuad); + } ccColor4B color4 = { _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity }; - + // special opacity for premultiplied textures - if (m_bOpacityModifyRGB) + if (m_bOpacityModifyRGB) { - color4.r *= _displayedOpacity/255.0f; - color4.g *= _displayedOpacity/255.0f; - color4.b *= _displayedOpacity/255.0f; + color4.r *= _displayedOpacity/255.0f; + color4.g *= _displayedOpacity/255.0f; + color4.b *= _displayedOpacity/255.0f; } m_sQuad.bl.colors = color4; @@ -912,12 +954,25 @@ void CCSprite::updateColor(void) m_sQuad.tl.colors = color4; m_sQuad.tr.colors = color4; + setQuadColorsDirty(true); // renders using batch node if (m_pobBatchNode) { if (m_uAtlasIndex != CCSpriteIndexNotInitialized) { - m_pobTextureAtlas->updateQuad(&m_sQuad, m_uAtlasIndex); + if(useRefForUpdateQuad){ + quad = &((m_pobTextureAtlas->getQuads())[m_uAtlasIndex]); + quad->bl.colors = color4; + quad->br.colors = color4; + quad->tl.colors = color4; + quad->tr.colors = color4; + m_pobTextureAtlas->setDirty(true); + }else{ + m_pobTextureAtlas->updateQuad(&m_sQuad, m_uAtlasIndex); + setQuadTexCoordsDirty(false); + setQuadVerticlesDirty(false); + } + setQuadColorsDirty(false); } else { @@ -1048,7 +1103,7 @@ void CCSprite::setBatchNode(CCSpriteBatchNode *pobSpriteBatchNode) m_sQuad.br.vertices = vertex3( x2, y1, 0 ); m_sQuad.tl.vertices = vertex3( x1, y2, 0 ); m_sQuad.tr.vertices = vertex3( x2, y2, 0 ); - + setQuadVerticlesDirty(true); } else { // using batch diff --git a/cocos2dx/sprite_nodes/CCSprite.h b/cocos2dx/sprite_nodes/CCSprite.h index 46bfbcf78f..1edd05b91c 100644 --- a/cocos2dx/sprite_nodes/CCSprite.h +++ b/cocos2dx/sprite_nodes/CCSprite.h @@ -494,6 +494,22 @@ protected: virtual void setReorderChildDirtyRecursively(void); virtual void setDirtyRecursively(bool bValue); + /** whether or not the colors attribute of Quad has been updated */ + inline bool isQuadColorsDirty(void) { return m_bQuadColorsDirty; } + /** specify if the colors attribute of Quad has been updated */ + inline void setQuadColorsDirty(bool bDirty) { m_bQuadColorsDirty = bDirty; } + + /** whether or not the verticles attribute of Quad has been updated */ + inline bool isQuadVerticlesDirty(void) { return m_bQuadVerticlesDirty; } + /** specify if the verticles attribute of Quad has been updated */ + inline void setQuadVerticlesDirty(bool bDirty) { m_bQuadVerticlesDirty = bDirty; } + + /** whether or not the texCoords attribute of Quad has been updated */ + inline bool isQuadTexCoordsDirty(void) { return m_bQuadTexCoordsDirty; } + /** specify if the texCoords attribute of Quad has been updated */ + inline void setQuadTexCoordsDirty(bool bDirty) { m_bQuadTexCoordsDirty = bDirty; } + + // // Data used when the sprite is rendered using a CCSpriteSheet // @@ -528,6 +544,11 @@ protected: // vertex coords, texture coords and color info ccV3F_C4B_T2F_Quad m_sQuad; + //flag to indicate if there is a change on the elements + bool m_bQuadColorsDirty; + bool m_bQuadVerticlesDirty; + bool m_bQuadTexCoordsDirty; + // opacity and RGB protocol bool m_bOpacityModifyRGB; diff --git a/cocos2dx/textures/CCTextureAtlas.h b/cocos2dx/textures/CCTextureAtlas.h index 6fc20ecc80..49ab75996c 100644 --- a/cocos2dx/textures/CCTextureAtlas.h +++ b/cocos2dx/textures/CCTextureAtlas.h @@ -202,6 +202,12 @@ public: /** listen the event that coming to foreground on Android */ void listenBackToForeground(CCObject *obj); + + /** whether or not the array buffer of the VBO needs to be updated*/ + inline bool isDirty(void) { return m_bDirty; } + /** specify if the array buffer of the VBO needs to be updated */ + inline void setDirty(bool bDirty) { m_bDirty = bDirty; } + private: void setupIndices(); void mapBuffers(); diff --git a/cocos2dx/tilemap_parallax_nodes/CCTileMapAtlas.cpp b/cocos2dx/tilemap_parallax_nodes/CCTileMapAtlas.cpp index 16578da1d9..286b46982c 100644 --- a/cocos2dx/tilemap_parallax_nodes/CCTileMapAtlas.cpp +++ b/cocos2dx/tilemap_parallax_nodes/CCTileMapAtlas.cpp @@ -173,7 +173,7 @@ ccColor3B CCTileMapAtlas::tileAt(const CCPoint& position) void CCTileMapAtlas::updateAtlasValueAt(const CCPoint& pos, const ccColor3B& value, unsigned int index) { - ccV3F_C4B_T2F_Quad quad; + ccV3F_C4B_T2F_Quad* quad = &((m_pTextureAtlas->getQuads())[index]); int x = pos.x; int y = pos.y; @@ -198,35 +198,39 @@ void CCTileMapAtlas::updateAtlasValueAt(const CCPoint& pos, const ccColor3B& val float bottom = top + itemHeightInPixels / textureHigh; #endif - quad.tl.texCoords.u = left; - quad.tl.texCoords.v = top; - quad.tr.texCoords.u = right; - quad.tr.texCoords.v = top; - 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; + quad->bl.texCoords.u = left; + quad->bl.texCoords.v = bottom; + quad->br.texCoords.u = right; + quad->br.texCoords.v = bottom; - quad.bl.vertices.x = (float) (x * m_uItemWidth); - quad.bl.vertices.y = (float) (y * m_uItemHeight); - quad.bl.vertices.z = 0.0f; - quad.br.vertices.x = (float)(x * m_uItemWidth + m_uItemWidth); - quad.br.vertices.y = (float)(y * m_uItemHeight); - quad.br.vertices.z = 0.0f; - quad.tl.vertices.x = (float)(x * m_uItemWidth); - quad.tl.vertices.y = (float)(y * m_uItemHeight + m_uItemHeight); - quad.tl.vertices.z = 0.0f; - quad.tr.vertices.x = (float)(x * m_uItemWidth + m_uItemWidth); - quad.tr.vertices.y = (float)(y * m_uItemHeight + m_uItemHeight); - quad.tr.vertices.z = 0.0f; + quad->bl.vertices.x = (float) (x * m_uItemWidth); + quad->bl.vertices.y = (float) (y * m_uItemHeight); + quad->bl.vertices.z = 0.0f; + quad->br.vertices.x = (float)(x * m_uItemWidth + m_uItemWidth); + quad->br.vertices.y = (float)(y * m_uItemHeight); + quad->br.vertices.z = 0.0f; + quad->tl.vertices.x = (float)(x * m_uItemWidth); + quad->tl.vertices.y = (float)(y * m_uItemHeight + m_uItemHeight); + quad->tl.vertices.z = 0.0f; + quad->tr.vertices.x = (float)(x * m_uItemWidth + m_uItemWidth); + quad->tr.vertices.y = (float)(y * m_uItemHeight + m_uItemHeight); + quad->tr.vertices.z = 0.0f; ccColor4B color = { _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity }; - quad.tr.colors = color; - quad.tl.colors = color; - quad.br.colors = color; - quad.bl.colors = color; + quad->tr.colors = color; + quad->tl.colors = color; + quad->br.colors = color; + quad->bl.colors = color; - m_pTextureAtlas->updateQuad(&quad, index); + m_pTextureAtlas->setDirty(true); + unsigned int totalQuads = m_pTextureAtlas->getTotalQuads(); + if (index + 1 > totalQuads) { + m_pTextureAtlas->increaseTotalQuadsWith(index + 1 - totalQuads); + } } void CCTileMapAtlas::updateAtlasValues() From f6b5208be5f35e1041b19280d64ec70ce76d5fc0 Mon Sep 17 00:00:00 2001 From: Yongqin Liu Date: Wed, 29 May 2013 10:45:47 +0800 Subject: [PATCH 2/5] CCLabelAtlas.cpp: add check for n when call setDirty in CCLabelAtlas::updateAtlasValues, there is the case that n equal to 0, so we need to check it before when call m_pTextureAtlas->setDirty(true) and do other process. Signed-off-by: Yongqin Liu --- cocos2dx/label_nodes/CCLabelAtlas.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cocos2dx/label_nodes/CCLabelAtlas.cpp b/cocos2dx/label_nodes/CCLabelAtlas.cpp index 69cc1fff91..89c0bf80c3 100644 --- a/cocos2dx/label_nodes/CCLabelAtlas.cpp +++ b/cocos2dx/label_nodes/CCLabelAtlas.cpp @@ -175,10 +175,12 @@ void CCLabelAtlas::updateAtlasValues() quads[i].bl.colors = c; quads[i].br.colors = c; } - m_pTextureAtlas->setDirty(true); - unsigned int totalQuads = m_pTextureAtlas->getTotalQuads(); - if (n > totalQuads) { - m_pTextureAtlas->increaseTotalQuadsWith(n - totalQuads); + if (n > 0 ){ + m_pTextureAtlas->setDirty(true); + unsigned int totalQuads = m_pTextureAtlas->getTotalQuads(); + if (n > totalQuads) { + m_pTextureAtlas->increaseTotalQuadsWith(n - totalQuads); + } } } From aea9e04fbfb4a633b22645c9176e87d34169b0c3 Mon Sep 17 00:00:00 2001 From: Yongqin Liu Date: Wed, 29 May 2013 19:18:46 +0800 Subject: [PATCH 3/5] Add out of bound check for the index before do the update Signed-off-by: Yongqin Liu --- cocos2dx/label_nodes/CCLabelAtlas.cpp | 3 +++ cocos2dx/sprite_nodes/CCSprite.cpp | 9 ++++++++- cocos2dx/tilemap_parallax_nodes/CCTileMapAtlas.cpp | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/cocos2dx/label_nodes/CCLabelAtlas.cpp b/cocos2dx/label_nodes/CCLabelAtlas.cpp index 89c0bf80c3..5508cd3254 100644 --- a/cocos2dx/label_nodes/CCLabelAtlas.cpp +++ b/cocos2dx/label_nodes/CCLabelAtlas.cpp @@ -129,8 +129,11 @@ void CCLabelAtlas::updateAtlasValues() itemHeightInPixels = m_uItemHeight; } + unsigned int capacity = m_pTextureAtlas->getCapacity(); + CCAssert( n <= capacity, "updateAtlasValues: Invalid String length"); ccV3F_C4B_T2F_Quad* quads = m_pTextureAtlas->getQuads(); for(unsigned int i = 0; i < n; i++) { + unsigned char a = s[i] - m_uMapStartChar; float row = (float) (a % m_uItemsPerRow); float col = (float) (a / m_uItemsPerRow); diff --git a/cocos2dx/sprite_nodes/CCSprite.cpp b/cocos2dx/sprite_nodes/CCSprite.cpp index 7069a14d9c..0c7cd2caef 100644 --- a/cocos2dx/sprite_nodes/CCSprite.cpp +++ b/cocos2dx/sprite_nodes/CCSprite.cpp @@ -457,9 +457,12 @@ void CCSprite::updateTransform(void) { ccV3F_C4B_T2F_Quad* quad = NULL; unsigned int totalQuads = 0; + unsigned int capacity = 0; bool useRefForUpdateQuad = false; bool bDirtyQuad = isQuadColorsDirty() || isQuadTexCoordsDirty(); if (m_pobTextureAtlas){ + capacity = m_pobTextureAtlas->getCapacity(); + CCAssert( m_uAtlasIndex <= capacity, "updateTransform: Invalid m_uAtlasIndex"); totalQuads = m_pobTextureAtlas->getTotalQuads(); useRefForUpdateQuad = (totalQuads >= m_uAtlasIndex + 1) && (!bDirtyQuad); } @@ -934,8 +937,12 @@ void CCSprite::updateColor(void) ccV3F_C4B_T2F_Quad* quad = NULL; bool bDirtyQuad = isQuadVerticlesDirty() || isQuadTexCoordsDirty(); unsigned int totalQuads = 0; + unsigned int capacity = 0; bool useRefForUpdateQuad = false; - if (m_uAtlasIndex != CCSpriteIndexNotInitialized){ + if (m_pobBatchNode && (m_uAtlasIndex != CCSpriteIndexNotInitialized)){ + capacity = m_pobTextureAtlas->getCapacity(); + CCAssert( m_uAtlasIndex <= capacity, "updateColor: Invalid m_uAtlasIndex"); + totalQuads = m_pobTextureAtlas->getTotalQuads(); useRefForUpdateQuad = ( totalQuads >= m_uAtlasIndex + 1) && (!bDirtyQuad); } diff --git a/cocos2dx/tilemap_parallax_nodes/CCTileMapAtlas.cpp b/cocos2dx/tilemap_parallax_nodes/CCTileMapAtlas.cpp index 286b46982c..e5ccbeba0d 100644 --- a/cocos2dx/tilemap_parallax_nodes/CCTileMapAtlas.cpp +++ b/cocos2dx/tilemap_parallax_nodes/CCTileMapAtlas.cpp @@ -173,6 +173,9 @@ ccColor3B CCTileMapAtlas::tileAt(const CCPoint& position) void CCTileMapAtlas::updateAtlasValueAt(const CCPoint& pos, const ccColor3B& value, unsigned int index) { + unsigned int capacity = m_pTextureAtlas->getCapacity(); + CCAssert( index >= 0 && index < capacity, "updateAtlasValueAt: Invalid index"); + ccV3F_C4B_T2F_Quad* quad = &((m_pTextureAtlas->getQuads())[index]); int x = pos.x; From d0f75ba92a53dd1aeb8d82699a77ff22ea1018d7 Mon Sep 17 00:00:00 2001 From: Yongqin Liu Date: Thu, 30 May 2013 09:54:09 +0800 Subject: [PATCH 4/5] Fix the bug for the bound check Signed-off-by: Yongqin Liu --- cocos2dx/sprite_nodes/CCSprite.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2dx/sprite_nodes/CCSprite.cpp b/cocos2dx/sprite_nodes/CCSprite.cpp index 0c7cd2caef..63f45d0c3a 100644 --- a/cocos2dx/sprite_nodes/CCSprite.cpp +++ b/cocos2dx/sprite_nodes/CCSprite.cpp @@ -462,7 +462,7 @@ void CCSprite::updateTransform(void) bool bDirtyQuad = isQuadColorsDirty() || isQuadTexCoordsDirty(); if (m_pobTextureAtlas){ capacity = m_pobTextureAtlas->getCapacity(); - CCAssert( m_uAtlasIndex <= capacity, "updateTransform: Invalid m_uAtlasIndex"); + CCAssert(m_uAtlasIndex >= 0 && m_uAtlasIndex < capacity, "updateTransform: Invalid m_uAtlasIndex"); totalQuads = m_pobTextureAtlas->getTotalQuads(); useRefForUpdateQuad = (totalQuads >= m_uAtlasIndex + 1) && (!bDirtyQuad); } @@ -941,7 +941,7 @@ void CCSprite::updateColor(void) bool useRefForUpdateQuad = false; if (m_pobBatchNode && (m_uAtlasIndex != CCSpriteIndexNotInitialized)){ capacity = m_pobTextureAtlas->getCapacity(); - CCAssert( m_uAtlasIndex <= capacity, "updateColor: Invalid m_uAtlasIndex"); + CCAssert( m_uAtlasIndex >= 0 && m_uAtlasIndex < capacity, "updateColor: Invalid m_uAtlasIndex"); totalQuads = m_pobTextureAtlas->getTotalQuads(); useRefForUpdateQuad = ( totalQuads >= m_uAtlasIndex + 1) && (!bDirtyQuad); From 1091e71bda523fd61daf8931ebcb398ab58cb638 Mon Sep 17 00:00:00 2001 From: Yongqin Liu Date: Thu, 30 May 2013 10:21:48 +0800 Subject: [PATCH 5/5] Revert the CCSprite.cpp and CCSprite.h to first merge other changes Signed-off-by: Yongqin Liu --- cocos2dx/sprite_nodes/CCSprite.cpp | 82 ++++-------------------------- cocos2dx/sprite_nodes/CCSprite.h | 21 -------- 2 files changed, 10 insertions(+), 93 deletions(-) diff --git a/cocos2dx/sprite_nodes/CCSprite.cpp b/cocos2dx/sprite_nodes/CCSprite.cpp index 63f45d0c3a..b0dea7ca5a 100644 --- a/cocos2dx/sprite_nodes/CCSprite.cpp +++ b/cocos2dx/sprite_nodes/CCSprite.cpp @@ -158,11 +158,6 @@ bool CCSprite::initWithTexture(CCTexture2D *pTexture, const CCRect& rect, bool r m_bRecursiveDirty = false; setDirty(false); - - //set them to dirty here to make it updated for the first time - setQuadTexCoordsDirty(true); - setQuadColorsDirty(true); - setQuadVerticlesDirty(true); m_bOpacityModifyRGB = true; @@ -359,7 +354,6 @@ void CCSprite::setTextureRect(const CCRect& rect, bool rotated, const CCSize& un m_sQuad.br.vertices = vertex3(x2, y1, 0); m_sQuad.tl.vertices = vertex3(x1, y2, 0); m_sQuad.tr.vertices = vertex3(x2, y2, 0); - setQuadVerticlesDirty(true); } } @@ -450,40 +444,19 @@ void CCSprite::setTextureCoords(CCRect rect) m_sQuad.tr.texCoords.u = right; m_sQuad.tr.texCoords.v = top; } - setQuadTexCoordsDirty(true); } void CCSprite::updateTransform(void) { - ccV3F_C4B_T2F_Quad* quad = NULL; - unsigned int totalQuads = 0; - unsigned int capacity = 0; - bool useRefForUpdateQuad = false; - bool bDirtyQuad = isQuadColorsDirty() || isQuadTexCoordsDirty(); - if (m_pobTextureAtlas){ - capacity = m_pobTextureAtlas->getCapacity(); - CCAssert(m_uAtlasIndex >= 0 && m_uAtlasIndex < capacity, "updateTransform: Invalid m_uAtlasIndex"); - totalQuads = m_pobTextureAtlas->getTotalQuads(); - useRefForUpdateQuad = (totalQuads >= m_uAtlasIndex + 1) && (!bDirtyQuad); - } CCAssert(m_pobBatchNode, "updateTransform is only valid when CCSprite is being rendered using an CCSpriteBatchNode"); // recalculate matrix only if it is dirty if( isDirty() ) { - if (m_pobTextureAtlas) - { - if (useRefForUpdateQuad){ - quad = &((m_pobTextureAtlas->getQuads())[m_uAtlasIndex]); - } - } // If it is not visible, or one of its ancestors is not visible, then do nothing: if( !m_bVisible || ( m_pParent && m_pParent != m_pobBatchNode && ((CCSprite*)m_pParent)->m_bShouldBeHidden) ) { m_sQuad.br.vertices = m_sQuad.tl.vertices = m_sQuad.tr.vertices = m_sQuad.bl.vertices = vertex3(0,0,0); - if ( useRefForUpdateQuad){ - quad->br.vertices = quad->tl.vertices = quad->tr.vertices = quad->bl.vertices = vertex3(0,0,0); - } m_bShouldBeHidden = true; } else @@ -534,24 +507,14 @@ void CCSprite::updateTransform(void) m_sQuad.br.vertices = vertex3( RENDER_IN_SUBPIXEL(bx), RENDER_IN_SUBPIXEL(by), m_fVertexZ ); m_sQuad.tl.vertices = vertex3( RENDER_IN_SUBPIXEL(dx), RENDER_IN_SUBPIXEL(dy), m_fVertexZ ); m_sQuad.tr.vertices = vertex3( RENDER_IN_SUBPIXEL(cx), RENDER_IN_SUBPIXEL(cy), m_fVertexZ ); - if ( useRefForUpdateQuad){ - quad->br.vertices = quad->tl.vertices = quad->tr.vertices = quad->bl.vertices = vertex3( RENDER_IN_SUBPIXEL(cx), RENDER_IN_SUBPIXEL(cy), m_fVertexZ ); - } } // MARMALADE CHANGE: ADDED CHECK FOR NULL, TO PERMIT SPRITES WITH NO BATCH NODE / TEXTURE ATLAS if (m_pobTextureAtlas) - { - if ( useRefForUpdateQuad){ - m_pobTextureAtlas->setDirty(true); - }else{ - m_pobTextureAtlas->updateQuad(&m_sQuad, m_uAtlasIndex); - setQuadColorsDirty(false); - setQuadTexCoordsDirty(false); - } - setQuadVerticlesDirty(false); + { + m_pobTextureAtlas->updateQuad(&m_sQuad, m_uAtlasIndex); } - + m_bRecursiveDirty = false; setDirty(false); } @@ -934,26 +897,14 @@ bool CCSprite::isFlipY(void) void CCSprite::updateColor(void) { - ccV3F_C4B_T2F_Quad* quad = NULL; - bool bDirtyQuad = isQuadVerticlesDirty() || isQuadTexCoordsDirty(); - unsigned int totalQuads = 0; - unsigned int capacity = 0; - bool useRefForUpdateQuad = false; - if (m_pobBatchNode && (m_uAtlasIndex != CCSpriteIndexNotInitialized)){ - capacity = m_pobTextureAtlas->getCapacity(); - CCAssert( m_uAtlasIndex >= 0 && m_uAtlasIndex < capacity, "updateColor: Invalid m_uAtlasIndex"); - - totalQuads = m_pobTextureAtlas->getTotalQuads(); - useRefForUpdateQuad = ( totalQuads >= m_uAtlasIndex + 1) && (!bDirtyQuad); - } ccColor4B color4 = { _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity }; - + // special opacity for premultiplied textures - if (m_bOpacityModifyRGB) + if (m_bOpacityModifyRGB) { - color4.r *= _displayedOpacity/255.0f; - color4.g *= _displayedOpacity/255.0f; - color4.b *= _displayedOpacity/255.0f; + color4.r *= _displayedOpacity/255.0f; + color4.g *= _displayedOpacity/255.0f; + color4.b *= _displayedOpacity/255.0f; } m_sQuad.bl.colors = color4; @@ -961,25 +912,12 @@ void CCSprite::updateColor(void) m_sQuad.tl.colors = color4; m_sQuad.tr.colors = color4; - setQuadColorsDirty(true); // renders using batch node if (m_pobBatchNode) { if (m_uAtlasIndex != CCSpriteIndexNotInitialized) { - if(useRefForUpdateQuad){ - quad = &((m_pobTextureAtlas->getQuads())[m_uAtlasIndex]); - quad->bl.colors = color4; - quad->br.colors = color4; - quad->tl.colors = color4; - quad->tr.colors = color4; - m_pobTextureAtlas->setDirty(true); - }else{ - m_pobTextureAtlas->updateQuad(&m_sQuad, m_uAtlasIndex); - setQuadTexCoordsDirty(false); - setQuadVerticlesDirty(false); - } - setQuadColorsDirty(false); + m_pobTextureAtlas->updateQuad(&m_sQuad, m_uAtlasIndex); } else { @@ -1110,7 +1048,7 @@ void CCSprite::setBatchNode(CCSpriteBatchNode *pobSpriteBatchNode) m_sQuad.br.vertices = vertex3( x2, y1, 0 ); m_sQuad.tl.vertices = vertex3( x1, y2, 0 ); m_sQuad.tr.vertices = vertex3( x2, y2, 0 ); - setQuadVerticlesDirty(true); + } else { // using batch diff --git a/cocos2dx/sprite_nodes/CCSprite.h b/cocos2dx/sprite_nodes/CCSprite.h index 1edd05b91c..46bfbcf78f 100644 --- a/cocos2dx/sprite_nodes/CCSprite.h +++ b/cocos2dx/sprite_nodes/CCSprite.h @@ -494,22 +494,6 @@ protected: virtual void setReorderChildDirtyRecursively(void); virtual void setDirtyRecursively(bool bValue); - /** whether or not the colors attribute of Quad has been updated */ - inline bool isQuadColorsDirty(void) { return m_bQuadColorsDirty; } - /** specify if the colors attribute of Quad has been updated */ - inline void setQuadColorsDirty(bool bDirty) { m_bQuadColorsDirty = bDirty; } - - /** whether or not the verticles attribute of Quad has been updated */ - inline bool isQuadVerticlesDirty(void) { return m_bQuadVerticlesDirty; } - /** specify if the verticles attribute of Quad has been updated */ - inline void setQuadVerticlesDirty(bool bDirty) { m_bQuadVerticlesDirty = bDirty; } - - /** whether or not the texCoords attribute of Quad has been updated */ - inline bool isQuadTexCoordsDirty(void) { return m_bQuadTexCoordsDirty; } - /** specify if the texCoords attribute of Quad has been updated */ - inline void setQuadTexCoordsDirty(bool bDirty) { m_bQuadTexCoordsDirty = bDirty; } - - // // Data used when the sprite is rendered using a CCSpriteSheet // @@ -544,11 +528,6 @@ protected: // vertex coords, texture coords and color info ccV3F_C4B_T2F_Quad m_sQuad; - //flag to indicate if there is a change on the elements - bool m_bQuadColorsDirty; - bool m_bQuadVerticlesDirty; - bool m_bQuadTexCoordsDirty; - // opacity and RGB protocol bool m_bOpacityModifyRGB;