From f02c12300133558efecd9d9b820e98f84575d62e Mon Sep 17 00:00:00 2001 From: Mikko Mononen Date: Tue, 16 Feb 2010 08:45:42 +0000 Subject: [PATCH] debug draw display list class. --- DebugUtils/Include/DebugDraw.h | 25 ++++++++++ DebugUtils/Source/DebugDraw.cpp | 81 +++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/DebugUtils/Include/DebugDraw.h b/DebugUtils/Include/DebugDraw.h index ec650c3..0372941 100644 --- a/DebugUtils/Include/DebugDraw.h +++ b/DebugUtils/Include/DebugDraw.h @@ -127,5 +127,30 @@ void duAppendBox(struct duDebugDraw* dd, float minx, float miny, float minz, float maxx, float maxy, float maxz, const unsigned int* fcol); +class duDisplayList : public duDebugDraw +{ + float* m_pos; + unsigned int* m_color; + int m_size; + int m_cap; + + bool m_depthMask; + duDebugDrawPrimitives m_prim; + float m_primSize; + + void resize(int cap); + +public: + duDisplayList(int cap = 512); + ~duDisplayList(); + virtual void depthMask(bool state); + virtual void begin(duDebugDrawPrimitives prim, float size = 1.0f); + virtual void vertex(const float x, const float y, const float z, unsigned int color); + virtual void vertex(const float* pos, unsigned int color); + virtual void end() {} + void clear(); + void draw(struct duDebugDraw* dd); +}; + #endif // DEBUGDRAW_H diff --git a/DebugUtils/Source/DebugDraw.cpp b/DebugUtils/Source/DebugDraw.cpp index bcaeed4..6cdf897 100644 --- a/DebugUtils/Source/DebugDraw.cpp +++ b/DebugUtils/Source/DebugDraw.cpp @@ -18,6 +18,7 @@ #define _USE_MATH_DEFINES #include +#include #include "DebugDraw.h" inline int bit(int a, int b) @@ -388,4 +389,84 @@ void duAppendCross(struct duDebugDraw* dd, const float x, const float y, const f dd->vertex(x,y,z+s, col); } +duDisplayList::duDisplayList(int cap) : + m_pos(0), + m_color(0), + m_size(0), + m_cap(0), + m_depthMask(true), + m_prim(DU_DRAW_LINES), + m_primSize(1.0f) +{ + if (cap < 8) + cap = 8; + resize(cap); +} +duDisplayList::~duDisplayList() +{ + delete [] m_pos; + delete [] m_color; +} + +void duDisplayList::resize(int cap) +{ + float* newPos = new float[cap*3]; + if (m_size) + memcpy(newPos, m_pos, sizeof(float)*3*m_size); + delete [] m_pos; + m_pos = newPos; + + unsigned int* newColor = new unsigned int[cap]; + if (m_size) + memcpy(newColor, m_color, sizeof(unsigned int)*m_size); + delete [] m_color; + m_color = newColor; + + m_cap = cap; +} + +void duDisplayList::clear() +{ + m_size = 0; +} + +void duDisplayList::depthMask(bool state) +{ + m_depthMask = state; +} + +void duDisplayList::begin(duDebugDrawPrimitives prim, float size) +{ + clear(); + m_prim = prim; + m_primSize = size; +} + +void duDisplayList::vertex(const float x, const float y, const float z, unsigned int color) +{ + if (m_size+1 >= m_cap) + resize(m_cap*2); + float* p = &m_pos[m_size*3]; + p[0] = x; + p[1] = y; + p[2] = z; + m_color[m_size] = color; + m_size++; +} + +void duDisplayList::vertex(const float* pos, unsigned int color) +{ + vertex(pos[0],pos[1],pos[2],color); +} + +void duDisplayList::draw(struct duDebugDraw* dd) +{ + if (!m_size) + return; + dd->depthMask(m_depthMask); + dd->begin(m_prim, m_primSize); + for (int i = 0; i < m_size; ++i) + dd->vertex(&m_pos[i*3], m_color[i]); + dd->end(); +}