Game Development Reference
In-Depth Information
Finally, we have the methods
getWidth()
and
getHeight()
, which simply return the size of the
artificial framebuffer stored by the
AndroidGraphics
class to which it renders internally.
AndroidFastRenderView
is the last class we need in order to implement.
AndroidFastRenderView: Loop, Stretch, Loop, Stretch
The name of this class should give away what lies ahead. In Chapter 4, we discussed using a
SurfaceView
to perform continuous rendering in a separate thread that could also house our
game's main loop. We developed a very simple class called
FastRenderView
, which was derived
from the
SurfaceView
class, we made sure we play nice with the activity life cycle, and we set
up a thread in order to constantly render the
SurfaceView
via a
Canvas
. Here, we'll reuse this
FastRenderView
class and augment it to do a few more things:
It keeps a reference to a
Game
instance from which it can get the active
Screen
. We constantly call the
Screen.update()
and
Screen.present()
methods from within the
FastRenderView
thread.
It keeps track of the delta time between frames that is passed to the
ļ?®
ļ?®
active
Screen
.
It takes the artificial framebuffer to which the
AndroidGraphics
instance draws, and draws it to
the
SurfaceView
, which is scaled if necessary.
Listing 5-15 shows the implementation of the
AndroidFastRenderView
class, with commentary
where appropriate.
Listing 5-15. AndroidFastRenderView.java, a Threaded SurfaceView Executing Our Game Code
package com.badlogic.androidgames.framework.impl;
import
android.graphics.Bitmap;
import
android.graphics.Canvas;
import
android.graphics.Rect;
import
android.view.SurfaceHolder;
import
android.view.SurfaceView;
public class
AndroidFastRenderView
extends
SurfaceView
implements
Runnable {
AndroidGame game;
Bitmap framebuffer;
Thread renderThread=
null
;
SurfaceHolder holder;
volatile boolean
running=
false
;
This should look familiar. We just need to add two more membersāan
AndroidGame
instance and
a
Bitmap
instance that represent our artificial framebuffer. The other members are the same as in
our
FastRenderView
from Chapter 3.
public
AndroidFastRenderView(AndroidGame game, Bitmap framebuffer) {
super
(game);
this
.game=game;