前端之家收集整理的这篇文章主要介绍了
cocos2d-x 纯代码创建透明格子精灵,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#define DARK_BYTE 0x99
#define LIGHT_BYTE 0xdd
void fillLinePixelsRGB888(const SIZE& size,const SIZE& cellSize,unsigned char*& ptr,int flag)
{
auto startp = ptr;
for (int i = 0; i < size.cx;)
{
switch (flag) {
case 1:
for (auto j = 0; j < cellSize.cx; ++j) {
*ptr++ = DARK_BYTE;
*ptr++ = DARK_BYTE;
*ptr++ = DARK_BYTE;
++i;
}
flag = 2;
break;
case 2:
for (auto j = 0; j < cellSize.cx; ++j) {
*ptr++ = LIGHT_BYTE;
*ptr++ = LIGHT_BYTE;
*ptr++ = LIGHT_BYTE;
++i;
}
flag = 1;
break;
}
}
auto sizeb = ptr - startp;
for (int loopi = 1; loopi < cellSize.cy; ++loopi)
{
::memcpy(ptr,startp,sizeb);
ptr += sizeb;
}
}
void fillLinePixelsRGBA8888(const SIZE& size,int flag)
{
auto startp = ptr;
for (int i = 0; i < size.cx;)
{
switch (flag) {
case 1:
for (auto j = 0; j < cellSize.cx; ++j) {
*ptr++ = DARK_BYTE;
*ptr++ = DARK_BYTE;
*ptr++ = DARK_BYTE;
*ptr++ = DARK_BYTE;
++i;
}
flag = 2;
break;
case 2:
for (auto j = 0; j < cellSize.cx; ++j) {
*ptr++ = LIGHT_BYTE;
*ptr++ = LIGHT_BYTE;
*ptr++ = LIGHT_BYTE;
*ptr++ = 0xff;
++i;
}
flag = 1;
break;
}
}
auto sizeb = ptr - startp;
for (int loopi = 1; loopi < cellSize.cy; ++loopi)
{
::memcpy(ptr,sizeb);
ptr += sizeb;
}
}
Sprite* createTransparentBackgrondRGB888(const SIZE& canvasSize,const SIZE& cellSize)
{
if (canvasSize.cx < cellSize.cx || canvasSize.cy < cellSize.cy)
return nullptr;
SIZE size;
int rx = canvasSize.cx % cellSize.cx;
int ry = canvasSize.cy % cellSize.cy;
size.cx = rx == 0 ? canvasSize.cx : canvasSize.cx + (cellSize.cy - rx);
size.cy = ry == 0 ? canvasSize.cy : canvasSize.cy + (cellSize.cy - ry);
Sprite* sp = nullptr;
auto bytesPixels = size.cx * size.cy * 3;
unsigned char* pixels((unsigned char*)malloc(bytesPixels));
memset(pixels,0x0,bytesPixels);
unsigned char* ptr = pixels;
fillLinePixelsRGB888(size,cellSize,ptr,1);
fillLinePixelsRGB888(size,2);
int lines = size.cy / cellSize.cy;
auto sizeb = ptr - pixels;
for (int loopi = 1; loopi < lines / 2; ++loopi)
{
memcpy(ptr,pixels,sizeb);
ptr += sizeb;
}
if (lines % 2 != 0) {
memcpy(ptr,(sizeb >> 1));
ptr += (sizeb >> 1);
}
assert((ptr - pixels) == bytesPixels);
Texture2D* texture = new Texture2D();
if (texture->initWithData(pixels,bytesPixels,Texture2D::PixelFormat::RGB888,size.cx,size.cy,Size(size.cx,size.cy)))
{
sp = Sprite::createWithTexture(texture);
}
texture->release();
free(pixels);
return sp;
}
Sprite* createTransparentBackgrondRGBA8888(const SIZE& canvasSize,const Color3B& color2*/)
{
if (canvasSize.cx < cellSize.cx || canvasSize.cy < cellSize.cy)
return nullptr;
SIZE size;
int rx = canvasSize.cx % cellSize.cx;
int ry = canvasSize.cy % cellSize.cy;
size.cx = rx == 0 ? canvasSize.cx : canvasSize.cx + (cellSize.cy - rx);
size.cy = ry == 0 ? canvasSize.cy : canvasSize.cy + (cellSize.cy - ry);
Sprite* sp = nullptr;
auto bytesPixels = size.cx * size.cy * sizeof(unsigned int);
unsigned char* pixels((unsigned char*)malloc(bytesPixels));
memset(pixels,bytesPixels);
auto ptr = pixels;
fillLinePixelsRGBA8888(size,1);
fillLinePixelsRGBA8888(size,Texture2D::PixelFormat::RGBA8888,size.cy)))
{
sp = Sprite::createWithTexture(texture);
}
texture->release();
free(pixels);
return sp;
}