I'm trying to draw onto a CCRenderTexture in a thread. I've got:
EAGLSharegroup *sharegroup = [[[[CCDirector sharedDirector] openGLView] context] sharegroup];
EAGLContext *k_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:sharegroup];
[EAGLContext setCurrentContext:k_context];
at the beginning of the thread. Everything worked besides CCSprite.draw . I've tested the render texture with:
[rt beginWithClear:1 g:1 b:1 a:1];
[sprite visit];
[rt end];
Calling [CCSprite visit] will not draw the sprite. glGetError returns 0 in all steps.
To further investigate this problem, I put everything in the UI thread, and removed the context calls, I'll see the sprite correctly drawn with the same code. I've also verified that the sprite is correct by adding this sprite to the scene.
And even if I don't use threads, putting the above "context setting calls" will make CCSprite.draw stop working, but only when drawing to a CCRenderTexture. If it's drawing on the screen, it works fine.
Any ideas how to solve this problem? Thanks in advance!
beginendpair withdispatch_sync(dispatch_get_main_queue(), ^{block addresses the problem. Maybe CCRenderTexture can only be drawn on the UI thread? - He Shiming