This commit is contained in:
Pan 2019-05-20 23:23:02 +12:00
parent 86c090c1e6
commit 5a6e65b196
2 changed files with 31 additions and 12 deletions

View File

@ -318,6 +318,7 @@ public class AssetManager {
rockets = null; rockets = null;
mainThread.gridMap.reset(); mainThread.gridMap.reset();
mainThread.totalGameTime = 0;
solidObject.globalUniqID = 0; solidObject.globalUniqID = 0;
postProcessingThread.reset(); postProcessingThread.reset();

View File

@ -26,7 +26,8 @@ public class mainThread extends JFrame implements KeyListener, ActionListener, M
public static int frameInterval; public static int frameInterval;
public static int frameIndex; public static int frameIndex;
public static int gameFrame; public static int gameFrame;
public static long lastDraw; public static long lastDraw, lastFrameTime, frameTime, totalGameTime;
public static long delta;
public static int sleepTime; public static int sleepTime;
public static int framePerSecond, cpuUsage; public static int framePerSecond, cpuUsage;
public static double thisTime, lastTime; public static double thisTime, lastTime;
@ -250,8 +251,9 @@ public class mainThread extends JFrame implements KeyListener, ActionListener, M
if(!gamePaused) { if(!gamePaused) {
if(gameStarted) if(gameStarted)
gameFrame++; gameFrame++;
if(gameFrame > 0)
timeString = secondsToString((int)(gameFrame*0.025)); totalGameTime+=frameTime;
timeString = secondsToString((int)(totalGameTime/1000000000));
//handle user's interaction with game GUI //handle user's interaction with game GUI
if(gameFrame == 1 && gameStarted){ if(gameFrame == 1 && gameStarted){
@ -588,16 +590,31 @@ public class mainThread extends JFrame implements KeyListener, ActionListener, M
} }
public void regulateFramerate(){ public void regulateFramerate(){
if(frameIndex%35==0){ //if(frameIndex%35==0){
double thisTime = System.currentTimeMillis(); // double thisTime = System.currentTimeMillis();
framePerSecond = (int)(1000/((thisTime - lastTime)/35)); // framePerSecond = (int)(1000/((thisTime - lastTime)/35));
lastTime = thisTime; // lastTime = thisTime;
} //}
long sleeptime = frameInterval - (System.currentTimeMillis() - lastDraw); long currentTime = System.nanoTime();
if(sleeptime > 0)
frameTime = currentTime - lastFrameTime;
try { try {
Thread.sleep(sleeptime); long timeSpent = currentTime - lastDraw;
int sleeptime = (int)(frameInterval - timeSpent/1000000);
if(delta >= 1000000) {
sleeptime-=1;
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
@ -618,7 +635,8 @@ public class mainThread extends JFrame implements KeyListener, ActionListener, M
lastDraw=System.currentTimeMillis(); lastDraw=System.nanoTime();
lastFrameTime= currentTime;
} }
public static String secondsToString(int pTime) { public static String secondsToString(int pTime) {