Repository navigation
Expand file tree
/
Copy pathapi.c
More file actions
3008 lines (2564 loc) · 73.4 KB
/
Copy pathapi.c
File metadata and controls
3008 lines (2564 loc) · 73.4 KB
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
/** @file api.c
*
* A portable PICO-8 emulator written in C.
*
* Copyright (c) 2025-2026, Michael Fitzmayer. All rights reserved.
* SPDX-License-Identifier: MIT
*
**/
#include <SDL3/SDL.h>
#include <stdbool.h>
#include <stdint.h>
#include <wchar.h>
#include "z8lua/lauxlib.h"
#include "z8lua/lua.h"
#include "z8lua/fix32.h"
#include "auxiliary.h"
#include "app.h"
#include "core.h"
#include "memory.h"
#include "p8scii.h"
#define TO_BE_DONE \
static bool warning_printed = false; \
if (!warning_printed) { \
SDL_Log("%s not yet implemented.", __func__); \
warning_printed = true; \
} \
return 0
static fix32_t seed_lo, seed_hi;
static fix32_t seconds_since_start;
// Frame timing info provided by core.c (frame_start in ms, frame_ms target in ms).
// These are declared in api.h and set inside core.c each frame.
uint32_t pico8_frame_start = 0;
uint32_t pico8_frame_ms = 0;
// Touch button state (when SDL_HINT_MOUSE_TOUCH_EVENTS is used).
uint8_t touch_button_state_player_0 = 0;
uint8_t touch_button_state_player_1 = 0;
// Input state: tracks how many consecutive frames each button has been held.
// Index [player][button], players 0-1, buttons 0-5.
static uint8_t btn_held_frames[2][6];
// Auxiliary functions.
// Helper function to apply the camera offset from memory
// Camera values at 0x5f29-0x5f2c are stored as two signed 16-bit integers (pixel values)
// 0x5f29-0x5f2a: camera X (int16_t in pixels)
// 0x5f2b-0x5f2c: camera Y (int16_t in pixels)
static void apply_camera_offset(int* x, int* y)
{
int cam_x = 0;
int cam_y = 0;
// Read camera X as 16-bit signed little-endian integer
uint16_t cam_x_unsigned = pico8_ram[0x5f29] | ((uint16_t)pico8_ram[0x5f2a] << 8);
int16_t cam_x_int = (int16_t)cam_x_unsigned;
// Read camera Y as 16-bit signed little-endian integer
uint16_t cam_y_unsigned = pico8_ram[0x5f2b] | ((uint16_t)pico8_ram[0x5f2c] << 8);
int16_t cam_y_int = (int16_t)cam_y_unsigned;
// Return as integer pixel offsets
cam_x = cam_x_int;
cam_y = cam_y_int;
*x -= cam_x;
*y -= cam_y;
}
static void pset(int x, int y, int* color)
{
if (((unsigned)x | (unsigned)y) >= 128)
{
return;
}
uint16_t pattern = (pico8_ram[0x5f31] << 8) | pico8_ram[0x5f32];
uint8_t p8_color = color ? (*color & 0x0F) : pico8_ram[0x5f25];
/* Apply draw palette remapping to the color */
uint8_t pal_entry = pico8_ram[0x5f00 + p8_color];
//if (pal_entry & 0x10)
//{
// /* Color is transparent in the draw palette, skip drawing */
// return;
//}
p8_color = pal_entry & 0x0F;
uint16_t addr = 0x6000 + (y << 6) + (x >> 1);
/* Fast path: no fill-pattern active (the default state).
* A single OR of both bytes lets the compiler use one branch
* with no extra memory reads beyond what the slow path needs. */
if (pattern == 0)
{
if (x & 1)
{
pico8_ram[addr] = (pico8_ram[addr] & 0x0F) | (p8_color << 4);
}
else
{
pico8_ram[addr] = (pico8_ram[addr] & 0xF0) | p8_color;
}
return;
}
int row_shift = 12 - ((y & 3) << 2);
uint8_t nibble = (pattern >> row_shift) & 0x0F;
if (nibble & (1 << (3 - (x & 3))))
{
return;
}
uint8_t mask = (x & 1) ? 0x0F : 0xF0;
uint8_t shift = (x & 1) ? 4 : 0;
pico8_ram[addr] = (pico8_ram[addr] & mask) | (p8_color << shift);
}
static uint8_t pget(int x, int y)
{
if (((unsigned)x | (unsigned)y) >= 128)
{
return 0;
}
apply_camera_offset((int*)&x, (int*)&y);
uint16_t addr = 0x6000 + (y << 6) + (x >> 1);
uint8_t shift = (x & 1) ? 4 : 0;
uint8_t p8_color = (pico8_ram[addr] >> shift) & 0x0F;
return p8_color;
}
static void hline(int x0, int x1, int y, int* color)
{
if ((unsigned)y >= 128) return;
if (x0 > x1) {
int t = x0; x0 = x1; x1 = t;
}
if (x0 < 0) x0 = 0;
if (x1 > 127) x1 = 127;
if (x0 > x1) return;
uint16_t pattern = (pico8_ram[0x5f31] << 8) | pico8_ram[0x5f32];
uint8_t p8_color = color ? (*color & 0x0F) : pico8_ram[0x5f25];
if (pattern == 0)
{
/* Apply draw palette remapping to the color */
uint8_t pal_entry = pico8_ram[0x5f00 + p8_color];
p8_color = pal_entry & 0x0F;
uint8_t* row = &pico8_ram[0x6000 + (y << 6)];
uint8_t color_pair = p8_color | (p8_color << 4);
int bx0 = x0 >> 1;
int bx1 = x1 >> 1;
if (x0 & 1)
{
row[bx0] = (row[bx0] & 0x0F) | (p8_color << 4);
bx0++;
}
if (!(x1 & 1))
{
row[bx1] = (row[bx1] & 0xF0) | p8_color;
bx1--;
}
if (bx0 <= bx1)
{
SDL_memset(&row[bx0], color_pair, bx1 - bx0 + 1);
}
}
else
{
for (int x = x0; x <= x1; x++)
{
pset(x, y, color);
}
}
}
static void draw_circle(int cx, int cy, int radius, int* color, bool fill)
{
/* Fast paths for the small radii (r=0,1,2). */
if (fill)
{
if (radius == 0) {
pset(cx, cy, color); return;
}
if (radius == 1)
{
hline(cx - 1, cx + 1, cy, color);
hline(cx, cx, cy - 1, color);
hline(cx, cx, cy + 1, color);
return;
}
if (radius == 2)
{
hline(cx - 1, cx + 1, cy - 2, color);
hline(cx - 2, cx + 2, cy - 1, color);
hline(cx - 2, cx + 2, cy, color);
hline(cx - 2, cx + 2, cy + 1, color);
hline(cx - 1, cx + 1, cy + 2, color);
return;
}
}
int x = 0;
int y = radius;
int d = 3 - 2 * radius;
while (x <= y)
{
if (fill)
{
hline(cx - x, cx + x, cy + y, color);
hline(cx - x, cx + x, cy - y, color);
hline(cx - y, cx + y, cy + x, color);
hline(cx - y, cx + y, cy - x, color);
}
else
{
pset(cx + x, cy + y, color);
pset(cx - x, cy + y, color);
pset(cx + x, cy - y, color);
pset(cx - x, cy - y, color);
pset(cx + y, cy + x, color);
pset(cx - y, cy + x, color);
pset(cx + y, cy - x, color);
pset(cx - y, cy - x, color);
}
if (d < 0)
{
d += 4 * x + 6;
}
else
{
d += 4 * (x - y) + 10;
y--;
}
x++;
}
}
static void draw_line(int x0, int y0, int x1, int y1, int* color)
{
/* Vertical fast path - big_chest particle lines are always x0==x1. */
if (x0 == x1)
{
if ((unsigned)x0 >= 128) return;
if (y0 > y1) {
int t = y0; y0 = y1; y1 = t;
}
if (y0 < 0) y0 = 0;
if (y1 > 127) y1 = 127;
uint8_t p8_color = color ? (*color & 0x0F) : pico8_ram[0x5f25];
uint16_t pattern = (pico8_ram[0x5f31] << 8) | pico8_ram[0x5f32];
if (pattern == 0)
{
uint8_t mask = (x0 & 1) ? 0x0F : 0xF0;
uint8_t shift = (x0 & 1) ? 4 : 0;
uint8_t bits = (uint8_t)(p8_color << shift);
for (int y = y0; y <= y1; y++)
{
uint8_t* p = &pico8_ram[0x6000 + (y << 6) + (x0 >> 1)];
*p = (*p & mask) | bits;
}
}
else
{
for (int y = y0; y <= y1; y++)
pset(x0, y, color);
}
return;
}
/* Horizontal fast path. */
if (y0 == y1)
{
hline(x0, x1, y0, color);
return;
}
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
int sx = (x0 < x1) ? 1 : -1;
int sy = (y0 < y1) ? 1 : -1;
int err = dx - dy;
while (true)
{
pset(x0, y0, color);
if (x0 == x1 && y0 == y1)
{
break;
}
int e2 = 2 * err;
if (e2 > -dy)
{
err -= dy;
x0 += sx;
}
if (e2 < dx)
{
err += dx;
y0 += sy;
}
}
}
static void draw_oval(int x0, int y0, int x1, int y1, int* color, bool fill)
{
int a = abs(x1 - x0) / 2;
int b = abs(y1 - y0) / 2;
int xc = (x0 + x1) / 2;
int yc = (y0 + y1) / 2;
int a2 = a * a;
int b2 = b * b;
int fa2 = 4 * a2, fb2 = 4 * b2;
int x = 0, y = b;
int sigma = 2 * b2 + a2 * (1 - 2 * b);
// First half.
while (b2 * x <= a2 * y)
{
if (fill)
{
hline(xc - x, xc + x, yc + y, color);
hline(xc - x, xc + x, yc - y, color);
}
else
{
pset(xc + x, yc + y, color);
pset(xc - x, yc + y, color);
pset(xc + x, yc - y, color);
pset(xc - x, yc - y, color);
}
if (sigma >= 0)
{
sigma += fa2 * (1 - y);
y--;
}
sigma += b2 * (4 * x + 6);
x++;
}
// Second half.
x = a;
y = 0;
sigma = 2 * a2 + b2 * (1 - 2 * a);
while (a2 * y <= b2 * x)
{
if (fill)
{
hline(xc - x, xc + x, yc + y, color);
hline(xc - x, xc + x, yc - y, color);
}
else
{
pset(xc + x, yc + y, color);
pset(xc - x, yc + y, color);
pset(xc + x, yc - y, color);
pset(xc - x, yc - y, color);
}
if (sigma >= 0)
{
sigma += fb2 * (1 - x);
x--;
}
sigma += a2 * (4 * y + 6);
y++;
}
}
static void draw_rect(int x0, int y0, int x1, int y1, int* color, bool fill)
{
// Normalize, callers may pass y0 > y1 (LIONKING does that)
if (x0 > x1) { int t = x0; x0 = x1; x1 = t; }
if (y0 > y1) { int t = y0; y0 = y1; y1 = t; }
if (fill)
{
for (int y = y0; y <= y1; y++)
{
hline(x0, x1, y, color);
}
}
else
{
for (int x = x0; x <= x1; x++)
{
pset(x, y0, color);
pset(x, y1, color);
}
for (int y = y0; y <= y1; y++)
{
pset(x0, y, color);
pset(x1, y, color);
}
}
}
static uint8_t map_get(int col, int row)
{
if ((unsigned)col >= 128 || (unsigned)row >= 64)
{
return 0;
}
if (row < 32)
{
return pico8_ram[0x2000 + row * 128 + col];
}
else
{
return pico8_ram[0x1000 + (row - 32) * 128 + col];
}
}
static void map_set(int col, int row, uint8_t sprite)
{
if ((unsigned)col >= 128 || (unsigned)row >= 64)
{
return;
}
if (row < 32)
{
pico8_ram[0x2000 + row * 128 + col] = sprite;
}
else
{
pico8_ram[0x1000 + (row - 32) * 128 + col] = sprite;
}
}
// Audio functions.
static int pico8_music(lua_State* L)
{
TO_BE_DONE;
}
static int pico8_sfx(lua_State* L)
{
TO_BE_DONE;
}
// Cart data functions.
static int pico8_cartdata(lua_State* L)
{
TO_BE_DONE;
}
static int pico8_dget(lua_State* L)
{
TO_BE_DONE;
}
static int pico8_dset(lua_State* L)
{
TO_BE_DONE;
}
// Co-Routine functions.
static int pico8_cocreate(lua_State* L)
{
luaL_checktype(L, 1, LUA_TFUNCTION);
lua_State* co = lua_newthread(L);
/* move function onto coroutine stack */
lua_pushvalue(L, 1);
lua_xmove(L, co, 1);
return 1;
}
static int pico8_coresume(lua_State* L)
{
lua_State* co = lua_tothread(L, 1);
if (!co)
{
lua_pushboolean(L, 0);
lua_pushstring(L, "not a coroutine");
return 2;
}
int nargs = lua_gettop(L) - 1;
for (int i = 0; i < nargs; i++)
{
lua_pushvalue(L, i + 2);
}
lua_xmove(L, co, nargs);
int status = lua_resume(co, L, nargs);
int nresults = lua_gettop(co);
if (status != LUA_OK && status != LUA_YIELD)
{
lua_pushboolean(L, 0);
if (lua_gettop(co) > 0)
{
lua_xmove(co, L, 1);
}
else
{
lua_pushstring(L, "coroutine error");
}
return 2;
}
lua_pushboolean(L, 1);
lua_xmove(co, L, nresults);
return 1 + nresults;
}
static int pico8_costatus(lua_State* L)
{
lua_State* co = lua_tothread(L, 1);
if (!co)
{
lua_pushstring(L, "dead");
return 1;
}
if (co == L)
{
lua_pushstring(L, "running");
return 1;
}
switch (lua_status(co))
{
case LUA_YIELD:
lua_pushstring(L, "suspended");
break;
case LUA_OK:
{
lua_Debug ar;
if (lua_getstack(co, 0, &ar))
{
lua_pushstring(L, "normal");
}
else if (lua_gettop(co) == 0)
{
lua_pushstring(L, "dead");
}
else
{
lua_pushstring(L, "suspended");
}
break;
}
default:
lua_pushstring(L, "dead");
break;
}
return 1;
}
static int pico8_yield(lua_State* L)
{
return lua_yield(L, lua_gettop(L));
}
// Debugging functions.
static int pico8_assert(lua_State* L)
{
TO_BE_DONE;
}
static int pico8_printh(lua_State* L)
{
TO_BE_DONE;
}
static int pico8_stat(lua_State* L)
{
uint32_t id = fix32_to_uint32(luaL_checkunsigned(L, 1));
static bool once = false;
static uint32_t inv_frame_ms_q16; // Q16.16 reciprocal
if (!once)
{
// 65536 == 1<<16 (Q16.16)
inv_frame_ms_q16 = (pico8_frame_ms != 0) ? ((1u << 16) / pico8_frame_ms) : 0;
once = true;
}
switch (id)
{
case 1:
{
// Return CPU usage as fraction [0,1].
// Usage = elapsed_time_since_frame_start / frame_ms.
if (pico8_frame_ms == 0)
{
lua_pushnumber(L, 0);
}
else
{
uint32_t now = (uint32_t)SDL_GetTicks();
uint32_t delta = now - pico8_frame_start;
// Q16.16 fixed-point usage = (delta / frame_ms)
uint32_t usage_fp = delta * inv_frame_ms_q16;
// clamp to 4.0 in Q16.16
uint32_t max_fp = (4u << 16);
if (usage_fp > max_fp)
{
usage_fp = max_fp;
}
// push to Lua without any custom float conversion
lua_pushnumber(L, (lua_Number)usage_fp * (lua_Number)(1.0 / 65536.0));
}
break;
}
case 26: // Legacy audio
case 38: // Relative x movement
case 39: // Relative y movement
lua_pushnumber(L, 0);
break;
}
return 1;
}
static int pico8_stop(lua_State* L)
{
TO_BE_DONE;
}
static int pico8_trace(lua_State* L)
{
TO_BE_DONE;
}
// Flow-control functions.
static int pico8_time(lua_State* L)
{
lua_pushnumber(L, seconds_since_start);
return 1;
}
// Graphics functions.
static int pico8_camera(lua_State* L)
{
// Read the current camera offset from memory (16-bit signed integers in pixels)
uint16_t prev_x_unsigned = pico8_ram[0x5f29] | ((uint16_t)pico8_ram[0x5f2a] << 8);
int16_t prev_x_int = (int16_t)prev_x_unsigned;
uint16_t prev_y_unsigned = pico8_ram[0x5f2b] | ((uint16_t)pico8_ram[0x5f2c] << 8);
int16_t prev_y_int = (int16_t)prev_y_unsigned;
// If arguments are provided, set the new camera offset.
// Otherwise, reset camera to (0, 0)
int new_x = 0;
int new_y = 0;
if (lua_gettop(L) >= 1)
{
fix32_t input = luaL_optnumber(L, 1, 0);
new_x = fix32_to_int(input);
}
if (lua_gettop(L) >= 2)
{
fix32_t input = luaL_optnumber(L, 2, 0);
new_y = fix32_to_int(input);
}
// Always store the new camera values (whether they came from arguments or defaults to 0)
int16_t value_x = (int16_t)new_x;
pico8_ram[0x5f29] = (uint8_t)(value_x & 0xFF);
pico8_ram[0x5f2a] = (uint8_t)((value_x >> 8) & 0xFF);
int16_t value_y = (int16_t)new_y;
pico8_ram[0x5f2b] = (uint8_t)(value_y & 0xFF);
pico8_ram[0x5f2c] = (uint8_t)((value_y >> 8) & 0xFF);
// Return the previous camera offset as an x,y tuple (fix32_t)
// Convert from integer pixels back to fix32 format
lua_pushnumber(L, fix32_from_int((int)prev_x_int));
lua_pushnumber(L, fix32_from_int((int)prev_y_int));
return 2;
}
static int pico8_circ(lua_State* L)
{
int cx = fix32_to_int(luaL_checknumber(L, 1));
int cy = fix32_to_int(luaL_checknumber(L, 2));
apply_camera_offset((int*)&cx, (int*)&cy);
int radius = fix32_to_int(luaL_optnumber(L, 3, fix32_value(4, 0)));
int color;
// Can be nil (out-of-range table lookup)
if (!lua_isnoneornil(L, 4))
{
color = fix32_to_int(luaL_checkinteger(L, 4));
draw_circle(cx, cy, radius, &color, false);
}
else
{
draw_circle(cx, cy, radius, NULL, false);
}
return 0;
}
static int pico8_circfill(lua_State* L)
{
int cx = fix32_to_int(luaL_checknumber(L, 1));
int cy = fix32_to_int(luaL_checknumber(L, 2));
apply_camera_offset((int*)&cx, (int*)&cy);
int radius = fix32_to_int(luaL_optnumber(L, 3, fix32_value(4, 0)));
int color;
if (!lua_isnoneornil(L, 4))
{
color = fix32_to_int(luaL_checkinteger(L, 4));
draw_circle(cx, cy, radius, &color, true);
}
else
{
draw_circle(cx, cy, radius, NULL, true);
}
return 0;
}
static int pico8_clip(lua_State* L)
{
TO_BE_DONE;
}
static int pico8_cls(lua_State* L)
{
int color = fix32_to_int32(luaL_optinteger(L, 1, 0));
uint8_t color_pair = (color & 0x0F) << 4 | (color & 0x0F);
SDL_memset(&pico8_ram[0x6000], color_pair, 0x2000);
// Clear clip rectangle.
// tbd.
// Reset cursor position.
pico8_ram[0x5f26] = 0x00;
pico8_ram[0x5f27] = 0x00;
return 0;
}
static int pico8_color(lua_State* L)
{
pico8_ram[0x5f25] = fix32_to_uint8(luaL_optinteger(L, 1, 6));
return 0;
}
static int pico8_cursor(lua_State* L)
{
pico8_ram[0x5f26] = fix32_to_uint8(luaL_checkunsigned(L, 1)); // X.
pico8_ram[0x5f27] = fix32_to_uint8(luaL_checkunsigned(L, 2)); // Y.
if (lua_gettop(L) == 3)
{
pico8_ram[0x5f25] = fix32_to_uint8(luaL_optinteger(L, 3, 0));
}
return 0;
}
static int pico8_fget(lua_State* L)
{
uint8_t n = (uint8_t)(lua_tonumber(L, 1) >> 16);
uint8_t flags = pico8_ram[0x3000 + n];
if (lua_gettop(L) >= 2)
{
int f = (int)(lua_tonumber(L, 2) >> 16);
if ((unsigned)f > 7)
{
lua_pushboolean(L, 0);
}
else
{
lua_pushboolean(L, (flags >> f) & 1);
}
}
else
{
lua_pushnumber(L, fix32_from_uint8(flags));
}
return 1;
}
static int pico8_fillp(lua_State* L)
{
uint32_t pattern = 0;
if (lua_type(L, 1) == LUA_TNUMBER)
{
pattern = fix32_to_uint32(luaL_optnumber(L, 1, 0));
}
else if (lua_type(L, 1) == LUA_TSTRING)
{
const char* str = luaL_checkstring(L, 1);
uint8_t fillc = str[0];
switch (fillc)
{
default:
case 128: // Solid.
pattern = 0b0000000000000000;
break;
case 129: // Checkerboard.
pattern = 0b0101101001011010;
break;
case 130: // Jelpi.
pattern = 0b0101000100011111;
break;
case 131: // Down key.
pattern = 0b0000000000000011;
break;
case 132: // Dot pattern.
pattern = 0b0111110101111101;
break;
case 133: // Throwing star.
pattern = 0b1011100000011101;
break;
case 134: // Ball.
pattern = 0b1111100110011111;
break;
case 135: // Heart.
pattern = 0b0101000110111111;
break;
}
}
pico8_ram[0x5f31] = (pattern & 0xFF00) >> 8;
pico8_ram[0x5f32] = pattern & 0x00FF;
return 0;
}
static int pico8_flip(lua_State* L)
{
// We might not need this function.
return 0;
}
static int pico8_fset(lua_State* L)
{
TO_BE_DONE;
}
static int pico8_line(lua_State* L)
{
int x0 = fix32_to_int(luaL_checknumber(L, 1));
int y0 = fix32_to_int(luaL_checknumber(L, 2));
int x1 = fix32_to_int(luaL_checknumber(L, 3));
int y1 = fix32_to_int(luaL_checknumber(L, 4));
apply_camera_offset((int*)&x0, (int*)&y0);
apply_camera_offset((int*)&x1, (int*)&y1);
int color;
if (!lua_isnoneornil(L, 5))
{
color = fix32_to_int(luaL_checkinteger(L, 5));
draw_line(x0, y0, x1, y1, &color);
}
else
{
draw_line(x0, y0, x1, y1, NULL);
}
return 0;
}
static int pico8_oval(lua_State* L)
{
int x0 = fix32_to_int(luaL_checknumber(L, 1));
int y0 = fix32_to_int(luaL_checknumber(L, 2));
int x1 = fix32_to_int(luaL_checknumber(L, 3));
int y1 = fix32_to_int(luaL_checknumber(L, 4));
int color;
apply_camera_offset((int*)&x0, (int*)&y0);
apply_camera_offset((int*)&x1, (int*)&y1);
if (!lua_isnoneornil(L, 5))
{
color = fix32_to_int(luaL_checkinteger(L, 5));
draw_oval(x0, y0, x1, y1, &color, false);
}
else
{
draw_oval(x0, y0, x1, y1, NULL, false);
}
return 0;
}
static int pico8_ovalfill(lua_State* L)
{
int x0 = fix32_to_int(luaL_checknumber(L, 1));
int y0 = fix32_to_int(luaL_checknumber(L, 2));
int x1 = fix32_to_int(luaL_checknumber(L, 3));
int y1 = fix32_to_int(luaL_checknumber(L, 4));
int color;
apply_camera_offset(&x0, &y0);
apply_camera_offset(&x1, &y1);
if (!lua_isnoneornil(L, 5))
{
color = fix32_to_int(luaL_checkinteger(L, 5));
draw_oval(x0, y0, x1, y1, &color, true);
}
else
{
draw_oval(x0, y0, x1, y1, NULL, true);
}
return 0;
}
static int pico8_pal(lua_State* L)
{
int argc = lua_gettop(L);
if (argc == 0)
{
// Reset draw palette (identity + color 0 transparent) and display palette (identity).
for (int i = 0; i < 16; i++)
{
pico8_ram[0x5f00 + i] = (uint8_t)i | (i == 0 ? 0x10 : 0x00);
pico8_ram[0x5f10 + i] = (uint8_t)i;
}
}
else if (argc == 1 && !lua_istable(L, 1))
{
int p = fix32_to_int(luaL_optnumber(L, 1, 0));
if (p == 0)
{
for (int i = 0; i < 16; i++)
{
pico8_ram[0x5f00 + i] = (uint8_t)i | (i == 0 ? 0x10 : 0x00);
}
}
else if (p == 1)
{
for (int i = 0; i < 16; i++)
{
pico8_ram[0x5f10 + i] = (uint8_t)i;
}
}
}
else if (lua_istable(L, 1))
{
/* First argument is a table: iterate through key-value pairs and apply palette mappings.
* Keys are c0 values (source colors), values are c1 values (target colors).