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();

    DebugDraw();
    ~DebugDraw();
    virtual void draw(void);

    void appendLine(CCPoint pt1, CCPoint 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(CCPoint pt, float r = 1, float g = 1, float b = 1);

private:
    std::vector* m_lines;
    std::vector* m_points;
};

DebugDraw.cpp

DebugDraw* DebugDraw::create()
{
    DebugDraw* draw = new DebugDraw();
    draw->autorelease();
    return draw;
}

DebugDraw::DebugDraw()
{
    m_lines = new std::vector();
    m_points = new std::vector();
}

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);
        ccDrawLine(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);
        ccDrawPoint(pt.pt);
    }
}

void DebugDraw::appendLine(CCPoint pt1, CCPoint 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(ccp(x, y), r, g, b);
}

void DebugDraw::appendPoint(CCPoint 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);
}

How to use:

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));
    ....
}

Thank you for your contribution.

I’ve added the link to sticky FAQ.

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

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

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

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?

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);

Thanks Yulei!

It’s a fixed code in cocos2d-x 3.0 beta to no warning
thanks

DebugDraw.h

#include "cocos2d.h"
#include &lsaquo;vector&rsaquo;
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;
};

DebugDraw.cpp

#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);
    }
}

displays nothing for me ?
Any idea what blank display, no errors or anything thought:(