diff --git a/cocos2dx/include/CCSpriteBatchNode.h b/cocos2dx/include/CCSpriteBatchNode.h index 8093279ab4..8681bd77db 100644 --- a/cocos2dx/include/CCSpriteBatchNode.h +++ b/cocos2dx/include/CCSpriteBatchNode.h @@ -163,6 +163,21 @@ namespace cocos2d virtual void removeAllChildrenWithCleanup(bool cleanup); virtual void draw(void); + protected: + /* IMPORTANT XXX IMPORTNAT: + * These 2 methods can't be part of CCTMXLayer since they call [super add...], and CCSpriteSheet#add SHALL not be called + */ + + /* Adds a quad into the texture atlas but it won't be added into the children array. + This method should be called only when you are dealing with very big AtlasSrite and when most of the CCSprite won't be updated. + For example: a tile map (CCTMXMap) or a label with lots of characters (BitmapFontAtlas) + */ + void addQuadFromSprite(CCSprite *sprite, unsigned int index); + /* This is the opposite of "addQuadFromSprite. + It add the sprite to the children and descendants array, but it doesn't update add it to the texture atlas + */ + CCSpriteBatchNode * addSpriteWithoutQuad(CCSprite*child, unsigned int z, int aTag); + private: void updateBlendFunc(); diff --git a/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp b/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp index 4e07d2b10c..f85da6131a 100644 --- a/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp +++ b/cocos2dx/sprite_nodes/CCSpriteBatchNode.cpp @@ -625,4 +625,62 @@ namespace cocos2d updateBlendFunc(); } + + // CCSpriteSheet Extension + //implementation CCSpriteSheet (TMXTiledMapExtension) + + void CCSpriteBatchNode::addQuadFromSprite(CCSprite *sprite, unsigned int index) + { + NSAssert( sprite != NULL, "Argument must be non-nil"); + /// @todo NSAssert( [sprite isKindOfClass:[CCSprite class]], @"CCSpriteSheet only supports CCSprites as children"); + + while(index >= m_pobTextureAtlas->getCapacity() || m_pobTextureAtlas->getCapacity() == m_pobTextureAtlas->getTotalQuads()) + { + this->increaseAtlasCapacity(); + } + // + // update the quad directly. Don't add the sprite to the scene graph + // + sprite->useBatchNode(this); + sprite->setAtlasIndex(index); + + ccV3F_C4B_T2F_Quad quad = sprite->getQuad(); + m_pobTextureAtlas->insertQuad(&quad, index); + + // XXX: updateTransform will update the textureAtlas too using updateQuad. + // XXX: so, it should be AFTER the insertQuad + sprite->setDirty(YES); + sprite->updateTransform(); + } + + CCSpriteBatchNode * CCSpriteBatchNode::addSpriteWithoutQuad(CCSprite*child, unsigned int z, int aTag) + { + NSAssert( child != NULL, "Argument must be non-nil"); + /// @todo NSAssert( [child isKindOfClass:[CCSprite class]], @"CCSpriteSheet only supports CCSprites as children"); + + // quad index is Z + child->setAtlasIndex(z); + + // XXX: optimize with a binary search + int i=0; + if (m_pobDescendants && m_pobDescendants->count() > 0) + { + NSMutableArray::NSMutableArrayIterator iter; + for (iter = m_pobDescendants->begin(); iter != m_pobDescendants->end(); ++iter) + { + // fast dispatch + if (!(*iter) || (*iter)->getAtlasIndex() >=z) + { + break; + } + ++i; + } + } + m_pobDescendants->insertObjectAtIndex(child, i); + + // IMPORTANT: Call super, and not self. Avoid adding it to the texture atlas array + CCNode::addChild(child, z, aTag); + return this; + } + } \ No newline at end of file