i know this is a very common problem, but even the forum search did not result in an answer.
i digged my old arkanoid out of its grave to work it completely over. the very first thing to do is getting smooth animations.
please keep in mind, that this is a first attempt not a final construct:
public static final int FRAME_RATE = 85;
public static int FRAME_DELAY = 1000/FRAME_RATE;
public void run()
{
long frameStart = 0;
long frameCount = 0;
long totalElapsedTime = 0;
long elapsedTime = 0;
long reportedFramerate = 0;
long startTime;
// main game loop
while(isRunning)
{
frameStart = System.currentTimeMillis();
// drawing to the double buffer
updateGame();
renderGame();
// drawing double buffer to panel's screen
renderScreen();
elapsedTime = System.currentTimeMillis()-frameStart;
try
{
if(elapsedTime < Arkanoid.FRAME_DELAY)
{
Thread.sleep(Arkanoid.FRAME_DELAY - elapsedTime);
}
}
catch (InterruptedException e) {}
frameCount++;
totalElapsedTime+=(System.currentTimeMillis()-frameStart);
if(totalElapsedTime > 1000)
{
fps = reportedFramerate = (long) ((double)frameCount/ (double)totalElapsedTime*1000.0);
frameCount = 0;
totalElapsedTime = 0;
}
}
}
the run thread is stored in a jpanel class which gets added
to a jframe (test on 800x600, windowed).
according to infos i got from inet i tested the granularity of
win xp and came to the result of 10ms. therefore it sdould be possible to get nearly up to 1sec/ 10 ms fps.
but i only get 30fps.
rest of the program does nothing except drawing the fps doublebuffered to the panel.
who has stolen the rest of my fps ?