Cocos2d-x3.3之DrawPrimitivesTest分析
发布时间:2020-12-14 19:03:34 所属栏目:百科 来源:网络整理
导读:1、代码列表 2、VisibleRect类 该类是test-cpp自带工具类 3、HelloWorldScene类 同前面代码 4、DrawPrimitivesDemo类 1).h文件 #include "cocos2d.h"#include "ui/CocosGUI.h"#include "VisibleRect.h"#include "renderer/CCRenderer.h"#include "renderer/
1、代码列表2、VisibleRect类
3、HelloWorldScene类同前面代码 4、DrawPrimitivesDemo类1).h文件#include "cocos2d.h" #include "ui/CocosGUI.h" #include "VisibleRect.h" #include "renderer/CCRenderer.h" #include "renderer/CCCustomCommand.h" USING_NS_CC; using namespace ui; class DrawPrimitivesDemo : public Scene { public: CREATE_FUNC(DrawPrimitivesDemo); virtual bool init(); }; class BaseTest : public cocos2d::Layer { public: CREATE_FUNC(BaseTest); std::string title() const; virtual std::string subtitle() const; void restartCallback(Ref* sender);//重新执行当前test void nextCallback(Ref* sender);//下一个test void backCallback(Ref* sender);//上一个test virtual bool init(); void menuCloseCallback(cocos2d::Ref* pSender);//关闭菜单回调函数 }; class DrawPrimitivesTest : public BaseTest { public: CREATE_FUNC(DrawPrimitivesTest); virtual bool init(); virtual std::string subtitle() const override; virtual void draw(Renderer* renderer,const Mat4 &transform,uint32_t flags) override; protected: void onDraw(const Mat4 &transform,uint32_t flags); CustomCommand _customCommand; }; class DrawNodeTest : public BaseTest { public: CREATE_FUNC(DrawNodeTest); virtual bool init(); virtual std::string subtitle() const override; }; 2).cpp文件#include "DrawPrimitivesDemo.h" #include "renderer/CCRenderer.h" #include "renderer/CCCustomCommand.h" #define CL(__className__) [](){ return __className__::create();} static int sceneIdx = -1; typedef Layer* (*NEWDRAWPRIMITIVESFUNC)(); #define DRAWPRIMITIVES_CREATE_FUNC(className) static Layer* create##className() { return new className(); } static std::function<Layer*()> createFunctions[] = { CL(DrawPrimitivesTest),CL(DrawNodeTest),}; #define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0])) static Layer* nextAction() { sceneIdx++; sceneIdx = sceneIdx % MAX_LAYER; auto layer = (createFunctions[sceneIdx])(); // layer->autorelease(); return layer; } static Layer* backAction() { sceneIdx--; int total = MAX_LAYER; if( sceneIdx < 0 ) sceneIdx += total; auto layer = (createFunctions[sceneIdx])(); // layer->autorelease(); return layer; } static Layer* restartAction() { auto layer = (createFunctions[sceneIdx])(); // layer->autorelease(); return layer; } bool BaseTest::init() { bool bRet = false; do{ CC_BREAK_IF(!Layer::init()); Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image,which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object auto closeItem = MenuItemImage::create( "CloseNormal.png","CloseSelected.png",CC_CALLBACK_1(BaseTest::menuCloseCallback,this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2,origin.y + visibleSize.height - closeItem->getContentSize().height/2)); // create menu,it's an autorelease object auto menu1 = Menu::create(closeItem,NULL); menu1->setPosition(Vec2::ZERO); this->addChild(menu1,1); std::string str = title(); const char * pTitle = str.c_str(); TTFConfig ttfConfig("tahoma.ttf",35); auto label = Label::createWithTTF(ttfConfig,pTitle); addChild(label,9999); label->setPosition( Vec2(VisibleRect::center().x,VisibleRect::top().y - 30) ); std::string strSubtitle = subtitle(); if( ! strSubtitle.empty() ) { ttfConfig.fontFilePath = "tahoma.ttf"; ttfConfig.fontSize = 30; auto l = Label::createWithTTF(ttfConfig,strSubtitle.c_str()); addChild(l,9999); l->setPosition( Vec2(VisibleRect::center().x,VisibleRect::top().y - 100) ); } auto item1 = MenuItemFont::create("backCallback",CC_CALLBACK_1(BaseTest::backCallback,this) ); auto item2 = MenuItemFont::create("restartCallback",CC_CALLBACK_1(BaseTest::restartCallback,this) ); auto item3 = MenuItemFont::create("nextCallback",CC_CALLBACK_1(BaseTest::nextCallback,this) ); auto menu = Menu::create(item1,item2,item3,NULL); menu->setPosition(Vec2::ZERO); item1->setPosition(Vec2(VisibleRect::center().x - item2->getContentSize().width*2,VisibleRect::bottom().y+item2->getContentSize().height/2)); item2->setPosition(Vec2(VisibleRect::center().x,VisibleRect::bottom().y+item2->getContentSize().height/2)); item3->setPosition(Vec2(VisibleRect::center().x + item2->getContentSize().width*2,VisibleRect::bottom().y+item2->getContentSize().height/2)); addChild(menu,9999); bRet = true; }while(0); return bRet; } void BaseTest::menuCloseCallback(Ref* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); return; #endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif } void BaseTest::restartCallback(cocos2d::Ref *sender) { auto s = new (std::nothrow) DrawPrimitivesDemo(); s->addChild(restartAction()); Director::getInstance()->replaceScene(s); s->release(); } void BaseTest::nextCallback(cocos2d::Ref *sender) { auto s = new (std::nothrow) DrawPrimitivesDemo(); s->addChild(nextAction()); Director::getInstance()->replaceScene(s); s->release(); } void BaseTest::backCallback(cocos2d::Ref *sender) { auto s = new (std::nothrow) DrawPrimitivesDemo(); s->addChild(backAction()); Director::getInstance()->replaceScene(s); s->release(); } std::string BaseTest::title() const { return "DrawPrimitives Test"; } std::string BaseTest::subtitle() const { return ""; } bool DrawPrimitivesDemo::init() { bool bRet = false; do{ CC_BREAK_IF(!Scene::init()); auto layer = nextAction(); addChild(layer); Director::getInstance()->replaceScene(this); bRet = true; }while(0); return bRet; } bool DrawPrimitivesTest::init() { bool bRet = false; do{ CC_BREAK_IF(!BaseTest::init()); bRet = true; }while(0); return bRet; } void DrawPrimitivesTest::draw(cocos2d::Renderer *renderer,const cocos2d::Mat4 &transform,uint32_t flags) { //重写draw函数,回调onDraw函数 _customCommand.init(_globalZOrder); _customCommand.func = CC_CALLBACK_0(DrawPrimitivesTest::onDraw,this,transform,flags); renderer->addCommand(&_customCommand); } void DrawPrimitivesTest::onDraw(const cocos2d::Mat4 &transform,uint32_t flags) { //初始化 Director* director = Director::getInstance(); director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW,transform); //画线 CHECK_GL_ERROR_DEBUG(); DrawPrimitives::drawLine(VisibleRect::leftBottom(),VisibleRect::rightTop());//参数是2个Vec2对象 //画线 CHECK_GL_ERROR_DEBUG(); glLineWidth(5.0f);//设置线条宽度 DrawPrimitives::setDrawColor4B(255,255);//设置颜色 DrawPrimitives::drawLine(VisibleRect::leftTop(),VisibleRect::rightBottom()); //画点 CHECK_GL_ERROR_DEBUG(); DrawPrimitives::setPointSize(64);//设置点大小 DrawPrimitives::setDrawColor4B(0,255,255);//设置颜色 DrawPrimitives::drawPoint(VisibleRect::center());//参数Vec2对象,表示圆心的位置 //画多个点 CHECK_GL_ERROR_DEBUG(); Vec2 points[] = {Vec2(60,160),Vec2(70,170),Vec2(60,160)}; DrawPrimitives::setPointSize(4); DrawPrimitives::setDrawColor4B(0,255); DrawPrimitives::drawPoints(points,4);//参数依次为圆心Vec2和点的个数 //画圆圈 CHECK_GL_ERROR_DEBUG(); glLineWidth(16); DrawPrimitives::setDrawColor4B(0,255); DrawPrimitives::drawCircle(VisibleRect::center(),100,10,false);//参数依次为:圆心、半径、角度、段数、是否画出一条半径 //画圆圈 CHECK_GL_ERROR_DEBUG(); glLineWidth(2); DrawPrimitives::setDrawColor4B(0,50,CC_DEGREES_TO_RADIANS(90),true); //画实心圆 CHECK_GL_ERROR_DEBUG(); glLineWidth(2);//线条宽度 DrawPrimitives::setDrawColor4B(255,255);//线条颜色 //参数依次为:圆心、半径、角度、段数、X轴缩放系数、Y轴缩放系数 DrawPrimitives::drawSolidCircle(VisibleRect::center() + Vec2(140,0),40,1.0f,1.0f); //画多边形图形,不闭合 CHECK_GL_ERROR_DEBUG(); glLineWidth(10); Vec2 vertices[] = {Vec2(0,Vec2(50,50),Vec2(100,100),100)}; DrawPrimitives::drawPoly(vertices,5,false);//参数依次为多边形定点、定点数、是否闭合 //绘制填充多边形 CHECK_GL_ERROR_DEBUG(); glLineWidth(1); Vec2 filledVertices[] = {Vec2(0,120),Vec2(25,200),Vec2(0,170)}; DrawPrimitives::drawSolidPoly(filledVertices,Color4F(0.5f,0.5f,1,1));//参数依次为定点、顶点个数、填充颜色 //绘制多边形,闭合 DrawPrimitives::setDrawColor4B(255,255); glLineWidth(2); Vec2 vertices2[] = {Vec2(90,330),Vec2(90,530),Vec2(150,500)}; DrawPrimitives::drawPoly(vertices2,3,true); //绘制贝塞尔曲线 CHECK_GL_ERROR_DEBUG(); //参数依次为起点、控制点、终点、段数 DrawPrimitives::drawQuadBezier(VisibleRect::leftTop(),VisibleRect::center(),VisibleRect::rightTop(),50); //画贝塞尔曲线 CHECK_GL_ERROR_DEBUG(); //参数依次为起点、控制点1、控制点2、终点、段数 DrawPrimitives::drawCubicBezier(VisibleRect::center(),Vec2(VisibleRect::center().x + 30,VisibleRect::center().y + 50),Vec2(VisibleRect::center().x + 60,VisibleRect::center().y - 50),VisibleRect::right(),100); //画填充闭合曲线 CHECK_GL_ERROR_DEBUG(); Vec2 vertices3[] = {Vec2(90,190),Vec2(140,160)}; DrawPrimitives::drawSolidPoly(vertices3,4,Color4F(1,1));//参数依次为顶点、顶点个数、填充颜色 glLineWidth(1); DrawPrimitives::setDrawColor4B(255,255); DrawPrimitives::setPointSize(1); CHECK_GL_ERROR_DEBUG(); director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); } std::string DrawPrimitivesTest::subtitle() const { return "draw primitives"; } bool DrawNodeTest::init() { bool bRet = false; do{ CC_BREAK_IF(!BaseTest::init()); auto s = Director::getInstance()->getWinSize(); //add DrawNode auto draw = DrawNode::create(); addChild(draw,10); //draw point,参数依次为位置、Size、填充颜色 draw->drawPoint(Vec2(s.width/2 - 120,s.height/2 - 120),Color4F(CCRANDOM_0_1(),CCRANDOM_0_1(),1)); draw->drawPoint(Vec2(s.width/2 + 120,s.height/2 + 120),1)); //draw points,同上 Vec2 position[] = {Vec2(60,60),70),60)}; draw->drawPoints(position,1)); //draw a line,参数依次为起点、终点、颜色 draw->drawLine(Vec2(0,Vec2(s.width,s.height),Color4F(1.0,0.0,0.5)); //draw a rectangle,绘制剪裁点,参数为剪裁起点、终点、颜色 draw->drawRect(Vec2(23,123),Vec2(7,1)); //draw circle,绘制圆圈,参数依次为:圆心、半径、角度、段数、是否绘制一条半径、X轴缩放、Y轴缩放、颜色 draw->drawCircle(VisibleRect::center() + Vec2(140,true,2.0f,0.5)); draw->drawCircle(VisibleRect::center() - Vec2(140,30,false,1)); //Draw some beziers,参数依次为:起点、控制点、终点、段数、颜色 draw->drawQuadBezier(Vec2(s.width - 150,s.height - 150),Vec2(s.width - 70,s.height - 10),Vec2(s.width - 10,0.5)); draw->drawQuadBezier(Vec2(0,Vec2(s.width/2,s.height/2),0.5)); //参数依次为:起点、控制点1、控制点2、终点、段数、颜色 draw->drawCubicBezier(VisibleRect::center(),Vec2(VisibleRect::center().x+30,VisibleRect::center().y+50),Vec2(VisibleRect::center().x+60,VisibleRect::center().y-50),0.5)); draw->drawCubicBezier(Vec2(s.width - 250,40),Vec2(s.width - 30,250),s.height - 50),0.5)); auto array = PointArray::create(20); array->addControlPoint(Vec2(0,0)); array->addControlPoint(Vec2(80,80)); array->addControlPoint(Vec2(s.width - 80,s.height - 80)); array->addControlPoint(Vec2(80,80)); array->addControlPoint(Vec2(s.width/2,s.height/2)); //绘制基本线条,参数依次为:点数组、张力、段数、颜色 draw->drawCardinalSpline(array,0.5,0.5)); auto array2 = PointArray::create(20); array2->addControlPoint(Vec2(s.width/2,30)); array2->addControlPoint(Vec2(s.width - 80,s.height - 80)); array2->addControlPoint(Vec2(s.width/2,30)); draw->drawCatmullRom(array2,0.5)); //open random color poly,绘制多边形,不封闭 Vec2 vertices[] = {Vec2(0,100)}; draw->drawPoly(vertices,1)); //closed random color poly,绘制多边形,封闭 Vec2 vertices2[] = {Vec2(30,130),Vec2(30,230),200)}; draw->drawPoly(vertices2,1)); //Draw 10 Circles,绘制圆圈 for(int i = 0; i < 10; i++) { //参数依次为:圆心、半径、颜色 draw->drawDot(Vec2(s.width/2,10*(10-i),1)); } //Draw polygons绘制闭合多边形,参数依次是顶点、顶点个数、填充颜色、线条宽度、线条颜色 Vec2 points[] = { Vec2(s.height/4,s.height/5),Vec2(s.width/3*2,s.height) }; // draw->drawPolygon(points,sizeof(points) / sizeof(points[0]),0.5),Color4F(0,0.5)); //star poly (triggers buggs)绘制多边形图形 { const float o = 80; const float w = 20; const float h = 50; Vec2 star[] = {Vec2(o+w,o-h),Vec2(o+w*2,o),Vec2(o+w*2+h,o+w),o+w*2)}; //参数依次为:顶点、顶点个数、填充颜色、线条宽度、线条颜色 draw->drawPolygon(star,sizeof(star) / sizeof(star[0]),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; Vec2 star[] = { Vec2(o,Vec2(o+w,// lower spike Vec2(o + w*2 + h,o+w ),Vec2(o + w*2,o+w*2),// right spike Vec2(o +w,o+w*2+h),Vec2(o,// top spike Vec2(o -h,}; // left spike draw->drawPolygon(star,sizeof(star)/sizeof(star[0]),1)); } //draw a solid polygon,绘制填充多边形 Vec2 vertices3[] = {Vec2(60,160)}; draw->drawSolidPoly(vertices3,1));//参数依次为:顶点、顶点个数、填充颜色 //draw a solid rectangle//绘制剪裁图形,参数依次为:起点、终点、颜色 draw->drawSolidRect(Vec2(10,10),Vec2(20,20),1)); //draw a solid circle绘制圆圈,参数依次为:终点、半径、角度、段数、X轴缩放、y轴缩放、颜色 draw->drawSolidCircle(VisibleRect::center() + Vec2(140,1)); //draw segment绘制线段,参数依次为:起点、终点、半径、颜色 draw->drawSegment(Vec2(20,s.height / 2),1)); draw->drawSegment(Vec2(10,0.5)); //draw triangle绘制三角形 draw->drawTriangle(Vec2(10,30),140),0.5)); bRet = true; }while(0); return bRet; } std::string DrawNodeTest::subtitle() const { return "Draw Node test"; } 5、效果图(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |