diff --git a/src/de/teamteamteam/spacescooter/GameFrame.java b/src/de/teamteamteam/spacescooter/GameFrame.java index 5159e9f..6464c3f 100644 --- a/src/de/teamteamteam/spacescooter/GameFrame.java +++ b/src/de/teamteamteam/spacescooter/GameFrame.java @@ -31,6 +31,11 @@ public class GameFrame extends JFrame { */ private Screen superScreen; + /** + * Time in nanoseconds the last draw() call took. + */ + private long frameTime; + /** * 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 */ public void draw() { + long frameStart = System.nanoTime(); //Record time the draw call started. Graphics2D bufferedGraphics = null; do { // while bufferStrategy.contentsLost() do { // bufferStrategy.contentsRestored() @@ -113,6 +119,7 @@ public class GameFrame extends JFrame { this.bufferStrategy.show(); //Show the drawn image } while (this.bufferStrategy.contentsLost()); //Redraw in case the VolatileImage got lost 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_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); } + + /** + * Returns the time in nanoseconds the last draw() call took. + */ + public long getFrameTime() { + return this.frameTime; + } } \ No newline at end of file diff --git a/src/de/teamteamteam/spacescooter/screen/GameScreen.java b/src/de/teamteamteam/spacescooter/screen/GameScreen.java index 646ad8c..84cf67a 100644 --- a/src/de/teamteamteam/spacescooter/screen/GameScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/GameScreen.java @@ -52,6 +52,7 @@ public class GameScreen extends Screen { while (this.entityPaintIterator.hasNext()) { this.entityPaintIterator.next().paint(g); } + g.drawString("Frame Time: " + "", 20, 20); } @Override diff --git a/src/de/teamteamteam/spacescooter/sound/SoundSystem.java b/src/de/teamteamteam/spacescooter/sound/SoundSystem.java index ae53f4f..39ca395 100644 --- a/src/de/teamteamteam/spacescooter/sound/SoundSystem.java +++ b/src/de/teamteamteam/spacescooter/sound/SoundSystem.java @@ -117,6 +117,8 @@ public class SoundSystem { sourceDataLine.drain(); sourceDataLine.close(); sound.close(); + } catch (javax.sound.sampled.LineUnavailableException lue) { + System.err.println("Could not play sound: " + fSoundURL); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/de/teamteamteam/spacescooter/thread/PaintThread.java b/src/de/teamteamteam/spacescooter/thread/PaintThread.java index a9e4d5b..1c4ca09 100644 --- a/src/de/teamteamteam/spacescooter/thread/PaintThread.java +++ b/src/de/teamteamteam/spacescooter/thread/PaintThread.java @@ -14,6 +14,11 @@ public class PaintThread extends TimedThread { */ private final Runnable paintRunnable; + /** + * Internal pointer to GameFrame. + */ + private final GameFrame gameFrame; + /** * Constructor. Sets the name of the Thread and creates the paintRunnable. */ @@ -25,6 +30,7 @@ public class PaintThread extends TimedThread { gameFrame.draw(); } }; + this.gameFrame = gf; } /** @@ -34,4 +40,13 @@ public class PaintThread extends TimedThread { //Trigger redrawing the things. Important: AWT-Context needed here! 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(); + } } diff --git a/src/de/teamteamteam/spacescooter/thread/TimedThread.java b/src/de/teamteamteam/spacescooter/thread/TimedThread.java index c9b5a13..b674f33 100644 --- a/src/de/teamteamteam/spacescooter/thread/TimedThread.java +++ b/src/de/teamteamteam/spacescooter/thread/TimedThread.java @@ -7,8 +7,17 @@ package de.teamteamteam.spacescooter.thread; */ public abstract class TimedThread extends Thread { + /** + * Internal interval in which to trigger work(). + * Calculated based on setHz() + */ 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. * @@ -28,7 +37,7 @@ public abstract class TimedThread extends Thread { this.work(); long workDone = System.nanoTime(); //calculate time of work - long workTime = (workDone - workStart); + this.workTime = (workDone - workStart); long timeToWait = this.workInterval - workTime; long msToWait = timeToWait / 1000000; @@ -59,5 +68,13 @@ public abstract class TimedThread extends Thread { * Do the actual thread work in here. */ 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; + } }