반응형
디버깅용으로 선을 그려야 할 때가 있다.
이 때 스프라이트를 상속받아 draw함수를 오버라이딩해서 openGL 코드를 사용해야 하는데,
이 걸 cocos2d-x 커뮤니티에서 dualface라는 분이 클래스화 시켜 놓으셨다.
그런데 이게 2.0 기준으로 작성되어서 경고가 좀 뜨길래, 3.0버전에 맞게 수정했다.
원본 링크 : http://www.cocos2d-x.org/forums/6/topics/3831?r=44760
DebugDraw.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include "cocos2d.h"
#include <vector>
using namespace cocos2d;
typedef struct
{
Point pt1;
Point pt2;
float r;
float g;
float b;
} DebugLine;
typedef struct
{
Point pt;
float r;
float g;
float b;
} DebugPoint;
class DebugDraw : public Node
{
public:
static DebugDraw* create();
DebugDraw();
~DebugDraw();
virtual void draw(void);
void appendLine(Point pt1, Point pt2, float r = 1, float g = 1, float b = 1);
void appendPoint(float x, float y, float r = 1, float g = 1, float b = 1);
void appendPoint(Point pt, float r = 1, float g = 1, float b = 1);
private:
std::vector<DebugLine>* m_lines;
std::vector<DebugPoint>* m_points;
};
|
cs |
DebugDraw.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#include "DebugDraw.h"
DebugDraw* DebugDraw::create()
{
DebugDraw* draw = new DebugDraw();
draw->autorelease();
return draw;
}
DebugDraw::DebugDraw()
{
m_lines = new std::vector<DebugLine>;
m_points = new std::vector<DebugPoint>;
}
DebugDraw::~DebugDraw()
{
delete m_lines;
delete m_points;
}
void DebugDraw::draw(void)
{
int c = m_lines->size();
for (int i = 0; i < c; i++)
{
DebugLine line = m_lines->at(i);
//glColor4f(line.r, line.g, line.b, 1);
DrawPrimitives::setDrawColor4F(line.r, line.g, line.b, 1);
//ccDrawLine(line.pt1, line.pt2);
DrawPrimitives::drawLine(line.pt1, line.pt2);
}
c = m_points->size();
for (int i = 0; i < c; i++)
{
DebugPoint pt = m_points->at(i);
//glColor4f(pt.r, pt.g, pt.b, 1);
DrawPrimitives::setDrawColor4F(pt.r, pt.g, pt.b, 1);
//ccDrawPoint(pt.pt);
DrawPrimitives::drawPoint(pt.pt);
}
}
void DebugDraw::appendLine(Point pt1, Point pt2, float r, float g, float b)
{
DebugLine line;
line.pt1 = pt1;
line.pt2 = pt2;
line.r = r;
line.g = g;
line.b = b;
m_lines->push_back(line);
}
void DebugDraw::appendPoint(float x, float y, float r, float g, float b)
{
appendPoint(Point(x, y), r, g, b);
}
void DebugDraw::appendPoint(Point pt, float r, float g, float b)
{
DebugPoint dp;
dp.pt = pt;
dp.r = r;
dp.g = g;
dp.b = b;
m_points->push_back(dp);
}
|
cs |
사용 예
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class MyScene : public CCScene { .... DebugDraw* m_debugDraw; .... } CCScene::CCScene() { m_debugDraw = DebugDraw::create(); scene->addChild(debugDraw); m_debugDraw->appendLine(ccp(0, 0), ccp(100, 100)); .... } | cs |
반응형
'C++' 카테고리의 다른 글
L1 캐시는 왜 Data와 Inst로 나뉘어 있나요? (번역) (0) | 2020.12.13 |
---|---|
TBB - 병렬 컨테이너 사용 (concurrent_hash_map) : GameServer에서 유저의 <id, index> 정보 관리 (0) | 2020.12.13 |
cocos2d-x사용 시 rc.exe이(가) 종료되었습니다 해결법 (0) | 2020.12.13 |
SKY BOX 제작 툴 : CubeMapGen (0) | 2020.12.12 |
Volatile : 멀티쓰레드 프로그래밍 시 거의 쓸모 없는 그 것 (0) | 2020.12.12 |