Merge commit 'refs/pull/3938/head' of git://github.com/cocos2d/cocos2d-x into drawnode

Conflicts:
	cocos/2d/CCDrawNode.h
This commit is contained in:
James Chen 2014-01-09 17:03:08 +08:00
commit 2133c0e566
3 changed files with 97 additions and 0 deletions

View File

@ -456,6 +456,86 @@ void DrawNode::drawPolygon(Point *verts, int count, const Color4F &fillColor, fl
free(extrude);
}
void DrawNode::drawTriangle(const Point &p1, const Point &p2, const Point &p3, const Color4F &color)
{
unsigned int vertex_count = 2*3;
ensureCapacity(vertex_count);
Color4B col = Color4B(color);
V2F_C4B_T2F a = {Vertex2F(p1.x, p1.y), col, Tex2F(0.0, 0.0) };
V2F_C4B_T2F b = {Vertex2F(p2.x, p2.y), col, Tex2F(0.0, 0.0) };
V2F_C4B_T2F c = {Vertex2F(p3.x, p3.y), col, Tex2F(0.0, 0.0) };
V2F_C4B_T2F_Triangle *triangles = (V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount);
V2F_C4B_T2F_Triangle triangle = {a, b, c};
triangles[0] = triangle;
_bufferCount += vertex_count;
_dirty = true;
}
void DrawNode::drawCubicBezier(const Point& from, const Point& control1, const Point& control2, const Point& to, unsigned int segments, const Color4F &color)
{
unsigned int vertex_count = (segments + 1) * 3;
ensureCapacity(vertex_count);
Tex2F texCoord = Tex2F(0.0, 0.0);
Color4B col = Color4B(color);
Vertex2F vertex;
Vertex2F firstVertex = Vertex2F(from.x, from.y);
Vertex2F lastVertex = Vertex2F(to.x, to.y);
float t = 0;
for(unsigned int i = segments + 1; i > 0; i--)
{
float x = powf(1 - t, 3) * from.x + 3.0f * powf(1 - t, 2) * t * control1.x + 3.0f * (1 - t) * t * t * control2.x + t * t * t * to.x;
float y = powf(1 - t, 3) * from.y + 3.0f * powf(1 - t, 2) * t * control1.y + 3.0f * (1 - t) * t * t * control2.y + t * t * t * to.y;
vertex = Vertex2F(x, y);
V2F_C4B_T2F a = {firstVertex, col, texCoord };
V2F_C4B_T2F b = {lastVertex, col, texCoord };
V2F_C4B_T2F c = {vertex, col, texCoord };
V2F_C4B_T2F_Triangle triangle = {a, b, c};
((V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount))[0] = triangle;
lastVertex = vertex;
t += 1.0f / segments;
_bufferCount += 3;
}
_dirty = true;
}
void DrawNode::drawQuadraticBezier(const Point& from, const Point& control, const Point& to, unsigned int segments, const Color4F &color)
{
unsigned int vertex_count = (segments + 1) * 3;
ensureCapacity(vertex_count);
Tex2F texCoord = Tex2F(0.0, 0.0);
Color4B col = Color4B(color);
Vertex2F vertex;
Vertex2F firstVertex = Vertex2F(from.x, from.y);
Vertex2F lastVertex = Vertex2F(to.x, to.y);
float t = 0;
for(unsigned int i = segments + 1; i > 0; i--)
{
float x = powf(1 - t, 2) * from.x + 2.0f * (1 - t) * t * control.x + t * t * to.x;
float y = powf(1 - t, 2) * from.y + 2.0f * (1 - t) * t * control.y + t * t * to.y;
vertex = Vertex2F(x, y);
V2F_C4B_T2F a = {firstVertex, col, texCoord };
V2F_C4B_T2F b = {lastVertex, col, texCoord };
V2F_C4B_T2F c = {vertex, col, texCoord };
V2F_C4B_T2F_Triangle triangle = {a, b, c};
((V2F_C4B_T2F_Triangle *)(_buffer + _bufferCount))[0] = triangle;
lastVertex = vertex;
t += 1.0f / segments;
_bufferCount += 3;
}
_dirty = true;
}
void DrawNode::clear()
{
_bufferCount = 0;

View File

@ -63,6 +63,15 @@ public:
* @endcode
*/
void drawPolygon(Point *verts, int count, const Color4F &fillColor, float borderWidth, const Color4F &borderColor);
/** draw a triangle with color */
void drawTriangle(const Point &p1, const Point &p2, const Point &p3, const Color4F &color);
/** draw a cubic bezier curve with color and number of segments */
void drawCubicBezier(const Point& from, const Point& control1, const Point& control2, const Point& to, unsigned int segments, const Color4F &color);
/** draw a quadratic bezier curve with color and number of segments */
void drawQuadraticBezier(const Point& from, const Point& control, const Point& to, unsigned int segments, const Color4F &color);
/** Clear the geometry in the node's buffer. */
void clear();

View File

@ -302,6 +302,14 @@ DrawNodeTest::DrawNodeTest()
draw->drawSegment(Point(20,s.height), Point(20,s.height/2), 10, Color4F(0, 1, 0, 1));
draw->drawSegment(Point(10,s.height/2), Point(s.width/2, s.height/2), 40, Color4F(1, 0, 1, 0.5));
// Draw triangle
draw->drawTriangle(Point(10, 10), Point(70, 30), Point(100, 140), Color4F(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 0.5));
// Draw some beziers
draw->drawQuadraticBezier(Point(s.width - 150, s.height - 150), Point(s.width - 70, s.height - 10), Point(s.width - 10, s.height - 10), 10, Color4F(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 0.5));
draw->drawCubicBezier(Point(s.width - 250, 40), Point(s.width - 70, 100), Point(s.width - 30, 250), Point(s.width - 10, s.height - 50), 10, Color4F(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 0.5));
}
string DrawNodeTest::title() const