package com.llymm;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
public class GLRender implements GLSurfaceView.Renderer {
public static int FLAG_DICE=0;
private Context mContext;
public float mAngleX=60.0f;
public float mAngleY=45.0f;
int one = 0x10000;
private IntBuffer mVertexBuffer;
private FloatBuffer mTexBuffer;
int mTextureID_front = 0, mTextureID__back = 0, mTextureID_left = 0,
mTextureID_right = 0, mTextureID_top = 0, mTextureID_bottom = 0;
int mTextureID[] = { mTextureID_front, mTextureID__back, mTextureID_left,
mTextureID_right, mTextureID_top, mTextureID_bottom };
Integer res[][] = new Integer[][] {
{ R.drawable.stu_1, R.drawable.stu_2, R.drawable.stu_3,
R.drawable.stu_4,R.drawable.stu_5, R.drawable.stu_6 },
{ R.drawable.hon_1, R.drawable.hon_2, R.drawable.hon_3,
R.drawable.hon_4,R.drawable.hon_5, R.drawable.hon_6 } };
Bitmap bitmap[][] = new Bitmap[2][6];
public GLRender(Context context) {
mContext = context;
for (int i = 0; i < res.length; i++)
for (int j = 0; j < 6; j++) {
bitmap[i][j] = BitmapFactory.decodeResource(mContext
.getResources(), res[i][j]);
}
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glDisable(GL10.GL_DITHER);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glRotatef(0.0f, 1.0f, 0, 0);
initCube();
}
public void onDrawFrame(GL10 gl) {
draw(gl);
}
public float getmAngleX() {
return mAngleX;
}
public void setmAngleX(float mAngleX) {
this.mAngleX = mAngleX;
}
public float getmAngleY() {
return mAngleY;
}
public void setmAngleY(float mAngleY) {
this.mAngleY = mAngleY;
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, 320, 320);
}
public void loadTexture(GL10 gl, int no) {
int[] textures = new int[1];
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID[no - 1]);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap[FLAG_DICE][no - 1], 0);
}
public void draw(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glTranslatef(0, 0.0f, -1.0f);
gl.glRotatef(mAngleX, 1, 0, 0);
gl.glRotatef(mAngleY, 0, 1, 0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// gl.glClearColor(0f, 0f, 1f, 0f);
// gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer);
// gl.glOrthof(-1.0f, 1.0f, -1.5f, -1.5f, -1.0f, 1.0f);
gl.glScalef(0.4f, 0.4f, 0.4f);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexBuffer);
for (int i = 0; i < 6; i++) {
loadTexture(gl, i + 1);
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID[i]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, i * 4, 4);
}
}
public void initCube() {
int vertices[] = {
// FRONT
-one, -one, one, one, -one, one,
-one, one, one, one, one, one,
// BACK
-one, -one, -one, -one, one, -one,
one, -one, -one, one, one, -one,
// LEFT
-one, -one, one, -one, one, one,
-one, -one, -one, -one, one,-one,
// RIGHT
one, -one, -one, one, one, -one,
one, -one, one, one, one, one,
// TOP
-one, one, one, one, one, one,
-one, one, -one, one, one, -one,
// BOTTOM
-one, -one, one, -one, -one, -one,
one, -one, one, one, -one,-one, };
float spriteTexcoords[] = {
// FRONT
0, 1, 1, 1, 0, 0, 1, 0,
// BACK
1, 1, 1, 0, 0, 1, 0, 0,
// LEFT
1, 1, 1, 0, 0, 1, 0, 0,
// RIGHT
1, 1, 1, 0, 0, 1, 0, 0,
// TOP
1, 0, 0, 0, 1, 1, 0, 1,
// BOTTOM
0, 0, 0, 1, 1, 0, 1, 1, };
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asIntBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);
ByteBuffer tbb = ByteBuffer.allocateDirect(spriteTexcoords.length * 4);
tbb.order(ByteOrder.nativeOrder());
mTexBuffer = tbb.asFloatBuffer();
mTexBuffer.position(0);
for (int i = 0; i < 48; i++) {
mTexBuffer.put(spriteTexcoords[i]);
}
mTexBuffer.position(0);
}
}
package com.llymm;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
public class GLView extends GLSurfaceView {
public GLRender glRender;
private float TOUCH_SCALE_Y=180/480;
private float TOUCH_SCALE_X=180/320;
Timer mTimer=new Timer();
TimerTask mTask;
int times=1;
int totalTimes=0;//�ܵ
public GLView(Context context) {
super(context);
glRender = new GLRender(context);
setRenderer(glRender);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
private float startX, moveX,stopX, dx, angleX;
private float startY, moveY, stopY,dy, angleY;
private long startTime,stopTime,dTime;
private float unitAngleX=30.0f,unitAngleY=45.0f;
int flag_moving=0;
float speed=0;
@Override
public boolean onTouchEvent(MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
startX = e.getX();
startY = e.getY();
startTime=android.os.SystemClock.currentThreadTimeMillis();
}
if (e.getAction() == MotionEvent.ACTION_MOVE) {
if(flag_moving==0){
moveX = e.getX();
moveY = e.getY();
dx = moveX - startX;
dy = moveY - startY;
angleX = glRender.getmAngleX();
angleY = glRender.getmAngleY();
glRender.setmAngleX(angleX + dy * TOUCH_SCALE_Y/5);
glRender.setmAngleY(angleY + dx * TOUCH_SCALE_X/5);
requestRender();//�ػ�
}
}
if(e.getAction()==MotionEvent.ACTION_UP){
glRender.setmAngleX(60);
glRender.setmAngleY(45);
stopX=e.getX();
stopY=e.getY();
stopTime=android.os.SystemClock.currentThreadTimeMillis();
dx=startX-stopX;
dy=startY-stopY;
dTime=stopTime-startTime;
speed=(float) (Math.sqrt((dx*dx+dy*dy))/dTime);
totalTimes=((int)speed+(int)(Math.random()*4))*3
mTask=new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
glRender.setmAngleX(glRender.getmAngleX() + unitAngleX);
glRender.setmAngleY(glRender.getmAngleY() + unitAngleY);
if(times==totalTimes){
mTask.cancel();
times=1;
totalTimes=0;
flag_moving=0;
}else{
times++;
}
requestRender();
}};
if(flag_moving==0){
flag_moving=1;
mTimer.schedule(mTask, 0,20);
}
}
return true;
}
public void freshDraw(){
requestRender();
}
}
package com.llymm;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity{
Context activityContext=this;
static final int INVISIBLE=LinearLayout.INVISIBLE;
static final int VISIBLE=LinearLayout.VISIBLE;
LinearLayout layout_linear;
LinearLayout layout_linear_start;
LinearLayout layout_linear_finish;
FrameLayout layout_frame;
public static final int ITEM0 = Menu.FIRST;
public static final int ITEM1 = Menu.FIRST + 1;
public static final int ITEM2 = Menu.FIRST + 2;
private GLView glView;
int tost_flag=0;
TextView txt_prompt;
String content="435654654①4要5...";
int index=0;
int maxSize=content.length();
int visible_flag=0;
long delayTime=500;
Timer myTimer=new Timer();
TimerTask myTask=new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
flashText();
}};
Handler mHandler=new Handler();
class myRun implements Runnable{//������ʾ���ֶ������߳�
@Override
public void run() {
// TODO Auto-generated method stub
if(index>=maxSize){
delayTime=1000;
if(index==maxSize+12){
myTask.cancel();
index=0;
}else{
switch(visible_flag){
case 0:txt_prompt.setText("");visible_flag=1;break;
case 1:txt_prompt.setText(content);visible_flag=0;break;
}
++index;
}
}else{
txt_prompt.setText(txt_prompt.getText().toString()+content.charAt(index));
index++;
}
}
}
class MyRunnable implements Runnable{//߳�
private Context context;
public MyRunnable(Context context){
this.context=context;
}
@Override
public void run() {
// TODO Auto-generated method stub
Toast myToast=new Toast(activityContext);
ImageView myImage=new ImageView(activityContext);
myImage.setImageResource(R.drawable.toast);
myImage.setMinimumHeight(120);
myImage.setMinimumWidth(200);
myToast.setView(myImage);
myToast.setDuration(Toast.LENGTH_LONG);
myToast.show();
++tost_flag;
}
}
class myFinishRun implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
finishActivity();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glView = new GLView(this);
// Use a surface format with an Alpha channel://
glView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
glView.setZOrderOnTop(true);
layout_frame=new FrameLayout(this);
layout_linear=new LinearLayout(this);
layout_linear.setBackgroundResource(R.drawable.background_game);
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);//��̬�IJ���
layout_linear_start=(LinearLayout)inflater.inflate(R.layout.main, null);
layout_linear_start.setBackgroundResource(R.drawable.background_start);
layout_linear_finish=new LinearLayout(this);
layout_linear_finish.setBackgroundResource(R.drawable.finish);
layout_linear_finish.setVisibility(INVISIBLE);
layout_linear.addView(glView);
layout_linear.setVisibility(INVISIBLE);
layout_frame.addView(layout_linear_finish);
layout_frame.addView(layout_linear);
layout_frame.addView(layout_linear_start);
setContentView(layout_frame);
glView.requestFocus();
glView.setFocusableInTouchMode(true);
txt_prompt=(TextView)findViewById(R.id.txt_prompt);//
myTimer.schedule(myTask, 500,delayTime);
}
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, ITEM0, 0, "DICE1").setIcon(R.drawable.style2_dice1);
menu.add(0, ITEM1, 0, "DICE2").setIcon(R.drawable.style2_dice2);
menu.add(0, ITEM2, 0, "EXIT").setIcon(R.drawable.exit_2);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case ITEM0://
GLRender.FLAG_DICE=0;
glView.freshDraw();
layout_linear.setVisibility(VISIBLE);
layout_linear_start.setVisibility(INVISIBLE);
showToast();
break;
case ITEM1://
GLRender.FLAG_DICE=1;
glView.freshDraw();
layout_linear.setVisibility(VISIBLE);
layout_linear_start.setVisibility(INVISIBLE);
showToast();
break;
case ITEM2://
layout_linear.setVisibility(INVISIBLE);
glView.setVisibility(INVISIBLE);
layout_linear_finish.setVisibility(VISIBLE);
mHandler.postDelayed(new myFinishRun(), 1200);break;
}
return super.onOptionsItemSelected(item);
}
public void showToast(){//
if(tost_flag==0){
mHandler.postDelayed(new MyRunnable(this){}, 700);
}
}
public void flashText(){
mHandler.postAtTime(new myRun(){}, android.os.SystemClock.uptimeMillis());
}
public void finishActivity(){
finish();
}
}