把.pvr.ccz文件转换成png

我用的是一个万能转换法,原理是先用CCSprite加载.pvr.ccz,然后把它绘制到一个CCRenderTexture上,然后再保存到文件里。这方法其实不只.pvr.ccz文件,其他所有能被cocos2dx直接加载的文件都可以用这种转换。有个弊端就是可能跟源文件数据稍有些差异(我这个就是看起来有点白边)。

用法是:比如有个文件夹叫Image,里面有个文件叫1.pvr.ccz。把Image文件夹拖拽到PngConverter.exe图标上。然后会在Image的旁边生成一个文件夹叫Image_png(里面有个文件叫1.png)

注意:目录不能带中文!!!!

生成的png文件名字不全的bug已经修正。

点击下载

最近发现有些不道德行为,转载请注明出处:http://www.cnblogs.com/mrblue/p/3420189.html

附上关键源码

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);

    // position the label on the center of the screen
    pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
        origin.y + visibleSize.height - pLabel->getContentSize().height));
    this->addChild(pLabel, 1);

    {//convert
        const char* pszSuffix = ".pvr.ccz";

        int nStartPos = -1;
        while ( nStartPos=g_Param.find('\\',nStartPos+1),-1!=nStartPos )
        {
            g_Param.replace(nStartPos,1,"/");
        }

        std::string strFileFilter = g_Param+"/*"+pszSuffix;

        _finddata_t fileDir;
        long lfDir;


        if((lfDir = _findfirst(strFileFilter.c_str(),&fileDir))==-1l)
        {
            char szMsg[128] = {0};
            sprintf(szMsg,"Please drag a folder which contains \"%s\" files onto this application's icon" ,pszSuffix);
            pLabel->setString(szMsg);
        }
        else
        {
            std::string strSaveFolderPath = g_Param+"_png/";

            BOOL ret = CreateDirectoryA(strSaveFolderPath.c_str(), NULL);
            if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
            {
                CC_ASSERT(false);
            }

            int nFileNum = 0;
            do
            {
                std::string strFileName = fileDir.name;
                std::string strFilePath = g_Param+'/'+strFileName;

                CCSprite *img = CCSprite::create(strFilePath.c_str());
                img->setPosition(ccp(0,0));
                img->setAnchorPoint(ccp(0,0));
                CCSize sz = img->getContentSize();
                
                CCRenderTexture* pRT = CCRenderTexture::create(sz.width, sz.height, kCCTexture2DPixelFormat_RGBA8888);
                pRT->clear(1,1,1,0);
                pRT->begin();
                img->visit();
                pRT->end();

                std::string strSaveFile = strFileName.substr(0,strFileName.rfind(pszSuffix));
                strSaveFile+=".png";
                std::string strSaveFullPath = strSaveFolderPath+strSaveFile;

                CCImage *pImage = pRT->newCCImage(true);
                pImage->saveToFile(strSaveFullPath.c_str(), false);
                CC_SAFE_DELETE(pImage);

                CCLOG("%s",fileDir.name);
                
                nFileNum++;

            }while( _findnext( lfDir, &fileDir ) == 0 );

            char szMsg[128] = {0};
            sprintf(szMsg,"%d files have been converted",nFileNum);
            pLabel->setString(szMsg);
        }
    }


    return true;
}

 

posted @ 2013-11-12 20:31  MrBlue  阅读(10786)  评论(6编辑  收藏  举报