-
Notifications
You must be signed in to change notification settings - Fork 3
/
FPSGraph.lua
146 lines (121 loc) · 4.44 KB
/
FPSGraph.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
--[[
MIT LICENSE
Copyright (c) 2014 Phoenix C. Enero
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]--
local fpsGraph = {}
fpsGraph.fpsFont = love.graphics.newFont(8)
-- creates a graph table (too lazy to make objects and stuff)
function fpsGraph.createGraph(x, y, width, height, delay, draggable)
-- create a value table such that the distance between two points is atleast 2 pixels
local vals = {}
for i=1, math.floor((width or 50)/2) do
table.insert(vals, 0)
end
-- return the table
return {
x = x or 0, -- | position of the graph
y = y or 0, -- |
width = width or 50, -- | dimensions of the graph
height = height or 30, --|
delay = delay or 0.5, -- delay until the next update
draggable = draggable or true, -- whether it is draggable or not
vals = vals, -- the values of the graph
vmax = 0, -- the maximum value of the graph
cur_time = 0, -- the current time of the graph
label = "graph", -- the label of the graph (changes when called by an update function)
_dx = 0, -- used for calculating the distance between the mouse and the pos
_dy = 0, -- as you are clicking the graph
_isDown = false -- check if the graph is still down
}
end
-- update the graph's values (make an update wrapper function if you want
-- to use this for your own purposes)
function fpsGraph.updateGraph(graph, val, label, dt)
-- update the current time of the graph
graph.cur_time = graph.cur_time + dt
-- get mouse position
local mouseX, mouseY = love.mouse.getPosition()
-- code for draggable graphs
if graph.draggable then
if (mouseX < graph.width+graph.x and mouseX > graph.x and
mouseY < graph.height+graph.y and mouseY > graph.y) or graph._isDown then
if love.mouse.isDown("l") then
graph._isDown = true
graph.x = mouseX - graph._dx
graph.y = mouseY - graph._dy
else
graph._isDown = false
graph._dx = mouseX - graph.x
graph._dy = mouseY - graph.y
end
end
end
-- when current time is bigger than the delay
while graph.cur_time >= graph.delay do
-- subtract current time by delay
graph.cur_time = graph.cur_time - graph.delay
-- add new values to the graph while removing the first
table.remove(graph.vals, 1)
table.insert(graph.vals, val)
-- get the new max variable
local max = 0
for i=1, #graph.vals do
local v = graph.vals[i]
if v > max then
max = v
end
end
-- update the max and label variables
graph.vmax = max
graph.label = label
end
end
-- Updates the FPS graph
function fpsGraph.updateFPS(graph, dt)
local fps = 0.75*1/dt + 0.25*love.timer.getFPS()
fpsGraph.updateGraph(graph, fps, "FPS: " .. math.floor(fps*10)/10, dt)
end
-- Updates the Memory graph
function fpsGraph.updateMem(graph, dt)
local mem = collectgarbage("count")
fpsGraph.updateGraph(graph, mem, "Memory (KB): " .. math.floor(mem*10)/10, dt)
end
-- draws all the graphs in your list
function fpsGraph.drawGraphs(graphs)
-- set default font
love.graphics.setFont(fpsGraph.fpsFont)
-- loop through all of the graphs
for j=1, #graphs do
local v = graphs[j]
-- round values
local maxVal = math.ceil(v.vmax/10)*10+20
local len = #v.vals
local step = v.width/len
-- draw graph
for i=2, len do
local a = v.vals[i-1]
local b = v.vals[i]
love.graphics.line(step*(i-2)+v.x, v.height*(-a/maxVal+1)+v.y,
step*(i-1)+v.x, v.height*(-b/maxVal+1)+v.y)
end
-- print the label of the graph
love.graphics.print(v.label, v.x, v.height+v.y-8)
end
end
return fpsGraph