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";
}
}
}