Loop and FPS (one more newbie problem...)

Hi all

I’ve a problem with my game loop, sometimes the render is jerking, the drawing is not perfect

  • In my game, the background is scrolling to the left indefinitely, the drawing should be perfect, but the render is not constant (regular)
  • The garbage collector is never in action
  • The background is composed by sprite (bmp) but even with only one bmp it is still jerking
  • I’ve tried the loop on http://www.java-gaming.org/topics/game-loops/24220/view.html and this loop is also jerking (less than mine…)

I’m quasi sure that the loop I’m using is not perfect in term of timing (sleeping thread)

can you help me please?

here is the code I use:




public class GameLoopThread extends Thread
{
	private final static int 	MAX_FPS = 60;					// desired fps
	private final static int	MAX_FRAME_SKIPS = 5;			// maximum number of frames to be skipped
	private final static int	FRAME_PERIOD = 1000 / MAX_FPS;	// the frame period
	private static boolean running;									// flag to hold game state 

	private SurfaceHolder surfaceHolder;	// Surface holder that can access the physical surface
	private GameView gameview;		// The actual view that handles inputs and draws to the surface

	public GameLoopThread(SurfaceHolder surfaceHolder, GameView gameview)
	{
		super();
		this.surfaceHolder = surfaceHolder;
		this.gameview = gameview;
	}
	
	public static void setRunning(boolean runningstate)
	{
		running = runningstate;
	}

	@Override
	public void run()
	{
		Canvas canvas;

		long beginTime;		// the time when the cycle begun
		long timeDiff;		// the time it took for the cycle to execute
		int sleepTime;		// ms to sleep (<0 if we're behind)
		int framesSkipped;	// number of frames being skipped 

		sleepTime = 0;
		
		
		while (running)
		{
			canvas = null;
			// try locking the canvas for exclusive pixel editing in the surface
			try
			{
				canvas = surfaceHolder.lockCanvas();
				synchronized (surfaceHolder)
				{
					beginTime = System.currentTimeMillis();
					framesSkipped = 0;	// resetting the frames skipped

					
					
					
					gameview.update();		// update game state 
					gameview.render(canvas);	// render state to the screen draws the canvas on the panel

					
					
					
					timeDiff  = System.currentTimeMillis() - beginTime;		// calculate how long did the cycle take
					sleepTime = (int)(FRAME_PERIOD - timeDiff);		// calculate sleep time

					if (sleepTime > 0)
					{
						// if sleepTime > 0 we're OK
						try
						{
							Thread.sleep(sleepTime);	// send the thread to sleep for a short period very useful for battery saving
						}
						catch (InterruptedException e) {}
					}


					while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS)
					{
						// we need to catch up
						gameview.update(); // update without rendering
						sleepTime += FRAME_PERIOD;	// add frame period to check if in next frame
						framesSkipped++;
					}
				}
			}
			finally
			{
				// in case of an exception the surface is not left in an inconsistent state
				if (canvas != null)
				{
					surfaceHolder.unlockCanvasAndPost(canvas);
				}
			}	// end finally
		}

	}

}



thanks for having reading this

Bests regards