Back to project page NotATop.
The source code is released under:
GNU General Public License
If you think the Android project NotATop listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.gg.view; //from w w w .j av a 2 s . c o m import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import android.opengl.GLSurfaceView; import android.opengl.GLU; import android.opengl.GLUtils; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.opengles.GL11; import android.R.integer; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.text.TextUtils; import android.view.MotionEvent; import android.widget.Toast; import com.gg.game.ClassicGame; import com.gg.game.GameFrame; import com.gg.module.*; import com.gg.top.*; import com.gg.util.*; public class SelectSurfaceView extends GLSurfaceView { private MainActivity mainActivity; // ???????Activity private SceneRenderer sceneRender; // ????????? private int nextConeTextureId; private int nextCylinderTextureId; private int nextCircleTextureId; private int prevConeTextureId; private int prevCylinderTextureId; private int prevCircleTextureId; private int currentConeTextureId; // ????????Id private int currentCylinderTextureId; // ?????????Id private int currentCircleTextureId; // ???????Id private int hiddenConeTextureId; private int hiddenCylinderTextureId; private int hiddenCircleTextureId; private int[] initConeTextureIdArray; // ??????initTexture????????????????????? private int[] initCylinderTextureIdArray; private int[] initCircleTextureIdArray; private int backgroundTextureId; // ??????????Id private DrawTop nextDrawTop; private DrawTop prevDrawTop; private DrawTop currentDrawTop; private DrawTop hiddenDrawTop; private int index; private int numberOfTop; private boolean moveLeftFlag; private boolean moveRightFlag; private Toast noLeftToast; private Toast noRightToast; private boolean moveHiddenToLeftFlag = true; // ???hiddenDrawTop????????????????????hiddenDrawTop???????????? private DrawTrack drawTrack; // ?????????????? private DrawBackground drawBackground; // ???????????? private double downX; private double downY; private double upX; private double upY; public SelectSurfaceView(Context context) { super(context); mainActivity = (MainActivity) context; // ??????????Activity sceneRender = new SceneRenderer(); // ????????????? setRenderer(sceneRender); // ????????? setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);// ????????????????? index = SelectControl.getIndex(); numberOfTop = SelectControl.getNumberOfTop(); noLeftToast = Toast.makeText(context, "???????????", Toast.LENGTH_SHORT); noRightToast = Toast.makeText(context, "?????????????", Toast.LENGTH_SHORT); initConeTextureIdArray = new int[numberOfTop]; // ??????initTexture????????????????????? initCylinderTextureIdArray = new int[numberOfTop]; initCircleTextureIdArray = new int[numberOfTop]; } @Override public boolean onTouchEvent(MotionEvent e) { // ???????????bug switch(e.getAction()){ case MotionEvent.ACTION_DOWN: downX = Constant.convertX(e.getX()); downY = Constant.convertY(e.getY()); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: upX = Constant.convertX(e.getX()); upY = Constant.convertY(e.getY()); if(moveLeftFlag==false && moveRightFlag==false){ // ????????????????? if(downX<-1.0 && downY>0.3 && upX<-1.0 && upY>0.3){ mainActivity.getHandler().sendEmptyMessage(SurfaceViewFactory.MAIN_MENU); }else if(downX>1.0 && downY>0.3 && upX>1.0 && upY>0.3){ mainActivity.getHandler().sendEmptyMessage(SurfaceViewFactory.CLASSIC_GAME); } if(downX>-1.5 && downX<-0.9 && downY>-0.7 && downY<0.0){ // ?????????????????????????? if(upX>-1.5 && upX<-0.9 && upY>-0.7 && upY<0.0){ // ?????????????????????? if(index>0){ // ?????????????????? moveRightFlag = true; // ?????????????????????????? }else{ noLeftToast.show(); // ????????????? } } }else if(downX<1.5 && downX>0.9 && downY>-0.7 && downY<0.0){ // ?????????????????????????? if(upX<1.5 && upX>0.9 && upY>-0.7 && upY<0.0){ if(index<numberOfTop-1){ moveLeftFlag = true; }else{ noRightToast.show(); } } } if(downX>-0.9 && downX<0.9){ // ???????????????? if(upX-downX>0.5){ if(index>0){ moveRightFlag = true; }else{ noLeftToast.show(); } }else if(downX-upX>0.5){ // ?????????????? if(index<numberOfTop-1){ moveLeftFlag = true; }else{ noRightToast.show(); } } } } break; } drawTrack.onTouchEvent(e); // ???????????????? requestRender(); // ???????? return true; } private class SceneRenderer implements GLSurfaceView.Renderer { // ??????3D?????????? private int lightAngle = 90;// ???????? private int trackTextureId; public SceneRenderer() { // ???????????? } public void onSurfaceCreated(final GL10 gl, EGLConfig config) { // ???????????????????????????? gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // ?????????????? gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // ?????????? nextConeTextureId = initTexture(gl, R.drawable.next_cone); nextCylinderTextureId = initTexture(gl, R.drawable.next_cylinder); nextCircleTextureId = initTexture(gl, R.drawable.next_circle); prevConeTextureId = initTexture(gl, R.drawable.prev_cone); prevCylinderTextureId = initTexture(gl, R.drawable.prev_cylinder); prevCircleTextureId = initTexture(gl, R.drawable.prev_circle); initConeTextureIdArray[0] = initTexture(gl, R.drawable.cone0); initConeTextureIdArray[1] = initTexture(gl, R.drawable.cone1); initConeTextureIdArray[2] = initTexture(gl, R.drawable.cone2); initConeTextureIdArray[3] = initTexture(gl, R.drawable.cone3); initConeTextureIdArray[4] = initTexture(gl, R.drawable.cone4); initCylinderTextureIdArray[0] = initTexture(gl, R.drawable.cylinder0); initCylinderTextureIdArray[1] = initTexture(gl, R.drawable.cylinder1); initCylinderTextureIdArray[2] = initTexture(gl, R.drawable.cylinder2); initCylinderTextureIdArray[3] = initTexture(gl, R.drawable.cylinder3); initCylinderTextureIdArray[4] = initTexture(gl, R.drawable.cylinder4); initCircleTextureIdArray[0] = initTexture(gl, R.drawable.circle0); initCircleTextureIdArray[1] = initTexture(gl, R.drawable.circle1); initCircleTextureIdArray[2] = initTexture(gl, R.drawable.circle2); initCircleTextureIdArray[3] = initTexture(gl, R.drawable.circle3); initCircleTextureIdArray[4] = initTexture(gl, R.drawable.circle4); currentConeTextureId = initTexture(gl, SelectControl.getConeTextureId()); currentCylinderTextureId = initTexture(gl, SelectControl.getCylinderTextureId()); currentCircleTextureId = initTexture(gl, SelectControl.getCircleTextureId()); SelectControl.next(); hiddenConeTextureId = initTexture(gl, SelectControl.getConeTextureId()); hiddenCylinderTextureId = initTexture(gl, SelectControl.getCylinderTextureId()); hiddenCircleTextureId = initTexture(gl, SelectControl.getCircleTextureId()); SelectControl.prev(); backgroundTextureId = initTexture(gl, R.drawable.select_bg); // ??????????????? trackTextureId = initTexture(gl, R.drawable.track); prevDrawTop = new DrawTop(prevConeTextureId,prevCylinderTextureId,prevCircleTextureId); prevDrawTop.setRadius(0.2f); prevDrawTop.setBasicPoint(new Point(-1.2f, -2.0f)); prevDrawTop.setAngleSpeed(5); prevDrawTop.generateData(); nextDrawTop = new DrawTop(nextConeTextureId,nextCylinderTextureId,nextCircleTextureId); nextDrawTop.setRadius(0.2f); nextDrawTop.setBasicPoint(new Point(1.2f, -2.0f)); nextDrawTop.setAngleSpeed(5); nextDrawTop.generateData(); currentDrawTop = new DrawTop(currentConeTextureId,currentCylinderTextureId,currentCircleTextureId); currentDrawTop.setRadius(0.55f); currentDrawTop.setBasicPoint(new Point(0.0f, -1.65f)); currentDrawTop.setAngleSpeed(3); currentDrawTop.generateData(); hiddenDrawTop = new DrawTop(hiddenConeTextureId,hiddenCylinderTextureId,hiddenCircleTextureId); hiddenDrawTop.setRadius(0.55f); hiddenDrawTop.setBasicPoint(new Point(2.5f, -1.65f)); hiddenDrawTop.setAngleSpeed(3); hiddenDrawTop.generateData(); drawTrack = new DrawTrack(trackTextureId); // ????????????? drawBackground = new DrawBackground(backgroundTextureId); // ??????????????? gl.glDisable(GL10.GL_DITHER); // ???????? gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); // ???????Hint?????????????????????????? gl.glClearColor(0, 0, 0, 0); // ???????????????RGBA gl.glShadeModel(GL10.GL_SMOOTH); // ????????????????? gl.glEnable(GL10.GL_DEPTH_TEST); // ?????????? new Thread() { public void run() { while (true) { nextDrawTop.rotate(); prevDrawTop.rotate(); currentDrawTop.rotate(); hiddenDrawTop.rotate(); if(moveLeftFlag){ if(hiddenDrawTop.getBasicPoint().x>=0f){ currentDrawTop.setBasicPoint(new Point(currentDrawTop.getBasicPoint().x-0.1f, currentDrawTop.getBasicPoint().y)); hiddenDrawTop.setBasicPoint(new Point(hiddenDrawTop.getBasicPoint().x-0.1f, hiddenDrawTop.getBasicPoint().y)); }else{ moveLeftFlag = false; SelectControl.next(); index++; currentDrawTop.setConeTextureId(initConeTextureIdArray[index]); currentDrawTop.setCylinderTextureId(initCylinderTextureIdArray[index]); currentDrawTop.setCircleTextureId(initCircleTextureIdArray[index]); //currentDrawTop.generateData(); currentDrawTop.setBasicPoint(new Point(0f, currentDrawTop.getBasicPoint().y)); if(index<numberOfTop-1){ // ?????????????????hiddenDrawTop???????? index++; hiddenDrawTop.setConeTextureId(initConeTextureIdArray[index]); hiddenDrawTop.setCylinderTextureId(initCylinderTextureIdArray[index]); hiddenDrawTop.setCircleTextureId(initCircleTextureIdArray[index]); //hiddenDrawTop.generateData(); index--; } hiddenDrawTop.setBasicPoint(new Point(2.5f, hiddenDrawTop.getBasicPoint().y)); } } if(moveRightFlag){ if(moveHiddenToLeftFlag==true){ if(index>0){ // ?????? index--; hiddenDrawTop.setConeTextureId(initConeTextureIdArray[index]); hiddenDrawTop.setCylinderTextureId(initCylinderTextureIdArray[index]); hiddenDrawTop.setCircleTextureId(initCircleTextureIdArray[index]); //hiddenDrawTop.generateData(); index++; } hiddenDrawTop.setBasicPoint(new Point(-2.5f, hiddenDrawTop.getBasicPoint().y)); moveHiddenToLeftFlag = false; } if(hiddenDrawTop.getBasicPoint().x<=0f){ currentDrawTop.setBasicPoint(new Point(currentDrawTop.getBasicPoint().x+0.1f, currentDrawTop.getBasicPoint().y)); hiddenDrawTop.setBasicPoint(new Point(hiddenDrawTop.getBasicPoint().x+0.1f, hiddenDrawTop.getBasicPoint().y)); }else{ moveRightFlag = false; moveHiddenToLeftFlag = true; SelectControl.prev(); index--; currentDrawTop.setConeTextureId(initConeTextureIdArray[index]); currentDrawTop.setCylinderTextureId(initCylinderTextureIdArray[index]); currentDrawTop.setCircleTextureId(initCircleTextureIdArray[index]); //currentDrawTop.generateData(); currentDrawTop.setBasicPoint(new Point(0f, currentDrawTop.getBasicPoint().y)); if(index<numberOfTop-1){ index++; hiddenDrawTop.setConeTextureId(initConeTextureIdArray[index]); hiddenDrawTop.setCylinderTextureId(initCylinderTextureIdArray[index]); hiddenDrawTop.setCircleTextureId(initCircleTextureIdArray[index]); //hiddenDrawTop.generateData(); index--; } hiddenDrawTop.setBasicPoint(new Point(2.5f, hiddenDrawTop.getBasicPoint().y)); } } try{ sleep(Constant.INTERVAL); }catch(Exception e){ e.printStackTrace(); } } } }.start(); } public void onDrawFrame(GL10 gl) { // ?????????????????????????? // gl.glClearColor(0.5f, 0.5f, 0.5f, 0.0f); // ?????????????? gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // ?????????? gl.glMatrixMode(GL10.GL_MODELVIEW); // ???????????????? gl.glLoadIdentity(); // ????????????????? gl.glPushMatrix();// ????????????? float lx = 0; // ?????????? float ly = (float) (7 * Math.cos(Math.toRadians(lightAngle))); float lz = (float) (7 * Math.sin(Math.toRadians(lightAngle))); float[] positionParamsRed = { lx, ly, lz, 0 }; gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, positionParamsRed, 0); initMaterial(gl);// ????????? initLight(gl);// ???? gl.glTranslatef(0f, 0f, -100.0f); // ?????????????100?????????????????????????????? gl.glPushMatrix(); gl.glTranslatef(0, 0, -1); gl.glRotatef(-70, 1, 0, 0); nextDrawTop.drawSelf(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glTranslatef(0, 0, -1); gl.glRotatef(-70, 1, 0, 0); prevDrawTop.drawSelf(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glRotatef(-60, 1, 0, 0); currentDrawTop.drawSelf(gl); gl.glPopMatrix(); gl.glPushMatrix(); gl.glRotatef(-60, 1, 0, 0); hiddenDrawTop.drawSelf(gl); gl.glPopMatrix(); gl.glPushMatrix(); drawTrack.drawSelf(gl); gl.glPopMatrix(); gl.glPushMatrix(); // ??????????? // gl.glTranslatef(0, 0, -1); // if(showFlag){ // gl.glRotatef(showAngle, 0, 0, 1); // } drawBackground.drawSelf(gl); // ?????? gl.glPopMatrix(); // ??????????? closeLight(gl);// ??? gl.glPopMatrix();// ???????????? } public void onSurfaceChanged(GL10 gl, int width, int height) { // ?????????????????????????????? gl.glViewport(0, 0, width, height); // ??????????????? gl.glMatrixMode(GL10.GL_PROJECTION); // ???????????????? gl.glLoadIdentity(); // ????????????????? float ratio = (float) width / height; // ?????????????? // gl.glFrustumf(-ratio, ratio, -1, 1, 1, 100); // ??????????????????????? gl.glOrthof(-ratio, ratio, -1, 1, 1, 100); // ??????????????????3D?????????100????????? Constant.SCREEN_WIDTH = width; // ???????????Constant????????????????????????????????????? Constant.SCREEN_HEIGHT = height; Constant.SCREEN_RATE = (float) (0.5 * 4 / Constant.SCREEN_HEIGHT); } } private void initLight(GL10 gl) { // ?????????? gl.glEnable(GL10.GL_LIGHTING);// ??????? gl.glEnable(GL10.GL_LIGHT1);// ??1??? // ?????????? float[] ambientParams = { 0.2f, 0.2f, 0.2f, 1.0f };// ????? RGBA gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_AMBIENT, ambientParams, 0); // ???????? float[] diffuseParams = { 1f, 1f, 1f, 1.0f };// ????? RGBA gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_DIFFUSE, diffuseParams, 0); // ????????? float[] specularParams = { 1f, 1f, 1f, 1.0f };// ????? RGBA gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_SPECULAR, specularParams, 0); } private void closeLight(GL10 gl) {// ???? gl.glDisable(GL10.GL_LIGHT1); gl.glDisable(GL10.GL_LIGHTING); } private void initMaterial(GL10 gl) { // ????????? // ?????? float ambientMaterial[] = { 248f / 255f, 242f / 255f, 144f / 255f, 1.0f }; gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, ambientMaterial, 0); // ???? float diffuseMaterial[] = { 248f / 255f, 242f / 255f, 144f / 255f, 1.0f }; gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, diffuseMaterial, 0); // ?????? float specularMaterial[] = { 248f / 255f, 242f / 255f, 144f / 255f, 1.0f }; gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, specularMaterial, 0); gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, 100.0f); } public int initTexture(GL10 gl, int drawableId) // ????????? { // ????????ID int[] textures = new int[1]; gl.glGenTextures(1, textures, 0); int currTextureId = textures[0]; gl.glBindTexture(GL10.GL_TEXTURE_2D, currTextureId); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_NEAREST); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR_MIPMAP_LINEAR); ((GL11) gl).glTexParameterf(GL10.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP, GL10.GL_TRUE); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT); InputStream is = this.getResources().openRawResource(drawableId); Bitmap bitmapTmp; try { bitmapTmp = BitmapFactory.decodeStream(is); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmapTmp, 0); bitmapTmp.recycle(); return currTextureId; } }