Cocos2D-Android-1之源码详解:8.DrawPrimitivesTest
admin
2023-02-09 21:00:14
0

package org.cocos2d.tests;


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个设置//同之前

        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);//创建surface

        setContentView(mGLSurfaceView);//映射view

        


        // attach the OpenGL view to a window

        CCDirector.sharedDirector().attachInView(mGLSurfaceView);//把view给导演类


        // set landscape mode

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


        // show FPS

        CCDirector.sharedDirector().setDisplayFPS(true);//实时显示每秒多少帧


        // frames per second

        CCDirector.sharedDirector().setAnimationInterval(1.0f / 60);//标定显示多少帧


        CCScene scene = CCScene.node();

        scene.addChild(nextAction());//创建节点

        scene.runAction(CCRotateBy.action(4, -360));//旋转-360度,在4秒钟


        // Make the Scene active

        CCDirector.sharedDirector().runWithScene(scene);//导演开始把图层上面的元素给view来演

    }


    @Override

    public void onStart() {//下面3个老方法不赘述

        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[] = {//只有一个...还用个数组..

            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() {//重新返回一个这个实例

        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");//第一个菜单项目,其实是个按钮,第一个是没按,第二个是按下,然后是this里的方法,然后方法名字是最后一个参数,java有反射机制,所以他才敢这样写..但是这样不好混淆,后面2个按钮同理

            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个菜单项的按钮放进一个菜单


            menu.setPosition(CGPoint.make(0, 0));//给菜单设置位置,0.0就是在原点..

            item1.setPosition(CGPoint.make(s.width / 2 - 100, 30));//只要按钮项目设对就行了,所以菜单的坐标就只是个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();//生成图层节点

            s.addChild(restartAction());

            CCDirector.sharedDirector().runWithScene(s);//开始画这个图层

        }


        public void nextCallback(Object sender) {//执行下一个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) {//执行上一个切换观景模式

            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) {//一个绘画类

            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);//设置线平滑模式

            

            CCDrawingPrimitives.ccDrawLine(gl, CGPoint.ccp(0, 0), CGPoint.ccp(s.width, s.height));//运用绘画基元来画线


            // 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);//设置线宽5.0

            gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);//设置颜色

            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);//设置点大小

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

            CCDrawingPrimitives.ccDrawPoint(gl, CGPoint.make(s.width / 2, s.height / 2));//基元绘画,画点


            // draw 4 small points

            CGPoint points[] = {CGPoint.ccp(60, 60), CGPoint.ccp(70, 70), CGPoint.ccp(60, 70), CGPoint.ccp(70, 60)};//点数组

            gl.glPointSize(4);//设置点大小

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

            CCDrawingPrimitives.ccDrawPoints(gl, points, 4);//基元画点


            // draw a green circle with 10 segments

            gl.glLineWidth(16);//设置线宽

            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);//设置线宽

            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);//基元画圆..,参数:基元,中心点坐标,半径,弧度值(参数是角度),段数,是否画线到圆心


            // open yellow poly

            gl.glColor4f(1.0f, 1.0f, 0.0f, 1.0f);//设置颜色

            gl.glLineWidth(10);//设置线宽

            CGPoint vertices[] = {CGPoint.ccp(0, 0), CGPoint.ccp(50, 50), CGPoint.ccp(100, 50), CGPoint.ccp(100, 100), CGPoint.ccp(50, 100)};//设置点数组

            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)};//点数组

            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);//绘制班塞尔曲线...一种计算机用来画曲线的曲线,,参数:起点,控点,结束点,顶点数


            // 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);//绘制立体贝塞尔曲线。更高端了,参数:起点,控点1,控点2,结束点,顶点数..


            // restore original values//基元恢复数据

            gl.glLineWidth(1);//线宽1

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

            gl.glPointSize(1);//点大小1

        }


        public String title() {

            return "draw primitives";

        }

    }


}


相关内容

热门资讯

德国总理:美国正在被伊朗羞辱 德国之声4月27日报道,德国总理默茨在访问一所学校时表示,在当前的持续冲突中,伊朗领导层正试图羞辱美...
理响中国|“长”歌以行,风云激... 光阴如梭,东方潮阔。这里是中国的长三角,世界的长三角。无论过去、现在还是未来,这片土地都因时代而生,...
白宫:特朗普及其国安团队开会讨... 新华社华盛顿4月27日电 美国白宫新闻秘书莱维特27日在记者会上证实,总统特朗普及其国家安全团队当天...
人民日报刊文:日本放开杀伤性武... 日本放开杀伤性武器出口推高地缘冲突风险(国际论坛)常思纯《人民日报》(2026年04月28日 第 0...
医疗保障法草案二审:明确生育保... 满足多样化健康保障需求本报记者 彭 波4月27日,医疗保障法草案二审稿提请十四届全国人大常委会第二十...
天津一景区发生自转旋翼机事故1... 澎湃新闻记者 吕新文中国民用航空华北地区管理局4月22日公布《豪客通航“10•1”天津长芦汉盐旅游区...
卡塔尔埃米尔与美国总统特朗普通... 当地时间24日,卡塔尔埃米尔塔米姆与美国总统特朗普通电话,重点就中东地区局势以及伊朗与美国谈判问题交...
男子30年前被扣押2859克黄... 澎湃新闻记者 王鑫家住辽宁省大连市的潘永嘉近日向澎湃新闻反映称,三十年前,他在大连周水子机场被盖州市...
商务部:取消反制欧盟两家金融机... 中华人民共和国商务部令二〇二六年 第1号鉴于欧盟已取消对中国两家金融机构的制裁措施,现公布《关于取消...
过去24小时共有5艘船只通过霍... 总台记者当地时间24日获悉,过去24小时内,共有5艘船只通过霍尔木兹海峡,其中包括一艘伊朗油轮。(总...