While developing my game I am often asking myself if my way of handling different game screens (MainMenu, Game, Ending, Highscore) is a good one. I am currently using only one activity. The game loop is running in this class. The currently active screen gets a update and paint method called from this loop like this:
while (running) {
if (!holder.getSurface().isValid())
continue;
float deltaTime = (System.nanoTime() - startTime) / 1000000000.0f;
startTime = System.nanoTime();
if (deltaTime > 0.315) {
deltaTime = (float) 0.315;
}
game.getCurrentScreen().update(deltaTime);
game.getCurrentScreen().paint(deltaTime);
Canvas canvas = holder.lockCanvas();
canvas.getClipBounds(dstRect);
canvas.drawBitmap(framebuffer, null, dstRect, null);
holder.unlockCanvasAndPost(canvas);
}
One problem I am often facing is that the screen classes aren't activities, so I am unable to start a DialogFragment
, as an example, because it needs a FragmentActivity
. Since these classes are just instantiated the FragmentActivity
never gets started.
If I need a dialog, I am currently just making an Activity
to a custom dialog.
How would you handle a game framework? Are there any good examples out there?