ExtendedSpriteBatchNode for cocos2d-x

Over at Cocos2d-iPhone an extension was made to allow a single CCSpriteBatchNode render all sprite nodes at once and maintain proper z-ordering.
http://www.cocos2d-iphone.org/forum/topic/14747

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

pDirector->setDepthTest(true);

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

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER,0);

and at the end of the same function add

glDisable(GL_ALPHA_TEST);

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.

HI,

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?

D

EDIT:
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);


    CCSpriteBatchNode::draw();

}

I’m having the same problem. But I noticed in your sample that you setup the shader but don’t run it.
@
setShaderProgram(s_pShader);
@
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);
alphashader->setUniformLocationWith1f(loc,0.0f);

After creating my batch nodes I call the following:

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