I really hope I’m doing something wrong 
I do the following in my game loop
update()
render()
Thread.yeild()
then I check the elapsed time and this results in 0 frames over time (most of the time)
Then I do the frame rate capping loop.
It doesnt matter how I do this. I have tried several methods.
- I used the normal Gage way of using the sleep method you have that loops on a yeild
- I wrote my own that loops on nothing, an empty while loop. (thats why there is a yeild just after the render)
The results were the same
After the timer loop I check the elapsed time again so I can see how long that took
I also count frames that are under time and over time as a result of rendering
and also as a result of timing.
I know the system.out can cause trouble here. So I only print to System.out once in 400 frames. I collect The max timer results and print them, I also print the current results for the frame, so after doing this for a while you can see what a typical frame vs max frame is doing
while(running)
{
lastTime = gageTimer.getClockTicks();
timeModifier = 1;
//allows for dynamically altering the FPSTarget
ticksPerFrame=ticksPerSecond/(int)FPSTarget;
while(!paused)
{
update(timeModifier);
render2();
Thread.yield();
elapsedTime = (gageTimer.getClockTicks() - lastTime);
if (elapsedTime < ticksPerFrame)
{
//gageTimer.sleep(ticksPerFrame - elapsedTime); //use this
while(gageTimer.getClockTicks() < (lastTime + ticksPerFrame) ) // or this
{
//do nothing
}
elapse2 = gageTimer.getClockTicks()- lastTime;
if (elapse2 > (ticksPerFrame * 1.1)) // frame is now more than 10% over time due to timer
{
elapseBegin = elapsedTime; // this records the last overtime frame before printing
elapseAfter = elapse2; // this records the last overtime frame before printing
badCount1++;
}
else //frame is ok after timer
{
goodElapseBegin = elapsedTime; // this records the last good frame before printing
goodElapseAfter = elapse2; // this records the last good frame before printing
}
underTime++; //counts frames that should be undertime
}
else
{
overTime++; //counts frames that are overtime due to update/render/yeild
}
time = gageTimer.getClockTicks();
timeModifier = ((float)(time - lastTime)) / (float)ticksPerFrame;
//timeModifier = 1;
lastTime = time;
}
//Game PAUSED, Sleep to stop using CPU at 100%
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{}
}
gameLoopStopped=true;
}
Portion of my render method
//This saves the highest timeModifier
if (timeModifier > 1.1f)
{
if(timeModifier>maxAnimM)
{
maxAnimM = timeModifier;
}
}
//This records the highest elapsed time just after the timer of a bad frame caused by the timer
if(elapse2>maxElapse2)
{
maxElapse2=elapse2;
}
//this records the highest elapsed time on a good frame
if(elapsedTime>maxElapsed)
{
maxElapsed = elapsedTime;
}
if (i > 400) // I increment the x and y directions by i, so the box moves diagonal
{
forward = false;
System.out.println("badCount1: "+badCount1+" | total frames: "+count+" | over: "+overTime+" | under: "+underTime+" | MaxAnim: "+maxAnimM);
System.out.println("elapseBegin: "+elapseBegin+" | elapseAfter: "+elapseAfter+" | max Elapse2: "+maxElapse2);
System.out.println("goodElapseBegin: "+goodElapseBegin+" | goodElapseAfter: "+goodElapseAfter+" | max elapsedTime: "+maxElapsed);
System.out.println();
count=0;
badCount=0;
badCount1=0;
overTime =0;
underTime=0;
maxAnimM=0;
maxElapsed=0;
maxElapse2=0;
elapseBegin = 0;
elapseAfter =0;
//loopCount = 0;
loops =0;
goodElapseBegin = 0;
goodElapseAfter =0;
//loopCount = 0;
goodLoops =0;
}
if (i < 60)
{
forward = true;
}
Output
badCount1: 11 | total frames: 404 | over: 0 | under: 404 | MaxAnim: 1.6024841
elapseBegin: 3167 | elapseAfter: 31322 | max Elapse2: 31860
goodElapseBegin: 3312 | goodElapseAfter: 19896 | max elapsedTime: 10045
badCount1: 11 | total frames: 403 | over: 0 | under: 403 | MaxAnim: 1.8622146
elapseBegin: 3248 | elapseAfter: 31608 | max Elapse2: 37025
goodElapseBegin: 3311 | goodElapseAfter: 19898 | max elapsedTime: 10498
badCount1: 11 | total frames: 404 | over: 0 | under: 404 | MaxAnim: 1.5861913
elapseBegin: 3240 | elapseAfter: 30867 | max Elapse2: 31536
goodElapseBegin: 3165 | goodElapseAfter: 19899 | max elapsedTime: 9811
badCount1: 11 | total frames: 403 | over: 0 | under: 403 | MaxAnim: 1.8648295
elapseBegin: 3426 | elapseAfter: 31263 | max Elapse2: 37072
goodElapseBegin: 3461 | goodElapseAfter: 19895 | max elapsedTime: 9947
Sometimes the badCount varies a bit, this run just happened to be elevens.
Sometimes the over variable is 1-3 when the yeild kicks in longer or the hard drive runs
The over variable (update/render/yeild) numbers increase when running Jbuilder or a browser. The numbers above are after a reboot with no programs running (other than the startup stuff).
The max elapse2, a bad frame from the timer is (mostly) consistantly between 31,000 - 37,000 ticks, regardless of other running programs.
Yields (just after render) due to other programs vary quite a bit.
I did go through my msconfig>startup last year and cleared out a few things and checked again this morning, but saw nothing bad in there. I did shut off many taskbar programs and even shut off my cable modem, but nothing changed.
I can also remark out all but one System.out call and it still reports the same error.
So I’ll just assume my machine is doing something odd, deep down inside 