Constant Frame Rate

Hello again, I have another question.

Some small devices are faster than others, this will affect my game fps or it is handled (somehow) by J2ME?.

You have to take care of it yourself. Aim for 10/15 fps or something like that - 30/60 fps is typically unrealistic.

…thanks again

I don’t know how :-.

With J2ME I use System.nanoSeconds(), but J2ME doesn’t have it.
Any example J2ME game loop ???

Thanks!

System.currentTimeMillis();

I tried it, but I expect 16fps and I get up to 30fps :-.

Well, I suspect its because this resturns milis, not nanos… :frowning:

u can use System.currentTimeMillis() to get current time in million seconds.
then define your fps(10 for example, that is 100 million seconds per frame)
in your main loop of your game as follows

run ()
{
while(loop)
{
_frameCoheranceTimer = System.currentTimeMillis(); // this is your timer

         gamelogic();    // your logic

         repaint();   // paint
         serviceRepaints();

         // limit your fps here
        while( (System.currentTimeMillis() - _frameCoheranceTimer) < 100)
               Thread.yeild();
    }

}

That run loop will blow up on more phones than you can possibly imagine :slight_smile:

I use this game loop:

inf FPS=20;
int fps_delay=1000/FPS.
long time=System.currentTimeMillis(),elapsed=0,fps_error=0;
while (thread!=null)
{
	elapsed=System.currentTimeMillis()-time;
	time=System.currentTimeMillis();
	// Update Game Logic.
	update((int)elapsed);
	// Repaint.
	repaint();
	serviceRepaints();
	// Sleep.
	try
	{
		long t=System.currentTimeMillis();
		elapsed=fps_delay-(t-time)-fps_error;
		if (elapsed>0)
		{
			thread.sleep(elapsed);
			fps_error=(System.currentTimeMillis()-t)-elapsed;
		}
		else
		{
			thread.yield();
			fps_error=0;
		}
	}
	catch (Exception ex)
	{
	}
}

I use fps_error to correct “time errors” generated by thread.sleep(), without it the framerate is a bit worse if the sleep has not so much accuracy.
Maybe thread.yield() could be replaced by thread.sleep(1) to be sure it permit others threads execute, possibly we will lost some fps, but it will work better.

I use it in my qames for a lot of phone models, i think it works well, but suggestions are appreciated!! :wink:

Best regards