cocos2d-x 2.14 运行 PhysicsEditor Demo(更新GB2ShapeCache-x)

来源:互联网 发布:软件证书过期怎么办 编辑:程序博客网 时间:2024/05/06 09:53

 

由于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());#endifCCDictionary *dict = CCDictionary::createWithContentsOfFile(fullName.c_str());CCAssert(dict != NULL, "Shape-file not found");// not triggered - cocos2dx delivers empty dict if non was foundCCAssert(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 datafix->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);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 datafix->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[pElement->getStrKey()] = bodyDef;}}


以下是完整DEMO

 

跳到资源页面

原创粉丝点击