2012-04-19 14:35:52 +08:00
|
|
|
#include "DrawPrimitivesTest.h"
|
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
using namespace std;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
static int sceneIdx = -1;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* nextSpriteTestAction();
|
|
|
|
Layer* backSpriteTestAction();
|
|
|
|
Layer* restartSpriteTestAction();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
typedef Layer* (*NEWDRAWPRIMITIVESFUNC)();
|
2012-11-21 11:43:08 +08:00
|
|
|
#define DRAWPRIMITIVES_CREATE_FUNC(className) \
|
2013-06-20 14:17:10 +08:00
|
|
|
static Layer* create##className() \
|
2012-11-21 11:43:08 +08:00
|
|
|
{ return new className(); }
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
DRAWPRIMITIVES_CREATE_FUNC(DrawPrimitivesTest);
|
|
|
|
DRAWPRIMITIVES_CREATE_FUNC(DrawNodeTest);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
static NEWDRAWPRIMITIVESFUNC createFunctions[] =
|
|
|
|
{
|
|
|
|
createDrawPrimitivesTest,
|
|
|
|
createDrawNodeTest,
|
|
|
|
};
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
static Layer* nextAction()
|
2012-11-21 11:43:08 +08:00
|
|
|
{
|
|
|
|
sceneIdx++;
|
|
|
|
sceneIdx = sceneIdx % MAX_LAYER;
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* pLayer = (createFunctions[sceneIdx])();
|
2012-11-21 11:43:08 +08:00
|
|
|
pLayer->autorelease();
|
|
|
|
|
|
|
|
return pLayer;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
static Layer* backAction()
|
2012-11-21 11:43:08 +08:00
|
|
|
{
|
|
|
|
sceneIdx--;
|
|
|
|
int total = MAX_LAYER;
|
|
|
|
if( sceneIdx < 0 )
|
|
|
|
sceneIdx += total;
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* pLayer = (createFunctions[sceneIdx])();
|
2012-11-21 11:43:08 +08:00
|
|
|
pLayer->autorelease();
|
|
|
|
|
|
|
|
return pLayer;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
static Layer* restartAction()
|
2012-11-21 11:43:08 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* pLayer = (createFunctions[sceneIdx])();
|
2012-11-21 11:43:08 +08:00
|
|
|
pLayer->autorelease();
|
|
|
|
|
|
|
|
return pLayer;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
// BaseLayer
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
BaseLayer::BaseLayer()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
void BaseLayer::onEnter()
|
|
|
|
{
|
2013-06-07 08:12:28 +08:00
|
|
|
BaseTest::onEnter();
|
2012-11-21 11:43:08 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void BaseLayer::restartCallback(cocos2d::Object *pSender)
|
2012-11-21 11:43:08 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Scene *s = new DrawPrimitivesTestScene();
|
2012-11-21 11:43:08 +08:00
|
|
|
s->addChild(restartAction());
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Director::sharedDirector()->replaceScene(s);
|
2012-11-21 11:43:08 +08:00
|
|
|
s->release();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void BaseLayer::nextCallback(cocos2d::Object *pSender)
|
2012-11-21 11:43:08 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Scene *s = new DrawPrimitivesTestScene();;
|
2012-11-21 11:43:08 +08:00
|
|
|
s->addChild(nextAction());
|
2012-06-12 16:56:34 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Director::sharedDirector()->replaceScene(s);
|
2012-11-21 11:43:08 +08:00
|
|
|
s->release();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void BaseLayer::backCallback(cocos2d::Object *pSender)
|
2012-11-21 11:43:08 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Scene *s = new DrawPrimitivesTestScene();
|
2012-11-21 11:43:08 +08:00
|
|
|
s->addChild(backAction());
|
2012-06-12 16:56:34 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Director::sharedDirector()->replaceScene(s);
|
2012-11-21 11:43:08 +08:00
|
|
|
s->release();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
string BaseLayer::title()
|
|
|
|
{
|
|
|
|
return "No title";
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
string BaseLayer::subtitle()
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
// DrawPrimitivesTest
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
DrawPrimitivesTest::DrawPrimitivesTest()
|
|
|
|
{
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
void DrawPrimitivesTest::draw()
|
|
|
|
{
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
// 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);
|
2012-11-21 15:27:47 +08:00
|
|
|
ccDrawLine( VisibleRect::leftBottom(), VisibleRect::rightTop() );
|
2012-11-21 11:43:08 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
// line: color, width, aliased
|
|
|
|
// 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 );
|
|
|
|
ccDrawColor4B(255,0,0,255);
|
2012-11-21 15:27:47 +08:00
|
|
|
ccDrawLine( VisibleRect::leftTop(), VisibleRect::rightBottom() );
|
2012-11-21 11:43:08 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
// TIP:
|
|
|
|
// If you are going to use always the same color or width, you don't
|
|
|
|
// need to call it before every draw
|
|
|
|
//
|
|
|
|
// Remember: OpenGL is a state-machine.
|
|
|
|
|
|
|
|
// draw big point in the center
|
|
|
|
ccPointSize(64);
|
|
|
|
ccDrawColor4B(0,0,255,128);
|
2012-11-21 15:27:47 +08:00
|
|
|
ccDrawPoint( VisibleRect::center() );
|
2012-11-21 11:43:08 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
// draw 4 small points
|
2013-06-20 14:17:10 +08:00
|
|
|
Point points[] = { ccp(60,60), ccp(70,70), ccp(60,70), ccp(70,60) };
|
2012-11-21 11:43:08 +08:00
|
|
|
ccPointSize(4);
|
|
|
|
ccDrawColor4B(0,255,255,255);
|
|
|
|
ccDrawPoints( points, 4);
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
// draw a green circle with 10 segments
|
|
|
|
glLineWidth(16);
|
|
|
|
ccDrawColor4B(0, 255, 0, 255);
|
2012-11-21 15:27:47 +08:00
|
|
|
ccDrawCircle( VisibleRect::center(), 100, 0, 10, false);
|
2012-11-21 11:43:08 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
// draw a green circle with 50 segments with line to center
|
|
|
|
glLineWidth(2);
|
|
|
|
ccDrawColor4B(0, 255, 255, 255);
|
2012-11-21 15:27:47 +08:00
|
|
|
ccDrawCircle( VisibleRect::center(), 50, CC_DEGREES_TO_RADIANS(90), 50, true);
|
2012-11-21 11:43:08 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
// open yellow poly
|
|
|
|
ccDrawColor4B(255, 255, 0, 255);
|
|
|
|
glLineWidth(10);
|
2013-06-20 14:17:10 +08:00
|
|
|
Point vertices[] = { ccp(0,0), ccp(50,50), ccp(100,50), ccp(100,100), ccp(50,100) };
|
2012-11-21 11:43:08 +08:00
|
|
|
ccDrawPoly( vertices, 5, false);
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
// filled poly
|
|
|
|
glLineWidth(1);
|
2013-06-20 14:17:10 +08:00
|
|
|
Point filledVertices[] = { ccp(0,120), ccp(50,120), ccp(50,170), ccp(25,200), ccp(0,170) };
|
2012-11-21 11:43:08 +08:00
|
|
|
ccDrawSolidPoly(filledVertices, 5, ccc4f(0.5f, 0.5f, 1, 1 ) );
|
|
|
|
|
|
|
|
|
|
|
|
// closed purble poly
|
|
|
|
ccDrawColor4B(255, 0, 255, 255);
|
|
|
|
glLineWidth(2);
|
2013-06-20 14:17:10 +08:00
|
|
|
Point vertices2[] = { ccp(30,130), ccp(30,230), ccp(50,200) };
|
2012-11-21 11:43:08 +08:00
|
|
|
ccDrawPoly( vertices2, 3, true);
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
// draw quad bezier path
|
2012-11-21 15:27:47 +08:00
|
|
|
ccDrawQuadBezier(VisibleRect::leftTop(), VisibleRect::center(), VisibleRect::rightTop(), 50);
|
2012-11-21 11:43:08 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
|
|
|
|
// draw cubic bezier path
|
2012-10-23 17:48:50 +08:00
|
|
|
ccDrawCubicBezier(VisibleRect::center(), ccp(VisibleRect::center().x+30,VisibleRect::center().y+50), ccp(VisibleRect::center().x+60,VisibleRect::center().y-50),VisibleRect::right(),100);
|
2012-11-21 11:43:08 +08:00
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2012-06-12 16:56:34 +08:00
|
|
|
|
|
|
|
//draw a solid polygon
|
2013-06-20 14:17:10 +08:00
|
|
|
Point vertices3[] = {ccp(60,160), ccp(70,190), ccp(100,190), ccp(90,160)};
|
2012-06-11 10:59:57 +08:00
|
|
|
ccDrawSolidPoly( vertices3, 4, ccc4f(1,1,0,1) );
|
2012-11-21 11:43:08 +08:00
|
|
|
|
|
|
|
// restore original values
|
|
|
|
glLineWidth(1);
|
|
|
|
ccDrawColor4B(255,255,255,255);
|
|
|
|
ccPointSize(1);
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
string DrawPrimitivesTest::title()
|
|
|
|
{
|
|
|
|
return "draw primitives";
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
string DrawPrimitivesTest::subtitle()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
return "Drawing Primitives. Use DrawNode instead";
|
2012-11-21 11:43:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DrawNodeTest
|
|
|
|
DrawNodeTest::DrawNodeTest()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Size s = Director::sharedDirector()->getWinSize();
|
2012-11-21 11:43:08 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
DrawNode *draw = DrawNode::create();
|
2012-11-21 11:43:08 +08:00
|
|
|
addChild(draw, 10);
|
|
|
|
|
|
|
|
// Draw 10 circles
|
|
|
|
for( int i=0; i < 10; i++)
|
|
|
|
{
|
|
|
|
draw->drawDot(ccp(s.width/2, s.height/2), 10*(10-i), ccc4f(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw polygons
|
2013-06-20 14:17:10 +08:00
|
|
|
Point points[] = { Point(s.height/4,0), Point(s.width,s.height/5), Point(s.width/3*2,s.height) };
|
2012-11-21 11:43:08 +08:00
|
|
|
draw->drawPolygon(points, sizeof(points)/sizeof(points[0]), ccc4f(1,0,0,0.5), 4, ccc4f(0,0,1,1));
|
|
|
|
|
|
|
|
// star poly (triggers buggs)
|
|
|
|
{
|
|
|
|
const float o=80;
|
|
|
|
const float w=20;
|
|
|
|
const float h=50;
|
2013-06-20 14:17:10 +08:00
|
|
|
Point star[] = {
|
|
|
|
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
|
2012-11-21 11:43:08 +08:00
|
|
|
// {o +w, o+w*2+h}, {o,o+w*2}, // top spike
|
|
|
|
// {o -h, o+w}, {o,o}, // left spike
|
|
|
|
};
|
|
|
|
|
|
|
|
draw->drawPolygon(star, sizeof(star)/sizeof(star[0]), ccc4f(1,0,0,0.5), 1, ccc4f(0,0,1,1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2013-06-20 14:17:10 +08:00
|
|
|
Point star[] = {
|
|
|
|
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]), ccc4f(1,0,0,0.5), 1, ccc4f(0,0,1,1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Draw segment
|
|
|
|
draw->drawSegment(ccp(20,s.height), ccp(20,s.height/2), 10, ccc4f(0, 1, 0, 1));
|
|
|
|
|
|
|
|
draw->drawSegment(ccp(10,s.height/2), ccp(s.width/2, s.height/2), 40, ccc4f(1, 0, 1, 0.5));
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
string DrawNodeTest::title()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
return "Test DrawNode";
|
2012-11-21 11:43:08 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-21 11:43:08 +08:00
|
|
|
string DrawNodeTest::subtitle()
|
|
|
|
{
|
|
|
|
return "Testing DrawNode - batched draws. Concave polygons are BROKEN";
|
2010-08-26 10:53:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawPrimitivesTestScene::runThisTest()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* pLayer = nextAction();
|
2010-08-26 10:53:49 +08:00
|
|
|
addChild(pLayer);
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Director::sharedDirector()->replaceScene(this);
|
2010-08-26 10:53:49 +08:00
|
|
|
}
|