Simple question about the lunarlander example.

Posted by Smills on Stack Overflow See other posts from Stack Overflow or by Smills
Published on 2011-01-06T09:03:11Z Indexed on 2011/01/06 9:54 UTC
Read the original article Hit count: 218

Filed under:
|
|
|
|

I am basing my game off the lunarlander example. This is the run loop I am using (very similar to what is used in lunarlander). I am getting considerable performance issues associated with my drawing, even if I draw almost nothing.

I noticed the below method. Why is the canvas being created and set to null each cycle?

    @Override
    public void run() 
    {
        while (mRun) 
        {
            Canvas c = null;
            try 
            {
                c = mSurfaceHolder.lockCanvas();//null
                synchronized (mSurfaceHolder) 
                {
                    updatePhysics();
                    doDraw(c);
                }
            } finally 
            {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) 
                {
                    mSurfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }

Most of the times I have read anything about canvases it is more along the lines of:

mField = new Bitmap(...dimensions...);
Canvas c = new Canvas(mField);

My question is: why is Google's example done that way (null canvas), what are the benefits of this, and is there a faster way to do it?

© Stack Overflow or respective owner

Related posts about java

Related posts about android