From 69a4f8f6dd638364ba8bf35dd70031148ac4b4ef Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Thu, 5 Dec 2013 12:24:22 +0100 Subject: Add FPS counter. --- src/RobotRace.java | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'src') 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, @@ -95,6 +98,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]; @@ -186,11 +214,41 @@ public class RobotRace extends Base { gl.glEnable(GL_NORMALIZE); } + /** + * 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); -- cgit v1.2.1