/ld27

To get this branch, use:
bzr branch http://9ix.org/bzr/ld27

« back to all changes in this revision

Viewing changes to zoetrope/debug/performance.lua

  • Committer: Josh C
  • Date: 2013-08-24 19:37:30 UTC
  • Revision ID: josh@9ix.org-20130824193730-ahyzlm50f169fc24
slow level + tiny player

Show diffs side-by-side

added added

removed removed

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