Game Development Reference
In-Depth Information
this .framebuffer=framebuffer;
this .holder=getHolder();
}
In the constructor, we simply call the base class's constructor with the AndroidGame parameter
(which is an Activity ; this will be discussed in the following sections) and store the parameters
in the respective members. Once again, we get a SurfaceHolder , as in previous sections.
public void resume() {
running= true ;
renderThread= new Thread( this );
renderThread.start();
}
The resume() method is an exact copy of the FastRenderView.resume() method, so we won't
discuss it again. In short, the method makes sure that our thread interacts nicely with the activity
life cycle.
public void run() {
Rect dstRect= new Rect();
long startTime=System. nanoTime ();
while (running) {
if 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);
}
}
The run() method has a few more features. The first addition is its ability to track delta time
between each frame. For this, we use System.nanoTime() , which returns the current time in
nanoseconds as a long .
Note
A nanosecond is one-billionth of a second.
In each loop iteration, we start by taking the difference between the last loop iteration's start
time and the current time. To make it easier to work with that delta, we convert it into seconds.
Next, we save the current timestamp, which we'll use in the next loop iteration, to calculate
the next delta time. With the delta time at hand, we call the current Screen instance's update()
and present() methods, which will update the game logic and render things to the artificial
framebuffer. Finally, we get a hold of the Canvas for the SurfaceView and draw the artificial
 
Search WWH ::




Custom Search