4

I recently using CocosBuilder to build the interface of a game. I can load ccbi files in main thread without any problems.But when I load it in the background thread. I got an empty layer/node with black background. So my question is how to load them properly in background thread?

iamro00
  • 71
  • 4
  • 1
    I don't think this is possible. Cocos2d already asynchronously loads textures for sprites and such, and because it already handles that background loading, it is unsafe to load a texture in your own background thread. My hunch is that CCBReader just loads textures with Cocos2d and therefore is also not threadsafe. Best to figure out what you really need to load immediately and put what can be loaded later into other CCB files that can then be added to an active scene – Kiefer Aguilar Sep 06 '13 at 19:19
  • agree, i just wanted to say the same thing... – CodeSmile Sep 07 '13 at 13:09

1 Answers1

3

I find a solution.I can load the ccbi files and run the loading animation at the same time. Don't know if is right, but it works for me. Here's the code. The solution is found in the Cocos2d Multithread Test Example.

//loading animations here
    [self pushLoadingAnimation];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    CCGLView *view = (CCGLView*)[[CCDirector sharedDirector] view];
    NSAssert(view, @"Do not initialize the TextureCache before the Director");

    EAGLContext *auxGLcontext = [[EAGLContext alloc]
                                 initWithAPI:kEAGLRenderingAPIOpenGLES2
                                 sharegroup:[[view context] sharegroup]];

    if( [EAGLContext setCurrentContext:auxGLcontext] ) {

        // load the ccbi files here
        [self readCCBIfile];
        //push the scene in main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            [[CCDirector sharedDirector] replaceScene:world];
        });

        glFlush();

        [EAGLContext setCurrentContext:nil];
    } else {
        CCLOG(@"cocos2d: ERROR: TetureCache: Could not set EAGLContext");
    }
});
iamro00
  • 71
  • 4