无法使用 Cocos2D-X 在 Windows 的 plist 中为 png 充气

Posted

技术标签:

【中文标题】无法使用 Cocos2D-X 在 Windows 的 plist 中为 png 充气【英文标题】:Failing to inflate a png inside a plist with Windows using Cocos2D-X 【发布时间】:2012-07-05 19:53:30 【问题描述】:

我正在运行Coco2D-X 的particleWithFile() 函数,使用Mac 创建的plist 文件。使用“textureImageData”键将图像数据嵌入到 plist 文件中。 在 Mac 上它可以正常工作,但在 Windows 上它会失败,在 CCAssert(isOK) 上,请参阅下面的 Coco2D-X 代码 (CCParticleSystem.cpp):

char *textureData = (char*)valueForKey("textureImageData", dictionary);
CCAssert(textureData, "");

int dataLen = strlen(textureData);
if(dataLen != 0) 

    // if it fails, try to get it from the base64-gzipped data  
    int decodeLen = base64Decode((unsigned char*)textureData, (unsigned int)dataLen,          &buffer);
    CCAssert( buffer != NULL, "CCParticleSystem: error decoding textureImageData");
    CC_BREAK_IF(!buffer);

    int deflatedLen = ZipUtils::ccInflateMemory(buffer, decodeLen, &deflated);
    CCAssert( deflated != NULL, "CCParticleSystem: error ungzipping textureImageData");
    CC_BREAK_IF(!deflated);

    image = new CCImage();
    bool isOK = image->initWithImageData(deflated, deflatedLen);
    CCAssert(isOK, "CCParticleSystem: error init image with Data");
    CC_BREAK_IF(!isOK);
    m_pTexture = CCTextureCache::sharedTextureCache()->addUIImage(image, fullpath.c_str());

看来解码成功了,问题出在zlib的inflate()函数内部,解压png文件失败。

有什么建议吗?

【问题讨论】:

您使用了一堆表示不同含义的术语,因此您的代码可能会同样感到困惑。 “解压缩”与“解压缩”不同,与 png 文件中的内容不同。您不能“解压缩” png 文件。 png 文件包含 zlib 包装的数据。 zip、gzip 和 zlib 是三种不同的格式,它们都是原始 deflate 压缩数据的不同包装器。您需要检查 ccInflateMemory 期望的格式,并确保这是您提供的格式。 对不起,我不是专家,我想这就是我在这里提问的原因......正如我所说,这不是我的代码,而是使用 zlib 的 Cocos2d-x 代码。通过“解压缩”,我的意思是“解压缩” png 文件。我不认为格式有什么问题,因为它成功地膨胀了其他一些嵌入的 png。 【参考方案1】:

好的,我找到了问题。

显然我的资源不是 png 文件而是 tiff 文件。所以 Inflate() 返回了一个正确的缓冲区(一个 tiff 文件 - 对我来说看起来太长了,所以我怀疑它是错误的),但是 initWithImageData() 未能创建图像,因为 tiff 在 windows 上不受 Cocos2d-x 支持。

【讨论】:

【参考方案2】:

我遇到问题是因为我将 CC_USE_TIFF 设置为 0。

在我将 CC_USE_TIFF 恢复为 1 后,问题解决了。

【讨论】:

以上是关于无法使用 Cocos2D-X 在 Windows 的 plist 中为 png 充气的主要内容,如果未能解决你的问题,请参考以下文章

在Windows7上搭建Cocos2d-x

在Windows 10上安装cocos2d-x而不将python路径设置为旧版本

[游戏开发日志]Windows下Cocos2d-x 3.14环境搭建

关于windows下创建cocos2d lua工程 无法断点调试 relative (file not exist) 的问题

windows下cocos2d-x工程结构讲解

[01] cocos2d-x开发环境搭建