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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| 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 (lastFpsTime >= 1000) { lastFpsTime = 0; fps = 0; }
logic(delta); render(g);
frameCount++; while ((System.currentTimeMillis() - startTime) / MILI_FRAME_LENGTH < frameCount) { Thread.yield(); }
Toolkit.getDefaultToolkit().sync(); } } |