ExtendedSpriteBatchNode for cocos2d-x

ExtendedSpriteBatchNode for cocos2d-x
0.0 0


Over at Cocos2d-iPhone an extension was made to allow a single CCSpriteBatchNode render all sprite nodes at once and maintain proper z-ordering.

Attached is the ExtendedSpriteBatchNode that is converted to C++ for Cocos2d-x.

Adding particles has not been converted (yet) and the sorting algorithm has been changed from insertion sort (n^2 worst case) to smooth sort (nlogn worst case).

This class has helped me tremendously with isometric perspectives. Thanks again to the original author!

ExtendedSpriteBatchNode-x.zip (5.1 KB)


Thanks to your contribution. I added a link to this topic in the sticky FAQ, http://www.cocos2d-x.org/boards/6/topics/567


Thanks a lot!


There were a few bugs and performance issues with this class.
Line 254 should read: CCSpriteBatchNode::removeChild(sprite, cleanup);

I’ve ended up using vertexz instead, which is must faster. I’ll contribute the class in a day or two.


Just to follow up…

I’ve found that it is very easy to sort objects across CCSpriteBatchNodes using the follow method:

In the function AppDelegate::applicationDidFinishLaunching() add the following


then, at the beginning of the draw() function for CCSpriteBatchNode, etc, add the following


and at the end of the same function add


After those modifications are done the setVertexZ for sprites should work instead of using setZOrder. I usually scale my range values to within 1000, because much higher and stange things start to occur. Also, sprites sometimes have thin alpha “halos” because of the alpha testing.



We’re trying to do the same time, but I’m running into compile errors: it can’t find GL_ALPHA_TEST or glAlphaFunc(). I’m using cocos2d-x. 2.0rc1 on iPhone, so I’m thinking I might have some missing header files?


Hmm, ok these things are OpenGL ES1 only, so that seems why I can’t get access to it, does anyone have an example of the replacement code I could use for cocos2d-x 2?


I found this for OpenGL ES 2.0 but it doesn’t seem to work. any one have an idea?

void GameMob::draw()
    // OpenGL ES 1.0 only??
//    glEnable(GL_ALPHA_TEST);
//    glAlphaFunc( GL_GREATER, 0 );
//    glDisable(GL_ALPHA_TEST);

     CCGLProgram* s_pShader = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColorAlphaTest);

     int alphaValue = glGetUniformLocation( s_pShader->getProgram(), "u_alpha_value");

     s_pShader->setUniformLocationWith1f(alphaValue, 0.0f);




I’m having the same problem. But I noticed in your sample that you setup the shader but don’t run it.
I tried changing the default shader and setting the alpha value like you did but did not get any farther.
What happens when you set the shader?


I’m using the shader method now, but with batch nodes (not individual sprites).

In my game init I’m doing the following:

CCGLProgram* alphashader = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColorAlphaTest);
unsigned loc = glGetUniformLocation(alphashader->getProgram(), kCCUniformAlphaTestValue);

After creating my batch nodes I call the following:

batch->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey( kCCShader_PositionTextureColorAlphaTest ));