支持PhysicsEditor生成的关于box2d的pilst文件的解析文件 ,支持cocos2d-x-2.1.5 亲测通过
来源:互联网 发布:属于网络传输威胁的是 编辑:程序博客网 时间:2024/05/16 09:58
GB2ShapeCache_x.h
#ifndef __CCSHAPECACHE_H__#define __CCSHAPECACHE_H__#include "cocos2d.h"USING_NS_CC;class BodyDef;class b2Body;namespace cocos2d { class GB2ShapeCache : public CCObject{ public: static GB2ShapeCache* sharedGB2ShapeCache(void); public: bool init(); void addShapesWithFile(const std::string &plist); void addFixturesToBody(b2Body *body, const std::string &shape); cocos2d::CCPoint anchorPointForShape(const std::string &shape); void reset(); float getPtmRatio() { return ptmRatio; } ~GB2ShapeCache() {} private: std::map<std::string, BodyDef *> shapeObjects; GB2ShapeCache(void) {} float ptmRatio; };}#endif /* defined(__CCSHAPECACHE_H__) */
GB2ShapeCache_x.cpp
#include "GB2ShapeCache_x.h"#include "Box2D/Box2D.h"#include "..\cocoa\CCNS.h"using namespace cocos2d;/** * Internal class to hold the fixtures */class FixtureDef {public: FixtureDef() : next(NULL) {} ~FixtureDef() { delete next; delete fixture.shape; } FixtureDef *next; b2FixtureDef fixture; int callbackData;};class BodyDef {public:BodyDef(): fixtures(NULL) {}~BodyDef() {if (fixtures)delete fixtures;}FixtureDef *fixtures;CCPoint anchorPoint;};static GB2ShapeCache *_sharedGB2ShapeCache = NULL;GB2ShapeCache* GB2ShapeCache::sharedGB2ShapeCache(void) {if (!_sharedGB2ShapeCache) {_sharedGB2ShapeCache = new GB2ShapeCache(); _sharedGB2ShapeCache->init();}return _sharedGB2ShapeCache;}bool GB2ShapeCache::init() {return true;}void GB2ShapeCache::reset() {std::map<std::string, BodyDef *>::iterator iter;for (iter = shapeObjects.begin() ; iter != shapeObjects.end() ; ++iter) {delete iter->second;}shapeObjects.clear();}void GB2ShapeCache::addFixturesToBody(b2Body *body, const std::string &shape) {std::map<std::string, BodyDef *>::iterator pos = shapeObjects.find(shape);assert(pos != shapeObjects.end());BodyDef *so = (*pos).second;FixtureDef *fix = so->fixtures; while (fix) { body->CreateFixture(&fix->fixture); fix = fix->next; }}cocos2d::CCPoint GB2ShapeCache::anchorPointForShape(const std::string &shape) {std::map<std::string, BodyDef *>::iterator pos = shapeObjects.find(shape);assert(pos != shapeObjects.end());BodyDef *bd = (*pos).second;return bd->anchorPoint;}void GB2ShapeCache::addShapesWithFile(const std::string &plist) {CCDictionary *dict = CCDictionary::createWithContentsOfFileThreadSafe(plist.c_str());CCAssert(dict != NULL, "Shape-file not found"); // not triggered - cocos2dx delivers empty dict if non was found CCAssert(dict->count() != 0, "plist file empty or not existing");CCDictionary *metadataDict = (CCDictionary *)dict->objectForKey("metadata"); int format = static_cast<CCString *>(metadataDict->objectForKey("format"))->intValue(); ptmRatio = static_cast<CCString *>(metadataDict->objectForKey("ptm_ratio"))->floatValue();CCAssert(format == 1, "Format not supported");CCDictionary *bodyDict = (CCDictionary *)dict->objectForKey("bodies"); b2Vec2 vertices[b2_maxPolygonVertices];CCDictElement* pElement = NULL;CCDICT_FOREACH(bodyDict, pElement){BodyDef *bodyDef = new BodyDef();CCString *bodyName = ccs(pElement->getStrKey());CCDictionary *bodyData = (CCDictionary *)pElement->getObject();bodyDef->anchorPoint = CCPointFromString(static_cast<CCString *>(bodyData->objectForKey("anchorpoint"))->getCString());CCArray *fixtureList = (CCArray *)bodyData->objectForKey("fixtures");FixtureDef **nextFixtureDef = &(bodyDef->fixtures);CCObject *fixture = NULL;CCARRAY_FOREACH(fixtureList, fixture){b2FixtureDef basicData;CCDictionary *fixtureData = (CCDictionary *)fixture;int callbackData = 0;basicData.filter.categoryBits = static_cast<CCString *>(fixtureData->objectForKey("filter_categoryBits"))->intValue(); basicData.filter.maskBits = static_cast<CCString *>(fixtureData->objectForKey("filter_maskBits"))->intValue(); basicData.filter.groupIndex = static_cast<CCString *>(fixtureData->objectForKey("filter_groupIndex"))->intValue(); basicData.friction = static_cast<CCString *>(fixtureData->objectForKey("friction"))->floatValue(); basicData.density = static_cast<CCString *>(fixtureData->objectForKey("density"))->floatValue(); basicData.restitution = static_cast<CCString *>(fixtureData->objectForKey("restitution"))->floatValue(); basicData.isSensor = (bool)static_cast<CCString *>(fixtureData->objectForKey("isSensor"))->intValue();if(fixtureData->objectForKey("id")){basicData.userData = static_cast<CCString *>(fixtureData->objectForKey("id"));callbackData = static_cast<CCString *>(fixtureData->objectForKey("id"))->intValue();}std::string fixtureType = static_cast<CCString *>(fixtureData->objectForKey("fixture_type"))->getCString();//CCString *fixtureType = static_cast<CCString *>(fixtureData->objectForKey("fixture_type"))->getCString();if (fixtureType == "POLYGON") {//CCDictionary *polygons = (CCDictionary *)fixtureData->objectForKey("polygons");CCArray *polygons = (CCArray *)fixtureData->objectForKey("polygons");//CCDictElement *polygon = NULL;CCObject *polygon = NULL;//CCDICT_FOREACH(polygons, polygon)CCARRAY_FOREACH(polygons, polygon){FixtureDef *fix = new FixtureDef();fix->fixture = basicData; // copy basic datafix->callbackData = callbackData;b2PolygonShape *polyshape = new b2PolygonShape(); int vindex = 0;//CCDictionary *polygonData = (CCDictionary *)polygon->getObject();CCArray *polygonData = (CCArray *)polygon;assert(polygonData->count() <= b2_maxPolygonVertices);//CCDictElement *offset = NULL;CCObject *offset = NULL;//CCDICT_FOREACH(polygonData, offset)CCARRAY_FOREACH(polygonData, offset){CCString *pStr = (CCString *)offset;CCPoint p = CCPointFromString(pStr->getCString());vertices[vindex].x = (p.x / ptmRatio) ; vertices[vindex].y = (p.y / ptmRatio) ; vindex++;}polyshape->Set(vertices, vindex); fix->fixture.shape = polyshape;*nextFixtureDef = fix; nextFixtureDef = &(fix->next);}} else if (fixtureType == "CIRCLE") {FixtureDef *fix = new FixtureDef(); fix->fixture = basicData; // copy basic data fix->callbackData = callbackData; CCDictionary *circleData = (CCDictionary *)fixtureData->objectForKey("circle"); b2CircleShape *circleShape = new b2CircleShape(); circleShape->m_radius = static_cast<CCString *>(circleData->objectForKey("radius"))->floatValue() / ptmRatio;CCPoint p = CCPointFromString(static_cast<CCString *>(circleData->objectForKey("position"))->getCString()); circleShape->m_p = b2Vec2(p.x / ptmRatio, p.y / ptmRatio); fix->fixture.shape = circleShape; // create a list *nextFixtureDef = fix; nextFixtureDef = &(fix->next);} else {CCAssert(0, "Unknown fixtureType");}// add the body element to the hashshapeObjects[bodyName->getCString()] = bodyDef;}}}
- 支持PhysicsEditor生成的关于box2d的pilst文件的解析文件 ,支持cocos2d-x-2.1.5 亲测通过
- 用于解析PhysicsEditor生成的plist文件 ,支持cocos2d-x-2.1.5 亲测通过
- cocos2d-x+box2d+PhysicsEditor+win7强大的box2d工具
- 【Cocos2d-x】PhysicsEditor的使用
- 让quick-cocos2d-x支持加密的plist文件
- 对iOS中pilst文件的修改
- 【Cocos2d-x】支持 i18n 国际化(2)——i18n XML 解析生成头文件
- cocos2d-x 3.0加载PhysicsEditor生成plist的加载器并生成CCPhysicsBody
- cocos2d-x 3.0加载PhysicsEditor生成plist的加载器并生成CCPhysicsBody
- directx10不再支持.x文件的解决办法
- Cocos2d-x tinyxml2用于xml的解析,自动生成xml文件和解析xml文件
- 关于cocos2d-x对etc1图片支持的分析
- 关于cocos2d-x对etc1图片支持的分析
- 关于cocos2d-x对etc1图片支持的分析
- 关于cocos2d-x对etc1图片支持的分析
- 关于cocos2d-x对etc1图片支持的分析
- 关于cocos2d-x对etc1图片支持的分析
- 玩转cocos2d-x(二)支持PhysicsEditor
- 项目管理摘要
- Ubuntu 查看磁盘空间大小命令
- hadoop的datanode异常结束
- 内核对象
- LabVIEW比较数值数组元素大小
- 支持PhysicsEditor生成的关于box2d的pilst文件的解析文件 ,支持cocos2d-x-2.1.5 亲测通过
- ubuntu12.04安装QQ2012教程
- tcpdump examples
- Android电池电量更新 - BatteryService(转)
- 初探使用iOS 7 Sprite Kit与Cocos2d开发游戏的对比
- rpm升级 配置文件处理规则
- Python pip 安装
- Android NDK开发----- Java与C互相调用实例详解
- 通过request获得客户机访问的ip地址