axmol/tests/cpp-tests/Classes/DrawPrimitivesTest/DrawPrimitivesTest.cpp

333 lines
9.7 KiB
C++
Raw Normal View History

#include "DrawPrimitivesTest.h"
2013-12-25 11:44:05 +08:00
#include "renderer/CCRenderer.h"
#include "renderer/CCCustomCommand.h"
2012-11-21 11:43:08 +08:00
using namespace std;
2012-11-21 11:43:08 +08:00
static int sceneIdx = -1;
Layer* nextSpriteTestAction();
Layer* backSpriteTestAction();
Layer* restartSpriteTestAction();
typedef Layer* (*NEWDRAWPRIMITIVESFUNC)();
2012-11-21 11:43:08 +08:00
#define DRAWPRIMITIVES_CREATE_FUNC(className) \
static Layer* create##className() \
2012-11-21 11:43:08 +08:00
{ return new className(); }
2012-11-21 11:43:08 +08:00
DRAWPRIMITIVES_CREATE_FUNC(DrawPrimitivesTest);
DRAWPRIMITIVES_CREATE_FUNC(DrawNodeTest);
2012-11-21 11:43:08 +08:00
static NEWDRAWPRIMITIVESFUNC createFunctions[] =
{
createDrawPrimitivesTest,
createDrawNodeTest,
};
2012-11-21 11:43:08 +08:00
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
static Layer* nextAction()
2012-11-21 11:43:08 +08:00
{
sceneIdx++;
sceneIdx = sceneIdx % MAX_LAYER;
auto layer = (createFunctions[sceneIdx])();
layer->autorelease();
2012-11-21 11:43:08 +08:00
return layer;
2012-11-21 11:43:08 +08:00
}
static Layer* backAction()
2012-11-21 11:43:08 +08:00
{
sceneIdx--;
int total = MAX_LAYER;
if( sceneIdx < 0 )
sceneIdx += total;
auto layer = (createFunctions[sceneIdx])();
layer->autorelease();
2012-11-21 11:43:08 +08:00
return layer;
2012-11-21 11:43:08 +08:00
}
static Layer* restartAction()
2012-11-21 11:43:08 +08:00
{
auto layer = (createFunctions[sceneIdx])();
layer->autorelease();
2012-11-21 11:43:08 +08:00
return layer;
2012-11-21 11:43:08 +08:00
}
2012-11-21 11:43:08 +08:00
// BaseLayer
2012-11-21 11:43:08 +08:00
BaseLayer::BaseLayer()
{
}
2012-11-21 11:43:08 +08:00
void BaseLayer::onEnter()
{
BaseTest::onEnter();
2012-11-21 11:43:08 +08:00
}
void BaseLayer::restartCallback(cocos2d::Ref *pSender)
2012-11-21 11:43:08 +08:00
{
auto s = new DrawPrimitivesTestScene();
2012-11-21 11:43:08 +08:00
s->addChild(restartAction());
Director::getInstance()->replaceScene(s);
2012-11-21 11:43:08 +08:00
s->release();
}
void BaseLayer::nextCallback(cocos2d::Ref *pSender)
2012-11-21 11:43:08 +08:00
{
auto s = new DrawPrimitivesTestScene();;
2012-11-21 11:43:08 +08:00
s->addChild(nextAction());
2012-06-12 16:56:34 +08:00
Director::getInstance()->replaceScene(s);
2012-11-21 11:43:08 +08:00
s->release();
}
void BaseLayer::backCallback(cocos2d::Ref *pSender)
2012-11-21 11:43:08 +08:00
{
auto s = new DrawPrimitivesTestScene();
2012-11-21 11:43:08 +08:00
s->addChild(backAction());
2012-06-12 16:56:34 +08:00
Director::getInstance()->replaceScene(s);
2012-11-21 11:43:08 +08:00
s->release();
}
string BaseLayer::title() const
2012-11-21 11:43:08 +08:00
{
return "No title";
}
string BaseLayer::subtitle() const
2012-11-21 11:43:08 +08:00
{
return "";
}
2012-11-21 11:43:08 +08:00
// DrawPrimitivesTest
2012-11-21 11:43:08 +08:00
DrawPrimitivesTest::DrawPrimitivesTest()
{
}
2014-04-08 22:07:35 +08:00
void DrawPrimitivesTest::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
2012-11-21 11:43:08 +08:00
{
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(DrawPrimitivesTest::onDraw, this, transform, transformUpdated);
renderer->addCommand(&_customCommand);
2013-12-25 11:44:05 +08:00
}
2014-04-10 10:16:16 +08:00
void DrawPrimitivesTest::onDraw(const Matrix &transform, bool transformUpdated)
2013-12-25 11:44:05 +08:00
{
Director* director = Director::getInstance();
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);
2012-11-21 11:43:08 +08:00
2013-12-25 11:44:05 +08:00
//draw
CHECK_GL_ERROR_DEBUG();
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
// draw a simple line
// The default state is:
// Line Width: 1
// color: 255,255,255,255 (white, non-transparent)
// Anti-Aliased
// glEnable(GL_LINE_SMOOTH);
DrawPrimitives::drawLine( VisibleRect::leftBottom(), VisibleRect::rightTop() );
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
CHECK_GL_ERROR_DEBUG();
2012-11-21 11:43:08 +08:00
2013-12-25 11:44:05 +08:00
// line: color, width, aliased
2013-12-25 11:46:40 +08:00
// glLineWidth > 1 and GL_LINE_SMOOTH are not compatible
// GL_SMOOTH_LINE_WIDTH_RANGE = (1,1) on iPhone
// glDisable(GL_LINE_SMOOTH);
glLineWidth( 5.0f );
DrawPrimitives::setDrawColor4B(255,0,0,255);
DrawPrimitives::drawLine( VisibleRect::leftTop(), VisibleRect::rightBottom() );
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
CHECK_GL_ERROR_DEBUG();
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
// TIP:
// If you are going to use always thde same color or width, you don't
// need to call it before every draw
//
// Remember: OpenGL is a state-machine.
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
// draw big point in the center
DrawPrimitives::setPointSize(64);
DrawPrimitives::setDrawColor4B(0,0,255,128);
DrawPrimitives::drawPoint( VisibleRect::center() );
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
CHECK_GL_ERROR_DEBUG();
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
// draw 4 small points
2014-04-15 17:29:23 +08:00
Vector2 points[] = { Vector2(60,60), Vector2(70,70), Vector2(60,70), Vector2(70,60) };
2013-12-25 11:46:40 +08:00
DrawPrimitives::setPointSize(4);
DrawPrimitives::setDrawColor4B(0,255,255,255);
DrawPrimitives::drawPoints( points, 4);
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
CHECK_GL_ERROR_DEBUG();
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
// draw a green circle with 10 segments
glLineWidth(16);
DrawPrimitives::setDrawColor4B(0, 255, 0, 255);
DrawPrimitives::drawCircle( VisibleRect::center(), 100, 0, 10, false);
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
CHECK_GL_ERROR_DEBUG();
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
// draw a green circle with 50 segments with line to center
glLineWidth(2);
DrawPrimitives::setDrawColor4B(0, 255, 255, 255);
DrawPrimitives::drawCircle( VisibleRect::center(), 50, CC_DEGREES_TO_RADIANS(90), 50, true);
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
CHECK_GL_ERROR_DEBUG();
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
// draw a pink solid circle with 50 segments
glLineWidth(2);
DrawPrimitives::setDrawColor4B(255, 0, 255, 255);
DrawPrimitives::drawSolidCircle( VisibleRect::center() + Point(140,0), 40, CC_DEGREES_TO_RADIANS(90), 50, 1.0f, 1.0f);
2013-12-25 11:46:40 +08:00
CHECK_GL_ERROR_DEBUG();
2013-12-25 11:46:40 +08:00
// open yellow poly
DrawPrimitives::setDrawColor4B(255, 255, 0, 255);
glLineWidth(10);
2014-04-15 17:29:23 +08:00
Vector2 vertices[] = { Vector2(0,0), Vector2(50,50), Vector2(100,50), Vector2(100,100), Vector2(50,100) };
2013-12-25 11:46:40 +08:00
DrawPrimitives::drawPoly( vertices, 5, false);
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
CHECK_GL_ERROR_DEBUG();
// filled poly
glLineWidth(1);
2014-04-15 17:29:23 +08:00
Vector2 filledVertices[] = { Vector2(0,120), Vector2(50,120), Vector2(50,170), Vector2(25,200), Vector2(0,170) };
2013-12-25 11:46:40 +08:00
DrawPrimitives::drawSolidPoly(filledVertices, 5, Color4F(0.5f, 0.5f, 1, 1 ) );
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
// closed purble poly
DrawPrimitives::setDrawColor4B(255, 0, 255, 255);
glLineWidth(2);
2014-04-15 17:29:23 +08:00
Vector2 vertices2[] = { Vector2(30,130), Vector2(30,230), Vector2(50,200) };
2013-12-25 11:46:40 +08:00
DrawPrimitives::drawPoly( vertices2, 3, true);
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
CHECK_GL_ERROR_DEBUG();
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
// draw quad bezier path
DrawPrimitives::drawQuadBezier(VisibleRect::leftTop(), VisibleRect::center(), VisibleRect::rightTop(), 50);
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
CHECK_GL_ERROR_DEBUG();
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
// draw cubic bezier path
DrawPrimitives::drawCubicBezier(VisibleRect::center(), Point(VisibleRect::center().x+30,VisibleRect::center().y+50), Point(VisibleRect::center().x+60,VisibleRect::center().y-50),VisibleRect::right(),100);
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
CHECK_GL_ERROR_DEBUG();
2012-06-12 16:56:34 +08:00
//draw a solid polygon
2014-04-15 17:29:23 +08:00
Vector2 vertices3[] = {Vector2(60,160), Vector2(70,190), Vector2(100,190), Vector2(90,160)};
DrawPrimitives::drawSolidPoly( vertices3, 4, Color4F(1,1,0,1) );
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
// restore original values
glLineWidth(1);
DrawPrimitives::setDrawColor4B(255,255,255,255);
DrawPrimitives::setPointSize(1);
2012-11-21 11:43:08 +08:00
2013-12-25 11:46:40 +08:00
CHECK_GL_ERROR_DEBUG();
2013-12-25 11:44:05 +08:00
//end draw
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
2012-11-21 11:43:08 +08:00
}
string DrawPrimitivesTest::title() const
2012-11-21 11:43:08 +08:00
{
return "draw primitives";
}
string DrawPrimitivesTest::subtitle() const
2012-11-21 11:43:08 +08:00
{
return "Drawing Primitives. Use DrawNode instead";
2012-11-21 11:43:08 +08:00
}
// DrawNodeTest
DrawNodeTest::DrawNodeTest()
{
auto s = Director::getInstance()->getWinSize();
2012-11-21 11:43:08 +08:00
auto draw = DrawNode::create();
2012-11-21 11:43:08 +08:00
addChild(draw, 10);
// Draw 10 circles
for( int i=0; i < 10; i++)
{
2013-07-12 14:11:55 +08:00
draw->drawDot(Point(s.width/2, s.height/2), 10*(10-i), Color4F(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 1));
2012-11-21 11:43:08 +08:00
}
// Draw polygons
Point points[] = { Point(s.height/4,0), Point(s.width,s.height/5), Point(s.width/3*2,s.height) };
draw->drawPolygon(points, sizeof(points)/sizeof(points[0]), Color4F(1,0,0,0.5), 4, Color4F(0,0,1,1));
2012-11-21 11:43:08 +08:00
// star poly (triggers buggs)
{
const float o=80;
const float w=20;
const float h=50;
Point star[] = {
2013-12-25 11:46:40 +08:00
Point(o+w,o-h), Point(o+w*2, o), // lower spike
Point(o + w*2 + h, o+w ), Point(o + w*2, o+w*2), // right spike
// {o +w, o+w*2+h}, {o,o+w*2}, // top spike
// {o -h, o+w}, {o,o}, // left spike
2012-11-21 11:43:08 +08:00
};
draw->drawPolygon(star, sizeof(star)/sizeof(star[0]), Color4F(1,0,0,0.5), 1, Color4F(0,0,1,1));
2012-11-21 11:43:08 +08:00
}
// star poly (doesn't trigger bug... order is important un tesselation is supported.
{
const float o=180;
const float w=20;
const float h=50;
Point star[] = {
2013-12-25 11:46:40 +08:00
Point(o,o), Point(o+w,o-h), Point(o+w*2, o), // lower spike
Point(o + w*2 + h, o+w ), Point(o + w*2, o+w*2), // right spike
Point(o +w, o+w*2+h), Point(o,o+w*2), // top spike
Point(o -h, o+w), // left spike
2012-11-21 11:43:08 +08:00
};
draw->drawPolygon(star, sizeof(star)/sizeof(star[0]), Color4F(1,0,0,0.5), 1, Color4F(0,0,1,1));
2012-11-21 11:43:08 +08:00
}
// Draw segment
2013-07-12 14:11:55 +08:00
draw->drawSegment(Point(20,s.height), Point(20,s.height/2), 10, Color4F(0, 1, 0, 1));
2012-11-21 11:43:08 +08:00
2013-07-12 14:11:55 +08:00
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));
2012-11-21 11:43:08 +08:00
}
string DrawNodeTest::title() const
2012-11-21 11:43:08 +08:00
{
return "Test DrawNode";
2012-11-21 11:43:08 +08:00
}
string DrawNodeTest::subtitle() const
2012-11-21 11:43:08 +08:00
{
return "Testing DrawNode - batched draws. Concave polygons are BROKEN";
2010-08-26 10:53:49 +08:00
}
void DrawPrimitivesTestScene::runThisTest()
{
auto layer = nextAction();
addChild(layer);
2010-08-26 10:53:49 +08:00
Director::getInstance()->replaceScene(this);
2010-08-26 10:53:49 +08:00
}