您的位置:首页 > 移动开发 > Cocos引擎

特效编辑器开发手记2——cocos2d-x粒子系统的plist文件

2012-07-24 10:49 447 查看
粒子编辑器必须要有一个保存粒子文件的功能,cocos2d-x使用plist来保存粒子文件(这是苹果的一种基于xml的文件格式,也适用于动画等)
于是我查看了cocos2d-x里面加载plist文件的代码,它是先把plist加载到一个CCDictionary类中,然后在这个类里面方便地通过key-value来获取相应的信息

CCParticleSystem.cpp
m_sPlistFile = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(plistFile);
CCDictionary *dict = CCDictionary::createWithContentsOfFileThreadSafe(m_sPlistFile.c_str());

CCDictionary.cpp
CCDictionary* CCDictionary::createWithContentsOfFileThreadSafe(const char *pFileName)
{
return ccFileUtils_dictionaryWithContentsOfFileThreadSafe(pFileName);
}

CCFileUtilsCommon_cpp.h
CCDictionary* ccFileUtils_dictionaryWithContentsOfFileThreadSafe(const char *pFileName)
{
CCDictMaker tMaker;
return tMaker.dictionaryWithContentsOfFile(pFileName);
}

CCFileUtilsCommon_cpp.h
CCDictionary* dictionaryWithContentsOfFile(const char *pFileName)
{
m_eResultType = SAX_RESULT_DICT;
CCSAXParser parser;

if (false == parser.init("UTF-8"))
{
return NULL;
}
parser.setDelegator(this);

parser.parse(pFileName);
return m_pRootDict;
}

bool CCSAXParser::parse(const char *pszFile)
{
bool bRet = false;
unsigned long size = 0;
char* pBuffer = (char*)CCFileUtils::sharedFileUtils()->getFileData(pszFile, "rt", &size);
if (pBuffer != NULL && size > 0)
{
bRet = parse(pBuffer, size);
}
CC_SAFE_DELETE_ARRAY(pBuffer);
return bRet;
}

bool CCSAXParser::parse(const char* pXMLData, unsigned int uDataLength)
{
/*
* this initialize the library and check potential ABI mismatches
* between the version it was compiled for and the actual shared
* library used.
*/
LIBXML_TEST_VERSION
xmlSAXHandler saxHandler;
memset( &saxHandler, 0, sizeof(saxHandler) );
// Using xmlSAXVersion( &saxHandler, 2 ) generate crash as it sets plenty of other pointers...
saxHandler.initialized = XML_SAX2_MAGIC;  // so we do this to force parsing as SAX2.
saxHandler.startElement = &CCSAXParser::startElement;
saxHandler.endElement = &CCSAXParser::endElement;
saxHandler.characters = &CCSAXParser::textHandler;

int result = xmlSAXUserParseMemory( &saxHandler, this, pXMLData, uDataLength );
if ( result != 0 )
{
return false;
}
/*
* Cleanup function for the XML library.
*/
xmlCleanupParser();
/*
* this is to debug memory for regression tests
*/
#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
xmlMemoryDump();
#endif

return true;
}


通过上面的代码可以看到,CCDictMaker通过调用CCSAXParser 这个类来进行plist文件的读取和解析,也就是 parse函数,然后生成一个CCDictionary
通过CCSAXParser 自己的startElement和endElement来对plist进行解析,然后插入到CCDictionary的哈希中,上层调用valueforkey即可获取对应的值
但说了这么多,我们如何保存呢?cocos2d-x提供了怎样的接口给我们进行保存呢?

查阅CCDictionary这个类,发现与valueforkey有那么点对应关系的,也就setObject这个函数,但这个函数只是把Object插入到CCDictionary本身的哈希中而已,并没有保存到plist,所以这里CCDictionary这个类跟plist唯一的联系仅仅只是从plist中解析,获取数据而已。

那么我们只能把目光转移到生成CCDictionary的CCDictMaker 这个类来了,CCDictionary 是由CCDictMaker调用CCSAXParser 生成的,那么CCDictMaker 可有提供保存到plist的方法?答案还是没有!

最后到了CCSAXParser这个类,只能看到,他调用xmlSAXUserParseMemory函数来将plist文件加载进来,至于xmlSAXUserParseMemory函数,那是在libxml/parser.h这个头文件中。

随后又查看了大量代码,从头到尾都没有保存plist文件的代码,至于CCUserDefault这个类,仅仅只是将数据保存在一个默认的xml文件中。

到了这里,也就不指望cocos提供什么接口了,直接查看粒子系统的plist文件,然后,根据其格式,自己实现一个plist类,用来保存粒子系统到plist文件。

plist文件格式大致如下,第一行是xml头,包含xml版本和字符编码
第二行是文档类型,"-//Apple//DTD PLIST 1.0//EN"是它的pubid "http://www.apple.com/DTDs/PropertyList-1.0.dtd"是它的sysid
第三行是plist的根节点,有version属性,下面有一个子节点dict,该子节点下面存放粒子系统的所有属性值
每个值由两个节点来表示,一个key节点,紧跟着还有一个value节点来表这个key的值,value节点有以下几种类型:<real>表示浮点数,<integer>表示整型,<string>表示字符串,<array>表示数组

<?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="1.0">
<dict>
<key>angle</key>
<real>270</real>
<key>angleVariance</key>
<real>0.0</real>
......
</dict>
</plist>


关于xml的操作,之前用过CMarkUpSTL,tinyxml,这两个用得比较熟,但cocos用的是libxml,也只能去学习一下libxml
这是官网地址http://www.xmlsoft.org/
还有相关example的地址http://www.xmlsoft.org/examples/testWriter.c
在项目中实现一段如下的测试代码来保存plist

//创建test.plist文件
m_Writer = xmlNewTextWriterFilename(plist, 0);

//设置编码为utf-8并开始文档
xmlTextWriterStartDocument(m_Writer, NULL, "UTF-8", NULL);
//设置文档类型
xmlTextWriterWriteDTD(m_Writer, BAD_CAST "plist", BAD_CAST "-//Apple//DTD PLIST 1.0//EN", BAD_CAST "http://www.apple.com/DTDs/PropertyList-1.0.dtd", NULL);
//根节点为plist,version属性为1.0
xmlTextWriterStartElement(m_Writer, BAD_CAST "plist");
xmlTextWriterWriteAttribute(m_Writer, BAD_CAST "version", BAD_CAST "1.0");

//dict节点
xmlTextWriterStartElement(m_Writer, BAD_CAST "dict");

//写入测试数据
xmlTextWriterWriteElement(m_Writer, BAD_CAST "key", BAD_CAST "key1");
xmlTextWriterWriteElement(m_Writer, BAD_CAST "real", BAD_CAST "key1");

//写入Base64数据
xmlTextWriterStartElement(m_Writer, BAD_CAST "Texture");
xmlTextWriterWriteBase64(m_Writer, "abcdefghij", 0, 12);
xmlTextWriterEndElement(m_Writer);

//结束并保存
xmlTextWriterEndDocument(m_Writer);


查看保存后的plist文件,发现内容是正确的,但既无缩进,又无回车,所有的代码都被写在一行,难看之极,谷歌之,需要在打开文档之前加入xmlKeepBlanksDefault(0);加了进去也毫无效果,那便算了,反正plist文件一般也是只用不看,有时间看一下libxml的源码再研究下吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐