Advertisement
Guest User

Untitled

a guest
Feb 4th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. /**
  2.  *
  3.  */
  4. package net.obviam.walking;
  5.  
  6. import net.obviam.walking.model.ElaineAnimated;
  7. import android.content.Context;
  8. import android.content.res.Resources;
  9. import android.graphics.Bitmap;
  10. import android.graphics.BitmapFactory;
  11. import android.graphics.Canvas;
  12. import android.graphics.Color;
  13. import android.graphics.Paint;
  14. import android.graphics.Rect;
  15. import android.graphics.drawable.BitmapDrawable;
  16. import android.util.Log;
  17. import android.view.MotionEvent;
  18. import android.view.SurfaceHolder;
  19. import android.view.SurfaceView;
  20.  
  21. /**
  22.  * @author impaler
  23.  * This is the main surface that handles the ontouch events and draws
  24.  * the image to the screen.
  25.  */
  26. public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback {
  27.  
  28.     private static final String TAG = MainGamePanel.class.getSimpleName();
  29.    
  30.     private MainThread thread;
  31.     private ElaineAnimated elaine;
  32.     private Bitmap background;
  33.  
  34.     // the fps to be displayed
  35.     private String avgFps;
  36.     public void setAvgFps(String avgFps) {
  37.         this.avgFps = avgFps;
  38.     }
  39.  
  40.     public MainGamePanel(Context context) {
  41.         super(context);
  42.        
  43.         // adding the callback (this) to the surface holder to intercept events
  44.         getHolder().addCallback(this);
  45.  
  46.         // create Elaine and load bitmap
  47.         elaine = new ElaineAnimated(
  48.                 BitmapFactory.decodeResource(getResources(), R.drawable.guybrush3)
  49.                 , 0, 0  // initial position
  50.                 , 30, 30    // width and height of sprite
  51.                 , 7, 6);    // FPS and number of frames in the animation
  52.        
  53.         Resources res = getResources();
  54.         background = BitmapFactory.decodeResource(res, R.drawable.background);
  55.        
  56.         // create the game loop thread
  57.         thread = new MainThread(getHolder(), this);
  58.        
  59.         // make the GamePanel focusable so it can handle events
  60.         setFocusable(true);
  61.     }
  62.  
  63.     @Override
  64.     public void surfaceChanged(SurfaceHolder holder, int format, int width,
  65.             int height) {
  66.     }
  67.  
  68.     @Override
  69.     public void surfaceCreated(SurfaceHolder holder) {
  70.         // at this point the surface is created and
  71.         // we can safely start the game loop
  72.         thread.setRunning(true);
  73.         thread.start();
  74.     }
  75.  
  76.     @Override
  77.     public void surfaceDestroyed(SurfaceHolder holder) {
  78.         Log.d(TAG, "Surface is being destroyed");
  79.         // tell the thread to shut down and wait for it to finish
  80.         // this is a clean shutdown
  81.         boolean retry = true;
  82.         while (retry) {
  83.             try {
  84.                 thread.join();
  85.                 retry = false;
  86.             } catch (InterruptedException e) {
  87.                 // try again shutting down the thread
  88.             }
  89.         }
  90.         Log.d(TAG, "Thread was shut down cleanly");
  91.     }
  92.    
  93.     @Override
  94.     public boolean onTouchEvent(MotionEvent event) {
  95.         if (event.getAction() == MotionEvent.ACTION_DOWN) {
  96.             // handle touch
  97.         }
  98.         return true;
  99.     }
  100.  
  101.     public void render(Canvas canvas) {
  102.        
  103.         canvas.drawColor(Color.BLACK);
  104.         canvas.drawBitmap(background, null, new Rect(0, 0, getWidth(), getHeight()), new Paint());
  105.         elaine.draw(canvas);
  106.         // display fps
  107.         displayFps(canvas, avgFps);
  108.     }
  109.  
  110.     /**
  111.      * This is the game update method. It iterates through all the objects
  112.      * and calls their update method if they have one or calls specific
  113.      * engine's update method.
  114.      */
  115.     public void update() {
  116.         elaine.update(System.currentTimeMillis());
  117.     }
  118.  
  119.     private void displayFps(Canvas canvas, String fps) {
  120.         if (canvas != null && fps != null) {
  121.             Paint paint = new Paint();
  122.             paint.setARGB(255, 255, 255, 255);
  123.             canvas.drawText(fps, this.getWidth() - 50, 20, paint);
  124.         }
  125.     }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement