In my gallery app, I asynchronously load images frequently, which are dumped into the CCTextureCache referenced by their filename. The thing is, I need to mask my textures -- and this masking really only needs to be done once, so ideally, I figured the best place to put it would be in the selector that addImageAsync
calls. I had to modify CCTexture by adding in a boolean variable which notifies me if the specific texture has already been masked or not, and that works very well.
In fact, even the masking code itself (which is accomplished using a CCRenderTexture following Ray's masking tutorial) also works well inside the selector code. The issue I am having now is, once I've applied my mask logic, I have the CCRenderTexture.sprite.texture and I need to update my CCTextureCache with this updated, masked texture so that in the future, any other Sprite or call that references the specified file name will make use of the fully prepared texture.
How can I update the texture data in CCTextureCache? Or more specifically, how can I replace an existing, cached texture with the one stored in my render texture?
I know the textures themselves (their data) is actually stored in OpenGL and not within the Cocos2D classes; so I'd imagine something along the lines of removing the OpenGL texture with the name (GLuint) of the cached texture, and then setting the cached texture's name (GLuint) to the name of the RenderTexture's sprite.texture... but I'm at a loss on how to go about actually accomplishing that (opengl code, cocos2d thread safe code, altering the cctexturecache's dictionary, etc).
Or maybe there is a simpler way to go about it without altering Cocos2D (eg utilizing opengl to replace the texture referenced by the texture cache with the render texture's texture, then letting the local RT variable die and get cleaned up automatically?
How can I update the texture data in CCTextureCache?
This is how you remove a texture from the cache:
CCTextureCache* cache = [CCTextureCache sharedTextureCache];
[cache removeTexture:tex];
// or:
[cache removeTextureForKey:@"someimage.png"];
You can then re-add the texture in any way you see fit, and it will force the cache to reload the texture.