Game Development Reference
In-Depth Information
In the
onCreate()
method, we perform the usual setup routine. We make the
Activity
go
full-screen and instantiate the
GLSurfaceView
, setting it as the content
View
. We also instantiate
all the other classes that implement framework interfaces, such as the
AndroidFileIO
and
AndroidInput
classes. Note that we reuse the classes we used in the
AndroidGame
class, except
for
AndroidGraphics
. Another important point is that we no longer let the
AndroidInput
class
scale the touch coordinates to a target resolution, as in
AndroidGame
. The scale values are both 1,
so we will get the real touch coordinates. It will become clear later on why we do that. The last
thing we do is create the
WakeLock
instance.
@Override
public void
onResume() {
super
.onResume();
glView.onResume();
wakeLock.acquire();
}
In the
onResume()
method, we let the
GLSurfaceView
start the rendering thread with a call to its
onResume()
method. We also acquire the
WakeLock
.
public void
onSurfaceCreated(GL10 gl, EGLConfig config) {
glGraphics.setGL(gl);
synchronized
(stateChanged) {
if
(state == GLGameState.
Initialized
)
screen = getStartScreen();
state = GLGameState.
Running
;
screen.resume();
startTime = System.
nanoTime
();
}
}
The
onSurfaceCreate()
method will be called next, which is, of course, invoked on the rendering
thread. Here, you can see how the
state
enums are used. If the application is started for the
first time,
state
will be
GLGameState.Initialized
. In this case, we call the
getStartScreen()
method to return the starting screen of the game. If the game is not in an initialized state but has
already been running, we know that we have just resumed from a paused state. In any case, we
set
state
to
GLGameState.Running
and call the current
Screen
's
resume()
method. We also keep
track of the current time, so we can calculate the delta time later on.
The synchronization is necessary, since the members we manipulate within the synchronized
block could be manipulated in the
onPause()
method on the UI thread. That's something we
have to prevent, so we use an object as a lock. We could have also used the
GLGame
instance
itself, or a proper lock.
public void
onSurfaceChanged(GL10 gl,
int
width,
int
height) {
}
The
onSurfaceChanged()
method is basically just a stub. There's nothing for us to do here.