forked from gonengazit/Celia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.lua
1962 lines (1756 loc) · 42.7 KB
/
api.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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local bit = require("numberlua").bit
local api = {}
local flr = math.floor
local function color(c)
c = flr(c or 0) % 16
pico8.color = c
setColor(c)
end
local function warning(msg)
log(debug.traceback("WARNING: " .. msg, 3))
end
local function _horizontal_line(lines, x0, y, x1)
table.insert(lines, { x0 + 0.5, y + 0.5, x1 + 1.5, y + 0.5 })
end
local function _plot4points(lines, cx, cy, x, y)
_horizontal_line(lines, cx - x, cy + y, cx + x)
if y ~= 0 then
_horizontal_line(lines, cx - x, cy - y, cx + x)
end
end
local function scroll(pixels)
local base = 0x6000
local delta = base + pixels * 0x40
local basehigh = 0x8000
api.memcpy(base, delta, basehigh - delta)
end
local function setfps(fps)
pico8.fps = flr(fps)
if pico8.fps <= 0 then
pico8.fps = 30
end
pico8.frametime = 1 / pico8.fps
end
local function getmousex()
return flr((love.mouse.getX() - xpadding) / scale)
end
local function getmousey()
return flr((love.mouse.getY() - ypadding) / scale)
end
-- extra functions provided by picolove
api.warning = warning
api.setfps = setfps
function api._picolove_end()
if
not pico8.cart._update
and not pico8.cart._update60
and not pico8.cart._draw
then
api.printh("cart finished")
end
end
function api._getpicoloveversion()
return __picolove_version
end
function api._getcursorx()
return pico8.cursor[1]
end
function api._getcursory()
return pico8.cursor[2]
end
function api._call(code)
code = patch_lua(code)
local f, e = loadstring(code, "repl")
if f == nil then
api.rectfill(0, api._getcursory(), 128, api._getcursory() + 5 + 6, 0)
api.print("syntax error", 14)
api.print(api.sub(e, 20), 6)
return false
else
setfenv(f, pico8.cart)
local ok, e = pcall(f)
if not ok then
api.rectfill(0, api._getcursory(), 128, api._getcursory() + 5 + 6, 0)
api.print("runtime error", 14)
api.print(api.sub(e, 20), 6)
end
end
return true
end
--------------------------------------------------------------------------------
-- PICO-8 API
function api.flip()
flip_screen()
love.timer.sleep(pico8.frametime)
end
function api.camera(x, y)
pico8.camera_x = flr(tonumber(x) or 0)
pico8.camera_y = flr(tonumber(y) or 0)
restore_camera()
end
function api.clip(x, y, w, h)
if type(x) == "number" then
love.graphics.setScissor(x, y, w, h)
pico8.clip = { x, y, w, h }
else
love.graphics.setScissor(0, 0, pico8.resolution[1], pico8.resolution[2])
pico8.clip = { 0, 0, pico8.resolution[1], pico8.resolution[2] }
end
end
function api.cls(col)
col = flr(tonumber(col) or 0) % 16
pico8.clip = nil
love.graphics.setScissor()
love.graphics.clear(col / 15, 0, 0, 1)
pico8.cursor = { 0, 0 }
end
function api.folder(dir)
if dir == nil then
love.system.openURL(
"file://" .. love.filesystem.getWorkingDirectory() .. currentDirectory
)
elseif dir == "bbs" then
api.print("not implemented", 14)
elseif dir == "backups" then
api.print("not implemented", 14)
elseif dir == "config" then
api.print("not implemented", 14)
elseif dir == "desktop" then
love.system.openURL(
"file://" .. love.filesystem.getUserDirectory() .. "Desktop"
)
else
api.print("useage: folder [location]", 14)
api.print("locations:", 6)
api.print("backups bbs config desktop", 6)
end
end
function api._completecommand(command, path)
-- TODO: handle depending on command
local startDir = ""
local pos = path:find("/", 1, true)
if pos ~= nil then
startDir = startDir .. path:sub(1, pos)
path = path:sub(pos + 1)
end
local files = love.filesystem.getDirectoryItems(currentDirectory .. startDir)
local filteredFiles = {}
for _, file in ipairs(files) do
if string.sub(file:lower(), 1, string.len(path)) == path then
filteredFiles[#filteredFiles + 1] = file
end
end
files = filteredFiles
local result
if #files == 0 then
result = path
elseif #files == 1 then
if
love.filesystem.getInfo(currentDirectory .. startDir .. files[1], "directory") ~= nil
then
result = files[1]:lower() .. "/"
else
result = files[1]:lower()
end
else
local matches
local match = path
repeat
result = match
if #match == #files[1] then
break
end
match = files[1]:sub(1, #match + 1)
matches = 0
for _, file in ipairs(files) do
if string.sub(file:lower(), 1, string.len(match)) == match then
matches = matches + 1
end
end
until matches ~= #files
result = result:lower()
if #result == #path then
-- TODO: remove duplicate code (see api.ls())
local output = {}
for _, file in ipairs(files) do
if love.filesystem.getInfo(currentDirectory .. file, "directory") ~= nil then
output[#output + 1] = { name = file:lower(), color = 14 }
elseif file:sub(-3) == ".p8" or file:sub(-4) == ".png" then
output[#output + 1] = { name = file:lower(), color = 6 }
else
output[#output + 1] = { name = file:lower(), color = 5 }
end
end
local count = 0
love.keyboard.setTextInput(false)
api.rectfill(0, api._getcursory(), 127, api._getcursory() + 6, 0)
api.print(#output .. " files", 12)
for _, item in ipairs(output) do
for j = 1, #item.name, 32 do
api.rectfill(0, api._getcursory(), 127, api._getcursory() + 6, 0)
api.print(item.name:sub(j, j + 32), item.color)
flip_screen()
count = count + 1
if count == 20 then
api.rectfill(0, api._getcursory(), 127, api._getcursory() + 6, 0)
api.print("--more--", 12)
flip_screen()
local y = api._getcursory() - 6
api.cursor(0, y)
api.rectfill(0, y, 127, y + 6, 0)
api.color(item.color)
while true do
local e = love.event.wait()
if e == "keypressed" then
break
end
end
count = 0
end
end
end
love.keyboard.setTextInput(true)
end
end
return command .. " " .. startDir .. result
end
-- TODO: move interactive implementation into nocart
-- TODO: should return table of strings
function api.ls()
local files = love.filesystem.getDirectoryItems(currentDirectory)
api.rectfill(0, api._getcursory(), 128, api._getcursory() + 5, 0)
api.print("directory: " .. currentDirectory, 12)
local output = {}
for _, file in ipairs(files) do
if love.filesystem.getInfo(currentDirectory .. file, "directory") ~= nil then
output[#output + 1] = { name = file:lower(), color = 14 }
elseif file:sub(-3) == ".p8" or file:sub(-4) == ".png" then
output[#output + 1] = { name = file:lower(), color = 6 }
else
output[#output + 1] = { name = file:lower(), color = 5 }
end
end
local count = 0
love.keyboard.setTextInput(false)
for _, item in ipairs(output) do
for j = 1, #item.name, 32 do
api.rectfill(0, api._getcursory(), 128, api._getcursory() + 5, 0)
api.print(item.name:sub(j, j + 32), item.color)
flip_screen()
count = count + 1
if count == 20 then
api.rectfill(0, api._getcursory(), 128, api._getcursory() + 5, 0)
api.print("--more--", 12)
flip_screen()
local y = api._getcursory() - 6
api.cursor(0, y)
api.rectfill(0, y, 127, y + 6, 0)
api.color(item.color)
while true do
local e, a = love.event.wait()
if e == "keypressed" then
if a == "escape" then
love.keyboard.setTextInput(true)
return
else
love.event.clear() -- consume keypress
end
break
end
end
count = 0
end
end
end
love.keyboard.setTextInput(true)
end
api.dir = api.ls
function api.cd(name)
local output, count
if #name > 0 then
name = name .. "/"
end
-- filter /TEXT//$ -> /
count = 1
while count > 0 do
name, count = name:gsub("//", "/")
end
local newDirectory = currentDirectory .. name
if name == "/" then
newDirectory = "/"
end
-- filter /TEXT/../ -> /
count = 1
while count > 0 do
newDirectory, count = newDirectory:gsub("/[^/]*/%.%./", "/")
end
-- filter /TEXT/..$ -> /
count = 1
while count > 0 do
newDirectory, count = newDirectory:gsub("/[^/]*/%.%.$", "/")
end
local failed = newDirectory:find("%.%.") ~= nil
failed = failed or newDirectory:find("/[ ]+/") ~= nil
if #name == 0 then
output = "directory: " .. currentDirectory
elseif failed then
if newDirectory == "/../" then
output = "cd: failed"
else
output = "directory not found"
end
elseif love.filesystem.getInfo(newDirectory) ~= nil then
currentDirectory = newDirectory
output = currentDirectory
else
failed = true
output = "directory not found"
end
if not failed then
api.rectfill(
0,
api._getcursory(),
128,
api._getcursory() + 5 + api.flr(#output / 32) * 6,
0
)
api.color(12)
for i = 1, #output, 32 do
api.print(output:sub(i, i + 32))
end
else
api.rectfill(0, api._getcursory(), 128, api._getcursory() + 5, 0)
api.print(output, 7)
end
end
function api.mkdir(...)
local name = select(1, ...)
if select("#", ...) == 0 then
api.rectfill(0, api._getcursory(), 128, api._getcursory() + 5, 0)
api.print("mkdir [name]", 6)
elseif name ~= nil then
love.filesystem.createDirectory(currentDirectory .. name)
end
end
function api.install_demos()
-- TODO: implement this
end
function api.install_games()
-- TODO: implement this
end
function api.keyconfig()
-- TODO: implement this
end
function api.splore()
-- TODO: implement this
end
function api.pset(x, y, col)
if col then
color(col)
end
love.graphics.point(flr(x), flr(y))
end
function api.pget(x, y)
x= x - pico8.camera_x
y= y - pico8.camera_y
if
x >= 0
and x < pico8.resolution[1]
and y >= 0
and y < pico8.resolution[2]
then
love.graphics.setCanvas()
local __screen_img = pico8.screen:newImageData()
love.graphics.setCanvas(pico8.screen)
local r = __screen_img:getPixel(flr(x), flr(y))
return r * 15
end
warning(string.format("pget out of screen %d, %d", x, y))
return 0
end
function api.color(col)
color(col)
end
-- workaround for non printable chars
local tostring_org = tostring
local function tostring(str)
return tostring_org(str)
--return (tostring_org(str):gsub("[^%z\32-\127]", "8"))
end
function api.print(...)
--TODO: support printing special pico8 chars
local argc = select("#", ...)
if argc == 0 then
return
end
local x = nil
local y = nil
local col = nil
local str = select(1, ...)
if argc == 2 then
col = select(2, ...) or 0
elseif argc > 2 then
x = select(2, ...) or 0
y = select(3, ...) or 0
if argc >= 4 then
col = select(4, ...) or 0
end
end
if col ~= nil then
color(col)
end
local canscroll = y == nil
if y == nil then
y = pico8.cursor[2]
pico8.cursor[2] = pico8.cursor[2] + 6
end
if x == nil then
x = pico8.cursor[1]
end
if canscroll and y > 121 then
local c = col or pico8.color
scroll(6)
y = 120
api.rectfill(0, y, 127, y + 6, 0)
api.color(c)
api.cursor(0, y + 6)
end
local to_print = tostring(api.tostr(str))
to_print=to_print:gsub('.', function (c)
-- print(c, string.byte(c), pico8_glyphs[string.byte(c)])
local gl = pico8_glyphs[string.byte(c)]
if not gl then return c end
return glyph_edgecases[gl] or gl end)
love.graphics.setShader(pico8.text_shader)
love.graphics.print(to_print, flr(x), flr(y))
end
api.printh = print
function api.cursor(x, y, col)
if col then
color(col)
end
x = flr(tonumber(x) or 0) % 256
y = flr(tonumber(y) or 0) % 256
pico8.cursor = { x, y }
end
function api.tonum(val, format)
local kind = type(val)
if kind ~= "number" and kind ~= "string" and kind ~= "boolean" then
return
elseif kind == "number" then
return val
end
if type(format) == "string" then
format = tonumber(format)
elseif type(format) ~= "number" then
format = nil
end
local base = 10
local shift = false
local zeroreturn = false
if type(format) == "number" then
base = bit.band(format, 1) ~= 0 and 16 or 10
shift = bit.band(format, 2) ~= 0
zeroreturn = bit.band(format, 4) ~= 0
end
if kind == "boolean" then
val = val and 1 or 0
return shift and 0 or val
end
local result = tonumber(val, base)
if result ~= nil then
return shift and result / 0x10000 or result
elseif zeroreturn then
return 0
end
end
function api.chr(num)
--GTODO: stuff
local n = tonumber(num)
if n == nil then
return
end
n = n % 256
return tostring(string.char(n))
end
function api.ord(...)
local str = select(1, ...)
if str == nil then
return nil
end
local argc = select("#", ...)
local index = select(2, ...) or 0
local count = select(3, ...) or 0
if argc == 1 then
return string.byte(str)
elseif argc == 2 then
return string.byte(str, index)
elseif argc >= 3 then
local values = {}
for i = 1, count do
if index + i > 1 then
values[i] = string.byte(str, index + i - 1)
api.printh(values[i], i)
end
end
return unpack(values, 1, count)
end
return nil
end
function api.tostr(...)
if select("#", ...) == 0 then
return ""
end
local val = select(1, ...)
local kind = type(val)
if kind == "string" then
return val
elseif kind == "number" then
local format = select(2, ...)
if format == true then
format = 1
end
if format and bit.band(format, 1) ~= 0 then
val = val * 0x10000
local part1 = bit.rshift(bit.band(val, 0xFFFF0000), 16)
local part2 = bit.band(val, 0xFFFF)
if bit.band(format, 2) ~= 0 then
return string.format("0x%04x%04x", part1, part2)
else
return string.format("0x%04x.%04x", part1, part2)
end
else
if format and bit.band(format, 2) ~= 0 then
val = val * 0x10000
end
return tostring(val)
end
elseif kind == "boolean" then
return tostring(val)
else
return "[" .. kind .. "]"
end
end
--sync spritesheet_data and spritesheet
local function refresh_spritesheet()
if pico8.spritesheet_changed then
pico8.spritesheet_changed=false
pico8.spritesheet:replacePixels(pico8.spritesheet_data)
end
end
function api.spr(n, x, y, w, h, flip_x, flip_y)
love.graphics.setShader(pico8.sprite_shader)
n = flr(tonumber(n) or 0)
x = tonumber(x) or 0
y = tonumber(y) or 0
w = tonumber(w) or 1
h = tonumber(h) or 1
local q
if w == 1 and h == 1 then
q = pico8.quads[n]
if not q then
-- log("warning: sprite " .. n .. " is missing")
return
end
else
local id = string.format("%d-%d-%d", n, w, h)
if pico8.quads[id] then
q = pico8.quads[id]
else
q = love.graphics.newQuad(
flr(n % 16) * 8,
flr(n / 16) * 8,
8 * w,
8 * h,
128,
128
)
pico8.quads[id] = q
end
end
if not q then
log("missing quad", n)
end
refresh_spritesheet()
love.graphics.draw(
pico8.spritesheet,
q,
flr(x) + (w * 8 * (flip_x and 1 or 0)),
flr(y) + (h * 8 * (flip_y and 1 or 0)),
0,
flip_x and -1 or 1,
flip_y and -1 or 1
)
love.graphics.setShader(pico8.draw_shader)
end
function api.sspr(sx, sy, sw, sh, dx, dy, dw, dh, flip_x, flip_y)
-- Stretch rectangle from sprite sheet (sx, sy, sw, sh) // given in pixels
-- and draw in rectangle (dx, dy, dw, dh)
-- Color 0 drawn as transparent by default (see palt())
-- dw, dh defaults to sw, sh
-- flip_x = true to flip horizontally
-- flip_y = true to flip vertically
dw = dw or sw
dh = dh or sh
-- FIXME: cache this quad
local q =
love.graphics.newQuad(sx, sy, sw, sh, pico8.spritesheet:getDimensions())
love.graphics.setShader(pico8.sprite_shader)
refresh_spritesheet()
love.graphics.draw(
pico8.spritesheet,
q,
flr(dx) + (flip_x and dw or 0),
flr(dy) + (flip_y and dh or 0),
0,
dw / sw * (flip_x and -1 or 1),
dh / sh * (flip_y and -1 or 1)
)
love.graphics.setShader(pico8.draw_shader)
end
function api.rect(x0, y0, x1, y1, col)
-- GTODO: x0=x1
if col then
color(col)
end
love.graphics.rectangle(
"line",
flr(x0) + 1,
flr(y0) + 1,
flr(x1 - x0),
flr(y1 - y0)
)
end
function api.rectfill(x0, y0, x1, y1, col)
if col then
color(col)
end
if x1 < x0 then
x0, x1 = x1, x0
end
if y1 < y0 then
y0, y1 = y1, y0
end
love.graphics.rectangle(
"fill",
flr(x0),
flr(y0),
flr(x1 - x0) + 1,
flr(y1 - y0) + 1
)
end
function api.circ(ox, oy, r, col)
if col then
color(col)
end
ox = flr(ox) + 1
oy = flr(oy) + 1
r = flr(r)
local points = {}
local x = r
local y = 0
local decisionOver2 = 1 - x
while y <= x do
table.insert(points, { ox + x, oy + y })
table.insert(points, { ox + y, oy + x })
table.insert(points, { ox - x, oy + y })
table.insert(points, { ox - y, oy + x })
table.insert(points, { ox - x, oy - y })
table.insert(points, { ox - y, oy - x })
table.insert(points, { ox + x, oy - y })
table.insert(points, { ox + y, oy - x })
y = y + 1
if decisionOver2 < 0 then
decisionOver2 = decisionOver2 + 2 * y + 1
else
x = x - 1
decisionOver2 = decisionOver2 + 2 * (y - x) + 1
end
end
if #points > 0 then
love.graphics.points(points)
end
end
function api.circfill(cx, cy, r, col)
if col then
color(col)
end
cx = flr(cx)
cy = flr(cy)
r = flr(r)
local x = r
local y = 0
local err = 1 - r
local lines = {}
while y <= x do
_plot4points(lines, cx, cy, x, y)
if err < 0 then
err = err + 2 * y + 3
else
if x ~= y then
_plot4points(lines, cx, cy, y, x)
end
x = x - 1
err = err + 2 * (y - x) + 3
end
y = y + 1
end
if #lines > 0 then
for i = 1, #lines do
love.graphics.line(lines[i])
end
end
end
function api.oval(x0, y0, x1, y1, r, col)
--TODO: implement
end
function api.ovalfill(x0, y0, x1, y1, r, col)
--TODO: implement
end
function api.line(x0, y0, x1, y1, col)
if col then
color(col)
end
x0 = flr(tonumber(x0) or 0) + 1
y0 = flr(tonumber(y0) or 0) + 1
x1 = flr(tonumber(x1) or 0) + 1
y1 = flr(tonumber(y1) or 0) + 1
local dx = x1 - x0
local dy = y1 - y0
local stepx, stepy
local points = { { x0, y0 } }
if dx == 0 then
-- simple case draw a vertical line
points = {}
if y0 > y1 then
y0, y1 = y1, y0
end
for y = y0, y1 do
table.insert(points, { x0, y })
end
elseif dy == 0 then
-- simple case draw a horizontal line
points = {}
if x0 > x1 then
x0, x1 = x1, x0
end
for x = x0, x1 do
table.insert(points, { x, y0 })
end
else
if dy < 0 then
dy = -dy
stepy = -1
else
stepy = 1
end
if dx < 0 then
dx = -dx
stepx = -1
else
stepx = 1
end
if dx > dy then
local fraction = dy - bit.rshift(dx, 1)
while x0 ~= x1 do
if fraction >= 0 then
y0 = y0 + stepy
fraction = fraction - dx
end
x0 = x0 + stepx
fraction = fraction + dy
table.insert(points, { flr(x0), flr(y0) })
end
else
local fraction = dx - bit.rshift(dy, 1)
while y0 ~= y1 do
if fraction >= 0 then
x0 = x0 + stepx
fraction = fraction - dy
end
y0 = y0 + stepy
fraction = fraction + dx
table.insert(points, { flr(x0), flr(y0) })
end
end
end
love.graphics.points(points)
end
function api.pal(c0, c1, p)
-- GTODO: 0 vs 1 indexing
-- GTODO: support other variants of this func
local __palette_modified = false
local __display_modified = false
if type(c0) ~= "number" then
for i = 0, 15 do
if pico8.draw_palette[i] ~= i then
pico8.draw_palette[i] = i
__palette_modified = true
end
if pico8.display_palette[i] ~= pico8.palette[i] then
pico8.display_palette[i] = pico8.palette[i]
__display_modified = true
end
end
if __palette_modified then
pico8.draw_shader:send("palette", shdr_unpack(pico8.draw_palette))
pico8.sprite_shader:send("palette", shdr_unpack(pico8.draw_palette))
pico8.text_shader:send("palette", shdr_unpack(pico8.draw_palette))
end
if __display_modified then
pico8.display_shader:send("palette", shdr_unpack(pico8.display_palette))
end
-- According to PICO-8 manual:
-- pal() to reset to system defaults (including transparency values)
api.palt()
elseif p == 1 and c1 ~= nil then
c0 = flr(c0) % 16
c1 = flr(c1) % 16
pico8.display_palette[c0] = pico8.palette[c1]
pico8.display_shader:send("palette", shdr_unpack(pico8.display_palette))
elseif c1 ~= nil then
c0 = flr(c0) % 16
c1 = flr(c1) % 16
if pico8.draw_palette[c0] ~= c1 then
pico8.draw_palette[c0] = c1
pico8.draw_shader:send("palette", shdr_unpack(pico8.draw_palette))
pico8.sprite_shader:send("palette", shdr_unpack(pico8.draw_palette))
pico8.text_shader:send("palette", shdr_unpack(pico8.draw_palette))
end
end
end
function api.palt(c, t)
local __alpha_modified=false
c = tonumber(c)
if c == nil then
for i = 0, 15 do
local v = i == 0 and 0 or 1
if pico8.pal_transparent[i] ~= v then
pico8.pal_transparent[i] = v
__alpha_modified = true
end
end
else
c = flr(c) % 16
local v = t and 0 or 1
if pico8.pal_transparent[c] ~= v then
pico8.pal_transparent[c] = v
__alpha_modified = true
end
end
if __alpha_modified then
pico8.sprite_shader:send("transparent", shdr_unpack(pico8.pal_transparent))
end
end
function api.fillp(_)
-- TODO: implement this
end
function api.map(cel_x, cel_y, sx, sy, cel_w, cel_h, bitmask)
love.graphics.setShader(pico8.sprite_shader)
love.graphics.setColor(1, 1, 1, 1)
refresh_spritesheet()
cel_x = flr(tonumber(cel_x) or 0)
cel_y = flr(tonumber(cel_y) or 0)
sx = flr(tonumber(sx) or 0)
sy = flr(tonumber(sy) or 0)
cel_w = flr(tonumber(cel_w) or 128)
cel_h = flr(tonumber(cel_h) or 64)
bitmask = tonumber(bitmask) or 0
for y = 0, cel_h - 1 do
if cel_y + y < 64 and cel_y + y >= 0 then
for x = 0, cel_w - 1 do
if cel_x + x < 128 and cel_x + x >= 0 then
local v = pico8.map[flr(cel_y + y)][flr(cel_x + x)]
if v > 0 then
if bitmask == 0 or
bit.band(pico8.spriteflags[v], bitmask) ~= 0 then
love.graphics.draw(
pico8.spritesheet,
pico8.quads[v],
sx + 8 * x,
sy + 8 * y
)
end
end
end
end
end
end
love.graphics.setShader(pico8.draw_shader)
end
-- deprecated pico-8 function
api.mapdraw = api.map
function api.mget(x, y)
x = flr(tonumber(x) or 0)
y = flr(tonumber(y) or 0)
if x >= 0 and x < 128 and y >= 0 and y < 64 then
return pico8.map[y][x]
end
return 0
end
function api.mset(x, y, v)
x = flr(tonumber(x) or 0)