AdvancedTimer at = new AdvancedTimer();
long tick = 0;
at.start();
int fps = 60;
long ticksPerFrame = (long)( AdvancedTimer.getTicksPerSecond()/(double)fps );
final int FPS_AVERAGING = 6;
int totalFPS = 0;
int [] previousFPS = new int[FPS_AVERAGING];
int fpsIndex = 0;
int paints = 0;
long lastTime;
lastTime = System.currentTimeMillis();
String fpsString = "";
while (running && game.running() && !exiting) {
/* fps calculation */
if (System.currentTimeMillis()-lastTime > 1000) {
lastTime+=1000;
totalFPS+=paints - previousFPS[fpsIndex];
previousFPS[fpsIndex] = paints;
fpsIndex=(fpsIndex+1)%FPS_AVERAGING;
fpsString = Integer.toString(paints);
paints = 0;
}
/* end fps calculation */
drawing = true;
if (visible) {
Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
try { game.paintFrame(g); }
catch (Throwable e) {
err.inform(e, "game.paintFrame(g)");
crashed = true;
dispose();
}
paints++;
if (DRAWS_FPS) {
g.setColor(Color.black);
g.drawString(fpsString, 0, GAME_HEIGHT);
g.setColor(Color.white);
g.drawString(fpsString, 1, GAME_HEIGHT);
}
g.dispose();
showStrategy();
paints++;
drawing = false;
try {
game.manageObjects((double)(at.getClockTicks()-tick) / AdvancedTimer.getTicksPerSecond() * fps);
}
catch (Throwable e) {
err.inform(e, "manageObjects");
crashed = true;
dispose();
}
at.sleepUntil(tick + ticksPerFrame);
tick = at.getClockTicks();
}
else {
// setCursor(Cursor.getDefaultCursor());
// System.out.println("not visible");
try {
Thread.sleep(100);
tick = at.getClockTicks();
} catch (InterruptedException e) {
break;
}
}
}
For some reason, my games are stuck running at 120 fps in all modes of display even though you can see ‘fps’ is 60. Also, I’m somewhat worried about innacuracies in the number passed to manageObjects(). When I run my game at really small resolutions, the game speed seems inconsistent. One time I’ll run it and it’ll seem like it’s running at a normal speed. I run it again and the game speed looks like it’s cut in half. Does anyone see why that might be?