cocos2d-x 遍历assets下资源

来源:互联网 发布:苹果微信加附近人软件 编辑:程序博客网 时间:2024/06/07 11:19

首先说明,本人用的2dx版本是2.2.6。

但是遍历资源跟版本的跨度关系不大,使用的类是ZipFile。


第一步在:在ZipFile添加方法,因为_dataThread是个私有的,尽量不改变源码的情况下,添加一个get方法是最好的

这个getAllFile可以返回所有的文件目录

std::vector<std::string>ZipFile::getAllFile(){

    std::vector<std::string> vec;

    ZipFilePrivate::FileListContainer::iterator it1;

    for(it1=_dataThread->fileList.begin();it1!=_dataThread->fileList.end();++it1) {

        vec.push_back(it1->first.c_str());

    }

    return vec;

}

第二步:使用getAllFile的返回值做遍历,这里直接帖出IOS和android的同时遍历吧,同时搜索png和jpg的图片,可以用于加载资源,记得导入头文件

#include "support/zip_support/ZipUtils.h"


#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

#include <dirent.h>

#include <sys/stat.h>

#else

#include "platform/CCCommon.h"

#include "support/zip_support/unzip.h"

#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"

#endif


void ResourceLoadingLayer::getAllFile(std::string folderPath,int depth, std::vector<std::string> *list,std::string head){

#if CC_PLATFORM_IOS == CC_TARGET_PLATFORM

    DIR *dp;

    structdirent *entry;

    structstat statbuf;

    if((dp =opendir(folderPath.c_str())) ==NULL) {

        fprintf(stderr,"cannot open directory: %s\n", folderPath.c_str());

        return;

    }

    chdir(folderPath.c_str());

    while((entry =readdir(dp)) !=NULL) {

        lstat(entry->d_name,&statbuf);

        if(S_ISDIR(statbuf.st_mode)) {

            if(strcmp(".",entry->d_name) == 0 ||

               strcmp("..",entry->d_name) ==0)

                continue;

            getAllFile(entry->d_name,depth+4,list,head+entry->d_name);

        } else {

            if (head.length() ==0) {

                string name = entry->d_name;

                if (name.length()>3 && name.rfind(".") > 0 && (name.substr(name.rfind(".")+1,3) == "jpg" || name.substr(name.rfind(".")+1,3) == "png")) {

                    list->push_back(entry->d_name);

                }

            } else {

                string filename = head+"/" +entry->d_name;

                if (filename.length()>3 && filename.rfind(".") > 0 && (filename.substr(filename.rfind(".")+1,3) == "jpg" || filename.substr(filename.rfind(".")+1,3) == "png")) {

                    list->push_back(filename);

                }

            }

        }

    }

    chdir("..");

    closedir(dp);

#else

    ZipFile* pFile = new ZipFile(getApkPath(),"assets/");

    vector<string> vec = pFile->getAllFile();

    for (int i=0; i<vec.size(); i++) {

        string file = vec.at(i);

        if (file.compare("assets/")) {

            file = file.substr(7,file.length());

        }

        

        if(file.substr(0,folderPath.length()) == folderPath ){

            string filename = file.substr(file.rfind("/")+1,file.length());

            if (filename.length()>3 && filename.rfind(".") >0 && (filename.substr(filename.rfind(".")+1,3) =="jpg" || filename.substr(filename.rfind(".")+1,3) =="png") {

                list->push_back(filename);

            }

        }

    }

#endif

}


第三步,使用:

首先得拿到资源文件夹的完整路径,方法有很多,只举其一:

在HelloWorld的工程下的资源根目录有一张HelloWorld.png图片,我们可以直接获取它在程序中的完整路径,即

string fullpath = CCFileUtils::sharedFileUtils()->fullPathForFilename("HelloWorld.png");

所以 string resPath = fullpath.substr(0,fullpath.rfind("/")+1);

得到了完整的资源路径后,

vector<string> vec;

getAllFile(resPath, 0, &vec, "");

这样就大功告成了,vec会保存所有的png或jpg的资源,会带head名的哦,比如说bg/welcome.jpg


上面是遍历所有的文件夹,如果单独遍历资源文件下的某个目录,即是

getAllFile(resPath+"xxx",0,&vec,""); xxx 为资源根目录下的子目录,这样就可以分别加载某个目录了


下面帖上ZipFile文件下载地址

http://pan.baidu.com/s/1gd8n3hT





1 0
原创粉丝点击