3.0으로 바뀐뒤 디버그 모드 사용하는 소스를 정리하였습니다.
cpp파일
#include "MyDebug.h"
using namespace cocos2d;
Scene * MyDebug::createScene() {
auto *scene = Scene::create();
auto *layer = MyDebug::create();
scene->addChild(layer);
return scene;
}
bool MyDebug::init() {
if (!Layer::init()) {
return false;
}
debugDrawStart(true);
this->schedule(schedule_selector(MyDebug::tick));
return true;
}
//디버그 모드 호출 함수(디버그모드 사용하지 않을 경우 월드생성을 다른 곳에서 해야한다.)
/*
cocos2d-x에서 함수의 호출 순서
init();
onEnter();
onEnterTransitionDidFinish();
onExit();
dealloc();
디버그 모드 사용시 월드생성을 먼저 해야 하므로 위 함수 호출 순서를 참고해서 작성할 것
*/
bool MyDebug::debugDrawStart(bool debug) {
b2Vec2 gravity(0.0f, -19.8);
_world = new b2World(gravity);
if (debug) {
m_debugDraw = new GLESDebugDraw(PTM_RATIO);
_world->SetDebugDraw(m_debugDraw);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
m_debugDraw->SetFlags(flags);
}
return true;
}
/*2.x대 버전은 draw()를 오버라이딩 하지만 3.0버전에서는 final로 선언되어 아래 함수를 오버라이딩해야한다.*/
void MyDebug::draw(Renderer * renderer, const kmMat4 &transform, bool transformUpdated) {
ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position);
kmGLPushMatrix();
_world->DrawDebugData();
kmGLPopMatrix();
}
void MyDebug::tick(float dt) {
int velocityIterations = 3;
int positionIterations = 8;
_world->Step(dt, velocityIterations, positionIterations);
}
/*기본적인 box2d의 오브젝트 생성*/
void MyDebug::addNewSpriteAtPosition(Point location) {
b2BodyDef circleDef;
circleDef.position.Set(location.x/PTM_RATIO, location.y/PTM_RATIO);
circleDef.type = b2_dynamicBody;
b2Body *body = _world->CreateBody(&circleDef);
b2CircleShape myshape;
myshape.m_radius = 0.45;
b2FixtureDef fixture;
fixture.shape = &myshape;
fixture.density = 1.0f;
fixture.friction = 0.2f;
fixture.restitution = 0.6f;
body->CreateFixture(&fixture);
}
/* 월드안에 기본적인 바디생성*/
void MyDebug::onEnter() {
b2BodyDef groundDef;
groundDef.position.Set(0, 0);
b2Body *ground = _world->CreateBody(&groundDef);
b2FixtureDef boxEdge;
b2EdgeShape myEdge;
boxEdge.shape = &myEdge;
winSize = Director::getInstance()->getWinSize();
myEdge.Set(b2Vec2(0,0), b2Vec2(winSize.width / PTM_RATIO,0));
ground->CreateFixture(&boxEdge);
myEdge.Set(b2Vec2(0, 0), b2Vec2(0, winSize.height / PTM_RATIO));
ground->CreateFixture(&boxEdge);
myEdge.Set(b2Vec2(0, winSize.height / PTM_RATIO), b2Vec2(winSize.width / PTM_RATIO, winSize.height / PTM_RATIO));
ground->CreateFixture(&boxEdge);
myEdge.Set(b2Vec2(winSize.width / PTM_RATIO, 0), b2Vec2(winSize.width / PTM_RATIO, winSize.height / PTM_RATIO));
ground->CreateFixture(&boxEdge);
Layer::onEnter();
auto *listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(MyDebug::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(MyDebug::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(MyDebug::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
void MyDebug::onExit() {
Layer::onExit();
}
bool MyDebug::onTouchBegan(Touch *touch, Event *event) {
Point mylocation = touch->getLocation();
log("%d, %d", mylocation.x, mylocation.y);
addNewSpriteAtPosition(mylocation);
return true;
}
void MyDebug::onTouchMoved(Touch *touch, Event *event) {}
void MyDebug::onTouchEnded(Touch *touch, Event *event) {}
헤더파일
#ifndef __MyDebug__
#define __MyDebug__
#define PTM_RATIO 32
#include "cocos2d.h"
#include "Box2D/Box2D.h"
#include <GLES-Render.h>
class MyDebug :public cocos2d::Layer {
public:
static cocos2d::Scene * createScene();
virtual bool init();
CREATE_FUNC(MyDebug);
virtual void onEnter();
virtual void onExit();
virtual bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event);
virtual void onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event);
virtual void onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event);
void tick(float dt);
void draw(cocos2d::Renderer *renderer, const kmMat4 &transform, bool transformUpdated);
cocos2d::Size winSize;
b2World *_world;
bool debugDrawStart(bool debug);
GLESDebugDraw * m_debugDraw;
void addNewSpriteAtPosition(cocos2d::Point location);
};
#endif