This repository has been archived by the owner on Feb 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.lua
664 lines (546 loc) · 12.2 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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
-----------------------------------------
-- main callbacks
-----------------------------------------
local requirePath = love.filesystem.getRequirePath()
love.filesystem.setRequirePath(requirePath ..
';src/?.lua;src/?/init.lua' ..
';libs/?.lua;libs/?/init.lua'
)
OS = love.system.getOS()
mobile = OS == "Android" or OS == "iOS"
require "minify"
require "log"
require "error"
-- DEBUG!
--mobile = true
if mobile then
keyboard = require "keyboard"
keyboard.init()
end
giflib = require "gif"
QueueableSource = require "QueueableSource"
frameTime = 1 / config.fps
hostTime = 0
asm = require "asm-lua"
audio = require "audio"
carts = require "carts"
commands = require "commands"
api = require "api"
neko = require "neko8"
function love.load(arg)
love.filesystem.unmount(love.filesystem.getSource())
if arg then
DEBUG = arg[2] == "-d"
if DEBUG then
lurker = require "lurker"
lurker.protected = false
lurker.quiet = true
lurker.postswap = function(f)
editors.current.init()
carts.import(neko.loadedCart)
resizeCanvas(
love.graphics.getWidth(),
love.graphics.getHeight()
)
end
end
end
local joysticks = love.joystick.getJoysticks()
if joysticks[0] then
neko.joystick = joysticks[0]
end
log.info("neko 8 " .. config.version.string)
love.window.setTitle("neko 8 " .. config.version.string)
love.window.setDisplaySleepEnabled(false)
neko.init()
end
function love.joystickadded(joystick)
if not neko.joystick then
neko.joystick = joystick
end
end
function love.joystickremoved(joystick)
if neko.joystick == joystick then
neko.joystick = nil
end
end
function love.touchpressed()
-- open virtual keyboard when in code/sfx editor or command line on mobile
if ((editors.current == editors.modes[1] or editors.modes[4])
or editors.opened == false) and not neko.cart then
love.keyboard.setTextInput(true)
end
end
-- XXX Why is this in the global scope? Why isn't this part of some table?
mbt = 0
ss={up=true,x=0,c=0,isRunning=false}
function love.update(dt)
if not neko.focus then -- screensaver
return
end
neko.cursor.current = neko.cursor.default
if DEBUG then
lurker.update()
end
neko.update(dt)
if mobile then
keyboard.update()
end
if love.mouse.isDown(1) then
mbt = mbt + 1
else
mbt = 0
end
end
function love.draw()
--if not neko.focus then
-- api.flip()
-- return
--end
love.graphics.setCanvas(
canvas.renderable
)
love.graphics.setShader(
colors.drawShader
)
setClip()
setCamera()
neko.draw()
api.flip()
if mobile then
keyboard.update()
end
end
function love.wheelmoved(x, y)
triggerCallback("_wheel", y)
end
function love.resize(w, h)
resizeCanvas(w,h)
end
function love.joystickpressed(joystick, button)
if button == 4 then
love.keypressed("up", -1, false)
elseif button == 2 then
love.keypressed("down", -1, false)
elseif button == 3 then
love.keypressed("right", -1, false)
elseif button == 1 then
love.keypressed("left", -1, false)
end
end
function love.joystickreleased(joystick, button)
if button == 4 then
love.keyreleased("up")
elseif button == 2 then
love.keyreleased("down")
elseif button == 3 then
love.keyreleased("right")
elseif button == 1 then
love.keyreleased("left")
end
end
function love.keypressed(
key, scancode, isRepeat
)
for p = 0, 1 do
for i = 0, #api.keyMap[p] do
for _, k
in pairs(api.keyMap[p][i]) do
if key == k then
api.keyPressed[p][i] = -1
break
end
end
end
end
local handled = true
if love.keyboard.isDown("rctrl") or
love.keyboard.isDown("lctrl") then
if key == "r" then
if neko.loadedCart then
carts.run(neko.loadedCart)
end
elseif key == "v" then
love.textinput(
love.system.getClipboardText()
)
elseif key == "c" then
local text = triggerCallback("_copy")
if text then
love.system.setClipboardText(text)
end
elseif key == "x" then
local text = triggerCallback("_cut")
if text then
love.system.setClipboardText(text)
end
else
handled = false
end
elseif love.keyboard.isDown("lalt")
or love.keyboard.isDown("ralt") then
if (key == "return" or key == "kpenter")
and not isRepeat then
neko.fullscreen = not neko.fullscreen
love.window.setFullscreen(neko.fullscreen)
end
else
local shiftDown = love.keyboard.isDown("lshift")
or love.keyboard.isDown("rshift")
if (key == "escape" or (key == "return" and shiftDown))
and not isRepeat then
handled = false
if neko.cart then
neko.cart = nil
api.camera(0, 0)
api.clip()
elseif editors.opened then
editors.close()
else
editors.open()
end
elseif neko.cart == nil and editors.opened then
if key == "f1" then
editors.openEditor(1)
elseif key == "f2" then
editors.openEditor(2)
elseif key == "f3" then
editors.openEditor(3)
elseif key == "f4" then
editors.openEditor(4)
elseif key == "f5" then
editors.openEditor(5)
elseif key == "f6" then
editors.openEditor(6)
else
handled = false
end
end
if key == "f7" then
local s = love.graphics.newScreenshot(false)
local file = string.format("neko8-%s.png", os.time())
s:encode("png", file)
api.smes("saved screenshot")
elseif key == "f8" then
gif = giflib.new(string.format("neko8-%s.gif", os.time()), config.palette)
api.smes("started recording gif")
elseif key == "f9" then
if not gif then return end
gif:close()
gif = nil
api.smes("saved gif")
else
handled = false
end
end
if not handled then
triggerCallback(
"_keydown", key, isRepeat
)
end
end
function love.keyreleased(key)
for p = 0, 1 do
for i = 0, #api.keyMap[p] do
for _, k
in pairs(api.keyMap[p][i]) do
if key == k then
api.keyPressed[p][i] = nil
break
end
end
end
end
triggerCallback("_keyup", key)
end
function try(f, catch, finally)
local status, result = pcall(f)
if not status then
catch(result)
elseif finally then
return finally(result)
end
end
function runtimeError(error)
api.clip()
api.camera(0, 0)
log.error("runtime error:")
log.error(error)
editors.close()
neko.cart = nil
if error then
local pos = string.find(error, "\"]:")
if pos then
error = string.format("line %s", error:sub(pos + 3))
end
neko.core.sandbox.redraw_prompt(true)
api.print("")
api.color(8)
api.print(error)
end
api.print(debug.traceback("", 4):gsub("^%s*", ""), nil, nil, nil, true)
neko.core.sandbox.redraw_prompt()
end
function syntaxError(error)
api.camera(0, 0)
api.clip()
log.error("syntax error:")
log.error(error)
editors.close()
neko.cart = nil
if error then
local pos = string.find(error, "\"]:")
if pos then
error = string.format("line %s", error:sub(pos + 3))
end
neko.core.sandbox.redraw_prompt(true)
api.print("")
api.color(8)
api.print(error)
neko.core.sandbox.redraw_prompt()
end
end
function replaceChar(pos, str, r)
return string.sub(str, 1, pos - 1) .. r .. string.sub(str, pos + 1)
end
local function toUTF8(st)
if st <= 0x7F then
return string.char(st)
end
if st <= 0x7FF then
local byte0 = 0xC0 + math.floor(st / 0x40)
local byte1 = 0x80 + (st % 0x40)
return string.char(byte0, byte1)
end
if st <= 0xFFFF then
local byte0 = 0xE0 + math.floor(st / 0x1000)
local byte1 = 0x80 + (math.floor(st / 0x40) % 0x40)
local byte2 = 0x80 + (st % 0x40)
return string.char(byte0, byte1, byte2)
end
return ""
end
function validateText(text)
for i = 1, #text do
local c = string.sub(text, i, i)
local valid = c == " " or c == "\n" or c == "\r" or c == "\t"
if not valid then
for j = 1, #config.font.letters do
local ch = string.sub(config.font.letters, j, j)
if c == ch then
valid = true
break
end
end
end
if not valid then
text = ""
end
end
--[[if #text == 1 and api.key("ralt")
or api.key("lalt") then
local c = string.byte(string.sub(text, 1, 1))
if c and c >= 97
and c <= 122 then
text = replaceChar(
1, text, toUTF8(c + 95)
)
end
end--]]
-- todo
return text
end
function love.textinput(text)
text = validateText(text)
triggerCallback("_text", text)
end
function love.focus(focus)
neko.focus = focus
end
function love.run()
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
if love.timer then love.timer.step() end
local dt = 0
while true do
if love.event then
love.event.pump()
for e, a, b, c, d in
love.event.poll() do
if e == "quit" then
if not love.quit
or not love.quit() then
if love.audio then
love.audio.stop()
end
return
end
end
love.handlers[e](a,b,c,d)
end
end
if love.timer then
love.timer.step()
dt = dt + love.timer.getDelta()
end
local render = false
while dt >= frameTime do
hostTime = hostTime + dt
if hostTime >= 65536 then
hostTime = hostTime - 65536
end
if love.update then
love.update(frameTime)
audio.update(frameTime)
end
dt = dt - frameTime
render = true
end
if render and love.window
and love.graphics
and love.window.isCreated() then
love.graphics.origin()
if love.draw then love.draw() end
end
if love.timer then
love.timer.sleep(0.001)
end
end
end
function triggerCallback(c, ...)
if neko.cart then
if neko.cart.sandbox[c] then
local v = nil
local args = {...}
try(function()
v = neko.cart.sandbox[c](unpack(args))
end, runtimeError)
return v
end
elseif editors.opened then
if editors.current[c] then
return editors.current[c](...)
end
elseif neko.core.sandbox[c] then
return neko.core.sandbox[c](...)
end
return nil
end
-----------------------------------------
-- canvas helpers
-----------------------------------------
canvas = {
x = 0,
y = 0,
scaleX = 1,
scaleY = 1
}
function initCanvas()
canvas.renderable =
love.graphics.newCanvas(
config.canvas.width,
config.canvas.height
)
canvas.renderable:setFilter(
"nearest", "nearest"
)
canvas.support =
love.graphics.newCanvas(
config.canvas.width,
config.canvas.height
)
canvas.support:setFilter(
"nearest", "nearest"
)
canvas.gif =
love.graphics.newCanvas(
config.canvas.width * config.canvas.gifScale,
config.canvas.height * config.canvas.gifScale
)
canvas.gif:setFilter(
"nearest", "nearest"
)
resizeCanvas(
love.graphics.getWidth(),
love.graphics.getHeight()
)
end
function resizeCanvas(width, height)
local size = math.min(
width / config.canvas.width,
height / config.canvas.height
)
if not mobile then
size = math.floor(size)
end
canvas.scaleX = size
canvas.scaleY = size
canvas.x = (width - size * config.canvas.width) / 2
if mobile then
canvas.y = 0
else
canvas.y = (height - size * config.canvas.height) / 2
end
end
-----------------------------------------
-- font
-----------------------------------------
function initFont()
love.graphics.setDefaultFilter("nearest", "nearest")
font = love.graphics.newFont(
config.font.file, 4
)
font:setFilter("nearest", "nearest")
love.graphics.setFont(font)
end
-----------------------------------------
-- shaders
-----------------------------------------
colors = {}
colors.current = 7
function initPalette()
colors.palette = config.palette
colors.display = {}
colors.draw = {}
colors.sprites = {}
colors.transparent = {}
for i = 0, 15 do
colors.transparent[i] = (i == 0 and 0 or 1) --Black is transparent by default.
colors.draw[i] = i - 1
colors.sprites[i] = i - 1
colors.display[i] = config.palette[i]
end
colors.display[16] = { 0, 0, 0, 0 } --A bug in unpack ???
colors.draw[16] = 0
colors.sprites[16] = 0
colors.transparent[16] = 0
colors.drawShader =
love.graphics.newShader("assets/draw.frag")
colors.drawShader:send(
"palette",
unpack(colors.draw)
)
colors.spriteShader =
love.graphics.newShader("assets/sprite.frag")
colors.spriteShader:send(
"palette",
unpack(colors.sprites)
)
colors.spriteShader:send(
"transparent",
unpack(colors.transparent)
)
colors.displayShader =
love.graphics.newShader("assets/display.frag")
colors.displayShader:send(
"palette",
unpack(colors.display)
)
colors.supportShader = love.graphics.newShader("assets/support.frag")
end