Android gameLoop using SurfaceView; quickly changing camera 'shots' results in black flashes, etc

768 views Asked by At

I built a simple game engine using SurfaceView.

I use a RectF as a 'camera' window, to represent the scaled pixel width and height of the SurfaceView canvas, and to figure out what is on screen.

I use canvas.translate to move to where the camera is 'pointing' at. Here is the draw loop:

mCamera.UpdateCamera();
scaleWorld(c, mCamera);
c.translate(-mScreen.left, mScreen.top - miHeight
* (1 / mCamera.getScaleLevel()));
drawBehavior.drawGame(c);

Any way, if I shoot a projectile, the game renders smoothly, and I see no visual flashes of black, which is the color I am using to flush the canvas.

However, if I instantly change what the camera can see, perhaps 3 or more 'screens' away, there is often, but not always, flashes of black. Sometimes I get 'lucky' and the transition is seamless.

These flashes are usually in the positions of my bitmaps, as I draw the ground and background recursively using one or two bitmaps.

It seemed to me that the sudden change was the issue, so I tried to essentially trash the gameLoop thread to stop everything, then set the new Rect coordinates, then restart with another thread, but this does not work.

I suppose on reflection, I was simply delaying the 'shock' to the system by doing this, not treating it.

Before attempting to sweep this perhaps atrocious design under the rug by perhaps a fade out or other visual trick, I am wondering if anyone has a deeper understanding of the issue and could lead me to a more elegant solution.

1

There are 1 answers

0
Plastic Sturgeon On BEST ANSWER

I am guessing the screen is refreshing before the draw is complete. Multi threading still kinda loses me a bit - but here is something to try: Try locking the canvas at the start of your game loop, and unlock it at the end: http://developer.android.com/reference/android/view/SurfaceHolder.html#lockCanvas()