Advertisement
Guest User

Untitled

a guest
Mar 4th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. package com.example.alex.linedodger.Repository;
  2.  
  3. import android.graphics.Canvas;
  4. import android.view.SurfaceHolder;
  5.  
  6. import com.example.alex.linedodger.Constants;
  7. import com.example.alex.linedodger.Controller.GamePanel;
  8.  
  9. /**
  10.  * Created by alex on 01.03.2017.
  11.  */
  12.  
  13. public class MainThread extends Thread {
  14.  
  15.     private static Canvas canvas;
  16.  
  17.     private double average_FPS;
  18.     private SurfaceHolder surfaceHolder;
  19.     private GamePanel gamePanel;
  20.     private boolean running;
  21.  
  22.  
  23.     public MainThread(SurfaceHolder surfaceHolder, GamePanel gamePanel){
  24.         // To be sure that the implicit constructor of [Thread] class is called
  25.         super();
  26.         this.surfaceHolder = surfaceHolder;
  27.         this.gamePanel = gamePanel;
  28.     }
  29.     public void setRunning(boolean running){
  30.         this.running = running;
  31.     }
  32.  
  33.     @Override
  34.     public void run(){
  35.         long startTime;     // when we start
  36.         long timeMillis = 1000/ Constants.SETTINGS.MAX_FPS;
  37.         long waitTime;
  38.         int frameCount = 0;
  39.         int totalTime = 0;
  40.         int targetTime = 1000/Constants.SETTINGS.MAX_FPS;
  41.  
  42.         // Main loop
  43.         while (running){
  44.             startTime = System.nanoTime();
  45.             canvas = null;
  46.  
  47.             try{
  48.                 canvas = this.surfaceHolder.lockCanvas();
  49.                 /*The synchronized keyword is all about different
  50.                     threads reading and writing to the same variables, objects and resources
  51.                     Basically: Because we use [Threads], we use synchronize to make them work on the same thing
  52.                 */
  53.                 synchronized (surfaceHolder){
  54.                     this.gamePanel.update();
  55.                     this.gamePanel.draw(canvas);
  56.                 }
  57.  
  58.             }catch (Exception e){
  59.                 // Currently we only want to se where the error is.
  60.                 e.printStackTrace();
  61.             }finally {
  62.                 if (this.canvas != null){
  63.                     try {
  64.                         surfaceHolder.unlockCanvasAndPost(canvas);
  65.                     }catch (Exception e){
  66.                         e.printStackTrace();
  67.                     }
  68.                 }
  69.             }
  70.             timeMillis = (System.nanoTime() - startTime)/1000000;
  71.             waitTime = targetTime - timeMillis;
  72.             try {
  73.                 // if we finished the frame earlier than the target time
  74.                 if (waitTime > 0){
  75.                     this.sleep(waitTime);
  76.                 }
  77.             }catch (Exception e){
  78.                 e.printStackTrace();
  79.             }
  80.  
  81.             totalTime += System.nanoTime()  - startTime;
  82.             frameCount++;
  83.             if (frameCount == Constants.SETTINGS.MAX_FPS){
  84.                 average_FPS = 1000/((totalTime/ frameCount)/1000000);
  85.                 frameCount = 0;
  86.                 totalTime = 0;
  87.                 System.out.println(average_FPS);
  88.             }
  89.  
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement