I never ever used .sleep(), always .yield()
and its just not a code issue: this behavior only occurs on the DirectX pipeline. Using the OpenGL pipeline forces even a windowed game to VSync when using bufferedstrategy.
in that case, when using opengl, I obviously don't even need my game slow down class; since its automatically vsynced.
no screentearing ever on opengl. obviously that pipeline has so many problems...
this is my experience with java2D + pipelines... my engine is switching to lwjgl soon; I hope it will be gone alltogether.
here is how I do the slow down: (btw Awt.Toolkit.sync() results in abysmal slowdowns)
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
| public class FrameSkipper { private int fps; private long timeThen;
public FrameSkipper(int frameRate) { fps = frameRate; timeThen = System.nanoTime(); }
public void changeFPS(int frameRate) { fps = frameRate; }
public void sync() { long gapTo = 1000000000L / fps + timeThen; long timeNow = System.nanoTime();
while (gapTo > timeNow) { Thread.yield(); timeNow = System.nanoTime(); }
timeThen = timeNow; } } |
so you initialize with the FPS you want, and then just call sync every frame.
also note: I currently do not use any threads; I would like to maintain a frame based synced game, which should be entirely possible (like your standard C++ game)