blob: 4eac7b913d5eb99e4dea1f8ab1eb346575b0a498 [file] [log] [blame]
Serge Bazanski18c1a262022-07-07 14:24:53 +02001--[[
2 UNLICENSE
3
4 This is free and unencumbered software released into the public domain.
5
6 Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
7 software, either in source code form or as a compiled binary, for any purpose,
8 commercial or non-commercial, and by any means.
9
10 In jurisdictions that recognize copyright laws, the author or authors of this
11 software dedicate any and all copyright interest in the software to the public
12 domain. We make this dedication for the benefit of the public at large and to
13 the detriment of our heirs and successors. We intend this dedication to be an
14 overt act of relinquishment in perpetuity of all present and future rights to
15 this software under copyright law.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTBILITY, FITNESS
19 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT, IN NO EVENT SHALL THE AUTHORS BE
20 LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 For more information, please refer to <http://unlicense.org/>
25]]
26
27-- Code based on https://github.com/icrawler/FPSGraph
28
29local debugGraph = {}
30
31function debugGraph:new(type, x, y, width, height, delay, label, font)
32 if ({mem=0, fps=0, custom=0})[type] == nil then
33 error('Acceptable types: mem, fps, custom')
34 end
35
36 local instance = {
37 x = x or 0, -- X position
38 y = y or 0, -- Y position
39 width = width or 50, -- Graph width
40 height = height or 30, -- Graph height
41 delay = delay or 0.5, -- Update delay
42 label = label or type, -- Graph label
43 font = font or love.graphics.newFont(8),
44 data = {},
45 _max = 0,
46 _time = 0,
47 _type = type,
48 }
49
50 -- Build base data
51 for i = 0, math.floor(instance.width / 2) do
52 table.insert(instance.data, 0)
53 end
54
55 -- Updating the graph
56 function instance:update(dt, val)
57 local lastTime = self._time
58 self._time = (self._time + dt) % self.delay
59
60 -- Check if the minimum amount of time has past
61 if dt > self.delay or lastTime > self._time then
62 -- Fetch data if needed
63 if val == nil then
64 if self._type == 'fps' then
65 -- Collect fps info and update the label
66 val = 0.75 * 1 / dt + 0.25 * love.timer.getFPS()
67 self.label = "FPS: " .. math.floor(val * 10) / 10
68 elseif self._type == 'mem' then
69 -- Collect memory info and update the label
70 val = collectgarbage('count')
71 self.label = "Memory (KB): " .. math.floor(val * 10) / 10
72 else
73 -- If the val is nil then we'll just skip this time
74 return
75 end
76 end
77
78
79 -- pop the old data and push new data
80 table.remove(self.data, 1)
81 table.insert(self.data, val)
82
83 -- Find the highest value
84 local max = 0
85 for i=1, #self.data do
86 local v = self.data[i]
87 if v > max then
88 max = v
89 end
90 end
91
92 self._max = max
93 end
94 end
95
96 function instance:draw()
97 -- Store the currently set font and change the font to our own
98 local fontCache = love.graphics.getFont()
99 love.graphics.setFont(self.font)
100
101 local max = math.ceil(self._max/10) * 10 + 20
102 local len = #self.data
103 local steps = self.width / len
104
105 -- Build the line data
106 local lineData = {}
107 for i=1, len do
108 -- Build the X and Y of the point
109 local x = steps * (i - 1) + self.x
110 local y = self.height * (-self.data[i] / max + 1) + self.y
111
112 -- Append it to the line
113 table.insert(lineData, x)
114 table.insert(lineData, y)
115 end
116
117 -- Draw the line
118 love.graphics.line(unpack(lineData))
119
120 -- Print the label
121 if self.label ~= '' then
122 love.graphics.print(self.label, self.x, self.y + self.height - self.font:getHeight())
123 end
124
125 -- Reset the font
126 love.graphics.setFont(fontCache)
127 end
128
129 return instance
130end
131
132return debugGraph