ComputeFps
A utility class for calculating frames per second (FPS) using a rolling average.
ComputeFps maintains a circular buffer of recent frame times and calculates the average FPS over a configurable number of frames. This provides a more stable FPS reading than calculating from individual frame deltas.
Features
- Rolling Average: Smooths out FPS spikes and dips
- Configurable Window: Choose how many frames to average
- Capped Maximum: FPS capped at 999 to prevent display issues
- Lightweight: Minimal memory overhead with fixed buffer
Usage Example
var fpsCounter = new ComputeFps(30); // Average over 30 frames
// In your update loop
function update(delta:Float) {
fpsCounter.addFrame(delta);
trace('Current FPS: ' + fpsCounter.fps);
}
Instance Members
fps: Int
The current calculated frames per second.
This value is updated each time addFrame() is called and represents the average FPS over the last 'size' frames. Read-only from outside the class.
Range: 0-999
Records a frame and updates the FPS calculation.
Call this method once per frame with the time elapsed since the last frame. The FPS value is automatically updated based on the rolling average of recent frame times.
Name | Type | Description |
---|---|---|
delta |
Float | Time elapsed since last frame in seconds (e.g., 0.016 for 60 FPS) * haxe // In your game loop var lastTime = Timer.now(); * function update() { var currentTime = Timer.now(); var delta = currentTime - lastTime; lastTime = currentTime; * fpsCounter.addFrame(delta); // fpsCounter.fps now contains updated FPS } |
Creates a new FPS calculator.
Name | Type | Default | Description |
---|---|---|---|
size |
Int | 10 |
Number of frames to use for the rolling average. Larger values provide smoother results but slower response to FPS changes. Default: 10 * haxe // Quick response (10 frames) var fpsFast = new ComputeFps(10); * // Smooth reading (60 frames) var fpsSmooth = new ComputeFps(60); |
Private Members
Circular buffer storing recent frame delta times. Size is fixed at construction.
index: Int
Current position in the circular buffer. Wraps around when reaching the buffer size.
size: Int
Number of frames to average over. Larger values provide more stable readings.