cocos2dx 2.x 骨骼动画优化

时间:2021-03-27 07:02:05

本文原链接:http://www.cnblogs.com/zouzf/p/4450861.html

公司用的骨骼动画的版本貌似还停留在2.1之前的年代而已没有更新,该因各种历史原因吧,而有个大项目“一直”处于马上发布准备大推的阶段,没人敢动。恩,公司的骨骼动画貌似是用Flash做然后通过插件导出成 plist、png、xml格式的,现在,大项目负责人说骨骼动画卡,要优化,恩,交给我来做~~~

前期分析

通过耗时比较,90%的时间消耗在了 CCDataReaderHelper::addDataFromCache 这个方法里,其中90%的时间又花在 CCDataReaderHelper::decodeAnimation 这里。一看xml文件:3M多,25600多行,其中 animation部分有25000行。。。通过层层筛选分析,得出结论:xml文件过大导致xml树节点过多,最终导致遍历xml树构建 CCArmatureData、CCAnimationData、CCMovmentBoneData、CCFrameData等数据时消耗过大,其中遍历xml树时的查询也是比较耗时的一个环节。

方案选择

在涉及到文件的优化方式里,序列化一直是考虑首选。查看了 CCBoneData、CCFrameData的类之后,发现它们的数据成员除了 name 是string之外 其他的都是 int、float、bool,挺好的,比xml优化那时的满地字符串好多了,成员name可以用char[],大小固定64差不多了,就这样愉快地决定了用序列化来优化。

大概思路

CCArmatureData、CCAnimationData等几个类的关系是:CCArmatureData->CCBoneData->CCDisplayData; CCAnimationData->CCMovmentData->CCMovmentBoneData->CCFrameData; CCTextureData->CCContourData->CCContourVertex2,组合关系用了CCDictionary和CCArray。定义一系列struct和上面那十个类一一对应,如 arm_struct、ani_struct等,把类的数据都存到struct里,然后把struct直接写到文本;加载的时候就读文本,把数据写到struct里,然后根据struct构建出CCArmatureData等数据。

细节实现

CCArmatureData等类和struct之间转换时怎么实现呢?深度优先和广度优先二选一,由于CCArmatureData等类之间的关系是包含关系,就是一棵树,深度优先会更好一点。

骨骼数据转struct:(本来写了很多的,但又删了,还是代码说得清楚点)

 struct CC_DLL skeleton_struct
{
char name[];
float version; //version
int childCount_arm;
int childCount_arm_b;
int childCount_arm_d;
int childCount_ani;
int childCount_ani_m;
int childCount_ani_b;
int childCount_ani_f;
int childCount_tex;
int childCount_tex_con;
int childCount_tex_vt; //float frameRate; //no work yet?
}; // CCArmatureData
struct CC_DLL Armature_struct
{
//有多个struct_Armature_b
char name[];
int child_count;
int child_index;
}; // CCBoneData
struct CC_DLL Armature_b_struct
{
//有多个struct_Armature_b_d // BaseData_struct baseData;//只用到了 只Order 属性
char name[];
char parentName[];
int child_count;
int child_index;
int zOrder;
}; // CCDisplayData
struct CC_DLL Armature_b_d_struct
{
char name[];
int displayType; //displayType base on this gay
// int child_count;
// int child_index; //float pX; //no useed in cocos2dx
//float pY; //no useed in cocos2dx
};
 /**
* 深度遍历 CCArmatureData
*
* @return 返回 true 表示转换成功
*/
bool CCDataReaderHelper::ArmatureDataToStructData()
{
int child_index_arm = ;
int child_index_arm_b = ;
bool result = true; CCDictionary* arm_datas = CCArmatureDataManager::sharedArmatureDataManager()->getArmarureDatas();
CCDictElement* pArmElement;
CCDICT_FOREACH(arm_datas, pArmElement)
{
CCArmatureData* arm = (CCArmatureData*)pArmElement->getObject(); /* save data from CCArmatureData object to Armature_struct */
Armature_struct armStruct; /* set child info */
armStruct.child_count = arm->boneDataDic.count();
armStruct.child_index = child_index_arm;
child_index_arm += armStruct.child_count; /* length of name is beyond 63 */
if (isNameIllegal(arm->name))
{
result = false;
strncpy(armStruct.name, arm->name.c_str(), );
}
else
{
strncpy(armStruct.name, arm->name.c_str(), arm->name.length() + );
} wydArmLst.push_back(armStruct); /* ergodic CCBoneData in one CCArmatureData */
CCDictElement* pArmBElement;
CCDictionary* arm_b_dic = &(arm->boneDataDic);
CCDICT_FOREACH(arm_b_dic, pArmBElement)
{
CCBoneData* bone = (CCBoneData*)pArmBElement->getObject(); /* save data from CCBoneData object to Armature_b_struct */
Armature_b_struct boneStruct; boneStruct.zOrder = bone->zOrder;
// strcpy(boneStruct.name, bone->name.c_str());
// boneStruct.skewX = bone->skewX;
// boneStruct.skewY = bone->skewY;
// boneStruct.tweenRotate = bone->tweenRotate; boneStruct.child_count = bone->displayDataList.count();
boneStruct.child_index = child_index_arm_b;
child_index_arm_b += boneStruct.child_count; if (isNameIllegal(bone->name))
{
result = false;
strncpy(boneStruct.name, bone->name.c_str(), );
}
else
{
strncpy(boneStruct.name, bone->name.c_str(), bone->name.length() + );
} if (isNameIllegal(bone->parentName))
{
result = false;
strncpy(boneStruct.parentName, bone->parentName.c_str(), );
}
else
{
strncpy(boneStruct.parentName, bone->parentName.c_str(), bone->parentName.length() + );
} wydArm_bLst.push_back(boneStruct); /* ergodic CCDisplayData in one CCBoneData */
CCArray* displayArr = &(bone->displayDataList);
CCObject* objD;
CCARRAY_FOREACH(displayArr, objD)
{
CCDisplayData* display = (CCDisplayData*) objD; /* save data from CCDisplayData object to Armature_b_d_struct */
Armature_b_d_struct displayStruct;
displayStruct.displayType = display->displayType;//zou
std::string displayName; if (display->displayType == CS_DISPLAY_SPRITE)
{
displayName = ((CCSpriteDisplayData *)display)->displayName;
}
else
{
displayName = ((CCArmatureDisplayData *)display)->displayName;
} if (isNameIllegal(displayName))
{
result = false;
strncpy(displayStruct.name, displayName.c_str(), );
}
else
{
strncpy(displayStruct.name, displayName.c_str(), displayName.length() + );
} wydArm_dLst.push_back(displayStruct);// for write to file }
} } wydSkeleton.childCount_arm = arm_datas->count();
wydSkeleton.childCount_arm_b = child_index_arm;
wydSkeleton.childCount_arm_d = child_index_arm_b; return result;
}

里面的struct用到 child_index和child_count,这个东西用于 struct转armature时控制armature孩子的位置和数量的。上面代码的大概意思就是:遍历CCArmatureData、CCBoneData、CCDisplayData类,一一创建结构体 arm_struct、arm_b_struct、arm_b_d_struct,每次循环都对应创建一个struct然后加到对应的list列表里。

struct转骨骼数据:

 void CCDataReaderHelper::decodeArmatureStructData()
{
for (int i = ; i < wydSkeleton.childCount_arm; i++)
{ CCArmatureData* arm = CCArmatureData::create();
Armature_struct armStruct = wydArms[i];
arm->name = armStruct.name; for (int j = ; j < armStruct.child_count; j++)
{
CCBoneData* bone = CCBoneData::create();
Armature_b_struct boneStruct = wydArms_b[j + armStruct.child_index];
bone->name = boneStruct.name;
bone->parentName = boneStruct.parentName;
bone->zOrder = boneStruct.zOrder; for (int k = ; k < boneStruct.child_count; k++)
{
CCDisplayData* display;// = CCDisplayData::create();
Armature_b_d_struct displayStruct = wydArms_d[k + boneStruct.child_index]; if ((DisplayType)displayStruct.displayType == CS_DISPLAY_SPRITE)
{
display = CCSpriteDisplayData::create();
display->displayType = CS_DISPLAY_SPRITE;
((CCSpriteDisplayData *)display)->displayName = displayStruct.name;
}
else
{
display = CCArmatureDisplayData::create();
display->displayType = CS_DISPLAY_ARMATURE;
((CCArmatureDisplayData *)display)->displayName = displayStruct.name;
} bone->addDisplayData(display);
} arm->addBoneData(bone);
} s_armatureDataInfo.data1.push_back(arm->name);
CCArmatureDataManager::sharedArmatureDataManager()->addArmatureData(arm->name.c_str(), arm);
}
}

每个类型的struct都对应创建一个数组和一个list列表。

骨骼数组转struct时:遍历CCArmatureData、CCBoneData、CCDisplayData时,每次遍历都对应创建一个对应的struct加到list列表里,重点在于获取到struct对象的child_index和child_count。

struct转骨骼动画时:遍历arm_struct、arm_b_struct、arm_b_d_struct对应的数组,每次遍历都对应创建一个CCArmatureData、CCBoneData、CCDisplayData,重点在于根据struct对象的child_index和child_count来控制循环的次数、子节点在struct数组里的起始位置

优化效果

那个3M多、25600多行的xml文件,转成struct保存好,如果struct里没有 strMovement、strEvent 、strSound、strSoundEffect 这四个字段的话,大小是原来的一半,如果有这四个字节,大小是原来的两倍,其实,cocos2dx里对这四个字段的备注是: m_strMovement, m_strEvent, m_strSound, m_strSoundEffect do not support yet(2.1)。解析耗时这块,耗时大概减少80%~90%,甚是可观。

本文原链接:http://www.cnblogs.com/zouzf/p/4450861.html