In my game I'm using such method for loading sprites asynchronously:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[physicsSprite loadSprite];
dispatch_async(dispatch_get_main_queue(), ^{
[physicsSprite createAnimation];
[physicsSprite createBodyInWorld:world;
});
});
physicsSprite it's just node that holds CCSprite child. In method loadSprite - I do just self creating sprite by
physicsSprite.sprite = [CCSprite spriteWithSpriteFrameName:@"bla.png"];
And the method
[physicsSprite createAnimation];
using for adding loaded sprite to mainLayer node.
All this logic works well. However I'm thinking if I'm doing something wrong, because I'm NOT creating OpenGL context
So, I've tried my code with context:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
CCGLView *view = (CCGLView*)[[CCDirector sharedDirector] view];
EAGLContext *auxGLcontext = [[[EAGLContext alloc]
initWithAPI:kEAGLRenderingAPIOpenGLES2
sharegroup:[[view context] sharegroup]] autorelease];
if( [EAGLContext setCurrentContext:auxGLcontext] ) {
[physicsSprite loadSprite];
glFlush();
[EAGLContext setCurrentContext:nil];
}
dispatch_async(dispatch_get_main_queue(), ^{
[physicsSprite createAnimation];
[physicsSprite createBodyInWorld:world];
});
});
And as result I don't see any difference in gameplay.
But as I know, I need to create context for any sprite that loaded in another thread. So, is what I'm doing wrong? Without creating context...