Advertisement
Guest User

Untitled

a guest
Sep 9th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. package com.wiagames.pacman;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.opengl.GLSurfaceView;
  6. import android.os.Bundle;
  7. import android.util.AttributeSet;
  8. import android.view.MotionEvent;
  9. import android.view.View;
  10.  
  11. import javax.microedition.khronos.egl.EGLConfig;
  12. import javax.microedition.khronos.opengles.GL10;
  13.  
  14. public class MainActivity extends Activity implements View.OnTouchListener {
  15.  
  16.     private PacmanGLSurfaceView mPacmanSurfaceView;
  17.  
  18.     static {
  19.         System.loadLibrary("pacman");
  20.     }
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.         mPacmanSurfaceView = (PacmanGLSurfaceView) findViewById(R.id.gl_surface_view);
  27.         final PacmanRenderer renderer = new PacmanRenderer();
  28.         mPacmanSurfaceView.setRenderer(renderer);
  29.  
  30.         setupControls();
  31.     }
  32.  
  33.  
  34.     private void setupControls() {
  35.         View down = findViewById(R.id.down);
  36.         down.setOnTouchListener(this);
  37.         View up = findViewById(R.id.up);
  38.         up.setOnTouchListener(this);
  39.         View left = findViewById(R.id.left);
  40.         left.setOnTouchListener(this);
  41.         View right = findViewById(R.id.right);
  42.         right.setOnTouchListener(this);
  43.     }
  44.  
  45.     @Override
  46.     protected void onPause() {
  47.         super.onPause();
  48.         mPacmanSurfaceView.onPause();
  49.     }
  50.  
  51.     @Override
  52.     protected void onResume() {
  53.         super.onResume();
  54.         mPacmanSurfaceView.onResume();
  55.     }
  56.  
  57.  
  58.     @Override
  59.     public boolean onTouch(View view, MotionEvent motionEvent) {
  60.         if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
  61.             switch (view.getId()) {
  62.                 case R.id.down:
  63.                     PacmanGLSurfaceView.nativeDown();
  64.                     return true;
  65.  
  66.                 case R.id.up:
  67.                     PacmanGLSurfaceView.nativeUp();
  68.                     return true;
  69.  
  70.                 case R.id.left:
  71.                     PacmanGLSurfaceView.nativeLeft();
  72.                     return true;
  73.  
  74.                 case R.id.right:
  75.                     PacmanGLSurfaceView.nativeRight();
  76.                     return true;
  77.  
  78.                 default:
  79.                     return false;
  80.             }
  81.         } else {
  82.             return false;
  83.         }
  84.     }
  85. }
  86.  
  87. /**
  88.  * GL Surface View
  89.  *
  90.  * @author rankor777
  91.  */
  92. class PacmanGLSurfaceView extends GLSurfaceView {
  93.  
  94.     private PacmanRenderer mRenderer;
  95.  
  96.     public PacmanGLSurfaceView(Context context, AttributeSet attrs) {
  97.         super(context, attrs);
  98.  
  99.         mRenderer = new PacmanRenderer();
  100.     }
  101.  
  102.     private static native void nativePause();
  103.  
  104.     public static native void nativeDown();
  105.  
  106.     public static native void nativeUp();
  107.  
  108.     public static native void nativeLeft();
  109.  
  110.     public static native void nativeRight();
  111. }
  112.  
  113. /**
  114.  * GL Renderer
  115.  *
  116.  * @author rankor777
  117.  */
  118. class PacmanRenderer implements GLSurfaceView.Renderer {
  119.     public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  120.         nativeInit();
  121.     }
  122.  
  123.     public void onSurfaceChanged(GL10 gl, int w, int h) {
  124.         nativeResize(w, h);
  125.     }
  126.  
  127.     public void onDrawFrame(GL10 gl) {
  128.         nativeRender();
  129.     }
  130.  
  131.     private static native void nativeInit();
  132.  
  133.     private static native void nativeResize(int w, int h);
  134.  
  135.     private static native void nativeRender();
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement