cocos2dx的图片载入「建议收藏」

cocos2dx的图片载入

大家好,又见面了,我是全栈君。

//data: 图片文件数据  dataLen: 文件长度
bool Image::initWithImageData(const unsigned char * data, ssize_t dataLen)
{
    bool ret = false;

    do
    {
        CC_BREAK_IF(! data || dataLen <= 0);

        unsigned char* unpackedData = nullptr;
        ssize_t unpackedLen = 0;

        //解压缩pvr.ccz格式的图片
        //detecgt and unzip the compress file
        if (ZipUtils::isCCZBuffer(data, dataLen))
        {
            unpackedLen = ZipUtils::inflateCCZBuffer(data, dataLen, &unpackedData);
        }
        //解压缩pvr.gz格式的图片
        else if (ZipUtils::isGZipBuffer(data, dataLen))
        {
            unpackedLen = ZipUtils::inflateMemory(const_cast<unsigned char*>(data), dataLen, &unpackedData);
        }
        else
        {
            unpackedData = const_cast<unsigned char*>(data);
            unpackedLen = dataLen;
        }
        //识别文件类型
        _fileType = detectFormat(unpackedData, unpackedLen);

        switch (_fileType)
        {
        case Format::PNG:
            ret = initWithPngData(unpackedData, unpackedLen);
            break;
        ...
        //后面就是依据文件类型来进行图片解码初始化
        } while (0);

    return ret;
}

这里先介绍下图片解码到载入再到显示的流程。以后再具体地介绍每种格式图片的解码。

Texture2D * TextureCache::addImage(const std::string &path)
{
  ...
  //新建一个Image对象
  image = new (std::nothrow) Image();
  CC_BREAK_IF(nullptr == image);

  //图片解码
  bool bRet = image->initWithImageFile(fullpath);
  CC_BREAK_IF(!bRet);

  //新建一个2D的纹理
  texture = new (std::nothrow) Texture2D();

  //開始初始化纹理
  if( texture && texture->initWithImage(image) )
  {
    ...
  }
}

//使用指定像素格式来初始化(默认是auto,依据图片解码的结果来确定)
bool Texture2D::initWithImage(Image *image, PixelFormat format)
{
    ...
    //获取当前的OpenGL环境
    Configuration *conf = Configuration::getInstance();
    //推断纹理大小是否超出限制
    int maxTextureSize = conf->getMaxTextureSize();
    ...

    //获取mipmap贴图的数量
    if (image->getNumberOfMipmaps() > 1)
    {
        CCLOG("cocos2d: WARNING: This image has more than 1 mipmaps and we will not convert the data format");
        //载入mipmap贴图
        initWithMipmaps(image->getMipmaps(), image->getNumberOfMipmaps(), image->getRenderFormat(), imageWidth, imageHeight);

        return true;
    }
    else if (image->isCompressed())
    {
        ...
        initWithData(...)
        ...
        return true;
    }
    else
    {
        ...
        initWithData(...)
        ...
        return true;
    }
}

bool Texture2D::initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, PixelFormat pixelFormat, int pixelsWide, int pixelsHigh)
{
    ...
    //设置像素的行字节对齐,在一定平台下有性能的提高。并且若不加注意,会导致glTexImage中系函数的读取越界
    //Set the row align only when mipmapsNum == 1 and the data is uncompressed
    if (mipmapsNum == 1 && !info.compressed)
    {
        unsigned int bytesPerRow = pixelsWide * info.bpp / 8;

        if(bytesPerRow % 8 == 0)
        {
            glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
        }
        else if(bytesPerRow % 4 == 0)
        {
            glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
        }
        else if(bytesPerRow % 2 == 0)
        {
            glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
        }
        else
        {
            glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        }
    }else
    {
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    }
    ...
    //生成纹理索引
    glGenTextures(1, &_name);
    //bindTexture2函数中会调用glActiveTexture,glBindTexture来制定纹理单位和绑定纹理
    GL::bindTexture2D(_name);

    //依据mipmap贴图数和是否设置抗锯齿来选择纹理滤波方式,关于纹理滤波的选择后面会具体的再分析下
    if (mipmapsNum == 1)
    {
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _antialiasEnabled ? GL_LINEAR : GL_NEAREST);
    }else
    {
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _antialiasEnabled ? GL_LINEAR_MIPMAP_NEAREST : GL_NEAREST_MIPMAP_NEAREST);
    }

    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _antialiasEnabled ?

GL_LINEAR : GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); for (int i = 0; i < mipmapsNum; ++i) { unsigned char *data = mipmaps[i].address; GLsizei datalen = mipmaps[i].len; //纹理映射一个指定的纹理图像的一部分到每一个开启了纹理映射的图元上。在当前段着色器或顶点着色器使用内建纹理搜索函数时。贴图被启用。

if (info.compressed) { //压缩格式生成纹理 glCompressedTexImage2D(GL_TEXTURE_2D, i, info.internalFormat, (GLsizei)width, (GLsizei)height, 0, datalen, data); } else { //生成2D纹理 glTexImage2D(GL_TEXTURE_2D, i, info.internalFormat, (GLsizei)width, (GLsizei)height, 0, info.format, info.type, data); } if (i > 0 && (width != height || ccNextPOT(width) != width )) { CCLOG("cocos2d: Texture2D. WARNING. Mipmap level %u is not squared. Texture won't render correctly. width=%d != height=%d", i, width, height); } GLenum err = glGetError(); if (err != GL_NO_ERROR) { CCLOG("cocos2d: Texture2D: Error uploading compressed texture level: %u . glError: 0x%04X", i, err); return false; } //四分之中的一个大小的mipmap width = MAX(width >> 1, 1); height = MAX(height >> 1, 1); } _contentSize = Size((float)pixelsWide, (float)pixelsHigh); _pixelsWide = pixelsWide; _pixelsHigh = pixelsHigh; _pixelFormat = pixelFormat; _maxS = 1; _maxT = 1; //关闭alpha渐变 _hasPremultipliedAlpha = false; _hasMipmaps = mipmapsNum > 1; // shader setGLProgram(GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE)); }

对于非mipmaps的贴图直接指定为已mipmapsNum为1的形式进行初始化就可以,纹理纹理渲染完毕就可以增加到显示队列,当然这里仅仅是先简介下,关于渲染流程等我写完图片的解码部分再回来补充~

未完待续…

tp

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115390.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • CSS 颜色代码大全 CSS颜色对照表[通俗易懂]

    CSS 颜色代码大全 CSS颜色对照表[通俗易懂]转载地址:https://blog.csdn.net/u012117917/article/details/41604711HTML及CSS常用颜色英文词汇  黑色 银色 灰色 白色 茶色 红色 紫色 紫红 black silver gray white maroon …

    2022年5月17日
    139
  • gridview的属性_grid css

    gridview的属性_grid css正在做一个项目,其中用到了Gridview控件,内容如下即每行里又包括两小行,这个功能可以用两个嵌套的gridview实现,第二个要实现的是每个大行之间用实现相隔,每个小行之间用虚线网上很多资料都是关于Gridview的,但是关于样式的就不多后来发现可以在后台程序中动态改变CELL的式样protectedvoidGridView2_RowDataBound(obj

    2022年9月2日
    2
  • LINUX批量修改文件名

    LINUX批量修改文件名

    2020年11月19日
    182
  • Linux常用命令大全(非常全!!!)

    Linux常用命令大全(非常全!!!)Linux常用命令大全(非常全!!!)最近都在和Linux打交道,感觉还不错。我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人喜欢linux的原因,比较短小但却功能强大。我将我了解到的命令列举一下,仅供大家参考:系统信息arch显示机器的处理器架构uname-m显示机器的处理器架构uname-r显示正在使用的内核版本d…

    2022年4月27日
    30
  • Apache Web服务器安全配置全攻略[通俗易懂]

    Apache Web服务器安全配置全攻略[通俗易懂]作为最流行的Web服务器,ApacheServer提供了较好的安全特性,使其能够应对可能的安全威胁和信息泄漏。   Apache服务器的安全特性  1、采用选择性访问控制和强制性访问控制的安全策略  从Apache或Web的角度来讲,选择性访问控制DAC(DiscretionaryAccessControl)仍是基于用户名和密码的,强制性访问控制MAC(Mand

    2022年10月28日
    0
  • Linux之tail查看命令

    Linux之tail查看命令一、语法操作命令:tailoptions文件地址options:-f:该参数用于监视File文件增长。-cNumber:从Number字节位置读取指定文件-nNumber:从Number行位置读取…

    2022年6月4日
    56

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号