I took your mainloop and made a testrun, its very simple left-to-right animation. I have tried everything(?) to make it smooth as silk in a windowed Java application still not eating CPU100%. I think its close to impossible or I just can’t.
I can cleary see occasional jumps and small tearing.
//http://www.java-gaming.org/index.php/topic,19971.0.html
import java.util.*;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferStrategy;
import java.awt.DisplayMode; // for full-screen mode
public class GameLoop1 implements KeyListener {
Frame mainFrame;
private static final long NANO_IN_MILLI = 1000000L;
long desiredFPS = 60;
long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
long beginLoopTime;
long endLoopTime;
long currentPhysicTime;
long lastPhysicTime;
long fps;
long frameCounter;
long lastFpsTime;
Rectangle2D rect;
public GameLoop1(Map<String,String> args, GraphicsDevice device) {
try {
// Setup the frame
GraphicsConfiguration gc = device.getDefaultConfiguration();
mainFrame = new Frame(gc);
mainFrame.setUndecorated(true);
mainFrame.setIgnoreRepaint(true);
mainFrame.setVisible(true);
mainFrame.setSize(640, 480);
//mainFrame.setLocationRelativeTo();
mainFrame.setLocation(100,100);
mainFrame.createBufferStrategy(2);
mainFrame.addKeyListener(this);
if ("true".equalsIgnoreCase(args.get("fullscreen"))) {
device.setFullScreenWindow(mainFrame);
device.setDisplayMode(new DisplayMode(640, 480, 8, DisplayMode.REFRESH_RATE_UNKNOWN));
}
// Cache the buffer strategy and create a rectangle to move
BufferStrategy bufferStrategy = mainFrame.getBufferStrategy();
rect = new Rectangle2D.Float(0,100,64,64);
// loop initialization
currentPhysicTime = System.nanoTime();
lastFpsTime = currentPhysicTime;
// Main loop
while(true) {
beginLoopTime = System.nanoTime();
// Synchronise with the display hardware. Note that on
// Windows Vista this method may cause your screen to flash.
// If that bothers you, just comment it out.
//Toolkit.getDefaultToolkit().sync();
// **1) execute drawing
Graphics g = bufferStrategy.getDrawGraphics();
drawScreen(g);
g.dispose();
// Flip the buffer
if( !bufferStrategy.contentsLost() )
bufferStrategy.show();
// **2) execute physics
lastPhysicTime = currentPhysicTime;
currentPhysicTime = System.nanoTime();
updateWorld(currentPhysicTime - lastPhysicTime);
calculateFramesPerSecond();
// **3) execute logic
// executeLogic(); // handle keyboard and other inputs
endLoopTime = System.nanoTime();
adjustSpeed();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
device.setFullScreenWindow(null);
}
}
private void adjustSpeed() {
long deltaLoop = endLoopTime - beginLoopTime;
if(deltaLoop > desiredDeltaLoop) {
// do nothing. We are alreadyLate
System.out.println("Late, do not sleep");
} else {
try {
Thread.sleep((desiredDeltaLoop - deltaLoop) / NANO_IN_MILLI);
} catch (InterruptedException ex) { }
}
}
private void updateWorld(long elapsedTime) {
// speed: 150 pixels per second
double xMov = (140f/(NANO_IN_MILLI*1000)) * elapsedTime;
//double xMov = 2.0;
rect.setRect(rect.getX() + xMov, 100, 64, 64);
if( rect.getX() > mainFrame.getWidth() )
rect.setRect(-rect.getWidth(), 100, 64, 64);
}
private void drawScreen(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, mainFrame.getWidth(), mainFrame.getHeight());
g.setColor(Color.WHITE);
g.drawString("FPS: " + fps, 0, 17);
g.setColor(Color.RED);
g.fillRect((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight());
}
private void calculateFramesPerSecond() {
if( currentPhysicTime - lastFpsTime >= NANO_IN_MILLI*1000 ) {
fps = frameCounter;
frameCounter = 0;
lastFpsTime = currentPhysicTime;
}
frameCounter++;
}
public void keyPressed(KeyEvent e) {
if( e.getKeyCode() == KeyEvent.VK_ESCAPE ) {
System.exit(0);
}
}
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
public static void main(String[] args) {
try {
Map<String,String> mapArgs = parseArguments(args);
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = env.getDefaultScreenDevice();
new GameLoop1(mapArgs, device);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Parse commandline arguments, each parameter is a name-value pair.
* Example: java.exe MyApp "key1=value1" "key2=value2"
*/
private static Map<String,String> parseArguments(String[] args) {
Map<String,String> mapArgs = new HashMap<String,String>();
for(int idx=0; idx < args.length; idx++) {
String val = args[idx];
int delimIdx = val.indexOf('=');
if (delimIdx < 0) {
mapArgs.put(val, null);
} else if (delimIdx == 0) {
mapArgs.put("", val.substring(1));
} else {
mapArgs.put(
val.substring(0, delimIdx).trim(),
val.substring(delimIdx+1)
);
}
}
return mapArgs;
}
}
edit: another issue, my FPS counter clearly does not work. target fps is 60 but my debug prints +90fps all the time.