Skip to main content

How to check the actual frame rate (FPS) with Corona SDK

EDIT: This example has a follow-up in this blog post containing improved and object-oriented source code for the frame rate calculator.

In Corona SDK you set the target frame rate in config.lua to either 30 or 60 FPS (Frames Per Second). Then during application execution you can get this value through display.fps. However, remember that this is only the target frame rate, and the actual frame rate is something completely different. The actual FPS will fluctuate during program execution and can be significantly lower than the target FPS in case your app has too much to handle at any given moment.

I couldn't find any built-in way to get the actual FPS, so I decided to roll my own very simple solution to handle this. My frame rate calculator is just a Lua function that sets up an enter frame listener which then counts frames and calculates FPS based on time passed between the frames. The function startFrameRateCalculator accepts a single argument which is a callback function with a single parameter: the actual FPS.

Below is the source code for a little demo of this that spawns lots of colorful rectangles, moving from the center of the display towards the edges. The FPS value is displayed in a label, and the label is updated in the callback function injected to the startFrameRateCalculator function. If you run the program it should look something like this:

Actual FPS calculation demo
This was just a quick hack to test the idea, so feel free to improve the source code anyway you want. 

Comments