bzr branch
http://9ix.org/bzr/ld27
35
by Josh C
cluke009 zoetrope + my spritebatch changes |
1 |
-- Class: DebugPerformance |
2 |
-- Shows a live graph of frames per second. This only records data while visible. |
|
3 |
||
4 |
DebugPerformance = DebugInstrument:extend |
|
5 |
{ |
|
6 |
width = 'narrow', |
|
7 |
contentHeight = 30, |
|
8 |
samples = {}, |
|
9 |
sampleInterval = 0.25, |
|
10 |
samplePtr = 1, |
|
11 |
numSamples = 100, |
|
12 |
_sampleTimer = 0, |
|
13 |
average = '-', |
|
14 |
||
15 |
onNew = function (self) |
|
16 |
self.bars = self:add(Group:new()) |
|
17 |
end, |
|
18 |
||
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 .. ')' |
|
23 |
||
24 |
if self._sampleTimer > self.sampleInterval then |
|
25 |
-- record fps and percent of desired fps |
|
26 |
||
27 |
table.insert(self.samples, fps) |
|
28 |
table.insert(self.samples, fps / the.app.fps) |
|
29 |
||
30 |
if #self.samples > self.numSamples * 2 then |
|
31 |
table.remove(self.samples, 1) |
|
32 |
table.remove(self.samples, 1) |
|
33 |
end |
|
34 |
||
35 |
-- calculate average |
|
36 |
||
37 |
local sum = 0 |
|
38 |
||
39 |
for i = 1, #self.samples, 2 do |
|
40 |
sum = sum + self.samples[i] |
|
41 |
end |
|
42 |
||
43 |
self.average = math.floor(sum / #self.samples * 2 + 0.5) |
|
44 |
||
45 |
-- sync bars and title bar |
|
46 |
||
47 |
local barHeight = self.contentHeight - 2 * self.spacing |
|
48 |
||
49 |
for i, bar in ipairs(self._sampleBars) do |
|
50 |
local percent = self.samples[i * 2] |
|
51 |
||
52 |
if percent then |
|
53 |
bar.distort.y = percent |
|
54 |
||
55 |
if percent > 0.5 then |
|
56 |
-- blend yellow to green |
|
57 |
bar.fill = {255 * (1 - (percent / 2)), 255, 0} |
|
58 |
else |
|
59 |
-- blend red to yellow |
|
60 |
-- 510 * percent = 255 * percent / 2 |
|
61 |
bar.fill = {255, 510 * percent, 0} |
|
62 |
end |
|
63 |
else |
|
64 |
bar.distort.y = 0 |
|
65 |
end |
|
66 |
end |
|
67 |
||
68 |
self._sampleTimer = 0 |
|
69 |
end |
|
70 |
end, |
|
71 |
||
72 |
onResize = function (self, x, y, width, height) |
|
73 |
local oldSamples = self.numSamples |
|
74 |
||
75 |
self.y = y + self.spacing |
|
76 |
x = x + self.spacing |
|
77 |
self.numSamples = math.floor(width - 2 * self.spacing) / 2 |
|
78 |
||
79 |
if self.numSamples ~= oldSamples then |
|
80 |
self._sampleBars = {} |
|
81 |
||
82 |
while #self.bars.sprites > 0 do |
|
83 |
self.bars:remove(self.bars.sprites[1]) |
|
84 |
end |
|
85 |
||
86 |
for i = 1, self.numSamples do |
|
87 |
self._sampleBars[i] = self.bars:add(Fill:new |
|
88 |
{ |
|
89 |
x = x, |
|
90 |
y = self.y, |
|
91 |
width = 2, |
|
92 |
height = height - self.spacing * 2, |
|
93 |
origin = { y = height - self.spacing * 2 }, |
|
94 |
distort = { x = 1, y = 0 }, |
|
95 |
fill = {255, 255, 255}, |
|
96 |
}) |
|
97 |
||
98 |
x = x + 2 |
|
99 |
end |
|
100 |
end |
|
101 |
end |
|
102 |
} |