Cocos2D-Android-1之源碼詳解:8.DrawPrimitivesTest

package org.cocos2d.tests;

創(chuàng)新互聯(lián)公司專注于天門企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城開發(fā)。天門網(wǎng)站建設(shè)公司,為天門等地區(qū)提供建站服務(wù)。全流程按需開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

import javax.microedition.khronos.opengles.GL10;

import org.cocos2d.actions.interval.CCRotateBy;

import org.cocos2d.config.ccMacros;

import org.cocos2d.layers.CCLayer;

import org.cocos2d.layers.CCScene;

import org.cocos2d.menus.CCMenu;

import org.cocos2d.menus.CCMenuItemImage;

import org.cocos2d.nodes.CCDirector;

import org.cocos2d.opengl.CCDrawingPrimitives;

import org.cocos2d.opengl.CCGLSurfaceView;

import org.cocos2d.types.CGPoint;

import org.cocos2d.types.CGSize;

import android.app.Activity;

import android.os.Bundle;

import android.view.Window;

import android.view.WindowManager;

public class DrawPrimitivesTest extends Activity {//繪畫原語測試

    // private static final String LOG_TAG = DrawPrimitivesTest.class.getSimpleName();

    private CCGLSurfaceView mGLSurfaceView;//建立view

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);//3個(gè)設(shè)置//同之前

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,

                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        

        mGLSurfaceView = new CCGLSurfaceView(this);//創(chuàng)建surface

        setContentView(mGLSurfaceView);//映射view

        

        // attach the OpenGL view to a window

        CCDirector.sharedDirector().attachInView(mGLSurfaceView);//把view給導(dǎo)演類

        // set landscape mode

        CCDirector.sharedDirector().setLandscape(false);//不橫屏

        // show FPS

        CCDirector.sharedDirector().setDisplayFPS(true);//實(shí)時(shí)顯示每秒多少幀

        // frames per second

        CCDirector.sharedDirector().setAnimationInterval(1.0f / 60);//標(biāo)定顯示多少幀

        CCScene scene = CCScene.node();

        scene.addChild(nextAction());//創(chuàng)建節(jié)點(diǎn)

        scene.runAction(CCRotateBy.action(4, -360));//旋轉(zhuǎn)-360度,在4秒鐘

        // Make the Scene active

        CCDirector.sharedDirector().runWithScene(scene);//導(dǎo)演開始把圖層上面的元素給view來演

    }

    @Override

    public void onStart() {//下面3個(gè)老方法不贅述

        super.onStart();

    }

    @Override

    public void onPause() {

        super.onPause();

        CCDirector.sharedDirector().onPause();

    }

    @Override

    public void onResume() {

        super.onResume();

        CCDirector.sharedDirector().onResume();

    }

    @Override

    public void onDestroy() {

        super.onDestroy();

        CCDirector.sharedDirector().end();

        // CCTextureCache.sharedTextureCache().removeAllTextures();

    }

    static int sceneIdx = -1;

    static Class<?> transitions[] = {//只有一個(gè)...還用個(gè)數(shù)組..

            Test1.class,

    };

    public static CCLayer nextAction() {

        sceneIdx++;

        sceneIdx = sceneIdx % transitions.length;

        return restartAction();

    }

    public static CCLayer backAction() {

        sceneIdx--;

        int total = transitions.length;

        if (sceneIdx < 0)

            sceneIdx += total;

        return restartAction();

    }

    public static CCLayer restartAction() {//重新返回一個(gè)這個(gè)實(shí)例

        try {

            Class<?> c = transitions[sceneIdx];

            return (CCLayer) c.newInstance();

        } catch (Exception e) {

            return null;

        }

    }

    public static class TestDemo extends CCLayer {//測試

        public TestDemo() {

            CGSize s = CCDirector.sharedDirector().winSize();//得到大小

            CCMenuItemImage item1 = CCMenuItemImage.item("b1.png", "b2.png", this, "backCallback");//第一個(gè)菜單項(xiàng)目,其實(shí)是個(gè)按鈕,第一個(gè)是沒按,第二個(gè)是按下,然后是this里的方法,然后方法名字是最后一個(gè)參數(shù),java有反射機(jī)制,所以他才敢這樣寫..但是這樣不好混淆,后面2個(gè)按鈕同理

            CCMenuItemImage item2 = CCMenuItemImage.item("r1.png", "r2.png", this, "restartCallback");

            CCMenuItemImage item3 = CCMenuItemImage.item("f1.png", "f2.png", this, "nextCallback");

            CCMenu menu = CCMenu.menu(item1, item2, item3);//把這3個(gè)菜單項(xiàng)的按鈕放進(jìn)一個(gè)菜單

            menu.setPosition(CGPoint.make(0, 0));//給菜單設(shè)置位置,0.0就是在原點(diǎn)..

            item1.setPosition(CGPoint.make(s.width / 2 - 100, 30));//只要按鈕項(xiàng)目設(shè)對就行了,所以菜單的坐標(biāo)就只是個(gè)0.0

            item2.setPosition(CGPoint.make(s.width / 2, 30));

            item3.setPosition(CGPoint.make(s.width / 2 + 100, 30));

            addChild(menu, -1);//最后把菜單作為activity的子類,而且顯示順序是-1,有可能被遮擋的

        }

        /*

         * After setting the screen orientation to landscape,

         *   the Activity will be restarted, so it seems we should not call setLandscape here

         *  this is a bug, we should make full use of android's capability, but not partly.  

         */

        public void restartCallback(Object sender) {//重新加載,并切換觀景模式,橫豎屏

            boolean landscape = CCDirector.sharedDirector().getLandscape();//得到觀景模式

            CCDirector.sharedDirector().setLandscape(!landscape);//切換觀景模式

            CCScene s = CCScene.node();//生成圖層節(jié)點(diǎn)

            s.addChild(restartAction());

            CCDirector.sharedDirector().runWithScene(s);//開始畫這個(gè)圖層

        }

        public void nextCallback(Object sender) {//執(zhí)行下一個(gè)class并切換觀景模式

            boolean landscape = CCDirector.sharedDirector().getLandscape();

            CCDirector.sharedDirector().setLandscape(!landscape);

            CCScene s = CCScene.node();

            s.addChild(nextAction());

            CCDirector.sharedDirector().runWithScene(s);

        }

        public void backCallback(Object sender) {//執(zhí)行上一個(gè)切換觀景模式

            boolean landscape = CCDirector.sharedDirector().getLandscape();

            CCDirector.sharedDirector().setLandscape(!landscape);

            CCScene s = CCScene.node();

            s.addChild(backAction());

            CCDirector.sharedDirector().runWithScene(s);

        }

        String title() {

            return "No title";

        }

    }

    public static class Test1 extends TestDemo {//測試1

        public static CCLayer node() {

            return new Test1();

        }

        //

        // TIP:

        // Every CocosNode has a "draw" method.

        // In the "draw" method you put all the code that actually draws your node.

        // And Test1 is a subclass of TestDemo, which is a subclass of Layer, which is a subclass of CocosNode.

        //

        // As you can see the drawing primitives aren't CocosNode objects. They are just helper

        // functions that let's you draw basic things like: points, line, polygons and circles.

        //

        //

        // TIP:

        // Don't draw your stuff outside the "draw" method. Otherwise it won't get transformed.

        //

        //

        // TIP:

        // If you want to rotate/translate/scale a circle or any other "primtive", you can do it by rotating

        // the node. eg:

        //    this.rotation = 90;

        //

        public void draw(GL10 gl) {//一個(gè)繪畫類

            CGSize s = CCDirector.sharedDirector().winSize();//還是得到屏幕大小

            // draw a simple line

            // The default state is:

            // Line Width: 1

            // color: 255,255,255,255 (white, non-transparent)

            // Anti-Aliased

            gl.glEnable(GL10.GL_LINE_SMOOTH);//設(shè)置線平滑模式

            

            CCDrawingPrimitives.ccDrawLine(gl, CGPoint.ccp(0, 0), CGPoint.ccp(s.width, s.height));//運(yùn)用繪畫基元來畫線

            // line: color, width, aliased

            // glLineWidth > 1 and GL_LINE_SMOOTH are not compatible

            // GL_SMOOTH_LINE_WIDTH_RANGE = (1,1) on iPhone

            gl.glDisable(GL10.GL_LINE_SMOOTH);//撤銷平滑線模式

            gl.glLineWidth(5.0f);//設(shè)置線寬5.0

            gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);//設(shè)置顏色

            CCDrawingPrimitives.ccDrawLine(gl, CGPoint.ccp(0, s.height), CGPoint.ccp(s.width, 0));//然后又是基元繪畫線

            // 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

            gl.glPointSize(64);//設(shè)置點(diǎn)大小

            gl.glColor4f(0.0f, 0.0f, 1.0f, 0.5f);//基元顏色

            CCDrawingPrimitives.ccDrawPoint(gl, CGPoint.make(s.width / 2, s.height / 2));//基元繪畫,畫點(diǎn)

            // draw 4 small points

            CGPoint points[] = {CGPoint.ccp(60, 60), CGPoint.ccp(70, 70), CGPoint.ccp(60, 70), CGPoint.ccp(70, 60)};//點(diǎn)數(shù)組

            gl.glPointSize(4);//設(shè)置點(diǎn)大小

            gl.glColor4f(0.0f, 1.0f, 1.0f, 1.0f);//基元顏色

            CCDrawingPrimitives.ccDrawPoints(gl, points, 4);//基元畫點(diǎn)

            // draw a green circle with 10 segments

            gl.glLineWidth(16);//設(shè)置線寬

            gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);//基元顏色

            CCDrawingPrimitives.ccDrawCircle(gl, CGPoint.make(s.width / 2, s.height / 2), 100, 0, 10, false);//基元畫線

            // draw a green circle with 50 segments with line to center

            gl.glLineWidth(2);//設(shè)置線寬

            gl.glColor4f(0.0f, 1.0f, 1.0f, 1.0f);//顏色

            CCDrawingPrimitives.ccDrawCircle(gl, CGPoint.make(s.width / 2, s.height / 2), 50, ccMacros.CC_DEGREES_TO_RADIANS(90), 50, true);//基元畫圓..,參數(shù):基元,中心點(diǎn)坐標(biāo),半徑,弧度值(參數(shù)是角度),段數(shù),是否畫線到圓心

            // open yellow poly

            gl.glColor4f(1.0f, 1.0f, 0.0f, 1.0f);//設(shè)置顏色

            gl.glLineWidth(10);//設(shè)置線寬

            CGPoint vertices[] = {CGPoint.ccp(0, 0), CGPoint.ccp(50, 50), CGPoint.ccp(100, 50), CGPoint.ccp(100, 100), CGPoint.ccp(50, 100)};//設(shè)置點(diǎn)數(shù)組

            CCDrawingPrimitives.ccDrawPoly(gl, vertices, 5, false);//繪制多邊形

            // closed purple poly

            gl.glColor4f(1.0f, 0.0f, 1.0f, 1.0f);//顏色

            gl.glLineWidth(2);//線寬

            CGPoint vertices2[] = {CGPoint.ccp(30, 130), CGPoint.ccp(30, 230), CGPoint.ccp(50, 200)};//點(diǎn)數(shù)組

            CCDrawingPrimitives.ccDrawPoly(gl, vertices2, 3, true);//多邊形

            // draw quad bezier path

            CCDrawingPrimitives.ccDrawQuadBezier(gl, CGPoint.make(0,s.height), CGPoint.make(s.width/2,s.height/2), CGPoint.make(s.width, s.height), 50);//繪制班塞爾曲線...一種計(jì)算機(jī)用來畫曲線的曲線,,參數(shù):起點(diǎn),控點(diǎn),結(jié)束點(diǎn),頂點(diǎn)數(shù)

            // draw cubic bezier path

            CCDrawingPrimitives.ccDrawCubicBezier(gl, CGPoint.make(s.width/2, s.height/2), CGPoint.make(s.width/2+30, s.height/2+50),

                    CGPoint.make(s.width/2+60, s.height/2-50), CGPoint.make(s.width, s.height/2),100);//繪制立體貝塞爾曲線。更高端了,參數(shù):起點(diǎn),控點(diǎn)1,控點(diǎn)2,結(jié)束點(diǎn),頂點(diǎn)數(shù)..

            // restore original values//基元恢復(fù)數(shù)據(jù)

            gl.glLineWidth(1);//線寬1

            gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);//顏色黑

            gl.glPointSize(1);//點(diǎn)大小1

        }

        public String title() {

            return "draw primitives";

        }

    }

}

新聞名稱:Cocos2D-Android-1之源碼詳解:8.DrawPrimitivesTest
文章轉(zhuǎn)載:http://muchs.cn/article6/pidcig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、電子商務(wù)、商城網(wǎng)站、做網(wǎng)站、軟件開發(fā)、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)