游戏资源二次压缩

因为渠道的要求关系, 之前的游戏包过大, 需要再减小将近一半的体积; 再不去修改代码的情况下, 想到的首先是将图片大小直接缩小一半, 再在引擎中全部放大出来的办法;

###放大

就是在cocos2d-x 的贴图缓存加载的时候进行放大, 具体位置是的CCTextureCache.cppCCTexture2D * CCTextureCache::addImage(const char * path)方法中:

CCTexture2D * CCTextureCache::addImage(const char * path)
{
	......
	pImage = new CCImage();
	CC_BREAK_IF(NULL == pImage);
	
	bool bRet = pImage->initWithImageFile(fullpath.c_str(), eImageFormat);
	CC_BREAK_IF(!bRet);
	
	// pImage加载完文件内容之后, 对pImage统一放大
	CCTexture2D * pcOriTexture = new CCTexture2D();
	pcOriTexture->initWithImage(pImage);
	pcOriTexture->autorelease();
	
	pcOriTexture->setAliasTexParameters();
	
	int w = pcOriTexture->getPixelsWide() * 2;
	int h = pcOriTexture->getPixelsHigh() * 2;
	
	CCRenderTexture * pcTempRenderTex = CCRenderTexture::create(w, h);
	
	pcTempRenderTex->beginWithClear(0.0f, 0.0f, 0.0f, 0.0f);
	pcOriTexture->drawInRect(CCRectMake(0, 0, w, h)); 
	pcTempRenderTex->end(); 
	
	pImage = pcTempRenderTex->newCCImage();
	// 统一放大end
	
	//原来的加载, 但是pImage 已经放大过了
	texture = new CCTexture2D();
	
	if( texture &&
	    texture->initWithImage(pImage) )
	{
	......

}

###源图片压缩

使用ImageMagick工具的convert进行缩小, 再用ImageAlpha.app 进行压缩

#!/bin/bash

imgfiles=`find res -name "*.png"`

num=0
for file in $imgfiles
do
	num=$(($num+1))
done
echo "开始压缩...共${num}个文件..."
index=0
for file in $imgfiles
do
	target=${file/res/res_resize}
	index=$(($index+1))
	echo "${index}/${num}	${file}	${target}"
	convert -resize 50% $file $target
	/Applications/ImageAlpha.app/Contents/Resources/pngnq -f -e .png -s 1 -u15 -Q 10 -T 15 $target
done