Add a way to get the time a draw() or update() call took.

This commit is contained in:
Jan Philipp Timme 2014-11-11 14:54:46 +01:00
parent c0b62d59ac
commit 1c10c8bce0
5 changed files with 50 additions and 1 deletions

View File

@ -31,6 +31,11 @@ public class GameFrame extends JFrame {
*/ */
private Screen superScreen; private Screen superScreen;
/**
* Time in nanoseconds the last draw() call took.
*/
private long frameTime;
/** /**
* Default constructor. * Default constructor.
*/ */
@ -94,6 +99,7 @@ public class GameFrame extends JFrame {
* @see http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferStrategy.html * @see http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferStrategy.html
*/ */
public void draw() { public void draw() {
long frameStart = System.nanoTime(); //Record time the draw call started.
Graphics2D bufferedGraphics = null; Graphics2D bufferedGraphics = null;
do { // while bufferStrategy.contentsLost() do { // while bufferStrategy.contentsLost()
do { // bufferStrategy.contentsRestored() do { // bufferStrategy.contentsRestored()
@ -113,6 +119,7 @@ public class GameFrame extends JFrame {
this.bufferStrategy.show(); //Show the drawn image this.bufferStrategy.show(); //Show the drawn image
} while (this.bufferStrategy.contentsLost()); //Redraw in case the VolatileImage got lost } while (this.bufferStrategy.contentsLost()); //Redraw in case the VolatileImage got lost
Toolkit.getDefaultToolkit().sync(); //Tell the OS to update its graphics of the window. Toolkit.getDefaultToolkit().sync(); //Tell the OS to update its graphics of the window.
this.frameTime = System.nanoTime() - frameStart; //Update frameTime
} }
/** /**
@ -122,4 +129,11 @@ public class GameFrame extends JFrame {
bufferedGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); bufferedGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
bufferedGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); bufferedGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
} }
/**
* Returns the time in nanoseconds the last draw() call took.
*/
public long getFrameTime() {
return this.frameTime;
}
} }

View File

@ -52,6 +52,7 @@ public class GameScreen extends Screen {
while (this.entityPaintIterator.hasNext()) { while (this.entityPaintIterator.hasNext()) {
this.entityPaintIterator.next().paint(g); this.entityPaintIterator.next().paint(g);
} }
g.drawString("Frame Time: " + "", 20, 20);
} }
@Override @Override

View File

@ -117,6 +117,8 @@ public class SoundSystem {
sourceDataLine.drain(); sourceDataLine.drain();
sourceDataLine.close(); sourceDataLine.close();
sound.close(); sound.close();
} catch (javax.sound.sampled.LineUnavailableException lue) {
System.err.println("Could not play sound: " + fSoundURL);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -14,6 +14,11 @@ public class PaintThread extends TimedThread {
*/ */
private final Runnable paintRunnable; private final Runnable paintRunnable;
/**
* Internal pointer to GameFrame.
*/
private final GameFrame gameFrame;
/** /**
* Constructor. Sets the name of the Thread and creates the paintRunnable. * Constructor. Sets the name of the Thread and creates the paintRunnable.
*/ */
@ -25,6 +30,7 @@ public class PaintThread extends TimedThread {
gameFrame.draw(); gameFrame.draw();
} }
}; };
this.gameFrame = gf;
} }
/** /**
@ -34,4 +40,13 @@ public class PaintThread extends TimedThread {
//Trigger redrawing the things. Important: AWT-Context needed here! //Trigger redrawing the things. Important: AWT-Context needed here!
EventQueue.invokeLater(this.paintRunnable); EventQueue.invokeLater(this.paintRunnable);
} }
/**
* Return the workTime by asking the GameFrame for the time the
* draw() call took in nanoseconds.
*/
@Override
public long getWorkTime() {
return this.gameFrame.getFrameTime();
}
} }

View File

@ -7,8 +7,17 @@ package de.teamteamteam.spacescooter.thread;
*/ */
public abstract class TimedThread extends Thread { public abstract class TimedThread extends Thread {
/**
* Internal interval in which to trigger work().
* Calculated based on setHz()
*/
private long workInterval; private long workInterval;
/**
* Internal value representing runtime of the last work() call.
*/
private long workTime;
/** /**
* This method sets the actual working interval based on hz. * This method sets the actual working interval based on hz.
* *
@ -28,7 +37,7 @@ public abstract class TimedThread extends Thread {
this.work(); this.work();
long workDone = System.nanoTime(); long workDone = System.nanoTime();
//calculate time of work //calculate time of work
long workTime = (workDone - workStart); this.workTime = (workDone - workStart);
long timeToWait = this.workInterval - workTime; long timeToWait = this.workInterval - workTime;
long msToWait = timeToWait / 1000000; long msToWait = timeToWait / 1000000;
@ -60,4 +69,12 @@ public abstract class TimedThread extends Thread {
*/ */
public abstract void work(); public abstract void work();
/**
* Returns current value of workTime.
* Tells how many nanoseconds the last work() call needed to complete.
*/
public long getWorkTime() {
return this.workTime;
}
} }