This is the base game loop I’m using for a game for J4K 2013. But it is eating 100% of my CPU. I’m running on Ubuntu 32 bit and Java 6. Any tips how can I avoid eating 100% of CPU? Thanks!
int FPS = 60;
int MILI_FRAME_LENGTH = 1000 / FPS;
int frameCount = 0;
while (true) {
long startTime = System.currentTimeMillis();
long lastLoopTime = System.currentTimeMillis();
long lastFpsTime = 0;
long fps = 0;
while (true) {
long delta = System.currentTimeMillis() - lastLoopTime;
lastLoopTime = System.currentTimeMillis();
lastFpsTime += delta;
fps++;
// If more than 1000ms (1s) has passed reset the fps count.
if (lastFpsTime >= 1000) {
lastFpsTime = 0;
fps = 0;
}
logic(delta);
render(g);
frameCount++;
while ((System.currentTimeMillis() - startTime)
/ MILI_FRAME_LENGTH < frameCount) {
Thread.yield();
}
// Ensures that the display is up-to-date. This keeps the
// animation smoother for the Linux OS.
Toolkit.getDefaultToolkit().sync();
}
}