cocos2d-x 2.14 运行 PhysicsEditor Demo(更新GB2ShapeCache-x)
2013-08-28 16:47
429 查看
由于PhysicsEditor1.09不支持cocos2d-x2.14,编译Demo报错,所以上网找资料,发现千篇一律的垃圾代码.
最后终于找到一个能用的http://labs.easymobi.cn/?p=4124,支持cocos2d-2-1.0-x-2.0.4.
遂修改之,以下是GB2ShapeCache-x的代码.2.14亲测可用!
// // HelloWorldScene.h // Demo // // Created by Andreas Löw on 11.01.12. // Copyright codeandweb.de 2012. All rights reserved. // #ifndef __HELLO_WORLD_H__ #define __HELLO_WORLD_H__ // When you import this file, you import all the cocos2d classes #include "cocos2d.h" #include "Box2D/Box2D.h" #include "SimpleAudioEngine.h" #include "GB2ShapeCache-x.h" #include "GLES-Render.h" using namespace cocos2d; class HelloWorld : public cocos2d::CCLayer { public: ~HelloWorld(); HelloWorld(); // returns a Scene that contains the HelloWorld as the only child static cocos2d::CCScene* scene(); // adds a new sprite at a given coordinate void addNewSpriteWithCoords(cocos2d::CCPoint p); virtual void draw(); virtual void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event); void tick(float dt); private: b2World *world; GLESDebugDraw *m_debugDraw; }; #endif // __HELLO_WORLD_H__
#include "GB2ShapeCache-x.h" #include "Box2D/Box2D.h" 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 *_sharedShapeCache = NULL; GB2ShapeCache* GB2ShapeCache::sharedGB2ShapeCache(void) { if (!_sharedShapeCache) { _sharedShapeCache = new GB2ShapeCache(); _sharedShapeCache->init(); } return _sharedShapeCache; } 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) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) std::string fullName = CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile(filename,foldername); #else std::string fullName = CCFileUtils::sharedFileUtils()->fullPathForFilename(plist.c_str()); #endif CCDictionary *dict = CCDictionary::createWithContentsOfFile(fullName.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){ CCDictionary *bodyData = (CCDictionary *)pElement->getObject(); BodyDef *bodyDef = new BodyDef(); bodyDef->anchorPoint = CCPointFromString( static_cast<CCString *>(bodyData ->objectForKey("anchorpoint"))->getCString()); CCArray *fixtureList = (CCArray*)bodyData->objectForKey("fixtures"); FixtureDef **nextFixtureDef = &(bodyDef->fixtures); CCObject *obj; CCARRAY_FOREACH(fixtureList, obj){ b2FixtureDef basicData; CCDictionary *fixtureData = (CCDictionary *)obj; 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"))->floatValue(); CCString *cb = static_cast<CCString *> (fixtureData->objectForKey("userdataCbValue")); int callbackData = 0; if (cb) callbackData = cb->intValue(); std::string fixtureType = static_cast<CCString *> (fixtureData->objectForKey("fixture_type"))->getCString(); if (fixtureType == "POLYGON") { CCArray *polygonsArray = (CCArray *)(fixtureData-> objectForKey("polygons")); CCObject *obj1; CCARRAY_FOREACH(polygonsArray, obj1){ FixtureDef *fix = new FixtureDef(); fix->fixture = basicData; // copy basic data fix->callbackData = callbackData; b2PolygonShape *polyshape = new b2PolygonShape(); int vindex = 0; CCArray *polygonArray = (CCArray *)(obj1); assert(polygonArray->count() <= b2_maxPolygonVertices); CCObject *obj2; CCARRAY_FOREACH(polygonArray, obj2){ CCPoint offset = CCPointFromString(((CCString *)obj2) ->getCString()); vertices[vindex].x = (offset.x / ptmRatio) ; vertices[vindex].y = (offset.y / ptmRatio) ; vindex++; } polyshape->Set(vertices, vindex); 91a4 fix->fixture.shape = polyshape; // create a list *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 hash shapeObjects[pElement->getStrKey()] = bodyDef; } }
以下是完整DEMO
跳到资源页面
相关文章推荐
- cocos2d-x自制工具08:让XCode运行时自动更新资源(改进版)
- Mac下配置Android NDK环境并搭建Cocos2d-x环境并Eclipse正常编译运行Cocos2dX自带TestsDemo项目
- Mac下配置Android NDK环境并搭建Cocos2d-x环境并Eclipse正常编译运行Cocos2dX自带TestsDemo项目
- [QuickX]xcode运行Quick-cocos2d-x项目时自动更新lua资源文件
- 在android真机上运行cocos2d-html5-2.2自带的HelloHTML5World和其它demo
- 关于Cocos2d-x android的环境搭建、Demo运行的注意事项。
- 【iOS-cocos2d-X 游戏开发之三】Mac下配置Android NDK环境并搭建Cocos2d-x环境并Eclipse正常编译运行Cocos2dX自带TestsDemo项目!
- React-Native 运行demo遇到的相关问题(持续更新)
- cocos2d-x v3.0各个环境下创建项目以及编译、运行官方DEMO
- 【cocos2d-x】让XCode运行时自动更新资源【IOS】
- Mac下配置Android NDK环境并搭建Cocos2d-x环境并Eclipse正常编译运行Cocos2dX自带TestsDemo项目!
- Mac下配置Android NDK环境并搭建Cocos2d-x环境并Eclipse正常编译运行Cocos2dX自带TestsDemo项目!
- 【cocos2d-x】让XCode运行时自动更新资源【IOS】
- [QuickX]xcode运行Quick-cocos2d-x项目时自动更新lua资源文件
- cocos2d-x 在mac下运行 demo
- Mac下配置Android NDK环境并搭建Cocos2d-x环境并Eclipse正常编译运行Cocos2dX自带TestsDemo项目(转载)
- 让 cocos2d-x 2.X 版本能使用 physicsEditor (修改文件 GB2ShapeCache-x.cpp)
- CTS运行注意事项(不断更新中...)
- 【目标检测】OpenCV中dnn模块的SSD demo运行