-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua_functions.c
More file actions
581 lines (484 loc) · 15.5 KB
/
lua_functions.c
File metadata and controls
581 lines (484 loc) · 15.5 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
#include "lua/lua.h"
#include "lua/lualib.h"
#include "lua/lauxlib.h"
#include <string.h>
#include "lua_functions.h"
#include "graphics.h"
#include "memory.h"
#include "font.h"
#include "input.h"
#include "audio.h"
#include "tinybit.h"
char log_buffer[256];
void (*log_func)(const char*);
long frame_time = 0;
// Initialize Lua state with TinyBit libraries and global variables
void lua_setup(lua_State* L) {
// load lua libraries
static const luaL_Reg loadedlibs[] = {
{LUA_GNAME, luaopen_base},
{LUA_COLIBNAME, luaopen_coroutine},
{LUA_TABLIBNAME, luaopen_table},
{LUA_STRLIBNAME, luaopen_string},
{LUA_MATHLIBNAME, luaopen_math},
{NULL, NULL}
};
const luaL_Reg* lib;
for (lib = loadedlibs; lib->func; lib++) {
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1); /* remove lib */
}
// set lua waveforms
lua_pushinteger(L, SINE);
lua_setglobal(L, "SINE");
lua_pushinteger(L, SAW);
lua_setglobal(L, "SAW");
lua_pushinteger(L, SQUARE);
lua_setglobal(L, "SQUARE");
lua_pushinteger(L, NOISE);
lua_setglobal(L, "NOISE");
lua_pushinteger(L, TB_SCREEN_WIDTH);
lua_setglobal(L, "TB_SCREEN_WIDTH");
lua_pushinteger(L, TB_SCREEN_HEIGHT);
lua_setglobal(L, "TB_SCREEN_HEIGHT");
lua_pushinteger(L, TB_BUTTON_A);
lua_setglobal(L, "A");
lua_pushinteger(L, TB_BUTTON_B);
lua_setglobal(L, "B");
lua_pushinteger(L, TB_BUTTON_UP);
lua_setglobal(L, "UP");
lua_pushinteger(L, TB_BUTTON_DOWN);
lua_setglobal(L, "DOWN");
lua_pushinteger(L, TB_BUTTON_LEFT);
lua_setglobal(L, "LEFT");
lua_pushinteger(L, TB_BUTTON_RIGHT);
lua_setglobal(L, "RIGHT");
lua_pushinteger(L, TB_BUTTON_START);
lua_setglobal(L, "START");
lua_pushinteger(L, TB_BUTTON_SELECT);
lua_setglobal(L, "SELECT");
lua_pushcfunction(L, lua_sprite);
lua_setglobal(L, "sprite");
lua_pushcfunction(L, lua_copy_disp);
lua_setglobal(L, "duplicate");
lua_pushcfunction(L, lua_line);
lua_setglobal(L, "line");
lua_pushcfunction(L, lua_millis);
lua_setglobal(L, "millis");
lua_pushcfunction(L, lua_stroke);
lua_setglobal(L, "stroke");
lua_pushcfunction(L, lua_fill);
lua_setglobal(L, "fill");
lua_pushcfunction(L, lua_rect);
lua_setglobal(L, "rect");
lua_pushcfunction(L, lua_oval);
lua_setglobal(L, "oval");
lua_pushcfunction(L, lua_bpm);
lua_setglobal(L, "bpm");
lua_pushcfunction(L, lua_btn);
lua_setglobal(L, "btn");
lua_pushcfunction(L, lua_btnp);
lua_setglobal(L, "btnp");
lua_pushcfunction(L, lua_mycopy);
lua_setglobal(L, "copy");
lua_pushcfunction(L, lua_cls);
lua_setglobal(L, "cls");
lua_pushcfunction(L, lua_peek);
lua_setglobal(L, "peek");
lua_pushcfunction(L, lua_poke);
lua_setglobal(L, "poke");
lua_pushcfunction(L, lua_random);
lua_setglobal(L, "random");
lua_pushcfunction(L, lua_cursor);
lua_setglobal(L, "cursor");
lua_pushcfunction(L, lua_print);
lua_setglobal(L, "print");
lua_pushcfunction(L, lua_text);
lua_setglobal(L, "text");
lua_pushcfunction(L, lua_log);
lua_setglobal(L, "log");
lua_pushcfunction(L, lua_poly_add);
lua_setglobal(L, "poly_add");
lua_pushcfunction(L, lua_poly_clear);
lua_setglobal(L, "poly_clear");
lua_pushcfunction(L, lua_poly);
lua_setglobal(L, "draw_polygon");
lua_pushcfunction(L, lua_music);
lua_setglobal(L, "music");
lua_pushcfunction(L, lua_sfx);
lua_setglobal(L, "sfx");
lua_pushcfunction(L, lua_sfx_active);
lua_setglobal(L, "sfx_active");
lua_pushcfunction(L, lua_pset);
lua_setglobal(L, "pset");
lua_pushcfunction(L, lua_pget);
lua_setglobal(L, "pget");
lua_pushcfunction(L, lua_rgba);
lua_setglobal(L, "rgba");
lua_pushcfunction(L, lua_rgb);
lua_setglobal(L, "rgb");
lua_pushcfunction(L, lua_hsb);
lua_setglobal(L, "hsb");
lua_pushcfunction(L, lua_hsba);
lua_setglobal(L, "hsba");
lua_pushcfunction(L, lua_sleep);
lua_setglobal(L, "sleep");
}
// Lua function to log messages to the console
int lua_log(lua_State* L) {
if (log_func == NULL) {
return 0; // No log function set
}
int log_buffer_index = 0;
int nargs = lua_gettop(L);
for (int i = 1; i <= nargs; i++) {
if (lua_isstring(L, i)) {
const char* str = lua_tostring(L, i);
size_t str_len = strlen(str);
size_t written = 0;
while (written < str_len) {
size_t space_left = sizeof(log_buffer) - log_buffer_index - 2; // reserve for space and null
size_t chunk = (str_len - written > space_left) ? space_left : (str_len - written);
if (chunk > 0) {
memcpy(log_buffer + log_buffer_index, str + written, chunk);
log_buffer_index += chunk;
written += chunk;
}
if (written < str_len) {
// Buffer full, flush and continue
log_buffer[log_buffer_index] = '\0';
log_func(log_buffer);
log_buffer_index = 0;
}
}
// Always add a space after each argument
if (log_buffer_index < sizeof(log_buffer) - 2) {
log_buffer[log_buffer_index++] = ' ';
} else {
// Buffer full, flush and add space
log_buffer[log_buffer_index] = '\0';
log_func(log_buffer);
log_buffer_index = 0;
log_buffer[log_buffer_index++] = ' ';
}
}
}
// Add newline and flush
if (log_buffer_index < sizeof(log_buffer) - 1) {
log_buffer[log_buffer_index++] = '\n';
} else {
log_buffer[sizeof(log_buffer) - 2] = '\n';
log_buffer_index = sizeof(log_buffer) - 1;
}
log_buffer[log_buffer_index] = '\0';
log_func(log_buffer);
return 0;
}
// Lua function to draw a line between two points
int lua_line(lua_State* L) {
if (lua_gettop(L) != 4) {
return 0;
}
int sourceX1 = (int)luaL_checknumber(L, 1);
int sourceY1 = (int)luaL_checknumber(L, 2);
int sourceX2 = (int)luaL_checknumber(L, 3);
int sourceY2 = (int)luaL_checknumber(L, 4);
draw_line(sourceX1, sourceY1, sourceX2, sourceY2);
return 0;
}
int lua_sprite_copy(lua_State* L, TARGET target) {
if (lua_gettop(L) != 8 && lua_gettop(L) != 9) {
return 0;
}
int sourceX = (int)luaL_checknumber(L, 1);
int sourceY = (int)luaL_checknumber(L, 2);
int sourceW = (int)luaL_checknumber(L, 3);
int sourceH = (int)luaL_checknumber(L, 4);
int targetX = (int)luaL_checknumber(L, 5);
int targetY = (int)luaL_checknumber(L, 6);
int targetW = (int)luaL_checknumber(L, 7);
int targetH = (int)luaL_checknumber(L, 8);
if(lua_gettop(L) == 8) {
draw_sprite(sourceX, sourceY, sourceW, sourceH, targetX, targetY, targetW, targetH, target);
return 0;
}
int targetR = (int)luaL_checknumber(L, 9);
draw_sprite_rotated(sourceX, sourceY, sourceW, sourceH, targetX, targetY, targetW, targetH, targetR, target);
return 0;
}
// Lua function to draw a sprite (with optional rotation)
int lua_sprite(lua_State* L) {
return lua_sprite_copy(L, TARGET_SPRITESHEET);
}
int lua_copy_disp(lua_State* L) {
return lua_sprite_copy(L, TARGET_DISPLAY);
}
// Lua function to get current frame time in milliseconds
int lua_millis(lua_State* L) {
lua_Integer m = frame_time;
lua_pushinteger(L, m);
return 1;
}
// Lua function to generate random number within range
int lua_random(lua_State* L) {
if (lua_gettop(L) != 2) {
return 0;
}
int min = (int)luaL_checknumber(L, 1);
int max = (int)luaL_checknumber(L, 2);
lua_pushinteger(L, random_range(min, max));
return 1;
}
// Lua function: stroke(width, color) - set stroke width and color
int lua_stroke(lua_State* L) {
if (lua_gettop(L) != 2) {
return 0;
}
int width = (int)luaL_checknumber(L, 1);
uint16_t color = (uint16_t)luaL_checkinteger(L, 2);
set_stroke(width, color);
return 0;
}
// Lua function: fill(color) - set fill color
int lua_fill(lua_State* L) {
if (lua_gettop(L) != 1) {
return 0;
}
uint16_t color = (uint16_t)luaL_checkinteger(L, 1);
set_fill(color);
return 0;
}
// Lua function: text(color) - set text color
int lua_text(lua_State* L) {
if (lua_gettop(L) != 1) {
return 0;
}
uint16_t color = (uint16_t)luaL_checkinteger(L, 1);
font_text_color(color);
return 0;
}
// Lua function: rgba(r, g, b, a) - pack into RGBA4444
int lua_rgba(lua_State* L) {
int r = (int)luaL_checknumber(L, 1) & 0xFF;
int g = (int)luaL_checknumber(L, 2) & 0xFF;
int b = (int)luaL_checknumber(L, 3) & 0xFF;
int a = (int)luaL_checknumber(L, 4) & 0xFF;
lua_pushinteger(L, pack_color(r, g, b, a));
return 1;
}
// Lua function: rgb(r, g, b) - pack into RGBA4444 with alpha=255
int lua_rgb(lua_State* L) {
int r = (int)luaL_checknumber(L, 1) & 0xFF;
int g = (int)luaL_checknumber(L, 2) & 0xFF;
int b = (int)luaL_checknumber(L, 3) & 0xFF;
lua_pushinteger(L, pack_color(r, g, b, 255));
return 1;
}
// Helper: convert HSB (all 0-255) to RGB (0-255)
static void hsb_to_rgb(float h, float s, float b, int* r, int* g, int* bl) {
h = h * 360.0f / 255.0f;
s /= 255.0f;
b /= 255.0f;
float c = b * s;
float hh = h / 60.0;
float rem = hh - (int)(hh / 2.0) * 2.0;
float diff = rem - 1.0;
float x = c * (1.0 - (diff < 0 ? -diff : diff));
float m = b - c;
float rr, gg, bb;
if (h < 60) { rr = c; gg = x; bb = 0; }
else if (h < 120) { rr = x; gg = c; bb = 0; }
else if (h < 180) { rr = 0; gg = c; bb = x; }
else if (h < 240) { rr = 0; gg = x; bb = c; }
else if (h < 300) { rr = x; gg = 0; bb = c; }
else { rr = c; gg = 0; bb = x; }
*r = (int)((rr + m) * 255.0 + 0.5);
*g = (int)((gg + m) * 255.0 + 0.5);
*bl = (int)((bb + m) * 255.0 + 0.5);
}
// Lua function: hsba(h, s, b, a) - convert HSB + alpha to RGBA4444
int lua_hsba(lua_State* L) {
float h = luaL_checknumber(L, 1);
float s = luaL_checknumber(L, 2);
float b = luaL_checknumber(L, 3);
int a = (int)luaL_checknumber(L, 4) & 0xFF;
int r, g, bl;
hsb_to_rgb(h, s, b, &r, &g, &bl);
lua_pushinteger(L, pack_color(r & 0xFF, g & 0xFF, bl & 0xFF, a));
return 1;
}
// Lua function: hsb(h, s, b) - convert HSB to RGBA4444 with alpha=255
int lua_hsb(lua_State* L) {
float h = luaL_checknumber(L, 1);
float s = luaL_checknumber(L, 2);
float b = luaL_checknumber(L, 3);
int r, g, bl;
hsb_to_rgb(h, s, b, &r, &g, &bl);
lua_pushinteger(L, pack_color(r & 0xFF, g & 0xFF, bl & 0xFF, 255));
return 1;
}
// Lua function: pset(x, y, color) - set a pixel to a specific color
int lua_pset(lua_State* L) {
if (lua_gettop(L) != 3) {
return 0;
}
int x = (int)luaL_checknumber(L, 1);
int y = (int)luaL_checknumber(L, 2);
uint16_t color = (uint16_t)luaL_checkinteger(L, 3);
pset(x, y, color);
return 0;
}
// Lua function: pget(x, y) - get the color of a pixel as RGBA4444
int lua_pget(lua_State* L) {
if (lua_gettop(L) != 2) {
return 0;
}
int x = (int)luaL_checknumber(L, 1);
int y = (int)luaL_checknumber(L, 2);
uint16_t color = pget(x, y);
lua_pushinteger(L, color);
return 1;
}
// Lua function to draw a rectangle
int lua_rect(lua_State* L) {
if (lua_gettop(L) != 4) {
return 0;
}
int x = (int)luaL_checknumber(L, 1);
int y = (int)luaL_checknumber(L, 2);
int w = (int)luaL_checknumber(L, 3);
int h = (int)luaL_checknumber(L, 4);
draw_rect(x, y, w, h);
return 0;
}
// Lua function to draw an oval
int lua_oval(lua_State* L) {
if (lua_gettop(L) != 4) {
return 0;
}
int x = (int)luaL_checknumber(L, 1);
int y = (int)luaL_checknumber(L, 2);
int w = (int)luaL_checknumber(L, 3);
int h = (int)luaL_checknumber(L, 4);
draw_oval(x, y, w, h);
return 0;
}
// Lua function to add a point to polygon vertex list
int lua_poly_add(lua_State* L) {
if (lua_gettop(L) != 2) {
return 0;
}
int x = (int)luaL_checknumber(L, 1);
int y = (int)luaL_checknumber(L, 2);
poly_add(x, y);
return 0;
}
// Lua function to clear polygon vertex list
int lua_poly_clear(lua_State* L) {
poly_clear();
return 0;
}
// Lua function to draw the current polygon
int lua_poly(lua_State* L) {
draw_polygon();
return 0;
}
// Lua function to set beats per minute for audio timing
int lua_bpm(lua_State* L) {
int new_bpm = luaL_checkinteger(L, 1);
set_bpm(new_bpm);
return 0;
}
// Lua function to check if button is currently pressed
int lua_btn(lua_State* L) {
enum TinyBitButton btn = luaL_checkinteger(L, 1);
lua_pushboolean(L, input_btn(btn));
return 1;
}
// Lua function to check if button was just pressed this frame
int lua_btnp(lua_State* L) {
enum TinyBitButton btn = luaL_checkinteger(L, 1);
lua_pushboolean(L, input_btnp(btn));
return 1;
}
// Lua function to clear the display
int lua_cls(lua_State* L) {
draw_cls();
return 0;
}
// Lua function to copy memory between addresses
int lua_mycopy(lua_State* L) {
if (lua_gettop(L) != 3) {
return 0;
}
int dst = luaL_checkinteger(L, 1);
int src = luaL_checkinteger(L, 2);
int size = luaL_checkinteger(L, 3);
mem_copy(dst, src, size);
return 0;
}
// Lua function to read a byte from memory
int lua_peek(lua_State* L) {
if (lua_gettop(L) != 1) {
return 0;
}
int dst = luaL_checkinteger(L, 1);
lua_pushinteger(L, mem_peek(dst));
return 1;
}
// Lua function to write a byte to memory
int lua_poke(lua_State* L) {
if (lua_gettop(L) != 2) {
return 0;
}
int dst = luaL_checkinteger(L, 1);
int val = luaL_checkinteger(L, 2);
mem_poke(dst, val);
return 0;
}
// Lua function to set text cursor position
int lua_cursor(lua_State* L) {
if (lua_gettop(L) != 2) {
return 0;
}
int x = (int)luaL_checknumber(L, 1);
int y = (int)luaL_checknumber(L, 2);
font_cursor(x, y);
return 0;
}
// Lua function to print text at cursor position
int lua_print(lua_State* L) {
if (lua_gettop(L) != 1) {
return 0;
}
const char* str = luaL_checkstring(L, 1);
font_print(str);
return 0;
}
// Lua function to play a music track
int lua_music(lua_State* L) {
if (lua_gettop(L) != 1) {
return 0;
}
const char* str = luaL_checkstring(L, 1);
audio_load_abc(0, str, SINE, true);
return 0;
}
// Lua function to play a short sfx
int lua_sfx(lua_State* L) {
if (lua_gettop(L) != 1) {
return 0;
}
const char* str = luaL_checkstring(L, 1);
audio_load_abc(1, str, SINE, false);
return 0;
}
int lua_sfx_active(lua_State* L) {
lua_pushboolean(L, is_channel_active(1));
return 1;
}
int lua_sleep(lua_State* L) {
int ms = luaL_checkinteger(L, 1);
tinybit_sleep(ms);
return 0;
}