Android游戏引擎之Libgdx学习(二)

游戏引擎最直观的用法,就是在Init()中初始化你的场景,在Render()函数中画出你的场景,在Update(time)中更新你的场景信息。

而对于刚接触Android的我来说,系统类似windows中的无限循环到底是在哪。

看了一下《Android游戏开发入门》这书里边,大概了解到,它是放在一个独立的线程中,然后和Android的Acitity的生命周期是一样的。

代码如下:

public class AndroidFastRenderView extends SurfaceView implements Runnable { 
AndroidGame game;
Bitmap framebuffer;
Thread renderThread = null;
SurfaceHolder holder;
volatile boolean running = false;

public AndroidFastRenderView(AndroidGame game, Bitmap framebuffer) {
super(game);
this.game = game;
this.framebuffer = framebuffer;
this.holder = getHolder();
}

public void resume() {
running = true;
renderThread = new Thread(this);
renderThread.start();
}


public void run() {
Rect dstRect = new Rect();
long startTime = System.nanoTime();
while(running) {
if(!holder.getSurface().isValid())
continue;

float deltaTime = (System.nanoTime()-startTime) / 1000000000.0f;
startTime = System.nanoTime();

game.getCurrentScreen().update(deltaTime);
game.getCurrentScreen().present(deltaTime);

Canvas canvas = holder.lockCanvas();
canvas.getClipBounds(dstRect);
canvas.drawBitmap(framebuffer, null, dstRect, null);
holder.unlockCanvasAndPost(canvas);
}
}

public void pause() {
running = false;
while(true) {
try {
renderThread.join();
break;
} catch (InterruptedException e) {
// retry
}
}
}
}

之后这个类做为Activity的一个类成员,与Activity生命周期同步。

具体在libgdx中是放在哪。。我还没有找到,等找到再加上来。

原文地址:https://www.cnblogs.com/gameprogram/p/2360744.html