This repository has been archived by the owner on Mar 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconf.lua
589 lines (497 loc) · 14.7 KB
/
conf.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
-- Game flags (beyond love's own)
-- TODO: Read a preferences file for conf stuff?
_G.FLAGS = {
game_version = "LD38-1.0.0",
debug_mode = false,
show_perfhud = false,
show_overscan = false,
headless = false
}
local use = {
highdpi_hack = true,
hot_reloader = false,
fps_in_title = true,
handle_screenshots = true,
event_poll = true,
love_draw = true,
log_header = "Please report this on GitHub at https://github.com/excessive/ludum-dare-38 or\nsend a DM on Twitter to @LandonManning or @shakesoda."
}
if not _G.FLAGS.headless then
require "love.system"
require "love.window"
end
-- Add folders to require search path
love.filesystem.setRequirePath(
love.filesystem.getRequirePath()
.. ";libs/?.lua;libs/?/init.lua"
.. ";src/?.lua;src/?/init.lua"
)
-- Specify window flags here because we use some of them for the error screen.
local flags = {
title = "Murky Mind",
width = 1280,
height = 720,
fullscreen = false,
fullscreentype = "desktop",
msaa = 4,
vsync = true,
resizable = true,
highdpi = true
}
require "love.math"
if use.highdpi_hack and love.system.getOS() == "Linux" and not _G.FLAGS.headless then
flags.highdpi = false
local lume = require "lume"
local f = io.popen("gsettings get org.gnome.desktop.interface scaling-factor")
local _scale = lume.split(f:read() or "it's 1", " ")
local dpi_scale = _scale[2] and tonumber(_scale[2]) or 1.0
if dpi_scale >= 0.5 then
flags.width = flags.width * dpi_scale
flags.height = flags.height * dpi_scale
love.window.toPixels = function(v)
return v * dpi_scale
end
love.window.getPixelScale = function()
return dpi_scale
end
end
end
function love.conf(t)
t.version = "0.10.2"
for k, v in pairs(flags) do
t.window[k] = v
end
t.gammacorrect = true -- Always use gamma correction.
t.accelerometerjoystick = false -- Disable joystick accel on mobile
t.modules.physics = false
t.modules.audio = not _G.FLAGS.headless
io.stdout:setvbuf("no") -- Don't delay prints.
end
--------------------------------------------------
-- /!\ Here be dragons. Thou art forewarned /!\ --
--------------------------------------------------
-- Helpers for hot reloading the whole game.
-- I apologize for the global, but thou must.
local fire = {}
package.loaded.fire = fire
local pkg_cache = {}
local callbacks = {}
local world_saved = false
-- Save packages from startup so we can reset to this state at a later time
function fire.save_the_world()
world_saved = true
pkg_cache = {}
callbacks = {}
for k, v in pairs(package.loaded) do
pkg_cache[k] = v
end
for k, v in pairs(love) do
callbacks[k] = v
end
pkg_cache.main = nil
end
-- Restore saved cache so Lua has to reload everything.
function fire.reset_the_world()
if not world_saved then
print "[Fire] No state saved to reset the world to."
return
end
-- unload
if love.quit then
love.quit()
end
for k in pairs(package.loaded) do
package.loaded[k] = pkg_cache[k]
end
for _, k in ipairs {
'focus', 'keypressed', 'keyreleased', 'mousefocus', 'mousemoved',
'mousepressed', 'mousereleased', 'wheelmoved', 'textedited', 'textinput',
'resize', 'visible', 'gamepadaxis', 'gamepadpressed', 'gamepadreleased',
'joystickadded', 'joystickaxis', 'joystickhat', 'joystickpressed',
'lowmemory', 'joystickreleased', 'joystickremoved', 'filedropped',
'directorydropped', 'update', 'quit', 'load', 'draw'
} do
love[k] = nil
end
for k, v in pairs(callbacks) do
love[k] = v
end
if love.audio then
love.audio.stop()
love.audio.setVolume(1.0)
end
if love.mouse then
love.mouse.setGrabbed(false)
love.mouse.setVisible(true)
love.mouse.setRelativeMode(false)
end
-- Clean out everything, just to be sure.
collectgarbage("collect")
-- Note: you have to collect TWICE to make sure everything is GC'd.
collectgarbage("collect")
require "main"
print "Reloading game!"
return love.run()
end
-- A few convenience functions.
function fire.open_save()
love.system.openURL("file://" .. love.filesystem.getSaveDirectory())
end
function fire.toggle_fullscreen()
love.window.setFullscreen(not love.window.getFullscreen())
end
function fire.take_screenshot()
love.filesystem.createDirectory("Screenshots")
local ss = love.graphics.newScreenshot()
local path = string.format("%s/%s.png",
"Screenshots",
os.date("%Y-%m-%d_%H-%M-%S", os.time())
)
local f = love.filesystem.newFile(path)
ss:encode("png", path)
end
local fix_love10_colors = function(t) return t end
if select(2, love.getVersion()) <= 10 then
fix_love10_colors = function(t)
return { t[1] * 255, t[2] * 255, t[3] * 255, t[4] * 255 }
end
end
local perfhud = {
color = {
bg = fix_love10_colors { 0.0, 0.0, 0.0, 0.75 },
good = fix_love10_colors { 0.5, 1.0, 0.5, 0.75 },
bad = fix_love10_colors { 1.0, 0.0, 0.0, 1.0 }
},
limit = 1/20,
target = 1/60 + 1/400, -- tolerance
data = {},
pos = {
x = love.window.toPixels(10),
y = love.window.toPixels(10)
},
max_samples = 120,
bar_width = love.window.toPixels(5),
height = love.window.toPixels(40),
}
perfhud.spacing = math.floor(perfhud.bar_width * 1.2)
perfhud.width = perfhud.spacing * perfhud.max_samples - (perfhud.spacing - perfhud.bar_width)
local l3d_loaded = false
local binds = {}
function fire.clear_binds(key)
for k, v in pairs(binds) do
if (key ~= nil and key == k) or (key == nil) then
while #v > 0 do
table.remove(v)
end
end
end
end
function fire.bind(key, fn)
binds[key] = binds[key] or {}
-- only allow binding a function once per key
for _, _fn in ipairs(binds[key]) do
if _fn == fn then
return
end
end
table.insert(binds[key], fn)
end
function fire.trigger(key, scancode, is_repeat)
if not binds[key] or is_repeat then
return false
end
for _, fn in ipairs(binds[key]) do
local r = fn(key)
if r then
return true
end
end
return false
end
function love.run()
-- Add gamepads
if love.joystick then
if love.filesystem.isFile("assets/gamecontrollerdb.txt") then
love.joystick.loadGamepadMappings("assets/gamecontrollerdb.txt")
end
end
local fire = require "fire"
local reset = false
if use.love3d and not l3d_loaded then
l3d_loaded = true
require "love3d".import(true, false)
end
if use.hot_reloader then
fire.save_the_world()
end
if love.math then
love.math.setRandomSeed(os.time())
for i=1,3 do love.math.random() end
end
if love.event then
love.event.pump()
end
if love.load then love.load(arg) end
-- We don't want the first frame's dt to include time taken by love.load.
if love.timer then love.timer.step() end
local dt = 0
local last_update = -0.5 -- update immediately!
-- Main loop time.
while true do
-- fire.bind("f8", function()
-- _G.FLAGS.debug_mode = not _G.FLAGS.debug_mode
-- end)
if _G.FLAGS.debug_mode then
fire.bind("f9", function()
_G.FLAGS.show_perfhud = not _G.FLAGS.show_perfhud
end)
fire.bind("f10", function()
_G.FLAGS.show_overscan = not _G.FLAGS.show_overscan
end)
end
-- Process events.
if love.event then
love.event.pump()
if use.event_poll then
for name, a,b,c,d,e,f in love.event.poll() do
if name == "keypressed" and a == "f5" then
-- reset = true
end
if use.handle_screenshots then
if name == "keypressed" and a == "f11" then
fire.open_save()
end
if name == "keypressed" and a == "f12" then
fire.take_screenshot()
end
end
if name == "keypressed" and a == "return" then
if (love.keyboard.isDown("lalt") or love.keyboard.isDown("ralt")) then
fire.toggle_fullscreen()
end
end
if name == "keypressed" and a == "escape" and
(love.keyboard.isDown "lshift" or love.keyboard.isDown "rshift")
then
love.event.quit()
end
if name == "quit" then
if not love.quit or not love.quit() then
return
end
end
if name ~= "keypressed" or (name == "keypressed" and not fire.trigger(a,b,c,d,e,f)) then
love.handlers[name](a,b,c,d,e,f)
end
end
end
fire.clear_binds()
end
if use.hot_reloader and reset then
break
end
-- Update dt, as we'll be passing it to update
local skip_time = false
if love.timer then
love.timer.step()
dt = love.timer.getDelta()
-- dt = 1/60
if love.keyboard.isDown "tab" then
dt = dt * 4
else
-- Cap dt to 30hz - this results in slowmo, but that's less
-- bad than the things that enormous deltas can cause.
if dt > 1/30 then
-- Record full delta if we're skipping frames, so that
-- it can still be handled.
skip_time = dt
end
dt = math.min(dt, 1/30)
end
end
-- Call update and draw
if love.graphics and love.graphics.isActive() then
-- Discarding here causes issues with NVidia 352.41 on Linux
-- love.graphics.discard()
love.graphics.clear(love.graphics.getBackgroundColor())
-- will pass 0 if love.timer is disabled
if love.update then love.update(dt, skip_time) end
if use.love_draw and love.draw then love.draw() end
table.insert(perfhud.data, skip_time or dt)
while #perfhud.data > perfhud.max_samples do
table.remove(perfhud.data, 1)
end
if _G.FLAGS.show_perfhud then
love.graphics.setColor(perfhud.color.bg)
love.graphics.rectangle("fill", perfhud.pos.x, perfhud.pos.y, perfhud.width, perfhud.height)
for i = #perfhud.data, 1, -1 do
local x = (i-1) * perfhud.spacing
local bar_height = perfhud.height * (perfhud.data[i] / perfhud.limit)
if perfhud.data[i] <= perfhud.target then
love.graphics.setColor(perfhud.color.good)
else
love.graphics.setColor(perfhud.color.bad)
end
love.graphics.rectangle("fill",
perfhud.pos.x + x,
perfhud.pos.y + perfhud.height - bar_height,
perfhud.bar_width, bar_height
)
end
end
love.graphics.origin()
love.graphics.present()
-- Run a fast GC cycle so that it happens at predictable times.
-- This prevents GC work from building up and causing hitches.
collectgarbage("step")
-- collectgarbage("step")
-- surrender just a little bit of CPU time to the OS
if love.timer then love.timer.sleep(0.001) end
local now = love.timer.getTime()
if use.fps_in_title and now - last_update >= 0.25 then
last_update = now
love.window.setTitle(string.format(
"%s - %s (%5.4fms/f %2.2ffps)",
flags.title,
_G.FLAGS.game_version,
love.timer.getAverageDelta() * 1000,
love.timer.getFPS()
))
end
end
end
if use.hot_reloader and reset then
return fire.reset_the_world()
end
end
local debug, print = debug, print
local function error_printer(msg, layer)
local filename = "crash.log"
local file = ""
local time = os.date("%Y-%m-%d %H:%M:%S", os.time())
local err = debug.traceback(
"Error: " .. tostring(msg), 1+(layer or 1)
):gsub("\n[^\n]+$", "")
if love.filesystem.isFile(filename) then
file = love.filesystem.read(filename)
end
if file == "" then
file = use.log_header .. "\n\n"
else
file = file .. "\n\n"
end
file = file .. string.format([[
=========================
== %s ==
=========================
%s]], time, err)
love.filesystem.write(filename, file)
print(err)
end
function love.errhand(msg)
function rgba(color)
local a = math.floor((color / 16777216) % 256)
local r = math.floor((color / 65536) % 256)
local g = math.floor((color / 256) % 256)
local b = math.floor((color) % 256)
return r, g, b, a
end
msg = tostring(msg)
error_printer(msg, 2)
if not love.window or not love.graphics or not love.event then
return
end
if not love.graphics.isCreated() or not love.window.isOpen() then
local success, status = pcall(love.window.setMode, flags.width, flags.height)
if not success or not status then
return
end
end
love.window.setTitle(flags.title)
-- Reset state.
if love.mouse then
love.mouse.setVisible(true)
love.mouse.setGrabbed(false)
love.mouse.setRelativeMode(false)
end
if love.joystick then
-- Stop all joystick vibrations.
for i,v in ipairs(love.joystick.getJoysticks()) do
v:setVibration()
end
end
if love.audio then love.audio.stop() end
love.graphics.reset()
local font_path = "assets/fonts/NotoSans-Regular.ttf"
local head, font
if love.filesystem.isFile(font_path) then
head = love.graphics.newFont(font_path, math.floor(love.window.toPixels(22)))
font = love.graphics.newFont(font_path, math.floor(love.window.toPixels(14)))
else
print "Error screen font missing, using default instead."
head = love.graphics.newFont(math.floor(love.window.toPixels(22)))
font = love.graphics.newFont(math.floor(love.window.toPixels(14)))
end
love.graphics.setBackgroundColor(rgba(0xFF1E1E2C))
love.graphics.setColor(255, 255, 255, 255)
-- Don't show conf.lua in the traceback.
local trace = debug.traceback("", 2)
love.graphics.clear(love.graphics.getBackgroundColor())
love.graphics.origin()
local err = {}
table.insert(err, msg.."\n")
for l in string.gmatch(trace, "(.-)\n") do
if not string.match(l, "boot.lua") then
l = string.gsub(l, "stack traceback:", "Traceback\n")
table.insert(err, l)
end
end
local c = string.format("Please locate the crash.log file at: %s\n\nI can try to open the folder for you if you press F11!", love.filesystem.getSaveDirectory())
local h = "Oh no, it's broken!"
local p = table.concat(err, "\n")
p = string.gsub(p, "\t", "")
p = string.gsub(p, "%[string \"(.-)\"%]", "%1")
local function draw()
local pos = love.window.toPixels(70)
love.graphics.clear(love.graphics.getBackgroundColor())
love.graphics.setColor(rgba(0xFFF0A3A3))
love.graphics.setFont(head)
love.graphics.printf(h, pos, pos, love.graphics.getWidth() - pos)
love.graphics.setFont(font)
love.graphics.setColor(rgba(0xFFD2D5D0))
love.graphics.printf(c, pos, pos + love.window.toPixels(40), love.graphics.getWidth() - pos)
love.graphics.setColor(rgba(0xFFA2A5A0))
love.graphics.printf(p, pos, pos + love.window.toPixels(120), love.graphics.getWidth() - pos)
love.graphics.present()
end
local reset = false
while true do
love.event.pump()
for e, a, b, c in love.event.poll() do
if e == "quit" then
return
elseif e == "keypressed" and a == "f11" then
fire.open_save()
elseif e == "keypressed" and a == "f12" then
fire.take_screenshot()
elseif e == "keypressed" and a == "f5" then
reset = true
break
elseif e == "keypressed" and a == "escape" and (love.window.getFullscreen()) then
return
elseif e == "keypressed" and a == "escape" then --or e == "mousereleased" then
return
end
end
if use.hot_reloader and reset then
break
end
draw()
if love.timer then
love.timer.sleep(0.1)
end
end
if use.hot_reloader and reset then
return xpcall(fire.reset_the_world, love.errhand)
end
end