Draw sprite on Image?

Hi,

in my app I’m saving an image created from raw data (unsigned char*) like this:

auto timg = new Image();
timg->initWithRawData(thumbnailData, img->getDataLen() / thumbnailDivider, img->getWidth() / thumbnailDivider, img->getHeight() / thumbnailDivider, 4);
timg->saveToFile(imageThumbFilename, false);

Now, can I somehow, easily, draw a sprite on this Image object without bothering with rendering a sprite myself (modifying raw pixel data array)?

Hi,
Here is example:

auto sprite = Sprite::create("original.png");
auto renderTexture = RenderTexture::create(64, 64, Texture2D::PixelFormat::RGBA8888);
renderTexture->clear(0,0,0,0);
renderTexture->begin();
sprite->setAnchorPoint(Vec2(0, 0));
sprite->setPosition(Vec2(0,0));
sprite->visit();
renderTexture->end();
renderTexture->saveToFile("rendertexture.png", Image::Format::PNG);

You can use renderTexture->getSprite() if you need sprite.

Docs http://docs.cocos2d-x.org/api-ref/cplusplus/v3x/d9/ddc/classcocos2d_1_1_render_texture.html

2 Likes

oh yeah, I forgot about RenderTexture.

I can create Texture from Image, then create Sprite from it, draw it on renderTexture with the other sprite and then save it. Clever. Let me try it out.

2 Likes

Thanks, it worked. However I’ve discovered a strange bug. I’ve created a separate topic so it won’t mislead anyone: