forked from picolove/picolove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.lua
1622 lines (1461 loc) · 35.5 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 api = {}
local flr = math.floor
local ceil = math.ceil
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 + 0.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._getcursorx()
return pico8.cursor[1]
end
function api._getcursory()
return pico8.cursor[2]
end
function api._call(code)
code = patch_lua(code)
local ok, f, e = pcall(load, code, "repl")
if not ok or f == nil then
api.print("syntax error", nil, nil, 14)
api.print(api.sub(e, 20), nil, nil, 6)
return false
else
setfenv(f, pico8.cart)
ok, e = pcall(f)
if not ok then
api.print("runtime error", nil, nil, 14)
api.print(api.sub(e, 20), nil, nil, 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) * drawscale
pico8.camera_y = flr(tonumber(y) or 0) * drawscale
restore_camera()
end
function api.clip(x, y, w, h)
if type(x) == "number" then
x = x * drawscale
y = y * drawscale
w = w * drawscale
h = h * drawscale
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(c)
c = flr(tonumber(c) or 0) % 16
c = c + 1 -- TODO: fix workaround
love.graphics.clear(c * 16, 0, 0, 255)
pico8.cursor = {0, 0}
end
function api.folder()
love.system.openURL("file://" .. love.filesystem.getWorkingDirectory())
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]).filetype == "directory" 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.isDirectory(currentDirectory .. file) 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.color(12)
api.print(#output .. " files")
for _, item in ipairs(output) do
api.color(item.color)
for j = 1, #item.name, 32 do
api.print(item.name:sub(j, j + 32))
flip_screen()
count = count + 1
if count == 20 then
api.print("--more--", nil, nil, 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 implementatn into nocart
-- TODO: should return table of strings
function api.ls()
local files = love.filesystem.getDirectoryItems(currentDirectory)
api.print("directory: " .. currentDirectory, nil, nil, 12)
local output = {}
for _, file in ipairs(files) do
if love.filesystem.isDirectory(currentDirectory .. file) 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
api.color(item.color)
for j = 1, #item.name, 32 do
api.print(item.name:sub(j, j + 32))
flip_screen()
count = count + 1
if count == 20 then
api.print("--more--", nil, nil, 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
api.dir = api.ls
function api.cd(name)
local output, count
name = name .. "/"
-- 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.exists(newDirectory) then
currentDirectory = newDirectory
output = currentDirectory
else
failed = true
output = "directory not found"
end
if not failed then
api.color(12)
for i = 1, #output, 32 do
api.print(output:sub(i, i + 32))
end
else
api.color(7)
api.print(output)
end
end
function api.mkdir(name)
if name == nil then
api.color(6)
api.print("mkdir [name]")
else
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, c)
if c then
color(c)
end
if pointmode == "circle" then
love.graphics.circle("fill",flr(x*drawscale + drawscale / 2), flr(y*drawscale + drawscale / 2), drawscale / 2)
elseif pointmode == "rectanglestrict" then
love.graphics.rectangle("fill",flr(x)*drawscale, flr(y)*drawscale,drawscale,drawscale)
else
love.graphics.rectangle("fill",flr(x*drawscale), flr(y*drawscale),drawscale,drawscale)
end
end
function api.pget(x, 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*drawscale), flr(y*drawscale))
return flr(r / 17.0)
end
warning(string.format("pget out of screen %d, %d", x, y))
return 0
end
function api.color(c)
color(c)
end
-- workaround for non printable chars
local tostring_org = tostring
local function tostring(str)
return tostring_org(str):gsub("[^%z\32-\127]", "8")
end
function api.print(str, x, y, col)
--TODO: support printing special pico8 chars
if col 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(str):gsub("[^%z\32-\127]", " ")
love.graphics.setShader(pico8.text_shader)
love.graphics.print(to_print, flr(x*drawscale), flr(y*drawscale),0,drawscale/fontscale,drawscale/fontscale)
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*drawscale, y*drawscale}
end
function api.tonum(val)
return tonumber(val) -- not a direct assignment to prevent usage of the radix argument
end
function api.tostr(val, hex)
local kind = type(val)
if kind == "string" then
return val
elseif kind == "number" then
if hex then
val = val * 0x10000
local part1 = bit.rshift(bit.band(val, 0xFFFF0000), 16)
local part2 = bit.band(val, 0xFFFF)
return string.format("0x%04x.%04x", part1, part2)
else
return tostring(val)
end
elseif kind == "boolean" then
return tostring(val)
else
return "[" .. kind .. "]"
end
end
function api.spr(n, x, y, w, h, flip_x, flip_y)
love.graphics.setShader(pico8.sprite_shader)
pico8.sprite_shader:send("transparent", shdr_unpack(pico8.pal_transparent))
n = flr(n)
w = w or 1
h = 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
love.graphics.draw(
pico8.spritesheet,
q,
flr(x*drawscale) + (w * 8 * drawscale * (flip_x and 1 or 0)),
flr(y*drawscale) + (h * 8 * drawscale *(flip_y and 1 or 0)),
0,
flip_x and (0-drawscale) or drawscale,
flip_y and (0-drawscale) or drawscale
)
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
dw = dw * drawscale / spritescale
dh = dh * drawscale / spritescale
dx = dx or 0
dy = dy or 0
-- FIXME: cache this quad
local q =
love.graphics.newQuad(sx*spritescale, sy*spritescale, sw*spritescale, sh*spritescale, pico8.spritesheet:getDimensions())
love.graphics.setShader(pico8.sprite_shader)
pico8.sprite_shader:send("transparent", shdr_unpack(pico8.pal_transparent))
love.graphics.draw(
pico8.spritesheet,
q,
flr(dx*drawscale) + (flip_x and dw or 0)*spritescale,
flr(dy*drawscale) + (flip_y and dh or 0)*spritescale,
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)
if col then
color(col)
end
love.graphics.setLineWidth(drawscale)
love.graphics.rectangle(
"line",
flr(x0*drawscale+ceil(drawscale/2)),
flr(y0*drawscale+ceil(drawscale/2)),
flr(x1*drawscale - x0*drawscale - ceil(drawscale/2))+1,
flr(y1*drawscale - y0*drawscale - ceil(drawscale/2))+1
)
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*drawscale+ceil(drawscale/2)-1),
flr(y0*drawscale+ceil(drawscale/2)-1),
flr(x1*drawscale - x0*drawscale+drawscale),
flr(y1*drawscale - y0*drawscale+drawscale)
)
end
function api.circ(ox, oy, r, col)
if col then
color(col)
end
ox = flr(ox*drawscale)
oy = flr(oy*drawscale)
r = flr(r*drawscale)
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*drawscale)
cy = flr(cy*drawscale)
r = flr(r*drawscale)
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.line(x0, y0, x1, y1, col)
if col then
color(col)
end
if x0 ~= x0 or y0 ~= y0 or x1 ~= x1 or y1 ~= y1 then
warning("line has NaN value")
return
end
x0 = flr(x0*drawscale)
y0 = flr(y0*drawscale)
x1 = flr(x1*drawscale)
y1 = flr(y1*drawscale)
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.setPointSize(drawscale)
love.graphics.points(points)
end
local __palette_modified = true
function api.pal(c0, c1, p)
if type(c0) ~= "number" then
if __palette_modified == false then
return
end
for i = 1, 16 do
pico8.draw_palette[i] = i
pico8.display_palette[i] = pico8.palette[i]
end
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))
pico8.display_shader:send("palette", shdr_unpack(pico8.display_palette))
__palette_modified = false
-- 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
c1 = c1 + 1
c0 = c0 + 1
pico8.display_palette[c0] = pico8.palette[c1]
pico8.display_shader:send("palette", shdr_unpack(pico8.display_palette))
__palette_modified = true
elseif c1 ~= nil then
c0 = flr(c0) % 16
c1 = flr(c1) % 16
c1 = c1 + 1
c0 = c0 + 1
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))
__palette_modified = true
end
end
function api.palt(c, t)
if type(c) ~= "number" then
for i = 1, 16 do
pico8.pal_transparent[i] = i == 1 and 0 or 1
end
else
c = flr(c) % 16
pico8.pal_transparent[c + 1] = t and 0 or 1
end
pico8.sprite_shader:send("transparent", shdr_unpack(pico8.pal_transparent))
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(255, 255, 255, 255)
cel_x = flr(cel_x or 0)
cel_y = flr(cel_y or 0)
sx = flr(sx*drawscale or 0)
sy = flr(sy*drawscale or 0)
cel_w = flr(cel_w or 128)
cel_h = flr(cel_h or 64)
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 == nil or bitmask == 0 then
love.graphics.draw(
pico8.spritesheet,
pico8.quads[v],
sx + 8 * x * drawscale,
sy + 8 * y * drawscale,
0,
drawscale,
drawscale
)
else
if bit.band(pico8.spriteflags[v], bitmask) ~= 0 then
love.graphics.draw(
pico8.spritesheet,
pico8.quads[v],
sx + 8 * x * drawscale,
sy + 8 * y * drawscale,
0,
drawscale,
drawscale
)
end
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(x or 0)
y = flr(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(x or 0)
y = flr(y or 0)
v = flr(v or 0) % 256
if x >= 0 and x < 128 and y >= 0 and y < 64 then
pico8.map[y][x] = v
end
end
function api.fget(n, f)
if n == nil then
return nil
end
if f ~= nil then
-- return just that bit as a boolean
if not pico8.spriteflags[flr(n)] then
warning(string.format("fget(%d, %d)", n, f))
return false
end
return bit.band(pico8.spriteflags[flr(n)], bit.lshift(1, flr(f))) ~= 0
end
return pico8.spriteflags[flr(n)] or 0
end
function api.fset(n, f, v)
-- fset n [f] v
-- f is the flag index 0..7
-- v is boolean
if v == nil then
v, f = f, nil
end
if f then
-- set specific bit to v (true or false)
if v then
pico8.spriteflags[n] = bit.bor(pico8.spriteflags[n], bit.lshift(1, f))
else
pico8.spriteflags[n] =
bit.band(pico8.spriteflags[n], bit.bnot(bit.lshift(1, f)))
end
else
-- set bitfield to v (number)
pico8.spriteflags[n] = v
end
end
function api.sget(x, y)
-- return the color from the spritesheet
x = flr(tonumber(x) or 0)
y = flr(tonumber(y) or 0)
if x >= 0 and x < 128 and y >= 0 and y < 128 then
local c = pico8.spritesheet_data:getPixel(x, y)
return flr(c / 16)
end
return 0
end
function api.sset(x, y, c)
x = flr(tonumber(x) or 0)
y = flr(tonumber(y) or 0)
c = flr(tonumber(c) or 0)
pico8.spritesheet_data:setPixel(x, y, c * 16, 0, 0, 255)
pico8.spritesheet:refresh()
end
function api.music(n, fade_len, channel_mask) -- luacheck: no unused
-- TODO: implement fade out
if n == -1 then
if pico8.current_music then
for i = 0, 3 do
if pico8.music[pico8.current_music.music][i] < 64 then
pico8.audio_channels[i].sfx = nil
pico8.audio_channels[i].offset = 0
pico8.audio_channels[i].last_step = -1
end
end
pico8.current_music = nil
end
return
end
local m = pico8.music[n]
if not m then
warning(string.format("music %d does not exist", n))
return
end
local music_speed = nil
local music_channel = nil
for i = 0, 3 do
if m[i] < 64 then
local sfx = pico8.sfx[m[i]]
if music_speed == nil or music_speed > sfx.speed then
music_speed = sfx.speed
music_channel = i
end
end
end
pico8.audio_channels[music_channel].loop = false
pico8.current_music = {
music = n,
offset = 0,
channel_mask = channel_mask or 15,
speed = music_speed
}
for i = 0, 3 do
if pico8.music[n][i] < 64 then
pico8.audio_channels[i].sfx = pico8.music[n][i]
pico8.audio_channels[i].offset = 0
pico8.audio_channels[i].last_step = -1
end
end
end
function api.sfx(n, channel, offset)
-- n = -1 stop sound on channel
-- n = -2 to stop looping on channel
channel = channel or -1
if n == -1 and channel >= 0 then
pico8.audio_channels[channel].sfx = nil
return
elseif n == -2 and channel >= 0 then
pico8.audio_channels[channel].loop = false
end
offset = offset or 0
if channel == -1 then
-- find a free channel
for i = 0, 3 do
if pico8.audio_channels[i].sfx == nil then
channel = i
end
end
end
if channel == -1 then
return
end
local ch = pico8.audio_channels[channel]
ch.sfx = n
ch.offset = offset
ch.last_step = offset - 1
ch.loop = true
end
function api.peek(addr)
addr = flr(tonumber(addr) or 0)
if addr < 0 then
return 0
elseif addr < 0x2000 then -- luacheck: ignore 542
-- TODO: spritesheet data
elseif addr < 0x3000 then
addr = addr - 0x2000
return pico8.map[flr(addr / 128)][addr % 128]
elseif addr < 0x3100 then
return pico8.spriteflags[addr - 0x3000]
elseif addr < 0x3200 then -- luacheck: ignore 542
-- TODO: music data
elseif addr < 0x4300 then -- luacheck: ignore 542
-- TODO: sfx data
elseif addr < 0x5e00 then
return pico8.usermemory[addr - 0x4300]
elseif addr < 0x5f00 then
local val = pico8.cartdata[flr((addr - 0x5e00) / 4)] * 0x10000
local shift = (addr % 4) * 8
return bit.rshift(bit.band(val, bit.lshift(0xFF, shift)), shift)
elseif addr < 0x5f40 then
-- TODO: draw state
if addr == 0x5f20 then
return pico8.clip[1]
elseif addr == 0x5f21 then
return pico8.clip[2]
elseif addr == 0x5f22 then
return pico8.clip[1] + pico8.clip[3]
elseif addr == 0x5f23 then
return pico8.clip[2] + pico8.clip[4]
elseif addr == 0x5f25 then
return pico8.color
elseif addr == 0x5f26 then
return pico8.cursor[1]
elseif addr == 0x5f27 then
return pico8.cursor[2]
elseif addr == 0x5f28 then
return pico8.camera_x % 256
elseif addr == 0x5f29 then
return flr(pico8.camera_x / 256)
elseif addr == 0x5f2a then
return pico8.camera_y % 256
elseif addr == 0x5f2b then