DrawThread.java :  » Game » androiddefense » com » androiddefense » engine » Android Open Source

Android Open Source » Game » androiddefense 
androiddefense » com » androiddefense » engine » DrawThread.java
/**
 * 
 */
package com.androiddefense.engine;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;

/**
 * @author Steven Christou
 *
 */
public class DrawThread extends Thread {
  boolean shouldRun = true;
  boolean paused = false;
  GameModel_I myGame;
  
  SurfaceHolder holder;
  public DrawThread(SurfaceHolder holder, GameModel_I game) {
    this.holder = holder;
    myGame = game;
  }

  @Override
  public void run() {
    shouldRun = true;
    
    Log.i("GameExample","DrawThread loop starting");
    while(shouldRun){
      Canvas canvas = holder.lockCanvas();
      canvas.drawARGB(255, 0, 0, 0);
      myGame.stepForward();
      myGame.draw(canvas);
      holder.unlockCanvasAndPost(canvas);
      try {
        Thread.sleep(200);
      } catch (InterruptedException e) {
        Log.i("GameExample","DrawThread exn: " + e);
      }
      
      while (paused) {
        try {
          Thread.sleep(100);
        }
        catch (InterruptedException e) {
          Log.i("Paused error", "Exception when paused", e);
        }
      }
    }
    
    Log.i("GameExample","DrawThread loop ended");
  }
  
  public void stopRunning(){
    shouldRun = false;
  }
  
  public void togglePause() {
    paused = !paused;
  }
  
  public boolean drawStatus() {
    return paused;
  }
  
  public DrawThread getDrawThread() {
    return this;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.