Cocos2D-Android-1 source code explanation: 8. DrawPrimitivesTest

Source: Internet
Author: User

Cocos2D-Android-1 source code explanation: 8. DrawPrimitivesTest
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. o Pengl. 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 {// test the painting primitive // private static final String LOG_TAG = DrawPrimitivesTest. class. getSimpleName (); private CCGLSurfaceView mGLSurfaceView; // create a view @ Overri De protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); // three settings // same as the previous 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 C CGLSurfaceView (this); // create the surface setContentView (mGLSurfaceView); // map the view // attach the OpenGL view to a window CCDirector. shareddire (). attachInView (mGLSurfaceView); // give the view to the director class // set landscape mode CCDirector. shareddire (). setLandscape (false); // do not landscape screen // show FPS CCDirector. shareddire (). setDisplayFPS (true); // display the number of frames per second in real time // frames per second CCDirector. shareddire (). setAnimationInterval (1. 0f/60); // specify the number of frames displayed. node (); scene. addChild (nextAction (); // create a node scene. runAction (CCRotateBy. action (4,-360); // rotate-360 degrees, in 4 seconds // Make the Scene active CCDirector. shareddire (). runWithScene (scene); // The director starts to show the elements above the layer to the view.} @ Override public void onStart () {// The following three old methods do not repeat super. onStart () ;}@ Override public void onPause () {super. onPause (); CCDirector. shareddire (). onPause ();} @ Override public void onResume () {super. onResume (); CCDirector. shareddire (). onResume () ;}@ Override public void onDestroy () {super. onDestroy (); CCDirector. shareddire (). end (); // CCTextureCache. sharedTextureCache (). removeAllTextures ();} static int sceneIdx =-1; static Class <?> Transitions [] = {// only one... an array is also used .. 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 () {// return a new 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. shareddire (). winSize (); // obtain the size CCMenuItemImage item1 = CCMenuItemImage. item ("b1.png", "b2.png", this, "backCallback"); // The first menu item is actually a button, the first is not pressed, and the second is pressed, then there is the method in this, and the method name is the last parameter. java has a reflection mechanism, so he dared to write it like this .. but it is difficult to mix Second, the following two buttons are the same as 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 these three menu items into a menu. setPosition (CGPoint. make (0, 0); // set the position for the menu. 0.0 is at the origin .. item1.setPosition (CGPoint. make (s. width/2-100, 30); // you only need to set the right button for the project, so the coordinates of the menu are only 0.0 it Em2.setPosition (CGPoint. make (s. width/2, 30); item3.setPosition (CGPoint. make (s. width/2 + 100, 30); addChild (menu,-1); // Finally, the menu is used as a subclass of the activity, and the display sequence is-1, possibly blocked}/** After setting the screen orientation to landscape, * the Activity will be restarted, so it seems we shocould not call setLandscape here * this is a bug, we shoshould make full use of android's capability, but not partly. */public void RestartCallback (Object sender) {// reload and switch the viewing mode. boolean landscape = CCDirector ctor. shareddire (). getLandscape (); // get the viewing mode CCDirector. shareddire (). setLandscape (! Landscape); // switch the viewing mode CCScene s = CCScene. node (); // generate layer node s. addChild (restartAction (); CCDirector. shareddire (). runWithScene (s); // start to draw this layer} public void nextCallback (Object sender) {// execute the next class and switch the viewing mode boolean landscape = CCDirector. shareddire (). getLandscape (); CCDirector. shareddire (). setLandscape (! Landscape); CCScene s = CCScene. node (); s. addChild (nextAction (); CCDirector. shareddire (). runWithScene (s);} public void backCallback (Object sender) {// execute the previous switchover mode boolean landscape = CCDirector. shareddire (). getLandscape (); CCDirector. shareddire (). setLandscape (! Landscape); CCScene s = CCScene. node (); s. addChild (backAction (); CCDirector. shareddire (). 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 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 r Otate/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. shareddire (). winSize (); // still get The screen size // draw a simple line // The default state is: // Line Width: 1 // color: 255,255,255,255 (white, non-transparent) // Anti-Aliased gl. glable (GL10.GL _ LINE_SMOOTH); // sets the line smoothing mode CCDraw IngPrimitives. ccDrawLine (gl, CGPoint. ccp (0, 0), CGPoint. ccp (s. width, s. height); // draw a line using a painting element // line: color, width, aliased // glLineWidth> 1 and GL_LINE_SMOOTH are not compatible // GL_SMOOTH_LINE_WIDTH_RANGE =) on iPhone gl. glDisable (GL10.GL _ LINE_SMOOTH); // cancel the smooth line mode gl. glLineWidth (5.0f); // set the line width to 5.0 gl. glColor4f (1.0f, 0.0f, 0.0f, 1.0f); // set the color CCDrawingPrimitives. ccDrawLine (gl, CGPoint. ccp (0, s. h Eight), CGPoint. ccp (s. width, 0); // then it is a primitive Painting Line // 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); // set the 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, painting point // draw 4 small points CGPoint points [] = {CGPoint. ccp (60, 60), CGPoint. ccp (70, 70), CGPoint. ccp (60, 70), CGPoint. ccp (70, 60)}; // Dot Array gl. glPointSize (4); // set the point size gl. glColor4f (0.0f, 1.0f, 1.0f, 1.0f); // primitive color CCDrawingPrimitives. ccDrawPoints (gl, points, 4); // primitive painting point // draw a green circle with 10 segments gl. glLineWidth (16); // you can specify the gl line width. glColor4f (0.0f, 1.0f, 0.0f, 1.0f); // primitive color CCDrawingPrimitives. ccDrawCircle (gl, CGP Oint. make (s. width/2, s. height/2), 100, 0, 10, false); // primitive line drawing // draw a green circle with 50 segments with line to center gl. glLineWidth (2); // you can specify the gl line width. glColor4f (0.0f, 1.0f, 1.0f, 1.0f); // color CCDrawingPrimitives. ccDrawCircle (gl, CGPoint. make (s. width/2, s. height/2), 50, ccMacros. CC_DEGREES_TO_RADIANS (90), 50, true); // draws a circle as a primitive .., parameters: primitive, Center Coordinate, radius, radian value (Angle Parameter), number of segments, whether to draw a line to the center // open yellow poly gl. glColor4f (1. 0f, 1.0f, 0.0f, 1.0f); // set the color gl. glLineWidth (10); // you can specify CGPoint vertices [] = {CGPoint. ccp (0, 0), CGPoint. ccp (50, 50), CGPoint. ccp (100, 50), CGPoint. ccp (100,100), CGPoint. ccp (50,100)}; // set the Dot Array CCDrawingPrimitives. ccDrawPoly (gl, vertices, 5, false); // draw a polygon // closed purple poly gl. glColor4f (1.0f, 0.0f, 1.0f, 1.0f); // color gl. glLineWidth (2); // The width of the CGPoint vertices2 [] = {CGPoint. ccp (30,130), CGPoint. ccp (3 0,230), CGPoint. ccp (50,200)}; // point array CCDrawingPrimitives. ccDrawPoly (gl, vertices2, 3, true); // polygon // draw quad bezr path CCDrawingPrimitives. ccdrawquadbezr (gl, CGPoint. make (0, s. height), CGPoint. make (s. width/2, s. height/2), CGPoint. make (s. width, s. height), 50); // draw the bansel curve... A computer is used to draw curves. parameters: starting point, control point, end point, number of vertices // draw cubic bezr path CCDrawingPrimitives. ccdrawcubicbezr (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 three-dimensional besell curve. More advanced, parameters: starting point, control point 1, control point 2, end point, number of vertices .. // restore original values // restore data using a primitive gl. glLineWidth (1); // line width 1 gl. glColor4f (1.0f, 1.0f, 1.0f, 1.0f); // The color is black gl. glPointSize (1); // point size 1} public String title () {return "draw primitives ";}}}

Related Article

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.