【玩轉cocos2d-x之三十四】繪圖:CCDrawingPrimitives和CCDrawNode
最近忙出翔了,這年過的也揪心。好久沒來更新部落格了,今天就來寫一寫cocos2d-x中圖形的繪製。
1.概述
其實cocos2d-x封裝了大量的opengl的繪圖函式,我們可以很輕鬆的在遊戲中呼叫。但是實際上我們很少會這麼做,因為編碼繪圖的方式效率實在太低了,尤其是諸如一些高階貝塞爾曲線之類的,官方也不建議這麼做,使用影象能更有效的提高繪圖效率,同時也會更為的美觀,也減少了一些精靈遮擋問題和邊緣鋸齒問題。cocos2d-x中繪圖主要有兩種方式:CCDrawingPrimitives和CCDrawNode。它們都在cocos2dx\draw_nodes目錄下。
2.CCDrawingPrimitives(原生繪圖)
2.1.特點
CCDrawingPrimitives必須在節點的draw函式中呼叫,draw函式每幀被呼叫一次,主要做一些繪製的工作。但這並不意味著它就是靜態不可改變的,我們完全可以通過修改描繪內容的方式來動態繪圖。比如可以直接在用觸控的方式在螢幕上繪圖。
另外,使用CCDrawingPrimitives繪圖預設的Z-Order是0,所以可能會被其他節點覆蓋,因為它無法設定Z-Order,所以只能通過設定其他節點的Z-Order為負值使得繪圖在最前端得以看見。
2.2.API
- //繪製一個給定x,y值的點
-
void CC_DLL ccDrawPoint( const CCPoint& point );
- //繪製一組點
- void CC_DLL ccDrawPoints( const CCPoint *points, unsigned int numberOfPoints );
- //繪製一條給定起始點和目標點的直線
- void CC_DLL ccDrawLine( const CCPoint& origin, const CCPoint& destination );
- //繪製一個給定起始點和目標點的矩形
- void CC_DLL ccDrawRect( CCPoint origin, CCPoint destination );
- //繪製一個給定起始點和目標點和顏色的矩形
-
void CC_DLL ccDrawSolidRect( CCPoint origin, CCPoint destination, ccColor4F color );
- //繪製一個給定一組點的多邊形,可以是閉合或開放
- void CC_DLL ccDrawPoly( const CCPoint *vertices, unsigned int numOfVertices, bool closePolygon );
- //繪製一個給定一組點和顏色的多邊形
- void CC_DLL ccDrawSolidPoly( const CCPoint *poli, unsigned int numberOfPoints, ccColor4F color );
- //繪製一個給定中心,半徑,分段數的圓
- void CC_DLL ccDrawCircle( const CCPoint& center, float radius, float angle, unsigned int segments, bool drawLineToCenter, float scaleX, float scaleY);
- void CC_DLL ccDrawCircle( const CCPoint& center, float radius, float angle, unsigned int segments, bool drawLineToCenter);
- //繪製一個帶一個控制點的貝塞爾曲線(二次)
- void CC_DLL ccDrawQuadBezier(const CCPoint& origin, const CCPoint& control, const CCPoint& destination, unsigned int segments);
- //繪製一個帶兩個控制點的貝塞爾曲線(高階)
- void CC_DLL ccDrawCubicBezier(const CCPoint& origin, const CCPoint& control1, const CCPoint& control2, const CCPoint& destination, unsigned int segments);
- //繪製預設曲率的基數樣條
- void CC_DLL ccDrawCatmullRom( CCPointArray *arrayOfControlPoints, unsigned int segments );
- //繪製指定曲率的基數樣條
- void CC_DLL ccDrawCardinalSpline( CCPointArray *config, float tension, unsigned int segments );
- //設定繪製顏色,4個byte型
- void CC_DLL ccDrawColor4B( GLubyte r, GLubyte g, GLubyte b, GLubyte a );
- //設定繪製顏色,4個float型
- void CC_DLL ccDrawColor4F( GLfloat r, GLfloat g, GLfloat b, GLfloat a );
- //設定點大小,預設為1
- void CC_DLL ccPointSize( GLfloat pointSize );
2.3.示例
程式碼使用的是TestCpp中程式碼。
3.CCDrawNode(繪圖節點)
3.1.特點
CCDrawNode從CCNode繼承而來,是一個圖形繪製的節點類,它提供了幾個常用的點線面函式方便繪圖,其實在之前的遊戲遮罩一文中就有寫過。相較於CCDrawingPrimitives,這裡更推薦使用CCDrawNode,
(1)它將繪圖封裝為普通節點的方式進行處理,符合cocos2d-x樹狀程式設計的思想,同時也避免了一些遮蓋的問題。
(2)不需要在draw函式中呼叫。
(3)使用批處理繪圖,提高了繪圖效率。
3.2.API
- //繪製一個指定位置,大小,顏色的點
- void drawDot(const CCPoint &pos, float radius, const ccColor4F &color);
- //繪製一個指定起點終點,大小,顏色的點
- void drawSegment(const CCPoint &from, const CCPoint &to, float radius, const ccColor4F &color);
- //繪製一個指定點,填充顏色,邊框顏色的,邊框寬度的多邊形
- void drawPolygon(CCPoint *verts, unsigned int count, const ccColor4F &fillColor, float borderWidth, const ccColor4F &borderColor);
3.3.示例
程式碼也是TestCpp中的程式碼。