diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-18 13:59:06 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-18 13:59:06 +0100 |
commit | 3ea9b7b394e3d8a6e9ae726d0de190b5bd938a7d (patch) | |
tree | ff4e0e8b238a4ad242d04dcada211964ff5940b0 /same-android/src | |
parent | e048c5caf68df2b162c8f8f3d186f79fc5f59b57 (diff) |
Add Game classes in android project.
Add a SurfaceView and draw on it.
Diffstat (limited to 'same-android/src')
-rw-r--r-- | same-android/src/main/java/com/orbekk/GameController.java | 5 | ||||
-rw-r--r-- | same-android/src/main/java/com/orbekk/GameView.java | 89 |
2 files changed, 94 insertions, 0 deletions
diff --git a/same-android/src/main/java/com/orbekk/GameController.java b/same-android/src/main/java/com/orbekk/GameController.java new file mode 100644 index 0000000..01ad24d --- /dev/null +++ b/same-android/src/main/java/com/orbekk/GameController.java @@ -0,0 +1,5 @@ +package com.orbekk; + +public class GameController { + +} diff --git a/same-android/src/main/java/com/orbekk/GameView.java b/same-android/src/main/java/com/orbekk/GameView.java new file mode 100644 index 0000000..047955a --- /dev/null +++ b/same-android/src/main/java/com/orbekk/GameView.java @@ -0,0 +1,89 @@ +package com.orbekk; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.view.SurfaceHolder; +import android.view.SurfaceView; + +public class GameView extends SurfaceView implements SurfaceHolder.Callback { + private Logger logger = LoggerFactory.getLogger(getClass()); + private GameThread thread; + + static class GameThread extends Thread { + private int height = 0; + private int width = 0; + private int posX; + private int posY; + private SurfaceHolder holder; + private Context context; + private Paint paint; + + public GameThread(SurfaceHolder holder, Context context) { + this.holder = holder; + this.context = context; + paint = new Paint(); + paint.setAntiAlias(true); + paint.setARGB(255, 255, 0, 0); + } + + public void setSize(int width, int height) { + synchronized(holder) { + this.width = width; + this.height = height; + } + } + + private void doDraw(Canvas c) { + c.drawCircle(100.0f, 50.0f, 20.0f, paint); + } + + @Override public void run() { + Canvas c = null; + try { + c = holder.lockCanvas(); + synchronized(holder) { + doDraw(c); + } + } finally { + holder.unlockCanvasAndPost(c); + } + } + } + + public GameView(Context context) { + super(context); + getHolder().addCallback(this); + thread = new GameThread(getHolder(), context); + } + + @Override + protected void onDraw(Canvas canvas) { + Paint paint = new Paint(); + paint.setARGB(255, 255, 0, 0); + canvas.drawCircle(50.0f, 50.0f, 50.0f, paint); + } + + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int width, + int height) { + logger.info("SurfaceChanged(w={}, h={})", width, height); + thread.setSize(width, height); + } + + @Override + public void surfaceCreated(SurfaceHolder holder) { + logger.info("SurfaceCreated()"); + thread.start(); + } + + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + logger.info("SurfaceDestroyed()"); + // TODO: Stop thread. + } + +} |