Prevent concurrent modification errors by combining drawing and game logic into one thread.
This commit is contained in:
parent
3ab8de3a07
commit
287d8d3912
BIN
Meteo_Engine.jar
BIN
Meteo_Engine.jar
Binary file not shown.
@ -33,6 +33,7 @@ public class Meteo {
|
|||||||
public void run(){
|
public void run(){
|
||||||
while (true) {
|
while (true) {
|
||||||
long startTime = System.nanoTime();
|
long startTime = System.nanoTime();
|
||||||
|
runGameLoop();
|
||||||
p.repaint();
|
p.repaint();
|
||||||
long endTime = System.nanoTime();
|
long endTime = System.nanoTime();
|
||||||
long diff = endTime-startTime;
|
long diff = endTime-startTime;
|
||||||
@ -45,32 +46,7 @@ public class Meteo {
|
|||||||
int nanos = (int)(sleepTime-(((sleepTime)/1000000)*1000000));
|
int nanos = (int)(sleepTime-(((sleepTime)/1000000)*1000000));
|
||||||
//System.out.println("FRAME DRAWING: Sleeping for ("+millis+"ms,"+nanos+"ns) - "+(diff)+"ns");
|
//System.out.println("FRAME DRAWING: Sleeping for ("+millis+"ms,"+nanos+"ns) - "+(diff)+"ns");
|
||||||
DRAWTIME = (double)diff/1000000;
|
DRAWTIME = (double)diff/1000000;
|
||||||
Thread.sleep(millis,nanos);
|
f.setTitle("Game Loop: "+DRAWTIME+"ms");
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.start();
|
|
||||||
|
|
||||||
new Thread() {
|
|
||||||
public void run(){
|
|
||||||
while (true) {
|
|
||||||
long startTime = System.nanoTime();
|
|
||||||
runGameLoop();
|
|
||||||
long endTime = System.nanoTime();
|
|
||||||
long diff = endTime-startTime;
|
|
||||||
if (diff>TIMEPERTICK) { //Took longer than 1/60th of a second. No sleep.
|
|
||||||
System.err.println("Main Game Loop took longer than "+TIMEPERTICK+"ns to calculate ("+diff+"ns total)!");
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
long sleepTime = TIMEPERTICK - diff;
|
|
||||||
long millis = (sleepTime)/1000000;
|
|
||||||
int nanos = (int)(sleepTime-(((sleepTime)/1000000)*1000000));
|
|
||||||
//System.out.println("Sleeping for ("+millis+"ms,"+nanos+"ns) - "+(diff)+"ns");
|
|
||||||
GAMELOOPTIME = (double)diff/1000000;
|
|
||||||
f.setTitle("Game Loop: "+GAMELOOPTIME+"ms, Draw: "+DRAWTIME+"ms");
|
|
||||||
Thread.sleep(millis,nanos);
|
Thread.sleep(millis,nanos);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user