summaryrefslogtreecommitdiffstats
path: root/src/demos/util/FPSCounter.java
blob: b0ca2006a83aae0b2b71447620ff4a41f3d6eed2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed or intended for use
 * in the design, construction, operation or maintenance of any nuclear
 * facility.
 * 
 * Sun gratefully acknowledges that this software was originally authored
 * and developed by Kenneth Bradley Russell and Christopher John Kline.
 */

package demos.util;

import java.awt.Font;
import java.awt.geom.*;
import java.text.*;

import javax.media.opengl.*;
import com.sun.opengl.util.j2d.*;

/** A simple class which uses the TextRenderer to provide an FPS
    counter overlaid on top of the scene. */

public class FPSCounter {
  // Placement constants
  public static final int UPPER_LEFT  = 1;
  public static final int UPPER_RIGHT = 2;
  public static final int LOWER_LEFT  = 3;
  public static final int LOWER_RIGHT = 4;

  private int textLocation = LOWER_RIGHT;
  private GLDrawable drawable;
  private TextRenderer renderer;
  private DecimalFormat format = new DecimalFormat("####.00");
  private int frameCount;
  private long startTime;
  private String fpsText;
  private int fpsMagnitude;
  private int fpsWidth;
  private int fpsHeight;
  private int fpsOffset;
  
  /** Creates a new FPSCounter with the given font size. An OpenGL
      context must be current at the time the constructor is called.

      @param drawable the drawable to render the text to
      @param textSize the point size of the font to use
      @throws GLException if an OpenGL context is not current when the constructor is called
  */
  public FPSCounter(GLDrawable drawable, int textSize) throws GLException {
    this(drawable, new Font("SansSerif", Font.BOLD, textSize));
  }

  /** Creates a new FPSCounter with the given font. An OpenGL context
      must be current at the time the constructor is called.

      @param drawable the drawable to render the text to
      @param font the font to use
      @throws GLException if an OpenGL context is not current when the constructor is called
  */
  public FPSCounter(GLDrawable drawable, Font font) throws GLException {
    this(drawable, font, true, true);
  }

  /** Creates a new FPSCounter with the given font and rendering
      attributes. An OpenGL context must be current at the time the
      constructor is called.

      @param drawable the drawable to render the text to
      @param font the font to use
      @param antialiased whether to use antialiased fonts
      @param useFractionalMetrics whether to use fractional font
      @throws GLException if an OpenGL context is not current when the constructor is called
  */
  public FPSCounter(GLDrawable drawable,
                    Font font,
                    boolean antialiased,
                    boolean useFractionalMetrics) throws GLException {
    this.drawable = drawable;
    renderer = new TextRenderer(font, antialiased, useFractionalMetrics);
  }

  /** Gets the relative location where the text of this FPSCounter
      will be drawn: one of UPPER_LEFT, UPPER_RIGHT, LOWER_LEFT, or
      LOWER_RIGHT. Defaults to LOWER_RIGHT. */
  public int getTextLocation() {
    return textLocation;
  }

  /** Sets the relative location where the text of this FPSCounter
      will be drawn: one of UPPER_LEFT, UPPER_RIGHT, LOWER_LEFT, or
      LOWER_RIGHT. Defaults to LOWER_RIGHT. */
  public void setTextLocation(int textLocation) {
    if (textLocation < UPPER_LEFT || textLocation > LOWER_RIGHT) {
      throw new IllegalArgumentException("textLocation");
    }
    this.textLocation = textLocation;
  }

  /** Changes the current color of this TextRenderer to the supplied
      one, where each component ranges from 0.0f - 1.0f. The alpha
      component, if used, does not need to be premultiplied into the
      color channels as described in the documentation for {@link
      Texture Texture}, although premultiplied colors are used
      internally. The default color is opaque white.

      @param r the red component of the new color
      @param g the green component of the new color
      @param b the blue component of the new color
      @param alpha the alpha component of the new color, 0.0f =
        completely transparent, 1.0f = completely opaque
      @throws GLException If an OpenGL context is not current when this method is called
  */
  public void setColor(float r, float g, float b, float a) throws GLException {
    renderer.setColor(r, g, b, a);
  }

  /** Updates the FPSCounter's internal timer and counter and draws
      the computed FPS. It is assumed this method will be called only
      once per frame.
  */
  public void draw() {
    if (startTime == 0) {
      startTime = System.currentTimeMillis();
    }

    if (++frameCount >= 100) {
      long endTime = System.currentTimeMillis();
      float fps = 100.0f / (float) (endTime - startTime) * 1000;
      recomputeFPSSize(fps);
      frameCount = 0;
      startTime = System.currentTimeMillis();

      fpsText = "FPS: " + format.format(fps);
    }

    if (fpsText != null) {
      renderer.beginRendering(drawable.getWidth(), drawable.getHeight());
      // Figure out the location at which to draw the text
      int x = 0;
      int y = 0;
      switch (textLocation) {
        case UPPER_LEFT:
          x = fpsOffset;
          y = drawable.getHeight() - fpsHeight - fpsOffset;
          break;

        case UPPER_RIGHT:
          x = drawable.getWidth() - fpsWidth - fpsOffset;
          y = drawable.getHeight() - fpsHeight - fpsOffset;
          break;

        case LOWER_LEFT:
          x = fpsOffset;
          y = fpsOffset;
          break;

        case LOWER_RIGHT:
          x = drawable.getWidth() - fpsWidth - fpsOffset;
          y = fpsOffset;
          break;
      }

      renderer.draw(fpsText, x, y);
      renderer.endRendering();
    }
  }

  private void recomputeFPSSize(float fps) {
    String fpsText;
    int fpsMagnitude;
    if (fps >= 10000) {
      fpsText = "10000.00";
      fpsMagnitude = 5;
    } else if (fps >= 1000) {
      fpsText = "1000.00";
      fpsMagnitude = 4;
    } else if (fps >= 100) {
      fpsText = "100.00";
      fpsMagnitude = 3;
    } else if (fps >= 10) {
      fpsText = "10.00";
      fpsMagnitude = 2;
    } else {
      fpsText = "9.00";
      fpsMagnitude = 1;
    }

    if (fpsMagnitude > this.fpsMagnitude) {
      Rectangle2D bounds = renderer.getBounds("FPS: " + fpsText);
      fpsWidth = (int) bounds.getWidth();
      fpsHeight = (int) bounds.getHeight();
      fpsOffset = (int) (fpsHeight * 0.5f);
      this.fpsMagnitude = fpsMagnitude;
    }
  }
}