-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
208 lines (175 loc) · 4.73 KB
/
init.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
-- LoveProfiler for Love2D Framework
-- Author: Dmitri Smirnov <http://www.whoop.ee>
-- MIT License
-- VERSION: 1.0.1
local defaults = require("loveprofiler.defaults")
---@alias level
---| '"INFO"'
---| '"WARNING"'
---| '"ERROR"'
---@class LoveProfiler
---@field public config table
---@field private _out table
---@field private _log table
---@field private _driver table
local LoveProfiler = {
LOG_INFO = "INFO", ---[[@as level]]
LOG_WARN = "WARNING", ---[[@as level]]
LOG_ERROR = "ERROR", ---[[@as level]]
}
---Creates new instance aka constructor
---@param t? table - Config can be passed here.
---@return table - New instance of LoveProfiler.
function LoveProfiler:new(t)
t = t or {}
t._out = {}
t._log = {}
-- Merge configs.
local cfg = {}
for k, v in pairs(defaults) do
cfg[k] = v
end
if t.config then
for k, v in pairs(t.config) do
cfg[k] = v
end
end
t.config = cfg
---@private
t._driver = require("loveprofiler.drivers." .. t.config.driver)
setmetatable(t, self)
self.__index = self
return t
end
---Adds message to log.
---@param msg string - Message to be logged.
---@param level? level level Level of the logged
function LoveProfiler:addMessage(msg, level)
level = level or LoveProfiler.LOG_INFO
self._log[#self._log + 1] = {
["msg"] = msg,
["level"] = level,
}
if self.config.log_path then
local time = os.date(self.config.log_date_format)
local file = io.open(self.config.log_path, "a")
if file ~= nil then
file:write(string.format("%s %s %s\n", time, level, msg))
file:close()
end
end
end
--- Processed the log if log is enabled.
function LoveProfiler:processLog()
local start = #self._log > self.config.log_size
and #self._log - self.config.log_size
or 1
local stop = #self._log
for i = start, stop do
local color
if self._log[i].level == LoveProfiler.LOG_WARN then
color = self.config.log_color_warn
elseif self._log[i].level == LoveProfiler.LOG_ERROR then
color = self.config.log_color_error
else
color = self.config.log_color_info
end
self:appendToOutput(self._log[i].msg, color)
end
end
---Adds line to output.
---@param msg string Message (line) to be added.
---@param color? table Color as table {r, g, b, a}.
function LoveProfiler:appendToOutput(msg, color)
color = color or self.config.color
self._out[#self._out + 1] = {
["msg"] = msg .. "\n",
["color"] = color,
}
end
---Gets the name of OS.
---@return string
function LoveProfiler:getOS()
return love.system.getOS()
end
---Gets FPS count.
---@return number
function LoveProfiler:getFPS()
return love.timer.getFPS()
end
---Gets mouse coordinates.
---@return number, number
function LoveProfiler:mouse()
return love.mouse.getX(), love.mouse.getY()
end
---Gets the time between 2 frames in milliseconds.
---@return number
function LoveProfiler:getDelta()
return love.timer.getDelta() * 1000
end
---Gets the memory usage in bytes, textures + Lua + Love2d program.
---@return number
function LoveProfiler:getMemoryUsage()
local stats = love.graphics.getStats()
return math.floor(collectgarbage("count") + stats.texturememory + 0.5)
end
---Gets time duration
---@return number
function LoveProfiler:getDuration()
return love.timer.getTime()
end
---@return string
---Formats duration
function LoveProfiler:formatDuration()
local f = "Duration: %02d:%02d:%02d.%03d"
local t = LoveProfiler:getDuration()
local h = t % (24 * 3600) / 3600
t = t % 3600
local m = t / 60
t = t % 60
local s = t
local ms = (s - math.floor(s)) * 1000
return string.format(f, h, m, s, ms)
end
---Starts the profiler, should be used in love.draw() method.
function LoveProfiler:start()
local font = love.graphics.newFont(self.config.font_size)
love.graphics.setFont(font)
if self.config.show_os then
self:appendToOutput(string.format("OS: %s", self:getOS()))
end
if self.config.show_fps then
self:appendToOutput(string.format("FPS: %s", self:getFPS()))
end
if self.config.show_mem then
self:appendToOutput(
string.format("MEM: %s bytes", self:getMemoryUsage())
)
end
if self.config.show_coords and self.config.driver == "canvas" then
love.graphics.setColor(self.config.color)
local x, y = self:mouse()
local coords = string.format("%d, %d", x, y)
x = x + self.config.x_coords_offset
y = y + self.config.y_coords_offset
love.graphics.print(coords, x, y)
love.graphics.reset()
end
if self.config.show_delta then
self:appendToOutput(string.format("Delta: %.1fms", self:getDelta()))
end
if self.config.show_duration then
self:appendToOutput(LoveProfiler:formatDuration())
end
if self.config.log_enabled then
if #self._log > 0 then
self:appendToOutput("---------- Log ----------")
end
self:processLog()
end
for i = 1, #self._out do
self._driver.print(self, i)
end
self._driver.flush(self)
end
return LoveProfiler