Question

I am using CCSpriteBatchNode to draw a line basically one pixel at a time (using a 1x1 swatch). I need to do this because the user is controlling where the line goes in real time (it's not a drawing app but I think what I'm doing is comparable).

It turns out that when I add between 15-16k children to the batchNode it stops registering the new children. Basically it seems like the pen ran out of ink!

I poked around in the cocos2d code a little bit but at no point does it seem that anything goes wrong. It just stops registering the new child nodes. Does anyone know why this is?

Was it helpful?

Solution

First of all, and let me say this in a way that's truly a gigantic understatement:

Ouch!

Next, the technical reason. Your sprites will fail to render as soon as you have batched 16.384 of them. Since each sprite uses a quad (4 vertices) to render the sprite on screen, you hit the vertex limit of 65.536 vertices per batch operation.

More vertices won't get drawn per batch operation (VBO buffer limit). This is a technical limitation. You could try to use another CCSpriteBatchNode for additional sprites - but seriously you're going down a road to nowhere here.

If all you need is to draw lines, this is probably the worst possible approach I can imagine. Consider using line drawing (ccDrawLine), individual pixels (ccDrawPixel I believe exists too, but 15k pixels would be terribly slow) or shaders to accomplish the same or similar effect. You can even batch drawing primitives with cocos2d 2.1 by using CCDrawNode.

One other solution I've used successfully in YETIPIPI is to use a limited number of sprites, say 20 to 200, and draw them to a CCRenderTexture. Every time you add a new sprite, the least recently used sprite is re-used. By not clearing the rendertexture, you can continue to draw onto it with far fewer sprites while everything that's been drawn before remains. By drawing the sprites with low opacity you even get the effect of drawing at the same position for a longer period of time in order to make that area more opaque.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top