Advertisement
Guest User

Untitled

a guest
Apr 9th, 2013
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. /**
  2.  *
  3.  */
  4. package com.gorgo.pirates;
  5.  
  6. import java.text.DecimalFormat;
  7.  
  8. import android.graphics.Canvas;
  9. import android.os.Handler;
  10. import android.os.Message;
  11. import android.util.Log;
  12. import android.view.SurfaceHolder;
  13.  
  14. import com.gorgo.pirates.controller.GameEngine;
  15.  
  16. /**
  17.  * @author impaler
  18.  *
  19.  *         The Main thread which contains the game loop. The thread must have
  20.  *         access to the surface view and holder to trigger events every game
  21.  *         tick.
  22.  */
  23. public class MainThread extends Thread {
  24.  
  25.     private static final String TAG = MainThread.class.getSimpleName();
  26.  
  27.     // desired fps
  28.     private final static int MAX_FPS = 50;
  29.     // maximum number of frames to be skipped
  30.     private final static int MAX_FRAME_SKIPS = 5;
  31.     // the frame period
  32.     private final static int FRAME_PERIOD = 1000 / MAX_FPS;
  33.  
  34.     /* Stuff for stats */
  35.     private DecimalFormat df = new DecimalFormat("0.##"); // 2 dp
  36.     // we'll be reading the stats every second
  37.     private final static int STAT_INTERVAL = 1000; // ms
  38.     // the average will be calculated by storing
  39.     // the last n FPSs
  40.     private final static int FPS_HISTORY_NR = 10;
  41.     // the status time counter
  42.     private long statusIntervalTimer = 0l;
  43.     // number of frames skipped since the game started
  44.     private long totalFramesSkipped = 0l;
  45.     // number of frames skipped in a store cycle (1 sec)
  46.     private long framesSkippedPerStatCycle = 0l;
  47.  
  48.     // number of rendered frames in an interval
  49.     private int frameCountPerStatCycle = 0;
  50.     private long totalFrameCount = 0l;
  51.     // the last FPS values
  52.     private double fpsStore[];
  53.     // the number of times the stat has been read
  54.     private long statsCount = 0;
  55.     // the average FPS since the game started
  56.     private double averageFps = 0.0;
  57.  
  58.     // Surface holder that can access the physical surface
  59.     private SurfaceHolder surfaceHolder;
  60.     // The actual view that handles inputs
  61.     // and draws to the surface
  62.     private GameEngine gameEngine;
  63.     private Handler handler;
  64.  
  65.     // flag to hold game state
  66.     private boolean running;
  67.  
  68.     public void setRunning(boolean running) {
  69.         this.running = running;
  70.     }
  71.  
  72.     public MainThread(SurfaceHolder surfaceHolder, GameEngine gameEngine,
  73.             Handler handler) {
  74.         super();
  75.         this.surfaceHolder = surfaceHolder;
  76.         this.gameEngine = gameEngine;
  77.         this.handler = handler;
  78.     }
  79.  
  80.     @Override
  81.     public void run() {
  82.         Canvas canvas;
  83.         Log.d(TAG, "Starting game loop");
  84.         // initialise timing elements for stat gathering
  85.         initTimingElements();
  86.  
  87.         long beginTime; // the time when the cycle begun
  88.         long timeDiff; // the time it took for the cycle to execute
  89.         int sleepTime; // ms to sleep (<0 if we're behind)
  90.         int framesSkipped; // number of frames being skipped
  91.  
  92.         sleepTime = 0;
  93.  
  94.         while (running) {
  95.             canvas = null;
  96.  
  97.             // try locking the canvas for exclusive pixel editing
  98.             // in the surface
  99.             try {
  100.                 canvas = this.surfaceHolder.lockCanvas();
  101.                 synchronized (surfaceHolder) {
  102.                     beginTime = System.currentTimeMillis();
  103.                     framesSkipped = 0; // resetting the frames skipped
  104.                     // update game state
  105.                     this.gameEngine.update(this);
  106.                     // render state to the screen
  107.                     // draws the canvas on the panel
  108.                     this.gameEngine.render(canvas);
  109.                     // calculate how long did the cycle take
  110.                     timeDiff = System.currentTimeMillis() - beginTime;
  111.                     // calculate sleep time
  112.                     sleepTime = (int) (FRAME_PERIOD - timeDiff);
  113.  
  114.                     if (sleepTime > 0) {
  115.                         // if sleepTime > 0 we're OK
  116.                         try {
  117.                             // send the thread to sleep for a short period
  118.                             // very useful for battery saving
  119.                             Thread.sleep(sleepTime);
  120.                         } catch (InterruptedException e) {
  121.                         }
  122.                     }
  123.  
  124.                     while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
  125.                         // we need to catch up
  126.                         this.gameEngine.update(this); // update without
  127.                                                         // rendering
  128.                         sleepTime += FRAME_PERIOD; // add frame period to check
  129.                                                     // if in next frame
  130.                         framesSkipped++;
  131.                     }
  132.  
  133.                     // for statistics
  134.                     framesSkippedPerStatCycle += framesSkipped;
  135.                     // calling the routine to store the gathered statistics
  136.                     storeStats();
  137.  
  138.                 }
  139.             } finally {
  140.                 // in case of an exception the surface is not left in
  141.                 // an inconsistent state
  142.                 if (canvas != null) {
  143.                     surfaceHolder.unlockCanvasAndPost(canvas);
  144.                 }
  145.             } // end finally
  146.         }
  147.     }
  148.  
  149.     public void notifyMessage(int visibility) {
  150.         Message msg = handler.obtainMessage();
  151.         msg.what = visibility;
  152.         handler.sendMessage(msg);
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement