在Silverlight框架的WP8應用程序裏面,咱們畫幾何圖形的時候會經過Line等等的類在用C#代碼或者在XAML上畫圖,那麼在Cocos2d-x For WP8裏面咱們同樣也能夠實現這樣的功能。那麼在Cocos2d-x中畫圖的邏輯要放在本身寫的draw函數裏面,這樣圖形引擎纔會把你的圖形給畫出來。框架
好比說:函數
將畫一條直線放到下面這函數是畫不出來的spa
bool HelloWorld::init()rest
{code
ccDrawLine( ccp(0, 0), ccp(s.width, s.height) ); CHECK_GL_ERROR_DEBUG();blog
}ci
而放到draw函數就能畫出來,也不須要調用get
void HelloWorld::draw()it
{class
ccDrawLine( ccp(0, 0), ccp(s.width, s.height) ); CHECK_GL_ERROR_DEBUG();
}
示例代碼:
void HelloWorld::draw() { CCLayer::draw(); CCSize s = CCDirector::sharedDirector()->getWinSize(); // 畫兩條對角的直線 // 默認的數值: // Line Width: 1 // color: 255,255,255,255 (white, non-transparent) // Anti-Aliased //glEnable(GL_LINE_SMOOTH); //ccDrawLine( CCPointMake(0, 0), CCPointMake(s.width, s.height) ); // line: color, width, aliased //glDisable(GL_LINE_SMOOTH); //glLineWidth( 5.0f ); /*glColor4ub(255,0,0,255);*/ CCDrawingPrimitive::D3DColor4f(1.0, 0.0, 0.0, 1.0); ccDrawLine( CCPointMake(0, s.height), CCPointMake(s.width, 0) ); ccDrawLine( CCPointMake(0, 0), CCPointMake(s.width,s.height ) ); // TIP: //若是是使用同一種顏色則不須要從新去調用CCDrawingPrimitive::D3DColor4f方法去設置顏色 I // // Remember: OpenGL is a state-machine. // draw big point in the center //glPointSize(64); /*glColor4ub(0,0,255,128);*/ CCDrawingPrimitive::D3DColor4f(0.0, 0.0, 1.0, 0.5); ccDrawPoint( CCPointMake(65, 65) ); /*ccDrawPoint( CCPointMake(s.width / 2, s.height / 2) );*/ // draw 4 small points CCPoint points[] = { CCPointMake(60,60), CCPointMake(70,70), CCPointMake(60,70), CCPointMake(70,60) }; //glPointSize(4); /*glColor4ub(0,255,255,255);*/ CCDrawingPrimitive::D3DColor4f(0.0, 1.0, 1.0, 1.0); ccDrawPoints( points, 4); // draw a green circle with 10 segments //glLineWidth(16); /*glColor4ub(0, 255, 0, 255);*/ CCDrawingPrimitive::D3DColor4f(0.0, 1.0, 0.0, 1.0); ccDrawCircle( CCPointMake(s.width/2, s.height/2), 100, 0, 10, false); // draw a green circle with 50 segments with line to center //glLineWidth(2); /*glColor4ub(0, 255, 255, 255);*/ CCDrawingPrimitive::D3DColor4f(0.0, 1.0, 1.0, 1.0); ccDrawCircle( CCPointMake(s.width/2, s.height/2), 50, CC_DEGREES_TO_RADIANS(90), 50, true); // open yellow poly /*glColor4ub(255, 255, 0, 255);*/ CCDrawingPrimitive::D3DColor4f(1.0, 1.0, 0.0, 1.0); //glLineWidth(10); CCPoint vertices[] = { CCPointMake(0,0), CCPointMake(50,50), CCPointMake(100,50), CCPointMake(100,100), CCPointMake(50,100) }; ccDrawPoly( vertices, 5, false); // closed purble poly /*glColor4ub(255, 0, 255, 255);*/ CCDrawingPrimitive::D3DColor4f(1.0, 0.0, 1.0, 1.0); //glLineWidth(2); CCPoint vertices2[] = { CCPointMake(30,130), CCPointMake(30,230), CCPointMake(50,200) }; ccDrawPoly( vertices2, 3, true); // draw quad bezier path ccDrawQuadBezier(CCPointMake(0,s.height), CCPointMake(s.width/2,s.height/2), CCPointMake(s.width,s.height), 50); // draw cubic bezier path ccDrawCubicBezier(CCPointMake(s.width/2, s.height/2), CCPointMake(s.width/2+30,s.height/2+50), CCPointMake(s.width/2+60,s.height/2-50),CCPointMake(s.width, s.height/2),100); // restore original values //glLineWidth(1); /*glColor4ub(255,255,255,255);*/ CCDrawingPrimitive::D3DColor4f(1.0, 1.0, 1.0, 1.0); //glPointSize(1); }
運行的效果: