cocos2d-x 纯代码创建透明格子精灵

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xyzzf/article/details/51924628

效果图:

这里写图片描述

代码

//
// Copyright (c) 2015-2016 x-studio365 - All Rights Reserved.
//
#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;

    // copy remain bytes
    for (int loopi = 1; loopi < cellSize.cy; ++loopi)
    {
        ::memcpy(ptr, startp, sizeb);
        ptr += sizeb;
    }
}

void fillLinePixelsRGBA8888(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;
                *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;

    // copy remain bytes
    for (int loopi = 1; loopi < cellSize.cy; ++loopi)
    {
        ::memcpy(ptr, startp, sizeb);
        ptr += sizeb;
    }
}

Sprite* createTransparentBackgrondRGB888(const SIZE& canvasSize, const SIZE& cellSize/*, const Color3B& color1, 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 * 3;
    unsigned char* pixels((unsigned char*)malloc(bytesPixels));
    memset(pixels, 0x0, bytesPixels);

    // Fill Pixels
    unsigned char* ptr = pixels;

    // TODO: calculate somethings
    fillLinePixelsRGB888(size, cellSize, ptr, 1);
    fillLinePixelsRGB888(size, cellSize, ptr, 2);

    // copy remain bytes
    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, pixels, (sizeb >> 1));
        ptr += (sizeb >> 1);
    }

    assert((ptr - pixels) == bytesPixels);

    // create texture2d by pixels
    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 SIZE& cellSize/*, const Color3B& color1, 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, 0x0, bytesPixels);

    // Fill Pixels
    auto ptr = pixels;

    fillLinePixelsRGBA8888(size, cellSize, ptr, 1);
    fillLinePixelsRGBA8888(size, cellSize, ptr, 2);

    // copy remain bytes
    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, pixels, (sizeb >> 1));
        ptr += (sizeb >> 1);
    }

    assert((ptr - pixels) == bytesPixels);

    // create texture2d by pixels
    Texture2D* texture = new Texture2D();

    if (texture->initWithData(pixels, bytesPixels, Texture2D::PixelFormat::RGBA8888, size.cx, size.cy, Size(size.cx, size.cy)))
    {
        sp = Sprite::createWithTexture(texture);
    }
    texture->release();

    free(pixels);
    return sp;
}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页