Cocos2d-android-1 Source Detailed: 8.DrawPrimitivesTest

Source: Internet
Author: User

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 {//Painting primitives test

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

Private Ccglsurfaceview mglsurfaceview;//Set up view



@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Requestwindowfeature (window.feature_no_title);//3 settings//Same as before

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

Setcontentview (Mglsurfaceview);//Map view


Attach the OpenGL view to a window

Ccdirector.shareddirector (). Attachinview (Mglsurfaceview);//give the view to the Director class


Set Landscape mode

Ccdirector.shareddirector (). Setlandscape (false);//No horizontal screen


Show FPS

Ccdirector.shareddirector (). Setdisplayfps (true);//real-time display of how many frames per second


Frames per second

Ccdirector.shareddirector (). Setanimationinterval (1.0F/60);//calibration shows how many frames


Ccscene scene = Ccscene.node ();

Scene.addchild (Nextaction ());//Create Node

Scene.runaction (Ccrotateby.action (4,-360));//rotation-360 degrees, in 4 seconds


Make the Scene active

Ccdirector.shareddirector (). Runwithscene (scene);//The Director began to show the elements above the layer to the view.

}


@Override

public void OnStart () {///below 3 old methods do not repeat

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[] = {//Only one ... Also use a number of groups:

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 () {//re-returns an instance of this

try {

Class<?> C = Transitions[sceneidx];

Return (Cclayer) c.newinstance ();

} catch (Exception e) {

return null;

}

}


public static class Testdemo extends Cclayer {//test

Public Testdemo () {

Cgsize s = Ccdirector.shareddirector (). Winsize ();//Get size


Ccmenuitemimage item1 = Ccmenuitemimage.item ("B1.png", "b2.png", this, "backcallback");//The first menu item is actually a button, the first one is not pressed, The second one is pressed, then the method in this, then the method name is the last parameter, and Java has a reflection mechanism, so he dares to write it. But this is not confusing, the back of the 2 buttons the same

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);//Put the buttons of the 3 menu items in a


Menu.setposition (cgpoint.make (0, 0));//Set the menu position, 0.0 is at the origin point.

Item1.setposition (Cgpoint.make (S.WIDTH/2-100, 30));//As long as the button project is set to the right, so the menu coordinates are just a 0.0

Item2.setposition (Cgpoint.make (S.WIDTH/2, 30));

Item3.setposition (Cgpoint.make (S.WIDTH/2 + 100, 30));


AddChild (menu,-1);//finally the menu as a subclass of activity, and the display order is-1, there may be obscured

}


/*

* After setting the screens orientation to landscape,

* The Activity would be restarted, so it seems we should don't call Setlandscape here

* This was a bug, we should make full use of Android's capability, but not partly.

*/

public void Restartcallback (Object sender) {//reload, and toggle viewing mode, portrait and landscape

Boolean landscape = Ccdirector.shareddirector (). Getlandscape ();//Get view mode

Ccdirector.shareddirector (). Setlandscape (!landscape);//Toggle view mode


Ccscene s = Ccscene.node ();//Generate Layer nodes

S.addchild (Restartaction ());

Ccdirector.shareddirector (). Runwithscene (s);//start drawing this layer

}


public void Nextcallback (Object sender) {//Executes next class and toggles view mode

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) {//Perform the previous switch view mode

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 {//test 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 are a subclass of Layer, which is a subclass of Cocosnode.

//

As can see the drawing primitives aren ' t cocosnode objects. They is just helper

Functions that let's you draw the 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) {//a painting class

Cgsize s = Ccdirector.shareddirector (). Winsize ();//Get screen size



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);//Set Line smoothing mode

Ccdrawingprimitives.ccdrawline (GL, cgpoint.ccp (0, 0), CGPOINT.CCP (S.width, s.height));//Use drawing primitives to draw lines


Line:color, Width, aliased

Gllinewidth > 1 and Gl_line_smooth is not compatible

Gl_smooth_line_width_range = (all) on IPhone

Gl.gldisable (Gl10.gl_line_smooth);//undo Smooth Line Mode

Gl.gllinewidth (5.0f);//Set line width 5.0

gl.glcolor4f (1.0f, 0.0f, 0.0f, 1.0f);//Set color

Ccdrawingprimitives.ccdrawline (GL, CGPOINT.CCP (0, S.height), CGPOINT.CCP (s.width, 0));//Then primitive drawing line


TIP:

If you're going to use all 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);//Set point size

gl.glcolor4f (0.0f, 0.0f, 1.0f, 0.5f);//Primitive color

Ccdrawingprimitives.ccdrawpoint (GL, Cgpoint.make (S.WIDTH/2, S.HEIGHT/2));//primitive painting, drawing point


Draw 4 Small points

Cgpoint points[] = {CGPOINT.CCP (max.), CGPOINT.CCP ((+), CGPOINT.CCP (+), CGPOINT.CCP (70, 60)};//points Group

Gl.glpointsize (4);//Set point size

gl.glcolor4f (0.0f, 1.0f, 1.0f, 1.0f);//Primitive color

Ccdrawingprimitives.ccdrawpoints (GL, points, 4);//Primitive Draw point


Draw a green circle with ten segments

Gl.gllinewidth (16);//Set line width

gl.glcolor4f (0.0f, 1.0f, 0.0f, 1.0f);//Primitive color

Ccdrawingprimitives.ccdrawcircle (GL, Cgpoint.make (S.WIDTH/2, S.HEIGHT/2), +, 0, false);//Primitive Draw line


Draw a green circle with segments and line to center

Gl.gllinewidth (2);//Set line width

gl.glcolor4f (0.0f, 1.0f, 1.0f, 1.0f);//Color

Ccdrawingprimitives.ccdrawcircle (GL, Cgpoint.make (S.WIDTH/2, S.HEIGHT/2), Ccmacros.cc_degrees_to_radians (90), true);//primitive Draw circle ..., parameters: primitive, center point coordinate, radius, radian value (argument is angle), number of segments, whether to draw line to center


Open Yellow Poly

gl.glcolor4f (1.0f, 1.0f, 0.0f, 1.0f);//Set color

Gl.gllinewidth (10);//Set line width

Cgpoint vertices[] = {CGPOINT.CCP (0, 0), CGPOINT.CCP (50), CGPOINT.CCP (+), CGPOINT.CCP (+), CGPOINT.CCP , 100)};//set the points group

Ccdrawingprimitives.ccdrawpoly (GL, vertices, 5, false);//Draw Polygon


Closed Purple Poly

gl.glcolor4f (1.0f, 0.0f, 1.0f, 1.0f);//Color

Gl.gllinewidth (2);//Line width

Cgpoint vertices2[] = {CGPOINT.CCP (a), CGPOINT.CCP (50, 200) CGPOINT.CCP points Group

Ccdrawingprimitives.ccdrawpoly (GL, Vertices2, 3, true);//Polygon


Draw Quad Bézier 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 Bánzer curve ... A computer curve used to draw curves, parameters: Starting point, handle, end point, number of vertices


Draw cubic Bézier 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);//Draw a cubic Bezier curve. More advanced, Parameters: Start point, handle 1, handle 2, end point, number of vertices ...


Restore original values//Primitive recovery data

Gl.gllinewidth (1);//Line width 1

gl.glcolor4f (1.0f, 1.0f, 1.0f, 1.0f);//Color Black

Gl.glpointsize (1);//point size 1

}


Public String title () {

Return "Draw Primitives";

}

}


}


Cocos2d-android-1 Source Detailed: 8.DrawPrimitivesTest

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.