Skip to content

Instantly share code, notes, and snippets.

@rannerboy
Last active October 26, 2019 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rannerboy/5553cc6b2dc6024484f5cd594e214e17 to your computer and use it in GitHub Desktop.
Save rannerboy/5553cc6b2dc6024484f5cd594e214e17 to your computer and use it in GitHub Desktop.
FPS Calculator for Corona SDK
--[[
This is a simple example showing how to use the FPSCalculator class
Markus Ranner 2016
--]]
local FPSCalculator = require("FPSCalculator")
local fpsCalculator = FPSCalculator.new(
function(fps, fpsRatio)
print("fps = " .. fps .. ", ratio = " .. fpsRatio)
-- Do things here to handle a drop in frame rate, such as disable animations
end,
{
fpsRatioWarningThreshold = 0.95,
timeBetweenCalculationsMs = 10000,
}
)
fpsCalculator:start()
--[[
Frame rate calculator class, used to find out the actual frame rate of a game and react to drops in FPS.
Markus Ranner 2016
--]]
local FPSCalculator = {}
FPSCalculator.__index = FPSCalculator
--- Private functions --------------
function FPSCalculator:calculateFPS()
self.frameCounter = self.frameCounter + 1
local currentTimestampMs = system.getTimer()
if (not self.lastCalculationTimestampMs) then
self.lastCalculationTimestampMs = currentTimestampMs
end
local deltaTimeMs = currentTimestampMs - self.lastCalculationTimestampMs
-- Calculate average fps for the specified time period
if ( deltaTimeMs >= self.timeBetweenCalculationsMs ) then
local fps = self.frameCounter / (deltaTimeMs / 1000)
self.frameCounter = 0
self.lastCalculationTimestampMs = currentTimestampMs
local fpsRatio = fps/display.fps
if ((not self.fpsRatioWarningThreshold) or (fpsRatio <= self.fpsRatioWarningThreshold)) then
self.callback(fps, fpsRatio)
end
end
end
--- Public functions --------------
function FPSCalculator:start()
if (self.isStarted) then
return
end
-- Reset some state
self.isStarted = true
self.frameCounter = 0
self.lastCalculationTimestampMs = nil
-- Setup the calculation function
self.calculationFunction = function() self:calculateFPS() end
Runtime:addEventListener("enterFrame", self.calculationFunction)
end
function FPSCalculator:stop()
self.isStarted = false,
Runtime:removeEventListener("enterFrame", self.calculationFunction)
end
--[[
callback(fps, fpsRatio) - Required. A callback function with params:
fps - The average frame rate during the last calculation period
fpsRatio - The ratio between actual fps and target fps (fps/display.fps)
params - Optional table of configuration params. The following params are allowed and are optional.
timeBetweenCalculationsMs - How often to calculate fps. Default = 1000 ms.
fpsRatioWarningThreshold - A number between 0-1. If specified, the callback will only be called if fpsRatio drops below this threshold number.
--]]
function FPSCalculator.new(callback, params)
params = params or {}
local newCalculator = {
isStarted = false,
frameCounter = 0,
lastCalculationTimestampMs = nil,
calculationFunction = nil, -- Will be set when start() is called
callback = callback,
timeBetweenCalculationsMs = params.timeBetweenCalculationsMs or 1000,
fpsRatioWarningThreshold = params.fpsRatioWarningThreshold,
}
setmetatable(newCalculator, FPSCalculator)
return newCalculator
end
return FPSCalculator
@Geethcorona
Copy link

this is most usfull to me and make a new things soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment