想了解cocos2dx读取excel文件的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于cocos读取配置文件的相关问题,此外,我们还将为您介绍关于(4)cocos2dx读取csv数据文件、
想了解cocos2dx读取excel文件的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于cocos读取配置文件的相关问题,此外,我们还将为您介绍关于(4)cocos2dx读取csv数据文件、
- cocos2dx读取excel文件(cocos读取配置文件)
- (4)cocos2dx读取csv数据文件
关于cocos2dx接Android sdk的一些坑 - Bullet(Cocos2dx)之交叉编译Android,集成到cocos2dx3.x
- cocos2d-x 读取CSV文件,读取本地Excel配置表的方法
cocos2dx读取excel文件(cocos读取配置文件)
这一部分知识,我们会来简单的介绍一下cocos2dx的相关源码。但是这里需要了解一下函数模板,类模板,而且还有关联容器中的pair容器的相关知识。
首先说明,我们将源码分享到了下载频道,需要的话,可以去下载跑跑
(4)cocos2dx读取csv数据文件
cocos2dx中读取数据文件可能有很多种,像读取xml,lua,csv,json等,这些都可以作为配置数据的格式。
最近用到了读取csv数据文件,所以在网上找了一下关于这方面的技术博客。果然,网上各路大神都是不吝啬的,
不说废话了,直接上代码。代码如下(测试通过,可读取数据):
.h头文件
// // QLCSVFile.h // // Created by quasi_lee on 15-7-7. // // #ifndef __QLCSVFile__ #define __QLCSVFile__ #include <stdio.h> #include "cocos2d.h" USING_NS_CC; using namespace std; class QLCSVFile { public: QLCSVFile(); ~QLCSVFile(); //用以存储数据 vector<vector<string> > data; private: string fieldsep; int cols; void StringSplit(const string& str,vector<string>& tokens,const char& delimiters); void split(vector<string>& field,string line); int advplain(const string& line,string& fld,int); int advquoted(const string& line,int); public: //打开CSV文件 bool openFile(const char*fileName); //根据行列获取数据 const char* getData(int rows,int cols); //获取指定数据的列下标 int findColsData(int cols,const char* value); //得到总列数 inline int getCols(){return cols;} //得到总行数 inline int getRows(){return data.size();} }; #endif /* defined(__QLCSVFile__) */
.cpp文件
// // QLCSVFile.cpp // // Created by quasi_lee on 15-7-7. // // #include "QLCSVFile.h" QLCSVFile::QLCSVFile() : fieldsep(","),cols(0) { } //获取指定行列的数据 const char* QLCSVFile::getData(int rows,int cols) { if(rows < 0 || rows >= data.size() || cols < 0 || cols >= data[rows].size()) { return ""; } return data[rows][cols].c_str(); } //获取指定数据的列下标 int QLCSVFile::findColsData(int cols,const char *value) { for(int i = 0; i < data.size(); i++) { if(strcmp(getData(i,cols),value) == 0) { return i; } } return -1; } //解析CSV文件 bool QLCSVFile::openFile(const char *fileName) { string pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName); unsigned char* pBuffer = NULL; unsigned long bufferSize = 0; pBuffer = CCFileUtils::sharedFileUtils()->getFileData(pathKey.c_str(),"r",&bufferSize); string s = (char*)pBuffer; string str = s.substr(0,bufferSize); vector<string> line; StringSplit(str,line,'\n'); for(int i = 0; i < line.size(); i++) { vector<string> field; split(field,line[i]); data.push_back(field); cols = max(cols,(int)field.size()); } return true; } void QLCSVFile::StringSplit(const std::string &str,vector<string> &tokens,const char &delimiters) { string::size_type lastPos = str.find_first_not_of(delimiters,0); string::size_type pos = str.find_first_of(delimiters,lastPos); while (string::npos != pos || string::npos != lastPos) { tokens.push_back(str.substr(lastPos,pos - lastPos)); lastPos = str.find_first_not_of(delimiters,pos); pos = str.find_first_of(delimiters,lastPos); } } void QLCSVFile::split(vector<string>& field,string line) { string fld; int i = 0; int j = 0; if(line.length() == 0) { return; } do { if(i < line.length() && line[i] == '"') { j = advquoted(line,fld,++i); } else { j = advplain(line,i); } field.push_back(fld); i = j + 1; } while (j < line.length()); } int QLCSVFile::advquoted(const string &line,string &fld,int i) { int j = 0; fld = ""; for(j = i; j < line.length(); j++) { if(line[j] == '"' && line[++j] != '"') { int k = line.find_first_of(fieldsep,j); if(k > line.length()) { k = line.length(); } for(k -= j; k-- > 0; ) { fld += line[j++]; } break; } fld += line[j]; } return j; } int QLCSVFile::advplain(const string &line,int i) { int j = 0; j = line.find_first_of(fieldsep,i); if(j > line.length()) { j = line.length(); } fld = string(line,i,j - i); return j; } //析构函数,释放内存 QLCSVFile::~QLCSVFile() { for(int i = 0; i < data.size(); i++) { data[i].clear(); } data.clear(); }
关于cocos2dx接Android sdk的一些坑
简单说说UI线程 :在Android中,有个非常重要的家伙非常霸道,那就是UI线程。这霸道之一:不能被阻塞。 之二:系统对每一个组件的调用都从UI线程分发出去。
简单说说openGL线程:但凡cocos2dx 启动的绘制线程都是openGL线程。就这么多
任何SDK界面的调用,必须从UI线程中调用,所以需要放到主线程中。如果我们直接从GL线程中调用,轻则调用不了,重者程序蹦死。
解决办法:
得到主线程的handler,这里简单说一种,就是在onCreate中new一个静态handler。
或者new Handler(Looper.getMainLooper()),且叫mainHandler吧,
启动一个线程,
- ThreadsendThread=newThread(newRunnable(){
- publicvoidrun(){
- mainHandler.post(sendRun);
- }
- });
Bullet(Cocos2dx)之交叉编译Android,集成到cocos2dx3.x
Bullet(Cocos2dx)之交叉编译Android,集成到cocos2dx3.x
首先将src文件夹复制到jni文件夹,没有可以新建一个
新建两个文件Android.mk,Application.mk
Application.mk内容如下:
APP_ABI:=armeabiarmeabi-v7ax86
APP_PLATFORM:=android-8
APP_STL:=stlport_static
Android.mk内容如下:
LOCAL_PATH:=$(callmy-dir)
include$(CLEAR_VARS)
LOCAL_MODULE :=bullet_static(可随便起)
LOCAL_MODULE_FILENAME:=libbullet(可随便起)
LOCAL_C_INCLUDES:=src/(头文件位置)
(源文件,要列出所有用到的,这里用到BulletSoftBodyBulletDynamics
BulletCollisionsLinearMath)
最后还要加上一句,表示生成静态库如果将STATIC该文SHARED则生成动态库
include$(BUILD_STATIC_LIBRARY)
然后进入命令行
进入jni所在目录
ndk-build
等待编译完成
将三个目录下的libbullet.a分别复制到cocos2dx的安装目录下的
安装目录\Cocos\frameworks\cocos2d-x\prebuilt\android\相应文件下
进入Cocos\frameworks\cocos2d-x\external
新建文件夹Bullet,在Bullet下新建prebuild-mk
将Box2D\prebuild-mk下的Android.mk复制到Bullet/prebuild-mk
修改如下3行
LOCAL_MODULE:=bullet_static
LOCAL_MODULE_FILENAME:=libbullet
LOCAL_SRC_FILES:=../../../prebuilt/android/$(TARGET_ARCH_ABI)/libbullet.a
复制头文件
将bullet3的src的所有文件复制到新建的Bullet文件下
删除不是.h的所有文件
下一步任务繁重,
将所有用到BulletCollisionBulletDynamicsBulletSoftBodyLinearMath目录下的include都加上Bullet/,可以使用Notepad++在Bullet目录搜索Bullet,并替换为Bullet/Bullet,LinearMath并替换为Bullet/LinearMath
进入Cocos\frameworks\cocos2d-x\cocos\prebuilt-mk
在Android.mk
在LOCAL_WHOLE_STATIC_LIBRARIES+=Box2d_static下面添加
LOCAL_WHOLE_STATIC_LIBRARIES+=bullet_static
$(callimport-module,Box2D/prebuilt-mk)下面添加
$(callimport-module,Bullet/prebuilt-mk)
至此已经完成编译静态库
下载地址
cocos2d-x 读取CSV文件,读取本地Excel配置表的方法
//CSVReader.h
#define MAP_LINE std::map<std::string,std::string> //key为首行字符串,value为此列字符串 #define MAP_CONTENT std::map<int,MAP_LINE> //key为code,value为一行map #define VEC_MAP std::vector<std::pair<std::string,int>> //csv文件读取器 class CSVReader { public: CSVReader(); static CSVReader *getInst(); //获取实例 //解析csv. fileName:csv文件名,void parse(const char *fileName); //获取内容map. filename:文件名 const MAP_CONTENT &getContentMap(std::string filename); //获取一行map.filename:文件名, code一行code const MAP_LINE &getLineMap(std::string filename,int code); //获取某行某列的值 const std::string &getByCode(std::string filename,int code,const std::string &key); private: //读取csv的一行.line:一行的内容 void readCSVLine(const char *line,int index); VEC_MAP m_firstVector; //第一行的vector MAP_CONTENT m_contentMap; //内容map std::map<std::string,MAP_CONTENT> m_fileMap; //文件map static CSVReader *m_inst; //实例 };
//CSVReader.cpp
CSVReader *CSVReader::m_inst = NULL; //构造函数 CSVReader::CSVReader() { m_firstVector.clear(); m_contentMap.clear(); m_fileMap.clear(); } //获取实例 CSVReader *CSVReader::getInst() { if(!m_inst) {m_inst = new CSVReader();} return m_inst; } //获取内容map. filename:文件名 const MAP_CONTENT &CSVReader::getContentMap(std::string filename) { return m_fileMap.find(filename)->second; } //获取一行map.filename:文件名, code一行code const MAP_LINE &CSVReader::getLineMap(std::string filename,int code) { return getContentMap(filename).find(code)->second; } //获取某行某列的值 const std::string &CSVReader::getByCode(std::string filename,const std::string &key) { return getLineMap(filename,code).find(key)->second; } //解析csv. fileName:csv文件名,void CSVReader::parse(const char *fileName) { m_contentMap.clear(); //首先进行清理 std::string path = fileName; unsigned long size; const char *data = (const char*)(cocos2d::CCFileUtils::sharedFileUtils()->getFileData(path.c_str(),"r",&size)); CCAssert(data != NULL,"File is not exist."); if (data == NULL) return; char line[32768]; //一行最多字节数 const char *src = data; if (size == 0) size = strlen(src); char *pl = line; //指向line数组的指针 int index = 0; bool skip = false; //若一行为空,skip则标记为true while (data - src < size) { //读取到一行的末尾 if (*data == '\n' && !skip) { *pl = '\0'; readCSVLine(line,index); ++index; pl = line; } else if (*data == '\r') {} else { //任何一个字段能留空 if (*data == '"') skip = !skip; *pl = *data; ++pl; } ++data; } *pl = '\0'; //添加到map m_fileMap.insert(std::map<std::string,MAP_CONTENT>::value_type(fileName,m_contentMap)); } //读取csv的一行.line:一行的内容 void CSVReader::readCSVLine(const char *line,int index) { char value[32768]; //一行最多字节数 if (*line == '\0') return; char *pv[32]; char *tv = value; bool skip = false; int count = 0; *tv = '\0'; pv[count++] = tv; while (*line != '\0') { if (*line == ',' && !skip) { *tv = '\0'; ++tv; pv[count++] = tv; } else if (*line == '"') { skip = !skip; } else { *tv = *line; ++tv; } ++line; } *tv = '\0'; //临时数组 std::vector<std::pair<std::string,int> > tVector; for (int i=0; i<count; ++i) {tVector.push_back(std::map<std::string,int>::value_type(pv[i],i));} //第一行作为key if(index == 0) {m_firstVector = tVector;} //第2行为注释 else if(index > 1) { //一行的map std::map<string,string> tmp; for (int i = 0; i < m_firstVector.size(); i++) {tmp.insert(std::map<string,string>::value_type(m_firstVector[i].first,tVector[i].first));} m_contentMap.insert(std::map<int,std::map<string,string>>::value_type(atoi(tVector[0].first.c_str()),tmp)); } }
//用法如下
bool HelloWorldScene::init() { ////////////////////////////// CSVReader::getInst()->parse("haha.csv"); MAP_LINE map_line = CSVReader::getInst()->getLineMap("haha.csv",1000000); MAP_LINE::iterator it = map_line.begin(); while (it != map_line.end()) { cclog("key:%s,value:%s",it->first.c_str(),it->second.c_str()); it++; } }
//csv表格数据如下图
我们今天的关于cocos2dx读取excel文件和cocos读取配置文件的分享已经告一段落,感谢您的关注,如果您想了解更多关于(4)cocos2dx读取csv数据文件、
本文标签: