How to load ccbi files with CCBReader in background thread?

767 views Asked by At

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?

1

There are 1 answers

1
iamro00 On

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");
    }
});