Generate transparent PNG from CCRenderTexture

Hi,
I am using following code to dump PNG from CCRenderTexture object on XCode (Mac)

CCRenderTexture * rt = CCRenderTexture::renderTextureWithWidthAndHeight(1024,1024);
rt~~>beginWithClear;
rt~~>end();
rt->saveBuffer(kCCImageFormatPNG,“test.png”);

what i am getting is a yellow image , shouldnt i be getting a transparent image. Or is there a way to get dump transparent PNG from CCRenderTexture ??

Why did you use 1024*1024 ? Use the size the same as your screen pixel size will be better. Otherwise, the saved image outside the screen size will be the color you clear.

Hi,
The problem is i am setting alpha 0 still yellow color is shown. The dumped png should be completely transparent . Isn’t it ??

regards
Siddhant

I don’t think so. If the background color is yellow, the alpha value of your image is 0, it means that the image will not be shown, but the dumped png will contain the background color.

Thanks a lot for your reply. … so the alpha value is useful when blending this texture with other sprites ? . What i wanted to accomplish was to generate a sprite sheet from within cocos2d-x itself. My approach was arrange all the images to be packed on a sprite. Render the arranged sprite on a texture using CCRenderTexture , then dump the texture . The problem is that the images with transparency are getting filled with the background color of the CCRenderTexture.

Hi, i was this problem to, i’m solved:
in method CCRenderTexture::saveToFile, change
bRet = pImage~~>saveToFile, true); // if true save only RGB
//new line
bRet = pImage~~>saveToFile(fullpath.c_str(), false);// save all pixels component RGBA

Dude you wrote

rt~~>beginWithClear;
is the RGBA for YELLOW color, that’s why the background is yellow
try this
rt~~>beginWithClear(0.0f, 0.0f, 0.0f, 1.0f);

I met the same problem. I combined two texture into one and tried to export as an png file with transparency. however, render->saveToFile("pic.png", kCCImageFormatPNG); also exports the backgournd color

If you use Cocos 2.1.5 or upper try

render~~>saveToFile;
because kCCImageFormatPNG force bIsRGB = true
but PNG with alpha must have 32 bit. Attention, because this method wants path and not file name
this is a criptic code for me, and I have a problem too.
I’m working on it now, but when I try to load saved RenderTexture in a new RenderTexture, I saw a strange effect on the border of my image.
as if the border, which is nuanced, lose this nuance
Simple code for me
// SAVE
@
std::stringstream path; @
@ path << CCFileUtils::sharedFileUtils~~>getWritablePath;@
@ path << “DrawingTexture.png”;@
@ renderTexture~~>saveToFile.c_str);@
@ renderTexture~~>clear(0, 0, 0, 0);
@

// LOAD
@
std::stringstream path;
path << CCFileUtils::sharedFileUtils()>getWritablePath;
path << “DrawingTexture.png”;
CCSprite * sprite = CCSprite::create.c_str);
sprite
>setFlipY(false);
renderTexture~~>beginWithClear;
CCSize screenSize = CCDirector::sharedDirector~~>getWinSize;
CCPoint screenMiddle = ccp;
sprite~~>setPosition;
sprite~~>visit();
renderTexture->end();
@