summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2013-12-05 12:24:22 +0100
committerPeter Wu <lekensteyn@gmail.com>2013-12-05 12:24:22 +0100
commit69a4f8f6dd638364ba8bf35dd70031148ac4b4ef (patch)
treefea8312ebbfaef25179d42177bfa6688afe8d8b6
parent0f4533e3df3a519bbe8c647b8b9acdf926e7da1a (diff)
download2iv60-robots-69a4f8f6dd638364ba8bf35dd70031148ac4b4ef.tar.gz
Add FPS counter.
-rw-r--r--src/RobotRace.java58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/RobotRace.java b/src/RobotRace.java
index 5d5a99c..ba042af 100644
--- a/src/RobotRace.java
+++ b/src/RobotRace.java
@@ -4,15 +4,18 @@ import java.awt.Desktop;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
+import java.awt.Window;
import java.io.IOException;
import javax.media.opengl.GL;
import static javax.media.opengl.GL2.*;
import javax.swing.UIManager;
import robotrace.Base;
+import robotrace.MainFrame;
import robotrace.Vector;
import static java.lang.Math.*;
import java.net.URI;
import java.net.URISyntaxException;
+import java.util.Locale;
/**
* Handles all of the RobotRace graphics functionality,
@@ -96,6 +99,21 @@ public class RobotRace extends Base {
private boolean lightingEnabled = true;
/**
+ * The main window for this program.
+ */
+ private MainFrame mainWindow;
+
+ /**
+ * Frames per second monitor: total frames in the past period.
+ */
+ private float frames;
+ /**
+ * Frames per second monitor: the timestamp in milliseconds for the begin of
+ * the previous period (or 0 if there is no previous period.)
+ */
+ private long last_frame_time;
+
+ /**
* Constructs this robot race by initializing robots, camera, track, and
* terrain.
*/
@@ -104,6 +122,16 @@ public class RobotRace extends Base {
BetterBase.setGLU(glu);
BetterBase.setGLUT(glut);
+ // find the MainFrame
+ for (Window w : Window.getWindows()) {
+ if (w instanceof MainFrame) {
+ mainWindow = (MainFrame) w;
+ }
+ }
+ if (mainWindow == null) {
+ System.err.println("No window found, FPS will be unavailable!");
+ }
+
// Create a new array of four robots
robots = new Robot[4];
@@ -187,10 +215,40 @@ public class RobotRace extends Base {
}
/**
+ * Updates the title with frames per second.
+ */
+ private void updateFPS() {
+ // just to be sure, avoid accessing the window if unavailable
+ if (mainWindow == null) {
+ return;
+ }
+
+ long now = System.currentTimeMillis();
+ float timediff = (now - last_frame_time) / 1000.0f;
+ frames++;
+ // periodically update the frame time
+ if (timediff >= 1.0f) {
+ // prevent inaccurate FPS on start
+ if (last_frame_time != 0) {
+ float fps = frames / timediff;
+ // display FPS with 2 decimals at most
+ mainWindow.setTitle(String.format(Locale.getDefault(), "RobotRace (FPS %.2f)", fps));
+ }
+ last_frame_time = now;
+ frames = 0;
+ }
+ }
+
+ /**
* Configures the viewing transform.
*/
@Override
public void setView() {
+ // due to limitations of the Base class (unable to override display
+ // function), the FPS update is done in the first accessible function
+ // (here, in setView)
+ updateFPS();
+
// Select part of window.
gl.glViewport(0, 0, gs.w, gs.h);