Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Cocos2dx基础 | [cocos2dx 3.0 (一)] 对文件读写操作 +FileUtils类大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

下面看一下Data类

01 classCC_DLL Data
02 {
03 public:
04 staticconstData Null;
05 //构造函数
06 Data();
07 Data(Data& other);
08 Data(Data&& other);
09 ~Data();
10 // 重载符号
11 Data& operator= (12 Data& operator= (Data&& other);
13
14 unsignedchar* getBytes();@H_730_197@//获取数据
15 ssize_t getSize()//尺寸
16 voidcopy(unsigned* bytes,ssize_t sizE);@H_730_197@//从bytes复制
17 fastSet(unsigned//从bytes快速set,使用后bytes将不能在外部使用
18 clear();@H_730_197@//清除
19 boolisNull()//判空
20 private:
@H_948_34@move(Data& other);
21
22 23 * _bytes;
24 ssize_t _size;
25 };

unsigned char* getFileDataFromZip(const std::string& zipFilePath,const std::string& filename,ssize_t *sizE);//读取压缩文件数据(zip格式)

如果读取成功size中会返回文件的大小,否则返回0。

std::string fullPathForFilename(const std::string &fileName);//获取文件的完整路径

如果我们通过setSearchPaths()设置搜索路径("/mnt/sdcard/","internal_dir/"),然后通过setSearchResolutionsOrder()设置子区分路径("resources-ipadhd/","resources-ipad/","resources-iphonehd")。如果搜索文件名为'sprite.png' 那么会先在文件查找字典中查找key: sprite.png -> value: sprite.pvr.gz,然后搜索文件'sprite.pvr.gz'如下顺序:

1 /mnt/sdcard/resources-ipadhd/sprite.pvr.gz (ifnot found,search next)
2 /mnt/sdcard/resources-ipad/sprite.pvr.gz (:1.1em!important; font-family:Consolas,search next)
3 /mnt/sdcard/resources-iphonehd/sprite.pvr.gz (4 /mnt/sdcard/sprite.pvr.gz (5 internal_dir/resources-ipadhd/sprite.pvr.gz (6 internal_dir/resources-ipad/sprite.pvr.gz (7 internal_dir/resources-iphonehd/sprite.pvr.gz (8 internal_dir/sprite.pvr.gz (return"sprite.png")

如果找到返回完整路径,没找到返回'sprite.png'。

void loadFilenameLookupDictionaryFromFile(const std::string &fileName);//从文件导入文件名查找字典

文件为plist格式如下:

<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC"-//Apple//DTD PLIST 1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version=>
<Dict>
<key>filenames</key>
<key>sounds/click.wav</key>
<String>sounds/click.caf</String>
<key>sounds/endgame.wav</key>
<String>sounds/endgame.caf</String>
<key>sounds/gem-0.wav</key>
<String>sounds/gem-0.caf</String>
</Dict>
<key>metadata</key>
<Dict>
<key>version</key>
<Integer>1</Integer>
</Dict>
</plist>

key对应String

void setFilenameLookupDictionary(const ValueMap& filenameLookupDict);//从ValueMap中设置文件名查找字典

ValueMap的定义:

typedefstd::unordered_map<std::string,Value> ValueMap;

std::string fullPathFromRelativeFile(const std::string &filename,const std::string &relativeFilE);//获取相对应文件的完整路径

e.g. filename: Hello.png,pszRelativeFile: /User/path1/path2/Hello.plist Return: /User/path1/path2/Hello.pvr (If there a a key(Hello.png)-value(Hello.pvr) in FilenameLookup Dictionary. )

void setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder);//设置子搜索区分路径

见fullPathForFilename()。

void addSearchResolutionsOrder(const std::string &order);//增加子搜索路径

const std::vector<std::string>& getSearchResolutionsOrder();//获取子搜索区分路径

void setSearchPaths(const std::vector<std::string>& searchPaths);//设置搜索路径

void addSearchPath(const std::string & path);//增加搜索路径

const std::vector<std::string>& getSearchPaths() const;//获取搜索路径

std::string getWritablePath();//获取一个可写入文件的路径

经过测试在win32平台上,debug版本返回的是程序文件所在的路径,release返回的是“我的文档”路径。

bool isFileExist(const std::string& filePath);//判断文件是否存在

经过测试在win32平台上,如果路径中包含中文字符会找不到文件。所以可以自己写个

wFileIO::isFileExist(std::string& pFileName)
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
returnCCFileUtils::geTinstance()->isFileExist(pFileName);
#else
(GetFileAttributesA(pFilename.c_str()) == INVALID_FILE_ATTRIBUTES)
returnfalse;
true;
#endif
}

bool isAbsolutePath(const std::string& path);判断是否为绝对路径


void setPopupNotify(bool notify);

bool isPopupNotify();

Sets/Gets 当文件加载失败时弹出messagebox.

ValueMap getValueMapFromFile(const std::string& fileName);//从文件获取ValueMap

bool writeToFile(ValueMap& Dict,const std::string& fullPath);//写入一个ValueMap数据到plist格式文件

ValueVector getValueVectorFromFile(const std::string& fileName);//从文件获取ValueVector

ValueVector定义:

std::vector<Value> ValueVector;

函数就这么多了,就在这里记录下,到时要用再来看看奋斗

因为没发现有直接写文件的函数,所以我这里自己写了下,然不知道再其他平台会怎样,再windows上用着再说大笑

再win32上realse版本getWritablePath()会获取“我的文档”,还是改成当前路径吧

std::string wFileIO::getWritablePath()
CCFileUtils::geTinstance()->getWritablePath();
full_path[MAX_PATH + 1];
::GetModuleFilenameA(NULL,full_path,MAX_PATH + 1);
std::string ret((*)full_path);
// remove xxx.exe
ret = ret.substr(0,ret.rfind("\\") + 1);
ret = convertPathFormatToUnixStyle(ret);
ret;
下面是保存文件:

wFileIO::saveFile(const* pContentString,monospace!important; font-size:10pt!important; min-height:inherit!important">std::string fn=convertPathFormatToUnixStyle(pFileName);
intnp=fn.rfind('/');
(np!=std::string::npos)
(!mkDirM(fn.substr(0,np)))
std::string path = getWritablePath()+fn;
FILE* file =fopen(path.c_str(),"w"(filE)
fputs(pContentString,filE);
fclose(filE);
log("save file [%s]",path.c_str());
}
else
"fail to save file [%s]!" @H_730_197@//检测各级文件夹,不存在则创建
wFileIO::mkDirM(std::string& pDirName)
std::string path = getWritablePath();
26 np=pDirName.find(String" style="word-wrap:break-word; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,0);
27 while28 29 (!mkDir(path+pDirName.substr(0,np)))
30 31 :rgb(108,np+1);
32 }
@H_948_34@mkDir(path+pDirName);
33
34 35 36 //创建文件夹
37 wFileIO::mkDir(38 39 40 DIR *pDir = NULL;
41 //打开该路径
42 pDir = opendir (pDirName.c_str());
43 (! pDir)
44 45 //创建该路径
46 (!mkdir(pDirName.c_str(),S_IRWXU | S_IRWXG | S_IRWXO))
47 {
48 "fail to create dir [%s]":1.1em!important; font-family:Consolas,pDirName.c_str());
49 50 51 "create dir [%s]":1.1em!important; font-family:Consolas,pDirName.c_str());
52 53 54 ((GetFileAttributesA(pDirName.c_str())) == INVALID_FILE_ATTRIBUTES)
55 56 (!CreateDirectoryA(pDirName.c_str(),0))
57 58 59 60 61 62 63 64 65 66 67 //路径格式转为UnixStyle,"c:\xxx.txt" --> "c:/xxx.txt"
68 inlinestd::string convertPathFormatToUnixStyle(std::string& path)
69 70 std::string ret = path;len = ret.length();
71 for(i = 0; i < len; ++i)
72 73 (ret[i] =='\\')
74 75 ret[i] =@H_618_1563@76 77 78 79 }

大佬总结

以上是大佬教程为你收集整理的Cocos2dx基础 | [cocos2dx 3.0 (一)] 对文件读写操作 +FileUtils类全部内容,希望文章能够帮你解决Cocos2dx基础 | [cocos2dx 3.0 (一)] 对文件读写操作 +FileUtils类所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:33.0cocoscocos2dxdxfileutils基础操作文件读写
猜你在找的Cocos2d-x相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap