YuLei Liao DebugDraw - draw lines, points easy
Posts 59
Added by YuLei Liao over 1 year ago

DebugDraw.h

 1#include "cocos2d.h" 
 2
 3typedef struct
 4{
 5    CCPoint pt1;
 6    CCPoint pt2;
 7    float r;
 8    float g;
 9    float b;
10} DebugLine;
11
12typedef struct
13{
14    CCPoint pt;
15    float r;
16    float g;
17    float b;
18} DebugPoint;
19
20class DebugDraw : public CCNode
21{
22public:
23    static DebugDraw* create();
24
25    DebugDraw();
26    ~DebugDraw();
27    virtual void draw(void);
28
29    void appendLine(CCPoint pt1, CCPoint pt2, float r = 1, float g = 1, float b = 1);
30    void appendPoint(float x, float y, float r = 1, float g = 1, float b = 1);
31    void appendPoint(CCPoint pt, float r = 1, float g = 1, float b = 1);
32
33private:
34    std::vector<DebugLine>* m_lines;
35    std::vector<DebugPoint>* m_points;
36};
37

DebugDraw.cpp

 1DebugDraw* DebugDraw::create()
 2{
 3    DebugDraw* draw = new DebugDraw();
 4    draw->autorelease();
 5    return draw;
 6}
 7
 8DebugDraw::DebugDraw()
 9{
10    m_lines = new std::vector<DebugLine>();
11    m_points = new std::vector<DebugPoint>();
12}
13
14DebugDraw::~DebugDraw()
15{
16    delete m_lines;
17    delete m_points;
18}
19
20void DebugDraw::draw(void)
21{
22    int c = m_lines->size();
23    for (int i = 0; i < c; i++)
24    {
25        DebugLine line = m_lines->at(i);
26        glColor4f(line.r, line.g, line.b, 1);
27        ccDrawLine(line.pt1, line.pt2);
28    }
29
30    c = m_points->size();
31    for (int i = 0; i < c; i++)
32    {
33        DebugPoint pt = m_points->at(i);
34        glColor4f(pt.r, pt.g, pt.b, 1);
35        ccDrawPoint(pt.pt);
36    }
37}
38
39void DebugDraw::appendLine(CCPoint pt1, CCPoint pt2, float r, float g, float b)
40{
41    DebugLine line;
42    line.pt1 = pt1;
43    line.pt2 = pt2;
44    line.r = r;
45    line.g = g;
46    line.b = b;
47    m_lines->push_back(line);
48}
49
50void DebugDraw::appendPoint(float x, float y, float r, float g, float b)
51{
52    appendPoint(ccp(x, y), r, g, b);
53}
54
55void DebugDraw::appendPoint(CCPoint pt, float r, float g, float b)
56{
57    DebugPoint dp;
58    dp.pt = pt;
59    dp.r = r;
60    dp.g = g;
61    dp.b = b;
62    m_points->push_back(dp);
63}
64

How to use:

 1
 2class MyScene : public CCScene
 3{
 4    ....
 5
 6    DebugDraw* m_debugDraw;
 7
 8    ....
 9}
10
11CCScene::CCScene()
12{
13    m_debugDraw = DebugDraw::create();
14    scene->addChild(debugDraw);
15
16    m_debugDraw->appendLine(ccp(0, 0), ccp(100, 100));
17    ....
18}
19
Minggo Zhang RE: DebugDraw - draw lines, points easy
Posts 1641
Added by Minggo Zhang over 1 year ago

Thank you for your contribution.

Zhe Wang RE: DebugDraw - draw lines, points easy
Posts 1642
Location Amoy, China
Added by Zhe Wang over 1 year ago

I've added the link to sticky FAQ.

Enjoy Coding, Enjoy Life.

Ro Siade RE: DebugDraw - draw lines, points easy
Posts 12
Added by Ro Siade 10 months ago

Thanks! You forgot put some comment and name of the author :P take some credits man!.

PS: Can you attach files? (lazy mode on)

Zhe Wang RE: DebugDraw - draw lines, points easy
Posts 1642
Location Amoy, China
Added by Zhe Wang 10 months ago

Well, found a programmer lazier than me :)
You can find these functions wrapped as C functions (not C++ methods) in ccDrawPrimitive.cpp

Enjoy Coding, Enjoy Life.

RavalMatic BCN RE: DebugDraw - draw lines, points easy
Posts 9
Location Barcelona
Added by RavalMatic BCN 8 months ago

I'm using cocos2d-2.0-x-2.0.2, added DebugDraw to a node and couldn't make it work.

Are they any changes on the way cocos uses opengl to draw lines and points?

YuLei Liao RE: DebugDraw - draw lines, points easy
Posts 59
Added by YuLei Liao 8 months ago

Enrique Garcia Blanco wrote:

I'm using cocos2d-2.0-x-2.0.2, added DebugDraw to a node and couldn't make it work.

Are they any changes on the way cocos uses opengl to draw lines and points?

for cocos2d-x 2.x, use CCDrawing.

https://github.com/dualface/quick-cocos2d-x/tree/master/hosts/libs/CCDrawing

copy CCDrawing.cpp/.h to your project.

CCCircleShape* circle = CCCircleShape::create(100);
scene:addChild(circle);
RavalMatic BCN RE: DebugDraw - draw lines, points easy
Posts 9
Location Barcelona
Added by RavalMatic BCN 8 months ago

Thanks Yulei!


(1-7/7)