This commit is contained in:
Pan Hu 2019-05-21 10:57:54 +12:00
parent 051d1e56a7
commit 66a5a91b48

View File

@ -28,7 +28,7 @@ public class mainThread extends JFrame implements KeyListener, ActionListener, M
public static int gameFrame; public static int gameFrame;
public static long lastDraw; public static long lastDraw;
public static long delta; public static long delta;
public static int sleepTime; public static long sleepTime;
public static int framePerSecond, cpuUsage; public static int framePerSecond, cpuUsage;
public static double thisTime, lastTime; public static double thisTime, lastTime;
public static boolean JavaRTSLoaded; public static boolean JavaRTSLoaded;
@ -589,48 +589,19 @@ public class mainThread extends JFrame implements KeyListener, ActionListener, M
} }
public void regulateFramerate(){ public void regulateFramerate(){
//if(frameIndex%35==0){ //frameInterval is a constant 25 milliseconds.
// double thisTime = System.currentTimeMillis(); sleepTime = frameInterval - (System.currentTimeMillis()-lastDraw);
// framePerSecond = (int)(1000/((thisTime - lastTime)/35));
// lastTime = thisTime;
//}
long currentTime = System.nanoTime();
try { try {
long timeSpent = currentTime - lastDraw; if(sleepTime > 0)
Thread.sleep(sleepTime);
int sleeptime = (int)(frameInterval - timeSpent/1000000);
if(delta >= 1000000) {
sleeptime--;
delta-=1000000;
}
if(sleeptime > 0)
Thread.sleep(sleeptime);
delta+=timeSpent%1000000;
} catch (InterruptedException e1) { } catch (InterruptedException e1) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e1.printStackTrace(); e1.printStackTrace();
} }
/*sleepTime = 0;
while(System.currentTimeMillis()-lastDraw<frameInterval){
try {
Thread.sleep(1);
sleepTime++;
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}*/
lastDraw=System.currentTimeMillis();
lastDraw=System.nanoTime();
} }
public static String secondsToString(int pTime) { public static String secondsToString(int pTime) {