-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.lua
270 lines (222 loc) · 7.68 KB
/
main.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
require "run"
local color = require "colorFade"
local fps, frameTime, lastUpdate
local fpsMax, fpsTimer, fluctuating, fpsSpeed
local random, randomAmount, randomTime
local displays, display
local fullscreen
local vsync
local logLevel, logLevels
local deltaTimes, logLines, logWidth
local WIDTH, HEIGHT
local scenes = {}
scenes.x = 0
scenes.y = 0
local scene = 1
local loadScenes = function(width, height)
for i, file in ipairs(love.filesystem.getDirectoryItems("scenes")) do
local f, err = love.filesystem.load("scenes/" .. file)
if not f then
print("Could not load", file..":", err)
else
f, err = pcall(f)
if not f then
print("Could not run", file..":", err)
else
scenes[i] = err
err.load(width, height)
print(i, err)
end
end
end
end
local setDisplay = function(n)
local new_displays = love.window.getDisplayCount()
n = (n-1) % new_displays + 1
if n == display and new_displays == displays then return end
WIDTH, HEIGHT = love.window.getDesktopDimensions(n)
love.window.setMode(WIDTH, HEIGHT, {display = n, fullscreen = fullscreen, vsync = vsync and 1 or 0})
display = n
displays = new_displays
for i, scene in ipairs(scenes) do
scene.resize(WIDTH - scenes.x, HEIGHT - scenes.y)
end
end
local str = [[
actual FPS: %d
target FPS: %d (change with up/down arrow)
fullscreen: %s (toggle with ctrl+f)
busy wait: %s (toggle with b)
vsync: %s (toggle with s)
fluctuating: %s (toggle with f, change max with ctrl + up/down arrow, change speed with ctrl + left/right arrow)
random stutter: %s [%dms] (toggle with r, change max amount with alt + up/down arrow, shift to change faster)
display: %d/%d (switch with alt + left/right arrow)
log level: %d/%d (increase with l, wraps around)
selected scene: %d (%s)
Freesync will only work when the application is fullscreen on Linux.
Busy waiting is more precise, but much heavier on processor and battery.
Vsync should eliminate tearing, but increases input lag and adds no smoothness.
You can quit this program with the Escape or Q keys.]]
local sceneStr
love.load = function()
love.busy = false
WIDTH, HEIGHT = love.graphics.getDimensions()
local flags = select(3, love.window.getMode())
scenes.y = (#select(2, love.graphics.getFont():getWrap(str, WIDTH)) + 1) * love.graphics.getFont():getHeight() + 8
fps = flags.refreshrate - 5
fps = (fps > 0) and fps or 56
fpsMax = fps
fpsTimer = 0
fluctuating = false
fpsSpeed = 10
random = false
randomTime = 0
randomAmount = 0
displays = love.window.getDisplayCount()
display = 1
frameTime = 1 / fps
lastUpdate = 0
logLevel = 0
logLevels = 3 -- 0-2
deltaTimes = {}
logLines = math.floor((HEIGHT - scenes.y) / love.graphics.getFont():getHeight())
logWidth = love.graphics.getFont():getWidth("00000 µs") + 16
fullscreen = flags.fullscreen
vsync = flags.vsync > 0
love.keyboard.setKeyRepeat(true)
loadScenes(WIDTH - scenes.x, HEIGHT - scenes.y)
color.setColor(scenes[scene].color.fg, scenes[scene].color.bg)
end
love.update = function(dt)
if love.busy then
while lastUpdate + frameTime + randomTime > love.timer.getTime() do end
else
while lastUpdate + frameTime + randomTime > love.timer.getTime() do
love.timer.sleep(0)
end
end
lastUpdate = love.timer.getTime()
fpsTimer = fpsTimer + dt * fpsSpeed / 10
if fluctuating then
fpsCur = fps + (math.sin(fpsTimer)/2 + 0.5) * (fpsMax - fps)
frameTime = 1/fpsCur
end
if random then
randomTime = (love.math.random() - 0.5 ) * randomAmount/1000
end
scenes[scene].update(dt, fps)
color.update(dt)
if logLevel > 1 then
table.insert(deltaTimes, 1, string.format("%d µs", dt * 1000000))
deltaTimes[logLines + 1] = nil
end
end
love.draw = function()
local fstr = fluctuating and ("true [max: %d, speed: %d, current: %d]"):format(fpsMax, fpsSpeed, fpsCur) or "false"
local str = string.format(str,
love.timer.getFPS(),
fps,
tostring(fullscreen),
tostring(love.busy),
tostring(vsync),
fstr,
tostring(random), randomAmount,
display, displays,
logLevel, logLevels - 1,
scene,
scenes[scene].name)
love.graphics.print(str, 8, 8)
love.graphics.print(scenes[scene].str, WIDTH - scenes[scene].strWidth - 8, 8)
if logLevel > 0 then
love.graphics.printf(table.concat({love.graphics.getRendererInfo()}, "\n"), 0, scenes.y - love.graphics.getFont():getHeight() * 5, WIDTH - 8, "right")
end
scenes[scene].draw(scenes.x, scenes.y)
if logLevel > 1 then
--love.graphics.setColor(.75, 0, 0)
love.graphics.setColor(color.bg())
love.graphics.rectangle("fill", WIDTH - logWidth, scenes.y, logWidth + 1, HEIGHT)
love.graphics.setColor(color.fg())
for i, ms in ipairs(deltaTimes) do
love.graphics.printf(ms, 0, scenes.y + (i - 1) * love.graphics.getFont():getHeight(), WIDTH - 8, "right")
end
end
end
sanitize = function()
fps = math.max(1, fps)
frameTime = 1/fps
fpsMax = math.max(fpsMax, fps)
fpsSpeed = math.max(1, fpsSpeed)
randomAmount = math.max(math.min(randomAmount, 1000), 0)
end
love.keypressed = function(key, keycode)
local ctrl = love.keyboard.isDown("lctrl", "rctrl")
local shift = love.keyboard.isDown("lshift", "rshift")
local alt = love.keyboard.isDown("ralt", "lalt")
if ctrl then
if key == "up" then
fpsMax = fpsMax + 1
elseif key == "down" then
fpsMax = fpsMax - 1
elseif key == "left" then
fpsSpeed = fpsSpeed - 1
elseif key == "right" then
fpsSpeed = fpsSpeed + 1
elseif key == "f" then
if fullscreen then
love.window.setFullscreen(false)
love.window.setPosition(1, 1)
else
love.window.setFullscreen(true)
love.window.setPosition(0, 0)
end
fullscreen = not fullscreen
end
elseif alt then
if key == "up" then
randomAmount = randomAmount + (shift and 5 or 1)
elseif key == "down" then
randomAmount = randomAmount - (shift and 5 or 1)
elseif key == "left" then
setDisplay(display - 1)
return
elseif key == "right" then
setDisplay(display + 1)
return
end
else
if key == "up" then
fps = fps + 1
elseif key == "down" then
fps = fps - 1
elseif key == "f" then
fluctuating = not fluctuating
fpsTimer = 0
elseif key == "b" then
love.busy = not love.busy
elseif key == "s" then
local w, h, flags = love.window.getMode()
flags.vsync = (flags.vsync == 0) and 1 or 0
love.window.setMode(w, h, flags)
flags = select(3, love.window.getMode())
vsync = flags.vsync > 0
elseif key == "r" then
random = not random
randomTime = 0
elseif key == "escape" or key == "q" then
love.event.quit()
elseif key == "l" then
logLevel = (logLevel + 1) % logLevels
end
end
if tonumber(key) and scenes[tonumber(key)] then
scene = tonumber(key)
color.setTarget(scenes[scene].color.fg, scenes[scene].color.bg)
return
end
scenes[scene].keypressed(key, keycode)
sanitize()
end
love.textinput = function(str)
scenes[scene].textinput(str)
sanitize()
end