Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.vedi0boy.anaglyph;
- import android.graphics.Canvas;
- import android.util.Log;
- import android.view.SurfaceHolder;
- public class MainThread extends Thread{
- private static final String TAG = MainThread.class.getSimpleName();
- private SurfaceHolder holder;
- private Anaglyph gamePanel;
- private final static int MAX_FPS = 50;
- private final static int MAX_FRAME_SKIPS = 5;
- private final static int FRAME_PERIOD = 1000 / MAX_FPS;
- public MainThread(SurfaceHolder holder, Anaglyph gamePanel){
- super();
- this.holder = holder;
- this.gamePanel = gamePanel;
- }
- public static boolean running = false;
- public void setRunning(boolean running){
- this.running = running;
- }
- public void run(){
- Canvas canvas;
- long beginTime;
- long timeDiff;
- int sleepTime;
- int framesSkipped;
- sleepTime = 0;
- Log.d(TAG, "Starting Game Loop");
- while(running){
- canvas = null;
- Log.d(TAG,"Game loop running!");
- try{
- canvas = this.holder.lockCanvas();
- synchronized (holder){
- beginTime = System.currentTimeMillis();
- framesSkipped = 0;
- if(canvas != null){
- this.gamePanel.update();
- this.gamePanel.render(canvas);
- }
- timeDiff = System.currentTimeMillis()- beginTime;
- sleepTime = (int)(FRAME_PERIOD - timeDiff);
- if(sleepTime > 0){
- try{
- Thread.sleep(sleepTime);
- }catch(InterruptedException e){}
- }
- }
- while(sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS){
- if(canvas != null){
- this.gamePanel.update();
- }
- sleepTime += FRAME_PERIOD;
- framesSkipped++;
- }
- } finally{
- if(canvas != null){
- holder.unlockCanvasAndPost(canvas);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement