FrameRate

Solar2D performance test

Presentation

Here is a small code which allows you to test the performance of your phone.
It displays sprites of 128x128 pixels (source size) and increases their number in order to know how much your mobile can display sprites at the speed of 60 FPS (for info, the display of the text takes a huge time, so you must be able to trace more sprites than the number displayed)
The web version tests your PC at 30 FPS because browsers usually don't allow you to go faster!
This code was developed with Solar2D

Note: this is the modification of a code initially made by Markus Ranner

display.setStatusBar(display.HiddenStatusBar)                                   -- we hide the StatusBar

_G.wantedSmiley = 10 -- the maximum number of smileys
_G.nbSmiley = 0 -- the number of smailey currently displayed
_G.wantedFPS = 60 -- the desired frame rate
_G.previousTime = 0

math.randomseed(os.time()) -- initialization of the random number generator

local fpsLabel = display.newText({ -- the Text object displaying the frame rate
x = display.contentCenterX,
y = 40,
fontSize = 24,
font = native.systemFontBold,
text = "fps: " .. math.round(display.fps)
})

function createSmiley() -- smiley creation function
local smileyToCreate = _G.wantedSmiley - _G.nbSmiley -- how many we have to create

local centerX = display.contentCenterX
local centerY = display.contentCenterY
local random = math.random

if smileyToCreate > 0 then
for i = 1, smileyToCreate do
local smiley = display.newImage('smiley.png', 128, 128) -- we create a smiley
smiley.x = centerX -- we place it in the center of the screen
smiley.y = centerY
smiley.xScale = 0.01 -- we reduce it so that it is very small
smiley.yScale = 0.01
smiley:setFillColor(random(), random(), random()) -- we give the smiley a random color
smiley.alpha = 0.1 -- we make it transparent
smiley:toBack() -- we display it behind all the others

local angle = random(0, 360) -- we choose an angle
local x = display.actualContentWidth * math.cos(angle) + centerX -- we calculate where the smiley must go
local y = display.actualContentHeight * math.sin(angle) + centerY
transition.to(smiley, { -- Solar2D will take care of animating it!
x = x, -- by interpolating all the values
y = y,
rotation = random(-360, 360),
time = 2000,
xScale = 1,
yScale = 1,
alpha = 0.5,
onComplete = function() removeSmiley(smiley) end
})

_G.nbSmiley = _G.nbSmiley + 1 -- one more smiley on the screen
end
end
end

function removeSmiley(smiley) -- destruction function of a sprite
display.remove(smiley) -- at the end of the animation we destroy the sprite
_G.nbSmiley = _G.nbSmiley - 1
end

local function gameLoop(callbackFunction) -- the loop called 60 times per second if possible
local ms = 1500 / _G.wantedFPS -- the maximum number of ms

local curTime = system.getTimer()
local deltaTime = curTime - _G.previousTime -- how long has passed since the last iteration
_G.previousTime = curTime

if deltaTime < ms then -- if we are in the desired FPS
fpsLabel.text = "fps: " .. _G.wantedFPS .. ' - Smiley : ' .. _G.nbSmiley
_G.wantedSmiley = _G.wantedSmiley + 10 -- we try to increase the number of smileys
else
_G.wantedSmiley = _G.wantedSmiley - 10 -- otherwise we reduce it
end

createSmiley() -- we create the missing smileys
end

Runtime:addEventListener("enterFrame", gameLoop) -- we start the gameLoop function

Download

You can download the project archive running under Solar2D

Commentaires