C++

cocos2d-x 3.0 draw line (선 그리기)

Sorting 2020. 12. 13. 13:51
반응형

디버깅용으로 선을 그려야 할 때가 있다.

 

이 때 스프라이트를 상속받아 draw함수를 오버라이딩해서 openGL 코드를 사용해야 하는데,

 

이 걸 cocos2d-x 커뮤니티에서 dualface라는 분이 클래스화 시켜 놓으셨다.

 

그런데 이게 2.0 기준으로 작성되어서 경고가 좀 뜨길래, 3.0버전에 맞게 수정했다.

 

원본 링크 : http://www.cocos2d-x.org/forums/6/topics/3831?r=44760

 

DebugDraw - draw lines, points easy

DebugDraw.h #include "cocos2d.h" typedef struct { CCPoint pt1; CCPoint pt2; float r; float g; float b; } DebugLine; typedef struct { CCPoint pt; float r; float g; float b; } DebugPoint; class DebugDraw : public CCNode { public: static DebugDraw* create();

discuss.cocos2d-x.org

 

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 = 1float g = 1float b = 1);
    void appendPoint(float x, float y, float r = 1float g = 1float b = 1);
    void appendPoint(Point pt, float r = 1float g = 1float 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(00), ccp(100100));
    ....
}
cs
반응형