Edit: Don’t use the code below! Use https://github.com/britzl/defold-metrics
The below code below is faulty because of the way the engine updates dt in some cases, you need to sample real world like like the Metrics version above does.
Simple easy to use module !
local M = {}
local frames = {}
local frame_sum = 0
local frame_index = 1
local MAX_SAMPLES = 10
M.average_ms = 0
M.raw_average_time = 0
M.fps = 0
function M.update(dt)
if frames[frame_index] then frame_sum = frame_sum - frames[frame_index] end
frame_sum = frame_sum + dt
frames[frame_index] = dt
frame_index = math.fmod(frame_index + 1, MAX_SAMPLES)
M.raw_average_time = frame_sum / MAX_SAMPLES
M.average_ms = math.floor(M.raw_average_time * 100000) / 100
M.fps = math.floor(1.0 / M.raw_average_time * 100) / 100
end
return M
fps.lua (535 Bytes)
local fps = require("utils.fps")
function update(self, dt)
fps.update(dt)
print(fps.average_ms .. " ms" , fps.fps .. " FPS", fps.raw_average_time)
end
Should you display FPS when users want to see it? Yes! It’s important to follow user expectations. But you should also show the ms too. Modern games are beginning to show both instead of only FPS which is good. For example, here is Fortnite
And as always required reading on this subject
https://www.mvps.org/directx/articles/fps_versus_frame_time.htm