6th attempt at Meteos.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
meteo_engine/src/sig/Meteo.java

83 lines
3.3 KiB

package sig;
import javax.swing.JFrame;
public class Meteo {
public final static int SCREEN_WIDTH=640;
public final static int SCREEN_HEIGHT=640;
public static long FRAMECOUNT=0;
public static double DRAWTIME=0;
public static double GAMELOOPTIME=0;
public static JFrame f;
public static Board b;
public final static long TIMEPERTICK = 16666667l;
public static void runGameLoop() {
FRAMECOUNT++;
}
public static void main(String[] args) {
double[] val = {0,0,};
b = new Board(SCREEN_WIDTH/2,SCREEN_HEIGHT/2,16,16,8,14,0.05,1,4,1,val);
JFrame f = new JFrame("Meteo Engine");
Panel p = new Panel();
new Thread() {
public void run(){
while (true) {
long startTime = System.nanoTime();
p.repaint();
long endTime = System.nanoTime();
long diff = endTime-startTime;
if (diff>TIMEPERTICK) { //Took longer than 1/60th of a second. No sleep.
System.err.println("Frame Drawing 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("FRAME DRAWING: Sleeping for ("+millis+"ms,"+nanos+"ns) - "+(diff)+"ns");
DRAWTIME = (double)diff/1000000;
Thread.sleep(millis,nanos);
} 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);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}.start();
f.add(p);
f.setSize(SCREEN_WIDTH,SCREEN_HEIGHT);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}