Forum > C++ > DebugDraw - draw lines, points easy

DebugDraw - draw lines, points easy

By dualface Posted 2011-09-15 01:43 Comments 8
dualface
  • Posts: 61

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));
    ....
}
zhangxm
  • Posts: 2082
  • Location: Xiamen, China

#1 RE: 2011-09-15 01:48

Thank you for your contribution.

walzer
  • Posts: 1735
  • Location: Xiamen, China

#2 RE: 2011-09-15 23:02

I’ve added the link to sticky FAQ.

Less is More. Keep Focus.

rophiroth
  • Posts: 12

#3 RE: 2012-07-15 22:01

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

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

walzer
  • Posts: 1735
  • Location: Xiamen, China

#4 RE: 2012-07-16 12:38

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

Less is More. Keep Focus.

RavalMatic
  • Posts: 25
  • Location: Barcelona

#5 RE: 2012-10-08 14:06

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?

dualface
  • Posts: 61

#6 RE: 2012-10-10 12:02

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
  • Posts: 25
  • Location: Barcelona

#7 RE: 2012-10-12 16:34

Thanks Yulei!

seao
  • Posts: 1

#8 RE: 2014-02-12 10:01

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

DebugDraw.h

#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* m_lines;
    std::vector* 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;
    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);
        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);
    }
}
Loggin to reply

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps