Frage

I have searched for over a week, for an answer to this question, with no result.

Is there a way to make a core graphics texture on a background thread in cocos2d and then return a texture (CCTexture2D).

I have tried to render it on a background thread, but I only get a black texture(CCTexture2D) as a return.

This is the code that renders the level creation

1.I create a new NSThread

 -(void)loadLevelByString:(NSString*)level objects:(NSArray*)o{

        [NSThread detachNewThreadSelector:@selector(loadingLevelThread:) toTarget:self withObject:[NSArray arrayWithObjects:level,o, nil]];    

    }

2.The thread calls the loadingLevelThread method that loads the level data.

-(void)loadingLevelThread:(NSArray*)objects{

    //This line allocs the level class
    id object = [[[NSClassFromString(@"LevelLoader") alloc] initWithLevel:[objects objectAtIndex:0] objects:[objects objectAtIndex:1]] autorelease];

    if([object respondsToSelector:@selector(loadLevel)]){
        id loadedLevel = [object performSelector:@selector(loadLevel)];

        [self performSelectorOnMainThread:@selector(replaceSceneWithNew:) withObject:loadedLevel waitUntilDone:YES];

    }

}

I create the texture in the loadedLevel call(above). I create the texture like this

1.First I make a CGContext with core graphics and draws the texture that I like. This works.

2.Then I initialize the CCTexture2D class with the context image that I created above

CCTexture2D *texture = [[CCTexture2D alloc] initWithCGImage:contextImage.CGImage resolutionType:kCCTexture2DPixelFormat_RGBA8888];

3.Than I save the CCTexture too a global shared handler

[[runing_session_data sharedSession] setText:texture];

4.When the texture is created I create the CCSprite(shape) that the texture will be drawn on. When this CCSprite(shape) is created I add it to stage and replace cocos2d scene with the one I just loaded.

5.On the main thread I use the CCSprite(shape) -(void)draw method to render the open GL stuff. In here I also get the texture from the global shared handler and binds in to open GL context.

This is how the draw method code looks like.

CC_NODE_DRAW_SETUP();

//Here I get the texture that I created on the background thread and binds it to the GL_TEXTURE_2D
ccGLBindTexture2D( [[runing_session_data sharedSession] getTexture].name);
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords | kCCVertexAttribFlag_Color);

glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0 , bC);
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, bCC);

glDrawArrays(GL_TRIANGLES, beginReadVertsAt, endReadVertsAt);

When the output comes the draw method works fine. But it is all black. The texture has been remove somehow.

If there is someone who can answer, it would be helpful,

Thanks.

War es hilfreich?

Lösung

You would need multiple OpenGL contexts, one for each thread (a single OpenGL context may only be active in and used by a single thread), with the sharing enabled. I'm not sure this is even possible on mobile GPUs / iOS.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top