cocos jpg alpha_mask png

这段代码演示了如何在Cocos2d-x中通过结合jpg图像和png alpha_mask来创建一个新的纹理。首先加载jpg和png图片,然后将jpg的RGB数据与png的alpha通道数据合并,最后生成一个带有透明度信息的新纹理。
Texture2D* StartLayer::addJpgMaskPng(const char* jpgName, const char* maskName)
{
CCImage *jpgImage = new CCImage();
jpgImage->initWithImageFile(jpgName);
unsigned char *jpgData = jpgImage->getData();
int lenJPGPerPixel = jpgImage->getBitPerPixel() / 8;


int width = jpgImage->getWidth();
int height = jpgImage->getHeight();
int len = width * height;




CCImage *alphaImage = new CCImage();
alphaImage->initWithImageFile(maskName);
unsigned char *alphaData = alphaImage->getData();
int lenAlphaPerPixel = alphaImage->getBitPerPixel() / 8;


unsigned char *outPic = new unsigned char[width * height * 4];


int outIndex = 0;
int srcIndex = 0;


for (int i = 0; i < len; i++)
{
outPic[outIndex + 0] = jpgData[srcIndex + 0];
outPic[outIndex + 1] = jpgData[srcIndex + 1];
outPic[outIndex + 2] = jpgData[srcIndex + 2];
outPic[outIndex + 3] = alphaData[i*lenAlphaPerPixel];
srcIndex += 3;
outIndex += 4;
}


CCTexture2D *texture = new CCTexture2D();
texture->initWithData(outPic, len * 4, Texture2D::PixelFormat::RGBA8888, width, height, Size((float)width, (float)height));
delete jpgImage;
delete alphaImage;
delete[] outPic;
return texture;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值