Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualtom_mai78101

Posted 16 August 2012 - 08:05 AM

Notice: I have updated the code snippet. Please take a look at post #3. Updated to make the code more pure multi-threaded.

Link for the lazy :D ---> Link.

#1tom_mai78101

Posted 09 August 2012 - 04:12 AM

Please tell me what you think and criticisms are welcome.

Basic Activity:

package ff.ff;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

public class Basic extends Activity implements Runnable {
Thread thread;
GameView view;
Looper loop;
public void onCreate(Bundle b){
  super.onCreate(b);
  view = new GameView(this);
  setContentView(view);
}

public void run(){
  Looper.prepare();
  loop = Looper.myLooper();
  view.setHandler(new Handler());
  Looper.loop();
}

public void onPause(){
  super.onPause();
  loop.quit();
  boolean retry = true;
  while (retry){
   try{
    thread.join();
    retry = false;
   }
   catch (InterruptedException e){
    retry = true;
   }
  }
  view.onPause();
}

public void onResume(){
  super.onResume();
  thread = new Thread(this);
  thread.setName("Activity  Thread");
  thread.start();
  view.onResume();
}
}

GameView class, a subclass of SurfaceView:

package ff.ff;
import android.app.Activity;
import android.graphics.Canvas;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameView extends SurfaceView implements Runnable {
SurfaceHolder holder;
Thread gameThread;
volatile boolean running;
Handler handler;
public int red;

Runnable r1 = new Runnable() {
  public void run() {
   /*
    * This is the place where you update your objects' states in. This is
    * for the tick() method used below.
    */
   red++;
   if (red > 255)
    red = 0;
   Log.d("DEBUG", Integer.toString(red));
  }
};

public GameView(Activity a) {
  super(a);
  holder = getHolder();
}

public void setHandler(Handler h) {
  handler = h;
}

public void onPause() {
  running = false;
  boolean retry = true;
  while(retry) {
   try {
    gameThread.join();
    retry = false;
   }
   catch(InterruptedException e) {
    retry = true;
   }
  }
}

public void onResume() {
  running = true;
  gameThread = new Thread(this);
  gameThread.setName("Game Thread");
  gameThread.start();
}

public void run() {
  while(running) {
   tick();
   /*
    * This is where you do the rendering / drawing. The game loop's basic structure is now
    * complete. As for the tweaking that may be missing, it's up to the developers to
    * create and fix them.
    *
    * I only give out the basic template of the Multi-Threaded Looper game loop.
    */
   if (holder != null) {
    if (holder.getSurface().isValid()) {
	 Canvas c = holder.lockCanvas();
	 c.drawARGB(255, red, 255, 255);
	 holder.unlockCanvasAndPost(c);
    }
   }
  }
}

public void tick() {
  if (handler != null)
   handler.post(r1);
}
}

What do you say about it? Thanks in advance.

PARTNERS