《pvr批量转小图png》由会员分享,可在线阅读,更多相关《pvr批量转小图png(7页珍藏版)》请在金锄头文库上搜索。
1、Plist2Png.h 文件内容#ifndef _PLIST2PNG_SCENE_H_#define _PLIST2PNG_SCENE_H_#include cocos2d.hclass Plist2Png : public cocos2d:Layerpublic:/ theres no id in cpp, so we recommend returning the class instance pointerstatic cocos2d:Scene* createScene();/ Heres a difference. Method init in cocos2d-x returns b
2、ool, instead of returning id in cocos2d-iphonevirtual bool init();/ implement the static create() method manuallyCREATE_FUNC(Plist2Png);void getAllFileNameWithPath(std:vector *theResultVec,std:string filePath,int depth=0);void SavePngFromPlist(std:string plistName,std:string imageSaveDir = );void on
3、SaveImageToFile(cocos2d:RenderTexture *render,const std:string& filename, bool isRGBA,cocos2d:CustomCommand *_saveToFileCommand);private:;#endif / _HELLOWORLD_SCENE_H_Plist2Png.cpp 文件内容#include Plist2Png.h#include cocos2d.h#ifdef _WIN32#include #else#include #include #include #include #endifUSING_NS
4、_CC;Scene* Plist2Png:createScene()/ scene is an autorelease objectauto scene = Scene:create();/ layer is an autorelease objectauto layer = Plist2Png:create();/ add layer as a child to scenescene-addChild(layer);/ return the scenereturn scene;/ on init you need to initialize your instancebool Plist2P
5、ng:init()/ 1. super init firstif ( !Layer:init() )return false; std:vector allPlistVector;getAllFileNameWithPath(&allPlistVector,FileUtils:getInstance()-fullPathForFilename(monsters);for (int i=0; igetWritablePath()+tempDir;long dirOffset = tempDir.find_last_of(.); tempDir = tempDir.substr(0,dirOffs
6、et);tempDir = tempDir+ /;SavePngFromPlist(allPlistVectori, tempDir);return true;void Plist2Png:getAllFileNameWithPath(std:vector *theResultVec,std:string filePath,int depth)DIR *dp;struct dirent *entry;struct stat statebuf;if (dp=opendir(filePath.c_str()=NULL) fprintf(stderr,cannot open directory :%
7、sn,filePath.c_str();return ;chdir(filePath.c_str();while (entry=readdir(dp)!=NULL) lstat(entry-d_name, if (S_ISDIR(statebuf.st_mode) if (strcmp(., entry-d_name)=0|strcmp(., entry-d_name)=0) continue;printf(%*s%s/n,depth,entry-d_name);std:string tempDirName = filePath + / + entry-d_name;getAllFileNam
8、eWithPath(theResultVec,tempDirName,depth+4);elsestd:string fileName = entry-d_name;printf(%*s%sn,depth,entry-d_name);if (fileName.find(.plist)!=-1) theResultVec-push_back(filePath+ /+fileName); /存入路径为沙盒路径,请去沙盒路径下找分解的 png 小图。不懂得请百度void Plist2Png:SavePngFromPlist(std:string plistName,std:string imageS
9、aveDir)SpriteFrameCache:getInstance()-addSpriteFramesWithFile(plistName);std:string fullPath = FileUtils:getInstance()-fullPathForFilename(plistName);auto dict = FileUtils:getInstance()-getValueMapFromFile(fullPath);std:string texturePath();if (dict.find(metadata) != dict.end()ValueMap/ try to read
10、texture file name from meta datatexturePath = metadataDicttextureFileName.asString();if (!texturePath.empty()texturePath = FileUtils:getInstance()-fullPathFromRelativeFile(texturePath.c_str(), plistName);else/ default image nametexturePath = plistName;size_t startPos = texturePath.find_last_of(.); t
11、exturePath = texturePath.erase(startPos);texturePath = texturePath.append(.png);CCLOG(cocos2d: SpriteFrameCache: Trying to use file %s as texture, texturePath.c_str();auto *texture = Director:getInstance()-getTextureCache()-addImage(texturePath.c_str();if (!texture)CCLOG(cocos2d: SpriteFrameCache: C
12、ouldnt load texture);return;auto if(imageSaveDir.empty()imageSaveDir = FileUtils:getInstance()-getWritablePath() + output/;FileUtils:getInstance()-createDirectory(imageSaveDir);log(ImageSaveDir: %s,imageSaveDir.c_str();for (auto iter = framesDict.begin(); iter != framesDict.end(); +iter)auto std:str
13、ing spriteFrameName = iter-first;std:string imageFilePath = imageSaveDir + spriteFrameName;auto *sprite = Sprite:createWithSpriteFrameName(spriteFrameName);if(!sprite)continue;sprite-setAnchorPoint(Vec2(0,0);auto *render = CCRenderTexture:create(sprite-getContentSize().width,sprite-getContentSize().
14、height);render-begin();sprite-visit();render-end();bool isRGBA = false;if (spriteFrameName.find(.png) != std:string:npos)isRGBA = true;else if (spriteFrameName.find(.jpg) != std:string:npos)isRGBA = false;elseCCLOG(Only PNG and JPG format are supported now! %s,spriteFrameName.c_str();continue;auto *
15、 saveToFileCommand = new cocos2d:CustomCommand();saveToFileCommand-init(_globalZOrder);saveToFileCommand-func = CC_CALLBACK_0(Plist2Png:onSaveImageToFile, this, render, imageFilePath, isRGBA,saveToFileCommand);Director:getInstance()-getRenderer()-addCommand(saveToFileCommand);void Plist2Png:onSaveImageToFile(RenderTexture *render,const std:string& filename, bool isRGBA,cocos2d:CustomCommand *saveToFileCommand)Image *image = render-newImage(true);if (image)image-saveToFile