Android Open Source - NotATop Draw Track






From Project

Back to project page NotATop.

License

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.

Java Source Code

package com.gg.module;
/*  w w w.  j a  va2  s . c om*/
import java.nio.*;
import java.util.ArrayList;
import java.util.Random;
import javax.microedition.khronos.opengles.GL10;

import android.view.MotionEvent;

import com.gg.util.Constant;
import com.gg.util.TrackPoint;


public class DrawTrack{
  int textureId; // ????id
  int vertexNumber; // ??????????????
  private FloatBuffer vertexBuffer; // ??????????
  private FloatBuffer textureBuffer; // ??????
  
  private float[] trackPoint = new float[1024*3];
  private float[] vertexs = new float[1024*3];
//  private long[] birth = new long[1024];
  private int count = 0;

  public DrawTrack(int textureId) {
    this.textureId = textureId;
    generateData();
  }
  
  public void onTouchEvent(MotionEvent e){
    switch(e.getAction()){
    case MotionEvent.ACTION_DOWN:
      for(int i = 0 ; i < 20 ; i ++) {
        vertexs[3*i] = 0;
        vertexs[3*i+1] = 0;
        vertexs[3*i+2] = 0;
      }
      count = 0;
      break;
      
    case MotionEvent.ACTION_MOVE:
      if(count < 10){
        trackPoint[3*count] = Constant.convertX(e.getX());
        trackPoint[3*count+1] = Constant.convertY(e.getY());
        trackPoint[3*count+2] = 3f;
        
        
        count ++;
      }else{
        for(int i = 0; i < count - 1; i++) {
          trackPoint[i*3] = trackPoint[(i+1)*3];
          trackPoint[i*3+1] = trackPoint[(i+1)*3+1];
          trackPoint[i*3+2] = trackPoint[(i+1)*3+2];
        }
        trackPoint[3*(count-1)] = Constant.convertX(e.getX());
        trackPoint[3*(count-1)+1] = Constant.convertY(e.getY());
        trackPoint[3*(count-1)+2] = 3f;
      }
      
      
      vertexs[0] = trackPoint[0];
      vertexs[1] = trackPoint[1];
      vertexs[2] = trackPoint[2];
      
      for(int i = 1 ; i < count - 1 ; i ++) {
        vertexs[i*6-3] = leftPointX(trackPoint[(i-1)*3] , trackPoint[(i-1)*3+1] , trackPoint[i*3] , trackPoint[i*3+1], i);
        vertexs[i*6-2] = leftPointY(trackPoint[(i-1)*3] , trackPoint[(i-1)*3+1] , trackPoint[i*3] , trackPoint[i*3+1], i);
        vertexs[i*6-1] = 3f;
        
        vertexs[i*6] = rightPointX(trackPoint[(i-1)*3] , trackPoint[(i-1)*3+1] , trackPoint[i*3] , trackPoint[i*3+1], i);
        vertexs[i*6+1] = rightPointY(trackPoint[(i-1)*3] , trackPoint[(i-1)*3+1] , trackPoint[i*3] , trackPoint[i*3+1], i);
        vertexs[i*6+2] = 3f;
      }
      vertexs[51] = trackPoint[3*(count-1)];
      vertexs[52] = trackPoint[3*(count-1)+1];
      vertexs[53] = trackPoint[3*(count-1)+2];
      
      break;
      
    case MotionEvent.ACTION_UP:
      for(int j = 0 ; j < count; j ++)
        for(int i = 0; i < count - 1; i++) {
          trackPoint[i*3] = trackPoint[(i+1)*3];
          trackPoint[i*3+1] = trackPoint[(i+1)*3+1];
          trackPoint[i*3+2] = trackPoint[(i+1)*3+2];
        }
      
      count = 0;
      break;
    }
  }
  
  public void generateData(){
    
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertexs.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(vertexs);
    vertexBuffer.position(0);

    // ????
    float[] textures = generateTexCoor(12, 1);
//    float[] textures = new float[]{0.0f, 0.0f,
//                     1.0f, 0.0f,
//                     0.0f, 1.0f,
//                     0.0f, 1.0f,
//                     1.0f, 0.0f,
//                     1.0f, 1.0f
//    };
    ByteBuffer tbb = ByteBuffer.allocateDirect(textures.length * 4);
    tbb.order(ByteOrder.nativeOrder());
    textureBuffer = tbb.asFloatBuffer();
    textureBuffer.put(textures);
    textureBuffer.position(0);
  }
  
//  public float leftPointX(float x1 ,float y1 , float x2 , float y2 , long birth , long lastBirth , long totalTime) {
//    float angle = (float) (Math.atan2(y2 - y1, x2 - x1));
//    float len = Constant.convert((1 - (lastBirth - birth) / totalTime) * 10);
//    float x = (float) (x2 + len * Math.cos(angle + Math.PI / 2));
//    return x;
//  }
//  
//  public float leftPointY(float x1 ,float y1 , float x2 , float y2 , long birth , long lastBirth , long totalTime) {
//    float angle = (float) (Math.atan2(y2 - y1, x2 - x1));
//    float len = Constant.convert((1 - (lastBirth - birth) / totalTime) * 10);
//    float y = (float) (y2 - len * Math.sin(angle + Math.PI / 2));
//    return y;
//  }
//  
//  public float rightPointX(float x1 ,float y1 , float x2 , float y2 , long birth , long lastBirth, long totalTime) {
//    float angle = (float) (Math.atan2(y2 - y1, x2 - x1));
//    float len = Constant.convert((1 - (lastBirth - birth) / totalTime) * 10) ;
//    float x = (float) (x2 - len * Math.cos(angle + Math.PI / 2));
//    return x;
//  }
//  
//  public float rightPointY(float x1 ,float y1 , float x2 , float y2 , long birth , long lastBirth, long totalTime) {
//    float angle = (float) (Math.atan2(y2 - y1, x2 - x1));
//    float len = Constant.convert((1 - (lastBirth - birth) / totalTime) * 10) ;
//    float y = (float) (y2 + len * Math.sin(angle + Math.PI / 2));
//    return y;
//  }


  public void drawSelf(GL10 gl) {
//    gl.glLineWidth(1.0f);
    int number;
    if(count == 0){
      number = 0;
    }else
    if(count == 1) {
      number = 1;
    }else if(count != 10) {
      number = 2*count - 4;
    } else number = 2*count - 2;
    
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertexs.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(vertexs);
    vertexBuffer.position(0);

    // ????
    float[] textures = generateTexCoor(12, 1);
    ByteBuffer tbb = ByteBuffer.allocateDirect(textures.length * 4);
    tbb.order(ByteOrder.nativeOrder());
    textureBuffer = tbb.asFloatBuffer();
    textureBuffer.put(textures);
    textureBuffer.position(0);


    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);// ???????
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);// ?????????

    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);

    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, number);// ???????

    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);// ??????
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
  }

  // ?????????????
  public float getVectorLength(float x, float y, float z) {
    float pingfang = x * x + y * y + z * z;
    float length = (float) Math.sqrt(pingfang);
    return length;
  }

  // ?????????????????????????
  public float[] generateTexCoor(int bw, int bh) {
    float[] result = new float[bw * bh * 6 * 2];
    float sizew = 1.0f / bw;// ????????
    float sizeh = 1.0f / bh;// ????????
    int c = 0;
    for (int j = 0; j < bw; j++) {
      for (int i = 0; i < bh; i++) {
        // ????????????????????????????????????12??????????
        float s = j * sizew;
        float t = i * sizeh;

        result[c++] = s;
        result[c++] = t;

        result[c++] = s;
        result[c++] = t + sizeh;

        result[c++] = s + sizew;
        result[c++] = t;

        result[c++] = s;
        result[c++] = t + sizeh;

        result[c++] = s + sizew;
        result[c++] = t + sizeh;

        result[c++] = s + sizew;
        result[c++] = t;
      }
    }
    return result;
  }
  
  
  public float leftPointX(float x1 ,float y1 , float x2 , float y2 , int i) {
    float angle = (float) (Math.atan2(y2 - y1, x2 - x1));
    float len = Constant.convert((float)(i) / 10 * 15);
    float x = (float) (x2 - len * Math.sin(angle));
    return x;
  }
  
  public float leftPointY(float x1 ,float y1 , float x2 , float y2 , int i) {
    float angle = (float) (Math.atan2(y2 - y1, x2 - x1));
    float len = Constant.convert( (float)(i) / 10 * 15);
    float y = (float) (y2 + len * Math.cos(angle));
    return y;
  }
  
  public float rightPointX(float x1 ,float y1 , float x2 , float y2 , int i) {
    float angle = (float) (Math.atan2(y2 - y1, x2 - x1));
    float len = Constant.convert( (float)(i) / 10 * 15) ;
    float x = (float) (x2 + len * Math.sin(angle));
    return x;
  }
  
  public float rightPointY(float x1 ,float y1 , float x2 , float y2 , int i) {
    float angle = (float) (Math.atan2(y2 - y1, x2 - x1));
    float len = Constant.convert( (float)(i)  / 10 * 15) ;
    float y = (float) (y2 - len * Math.cos(angle));
    return y;
  }
  
}




Java Source Code List

com.gg.game.ClassicGame.java
com.gg.game.ClassicGame.java
com.gg.game.GameFrame.java
com.gg.game.GameFrame.java
com.gg.game.TimeGame.java
com.gg.game.TimeGame.java
com.gg.module.DrawBackground.java
com.gg.module.DrawBackground.java
com.gg.module.DrawScore.java
com.gg.module.DrawScore.java
com.gg.module.DrawTrack.java
com.gg.module.DrawTrack.java
com.gg.module.SelectControl.java
com.gg.module.SelectControl.java
com.gg.module.SoundControl.java
com.gg.module.SoundControl.java
com.gg.module.VoiceControl.java
com.gg.module.VoiceControl.java
com.gg.top.BasicTop.java
com.gg.top.BasicTop.java
com.gg.top.DrawCircle.java
com.gg.top.DrawCircle.java
com.gg.top.DrawCone.java
com.gg.top.DrawCone.java
com.gg.top.DrawCylinder.java
com.gg.top.DrawCylinder.java
com.gg.top.DrawTop.java
com.gg.top.DrawTop.java
com.gg.util.Circle.java
com.gg.util.Circle.java
com.gg.util.Constant.java
com.gg.util.Constant.java
com.gg.util.DateUtil.java
com.gg.util.DateUtil.java
com.gg.util.Direction.java
com.gg.util.Direction.java
com.gg.util.Line.java
com.gg.util.Line.java
com.gg.util.Point.java
com.gg.util.Point.java
com.gg.util.TextureRect.java
com.gg.util.TextureRect.java
com.gg.util.TrackPoint.java
com.gg.util.TrackPoint.java
com.gg.util.Track.java
com.gg.util.Track.java
com.gg.util.Utils.java
com.gg.util.Utils.java
com.gg.view.ClassicGameSurfaceView.java
com.gg.view.ClassicGameSurfaceView.java
com.gg.view.EndSurfaceView.java
com.gg.view.EndSurfaceView.java
com.gg.view.FirstTimeSurfaceView.java
com.gg.view.FirstTimeSurfaceView.java
com.gg.view.GameModeSurfaceView.java
com.gg.view.GameModeSurfaceView.java
com.gg.view.HelpSurfaceView.java
com.gg.view.HelpSurfaceView.java
com.gg.view.MainActivity.java
com.gg.view.MainActivity.java
com.gg.view.MainMenuSurfaceView.java
com.gg.view.MainMenuSurfaceView.java
com.gg.view.ScoreSurfaceView.java
com.gg.view.ScoreSurfaceView.java
com.gg.view.SelectSurfaceView.java
com.gg.view.SelectSurfaceView.java
com.gg.view.SurfaceViewFactory.java
com.gg.view.SurfaceViewFactory.java
com.gg.view.TimeGameSurfaceView.java
com.gg.view.TimeGameSurfaceView.java
com.gg.view.WelcomeSurfaceView.java
com.gg.view.WelcomeSurfaceView.java