2
votes

I am writing a small Android game using a SurfaceView class. The outline of the clas is as follows

   class GameView extends SurfaceView implements Runnable {
    Thread drawThread;
    boolean drawThreadIsRunning;
    Context context;
    SurfaceHolder surfaceHolder;
    Canvas canvas;

    public GameView(Context context)
  {
   super(context)
   this.context = context;
   surfaceHolder = getHolder();
   //...
  }

    public void resume() {
        // starts drawing Thread
    }

    public void pause() {
        // stops drawing Thread
    }

    public void run() {
        while (drawThreadIsRunning) {
            if (!surfaceHolder.getSurface().isValid())
                continue;

            canvas = surfaceHolder.lockCanvas();
            update(canvas);
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }

    private void update(canvas)
   {
     updateCoordinates(canvas);
     updateDrawings(canvas); 
     e.t.c.
   }
}

What I want to know is whether or not it's a good idea to use the update() method the way I have. Between the locking and the unlocking of the canvas, the update method moves the game elements, draws them, updates the score and so on. Will this have a negative impact on the performance? Is there a better way of structuring this?

1

1 Answers

1
votes

It looks fine to me. The only thing that you could do would be to move the calls to updateCoordinates(); updateDrawings(); etc. into the run() to save the overhead of that additional method call but it shouldn't make a noticeable difference.