Doubts about rendering to textures

Hi,

I’m trying to get some optimizations.

In my game, I started drawing several gradients and using them as background. That worked fine with Android, but when I tested with Lumia 435 it lagged horribly.

Then I rendered gradient to a texture using http://www.cocos2d-x.org/wiki/Render_To_Texture example, and speed improved greatly.

Sadly, I noticed in android that some times that texture gets lost when user pauses game and return to it later.

My question is:

  • Do I need to reload all textures when my game gets focus again? According to TextureCache::reloadAllTextures() API reference there is no need to call this method now.

  • Is the texture created using the RenderToTexture example stored in TextureCache by default? If not, how should I add it? RenderTexturert->getSprite()->getTexture() returns a Texture2D pointer but Director::getInstance()->getTextureCache()->addImage() only accepts a filename or a cocos2d::Image

Looks like RenderTexture has the functionality you’re wanting disabled:

void RenderTexture::listenToBackground(EventCustom *event)
{
    // We have not found a way to dispatch the enter background message before the texture data are destroyed.
    // So we disable this pair of message handler at present.

I think you have two viable options:

  1. Add the texture manually to VolatileTextureMgr (Texture2D will remove itself automatically on destruction)

  2. Write your texture to file after rendering and then use that texture by file name to create a sprite which will be added to the texture cache automatically. This approach can serve as a good optimisation if you want to release the texture from memory and obtain it later without the overhead of regenerating it, but has the cost of writing it to file in the first place.

Hope this helps

If you need a simple gradients for background, don’t draw it. You don’t need renderTexture either.
create a 1/10 size gradient image in photoshop or etc and scale it 10X.
There is no penalty for performance and quality.
Try it.

Hi

@almax27 haven’t been able to find VolatileTextureMgr help in cocos2d-x v3.10 API reference :frowning: maybe it has been deprecated?

Anyway, a mate already pointed me out the solution given my @amin13 (thanks @amin13 btw), just wanted to be sure if there were another way to solve my problem as I do find easier for testing purposes to change RGB values, compile and run rather than create texture, save, copy to resources and run.

So, if I load all my textures from files (either using a plist or direct png files) and use them in sprites, , will them be reloaded if my app loses focus and gains it later again?

Will be reloaded.

VolatileTextureMgr is defined in CCTextureCache.h, it is used for rebuilding your textures when the GL context is destroyed. It is very much not deprecated :smile: perhaps it’s not in the API reference because it does not have any documentation comments.

Ultimately if you don’t intend to change the texture at run time (colour, angle, offset of the gradient), and you’re unlikely to need many variations, then using packaged assets definitely makes more sense. Generating the gradient at runtime does afford you the benefit of reducing your apps download size though :stuck_out_tongue:

Thank for all your info @almax27! :smile: and thanks @amin13a for giving a really-short-but-useful answer to my question too! :grin: