cocos2d-x 圖形繪製

轉自:http://blog.csdn.net/zhy_cheng/article/details/8480048spa

圖形繪製的話,在cocos2d-x自帶的TestCpp裏有,包括繪製點,直線,多邊形(填充的和沒有填充的),圓,貝賽爾曲線。.net

首先在HelloWorld類中重寫父類的draw方法code

virtual void draw();  

在源文件中將init中的類容刪去,應爲init方法比draw先執行,會覆蓋咱們畫出的效果。刪除以後,init方法以下blog

bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        CC_BREAK_IF(! CCLayer::init());
        bRet = true;
    } while (0);

    return bRet;
}

而後在draw方法中寫以下代碼:ci

void HelloWorld::draw()
{
    CHECK_GL_ERROR_DEBUG();
    /*
    畫一條直線,默認的寬度是1,顏色是白色,不透明,glEnable(GL_LINE_SMOOTH);
    默認的狀況下是後面再也不設置顏色後線寬
    */

    glLineWidth( 1.0f );
    ccDrawColor4B(255,255,255,255);
    ccDrawLine(ccp(0,0),ccp(480,320));
    CHECK_GL_ERROR_DEBUG();

    glLineWidth( 5.0f );
    ccDrawColor4B(255,0,0,255);
    ccDrawLine(ccp(0,320), ccp(480,0));
    CHECK_GL_ERROR_DEBUG();

    //ccPointSize(128);這個沒用啊
    ccDrawColor4B(0,255,255,128);
    ccDrawPoint( ccp(240,200) );
    CHECK_GL_ERROR_DEBUG();

    // 4個點一塊兒畫
    CCPoint points[] = { ccp(60,60), ccp(70,70), ccp(60,70), ccp(70,60) };
    ccPointSize(4);
    ccDrawColor4B(0,255,255,255);
    ccDrawPoints( points, 4);

    // draw a green circle with 10 segments
    glLineWidth(1);
    ccDrawColor4B(0, 255, 0, 255);
    ccDrawCircle( ccp(240,160),//圓心
        100,//半徑
        1, //若是後面設置了從圓心到圓的連線爲true的話,
        //這個值是連線的角度,0爲水平向左,逆時針
        360,//將這個圓分爲多少塊
        true//是否有從圓心到圓的連線
        );

    //畫一個多邊形
    ccDrawColor4B(255, 255, 0, 255);
    glLineWidth(1);
    CCPoint vertices[] = { ccp(0,0), ccp(50,50), ccp(100,50), ccp(100,100), ccp(50,100) };
    ccDrawPoly( vertices, 5, true//是否封閉
        );

     // 填充的多邊形
    glLineWidth(1);
    CCPoint filledVertices[] = { ccp(0,120), ccp(50,120), ccp(50,170), ccp(25,200), ccp(0,170) };
    ccDrawSolidPoly(filledVertices, 5, ccc4f(0.5f, 0.5f, 1, 1 )//填充顏色
        );

    //貝塞爾曲線
    ccDrawColor4B(255, 255, 0, 255);
    ccDrawCubicBezier(ccp(0,0),//開始點
        ccp(50,50),//控制點
        ccp(250,100),//控制點
        ccp(300,300),//目標點
        100//分紅多少段獲得的曲線
        );

    // 還原默認值
    glLineWidth(1);
    ccDrawColor4B(255,255,255,255);
    ccPointSize(1);
}

相關文章
相關標籤/搜索