Grrr, I have yet another question about my gameloop. I cant seem to make an fps counter that is “fluid.” It worked perfectly in my other programs, but now it doesnt anymore,
When I try to use a sleep timer it doesnt work, can someone here take a look at my gameloop, and tell me what is wrong? Also, I would like to know if it will work well.
This is based off the LWJGL kev glass tut:
public void frameRendering() {
SystemTimer.sleep(lastLoopTime+2-SystemTimer.getTime());
// work out how long its been since the last update, this
// will be used to calculate how far the entities should
// move this loop
long delta = SystemTimer.getTime() - lastLoopTime;
lastLoopTime = SystemTimer.getTime();
lastFpsTime += delta;
//fps++;
// update our FPS counter if a second has passed
if (lastFpsTime >= 100) {
renderer.setTitle(title+" (fps: "+fps+")");
lastFpsTime = 0;
fps = 0;
}
// START LOGIC \\
// END LOGIC \\
//boolean leftPressed = window.isKeyPressed(KeyEvent.VK_LEFT);
// if escape has been pressed, stop the game
if (renderer.isKeyPressed(KeyEvent.VK_ESCAPE)) {
System.exit(0);
}
currentFrameFps = (SystemTimer.getTime()-lastLoopTime);
if (currentFrameFps > 0) {
fpsLog[fpsFrameCount] = (1000000/currentFrameFps);
}
fpsFrameCount++;
fps = 0;
for (long fps1: fpsLog) {
fps += fps1;
}
fps /= fpsLog.length;
if (fpsFrameCount >= fpsLog.length) { fpsFrameCount=0; }
}
Game constructor:
public Cosmos() {
// create a window based on a chosen rendering method
renderer = ResourceFactory.get().createRenderer();
renderer.setResolution(Display.getDisplayMode().getWidth(),
Display.getDisplayMode().getHeight());
renderer.setRendererCallback(this);
renderer.setTitle(title);
fpsLog = new long[8]; // keep 8 integer digits of fps logging
lastLoopTime = SystemTimer.getTime();
renderer.beginRendering();
}
declarations:
private long lastLoopTime = SystemTimer.getTime();
/** The time since the last record of fps */
private long lastFpsTime = 0;
/** The recorded fps */
private int fps;
private long currentFrameFps;
private long[] fpsLog;
private int fpsFrameCount = 0;