1
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

What you are doing is fine and will work.

Like your example, games are rather often structured as a Finite State Machine.

Since you are doing Android, however, take a look at LibGdx, and in particular Scene2D.

There is no real need to roll your own, unless you are using it as a learning experience.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .