1
-- Class: DebugPerformance
2
-- Shows a live graph of frames per second. This only records data while visible.
4
DebugPerformance = DebugInstrument:extend
15
onNew = function (self)
16
self.bars = self:add(Group:new())
19
onUpdate = function (self, elapsed)
20
self._sampleTimer = self._sampleTimer + elapsed
21
local fps = math.floor(1 / elapsed + 0.5)
22
self.title.text = 'FPS (' .. fps .. ', average ' .. self.average .. ')'
24
if self._sampleTimer > self.sampleInterval then
25
-- record fps and percent of desired fps
27
table.insert(self.samples, fps)
28
table.insert(self.samples, fps / the.app.fps)
30
if #self.samples > self.numSamples * 2 then
31
table.remove(self.samples, 1)
32
table.remove(self.samples, 1)
39
for i = 1, #self.samples, 2 do
40
sum = sum + self.samples[i]
43
self.average = math.floor(sum / #self.samples * 2 + 0.5)
45
-- sync bars and title bar
47
local barHeight = self.contentHeight - 2 * self.spacing
49
for i, bar in ipairs(self._sampleBars) do
50
local percent = self.samples[i * 2]
53
bar.distort.y = percent
56
-- blend yellow to green
57
bar.fill = {255 * (1 - (percent / 2)), 255, 0}
59
-- blend red to yellow
60
-- 510 * percent = 255 * percent / 2
61
bar.fill = {255, 510 * percent, 0}
72
onResize = function (self, x, y, width, height)
73
local oldSamples = self.numSamples
75
self.y = y + self.spacing
77
self.numSamples = math.floor(width - 2 * self.spacing) / 2
79
if self.numSamples ~= oldSamples then
82
while #self.bars.sprites > 0 do
83
self.bars:remove(self.bars.sprites[1])
86
for i = 1, self.numSamples do
87
self._sampleBars[i] = self.bars:add(Fill:new
92
height = height - self.spacing * 2,
93
origin = { y = height - self.spacing * 2 },
94
distort = { x = 1, y = 0 },
95
fill = {255, 255, 255},