If you think the Android project gameengine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.garrapeta.gameengine;
//www.java2s.comimport android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.garrapeta.gameengine.utils.LogX;
/**
* Vista sobre la que se renderiza el juego
*
* @author GaRRaPeTa
*/publicclass GameView extends SurfaceView implements SurfaceHolder.Callback {
// -----------------------------------------------------------------
// Constants
publicstaticfinal String TAG = GameWorld.TAG_GAME_ENGINE + ".gameView";
// -----------------------------------------------------------------
// Variables
private GameWorld mWorld;
/**
* Holder del la SurfaceView
*/private SurfaceHolder mHolder;
// private boolean waitingForDrawingDispatched = false;
// ---------------------------------------------------------------
// Constructor
/**
* Constructor
*
* @param context
* @param attrs
*/public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
mHolder = getHolder();
mHolder.addCallback(this);
setFocusableInTouchMode(true);
requestFocus();
}
// ----------------------------------------------------------- mtodos
// propios
/**
* @param gameWorld
*/finalvoid setGameWorld(GameWorld gameWorld) {
mWorld = gameWorld;
}
/**
* Pinta el mundo sobre la pantalla
*/
@SuppressLint("WrongCall")
finalvoid draw() {
if (mHolder != null) {
Canvas canvas = mHolder.lockCanvas();
if (canvas != null) {
onDraw(canvas);
mHolder.unlockCanvasAndPost(canvas);
}
}
}
@Override
protectedvoid onDraw(Canvas canvas) {
if (mWorld != null) {
mWorld.doDrawWorld(canvas);
}
}
@Override
publicfinalvoid surfaceCreated(SurfaceHolder holder) {
LogX.i(TAG, "surfaceCreated (" + getWidth() + ", " + getHeight() + ")");
// Draw the view when it's been created, to avoid a black screen
draw();
}
@Override
publicfinalvoid surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
LogX.i(TAG, "surfaceChanged (" + width + ", " + height + ")");
if (mWorld != null) {
mWorld.gameViewSizeChanged(this, width, height);
}
}
@Override
publicfinalvoid surfaceDestroyed(SurfaceHolder holder) {
LogX.i(TAG, "surfaceDestroyed");
}
}