-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_lua.c
699 lines (614 loc) · 16.2 KB
/
d_lua.c
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
#include <string.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#define D_IMPL
#include "d_app.h"
#include "d_fs.h"
#define lua_pushudata(L, T, TS, V) \
T* lv = lua_newuserdata(L, sizeof(T)); \
luaL_setmetatable(L, TS); \
memcpy(lv, V, sizeof(T)); \
typedef struct {
const char *str;
int val;
} luaL_Enum;
bool luaL_args0(lua_State* L) {
return lua_gettop(L) == 0;
}
bool luaL_args1(lua_State* L, int type) {
if (lua_gettop(L) == 1) {
if (lua_type(L, 1) == type) {
return true;
}
}
return false;
}
bool luaL_args2(lua_State* L, int t1, int t2) {
if (lua_gettop(L) == 2) {
if (lua_type(L, 1) == t1 && lua_type(L, 2) == t2) {
return true;
}
}
return false;
}
bool luaL_args3(lua_State* L, int t1, int t2, int t3) {
if (lua_gettop(L) == 3) {
if (lua_type(L, 1) == t1 && lua_type(L, 2) == t2 && lua_type(L, 3) == t3) {
return true;
}
}
return false;
}
bool luaL_args4(lua_State* L, int t1, int t2, int t3, int t4) {
if (lua_gettop(L) == 4) {
if (lua_type(L, 1) == t1 && lua_type(L, 2) == t2 && lua_type(L, 3) == t3 && lua_type(L, 4) == t4) {
return true;
}
}
return false;
}
void luaL_checktable(lua_State* L, int pos) {
luaL_checktype(L, pos, LUA_TTABLE);
}
bool luaL_checkboolean(lua_State* L, int pos) {
luaL_checktype(L, pos, LUA_TBOOLEAN);
return lua_toboolean(L, pos);
}
void luaL_opttable(lua_State* L, int pos) {
if (!lua_isnoneornil(L, pos)) {
luaL_checktable(L, pos);
}
}
void *luaL_optudata(lua_State* L, int pos, const char *type, void *def) {
return lua_isnoneornil(L, pos) ? def : luaL_checkudata(L, pos, type);
}
const char *lua_udatatype(lua_State* L, int pos) {
if (!lua_getmetatable(L, pos)) {
return NULL;
}
lua_getfield(L, -1, "__name");
const char *tname = lua_tostring(L, -1);
// TODO: stack management
lua_pop(L, 2);
return tname;
}
void luaL_regfuncs(lua_State* L, luaL_Reg *reg) {
for (int i = 0; reg[i].name != NULL; i++) {
lua_register(L, reg[i].name, reg[i].func);
}
}
int luaL_getenum(lua_State* L, luaL_Enum *map, const char *str) {
for (int i = 0; map[i].str != NULL; i++) {
if (strcmp(str, map[i].str) == 0) {
return map[i].val;
}
}
return 0;
}
void luaL_regenum(lua_State* L, const char *name, luaL_Enum *map) {
lua_newtable(L);
for (int i = 0; map[i].str != NULL; i++) {
lua_pushinteger(L, map[i].val);
lua_setfield(L, -2, map[i].str);
}
lua_setglobal(L, name);
}
void luaL_regtype(lua_State* L, const char *name, luaL_Reg *meta) {
luaL_newmetatable(L, name);
for (int i = 0; meta[i].name != NULL; i++) {
lua_pushstring(L, meta[i].name);
lua_pushcfunction(L, meta[i].func);
lua_settable(L, -3);
// lua_pushcfunction(L, reg[i].func);
// lua_setfield(L, -2, reg[i].name);
}
lua_pop(L, 1);
}
static luaL_Enum l_app_mouse_map[] = {
{ "left", D_MOUSE_LEFT, },
{ "right", D_MOUSE_RIGHT, },
{ "middle", D_MOUSE_MIDDLE, },
{ NULL, 0, },
};
static luaL_Enum l_app_key_map[] = {
{ "q", D_KEY_Q, },
{ "w", D_KEY_W, },
{ "e", D_KEY_E, },
{ "r", D_KEY_R, },
{ "t", D_KEY_T, },
{ "y", D_KEY_Y, },
{ "u", D_KEY_U, },
{ "i", D_KEY_I, },
{ "o", D_KEY_O, },
{ "p", D_KEY_P, },
{ "a", D_KEY_A, },
{ "s", D_KEY_S, },
{ "d", D_KEY_D, },
{ "f", D_KEY_F, },
{ "g", D_KEY_G, },
{ "h", D_KEY_H, },
{ "j", D_KEY_J, },
{ "k", D_KEY_K, },
{ "l", D_KEY_L, },
{ "z", D_KEY_Z, },
{ "x", D_KEY_X, },
{ "c", D_KEY_C, },
{ "v", D_KEY_V, },
{ "b", D_KEY_B, },
{ "n", D_KEY_N, },
{ "m", D_KEY_M, },
{ "1", D_KEY_1, },
{ "2", D_KEY_2, },
{ "3", D_KEY_3, },
{ "4", D_KEY_4, },
{ "5", D_KEY_5, },
{ "6", D_KEY_6, },
{ "7", D_KEY_7, },
{ "8", D_KEY_8, },
{ "9", D_KEY_9, },
{ "0", D_KEY_0, },
{ "f1", D_KEY_F1, },
{ "f2", D_KEY_F2, },
{ "f3", D_KEY_F3, },
{ "f4", D_KEY_F4, },
{ "f5", D_KEY_F5, },
{ "f6", D_KEY_F6, },
{ "f7", D_KEY_F7, },
{ "f8", D_KEY_F8, },
{ "f9", D_KEY_F9, },
{ "f10", D_KEY_F10, },
{ "f11", D_KEY_F11, },
{ "f12", D_KEY_F12, },
{ "-", D_KEY_MINUS, },
{ "=", D_KEY_EQUAL, },
{ ",", D_KEY_COMMA, },
{ ".", D_KEY_PERIOD, },
{ "`", D_KEY_BACKTICK, },
{ "/", D_KEY_SLASH, },
{ "\\", D_KEY_BACKSLASH, },
{ ";", D_KEY_SEMICOLON, },
{ "'", D_KEY_QUOTE, },
{ "up", D_KEY_UP, },
{ "down", D_KEY_DOWN, },
{ "left", D_KEY_LEFT, },
{ "right", D_KEY_RIGHT, },
{ "esc", D_KEY_ESC, },
{ "tab", D_KEY_TAB, },
{ " ", D_KEY_SPACE, },
{ "space", D_KEY_SPACE, },
{ "bksp", D_KEY_BACKSPACE, },
{ "backspace", D_KEY_BACKSPACE, },
{ "enter", D_KEY_ENTER, },
{ "[", D_KEY_LBRACKET, },
{ "]", D_KEY_RBRACKET, },
{ "lshft", D_KEY_LSHIFT, },
{ "rshft", D_KEY_RSHIFT, },
{ "lalt", D_KEY_LALT, },
{ "ralt", D_KEY_RALT, },
{ "lmeta", D_KEY_LMETA, },
{ "rmeta", D_KEY_RMETA, },
{ "lctrl", D_KEY_LCTRL, },
{ "rctrl", D_KEY_RCTRL, },
{ NULL, 0, },
};
typedef struct {
lua_State* lua;
int init_ref;
int frame_ref;
int quit_ref;
int err_ref;
} l_app_ctx;
static l_app_ctx l_app;
static void l_init_inner(void) {
lua_rawgeti(l_app.lua, LUA_REGISTRYINDEX, l_app.init_ref);
if (lua_isfunction(l_app.lua, -1)) {
lua_call(l_app.lua, 0, 0);
}
}
static void l_frame_inner(void) {
lua_rawgeti(l_app.lua, LUA_REGISTRYINDEX, l_app.frame_ref);
if (lua_isfunction(l_app.lua, -1)) {
lua_call(l_app.lua, 0, 0);
}
}
static void l_quit_inner(void) {
lua_rawgeti(l_app.lua, LUA_REGISTRYINDEX, l_app.quit_ref);
if (lua_isfunction(l_app.lua, -1)) {
lua_call(l_app.lua, 0, 0);
}
}
static int l_app_run(lua_State* L) {
luaL_checktable(L, 1);
lua_getfield(L, 1, "init");
l_app.init_ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_getfield(L, 1, "frame");
l_app.frame_ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_getfield(L, 1, "quit");
l_app.quit_ref = luaL_ref(L, LUA_REGISTRYINDEX);
// TODO: luaL_check* does not give meaningful error for table fields
d_app_run((d_app_desc) {
.init = l_init_inner,
.frame = l_frame_inner,
.quit = l_quit_inner,
.title = lua_getfield(L, 1, "title") ? luaL_checkstring(L, -1) : NULL,
.width = lua_getfield(L, 1, "width") ? luaL_checkinteger(L, -1) : 0,
.height = lua_getfield(L, 1, "height") ? luaL_checkinteger(L, -1) : 0,
.fullscreen = lua_getfield(L, 1, "fullscreen") ? luaL_checkboolean(L, -1) : false,
});
return 0;
}
static int l_app_quit(lua_State* L) {
d_app_quit();
return 0;
}
static int l_app_time(lua_State* L) {
lua_pushnumber(L, d_app_time());
return 1;
}
static int l_app_dt(lua_State* L) {
lua_pushnumber(L, d_app_dt());
return 1;
}
static int l_app_key_pressed(lua_State* L) {
const char *k = luaL_checkstring(L, 1);
lua_pushboolean(L, d_app_key_pressed(luaL_getenum(L, l_app_key_map, k)));
return 1;
}
static int l_app_key_rpressed(lua_State* L) {
const char *k = luaL_checkstring(L, 1);
lua_pushboolean(L, d_app_key_rpressed(luaL_getenum(L, l_app_key_map, k)));
return 1;
}
static int l_app_key_released(lua_State* L) {
const char *k = luaL_checkstring(L, 1);
lua_pushboolean(L, d_app_key_released(luaL_getenum(L, l_app_key_map, k)));
return 1;
}
static int l_app_key_down(lua_State* L) {
const char *k = luaL_checkstring(L, 1);
lua_pushboolean(L, d_app_key_down(luaL_getenum(L, l_app_key_map, k)));
return 1;
}
static int l_app_mouse_pressed(lua_State* L) {
const char *m = luaL_checkstring(L, 1);
lua_pushboolean(L, d_app_mouse_pressed(luaL_getenum(L, l_app_mouse_map, m)));
return 1;
}
static int l_app_mouse_released(lua_State* L) {
const char *m = luaL_checkstring(L, 1);
lua_pushboolean(L, d_app_mouse_released(luaL_getenum(L, l_app_mouse_map, m)));
return 1;
}
static int l_app_mouse_down(lua_State* L) {
const char *m = luaL_checkstring(L, 1);
lua_pushboolean(L, d_app_mouse_down(luaL_getenum(L, l_app_mouse_map, m)));
return 1;
}
static int l_app_mouse_pos(lua_State* L) {
d_vec2 mpos = d_app_mouse_pos();
lua_pushudata(L, d_vec2, "vec2", &mpos);
return 1;
}
static int l_app_mouse_dpos(lua_State* L) {
d_vec2 dpos = d_app_mouse_dpos();
lua_pushudata(L, d_vec2, "vec2", &dpos);
return 1;
}
static int l_app_mouse_moved(lua_State* L) {
lua_pushboolean(L, d_app_mouse_moved());
return 1;
}
static int l_app_input(lua_State* L) {
char c = d_app_input();
if (c) {
lua_pushlstring(L, &c, 1);
} else {
lua_pushnil(L);
}
return 1;
}
static int l_app_width(lua_State* L) {
lua_pushinteger(L, d_app_width());
return 1;
}
static int l_app_height(lua_State* L) {
lua_pushinteger(L, d_app_height());
return 1;
}
static int l_app_fullscreen(lua_State* L) {
if (lua_isnoneornil(L, 1)) {
lua_pushboolean(L, d_app_is_fullscreen());
return 1;
} else {
bool b = luaL_checkboolean(L, 1);
d_app_set_fullscreen(b);
return 0;
}
}
static int l_app_mouse_locked(lua_State* L) {
if (lua_isnoneornil(L, 1)) {
lua_pushboolean(L, d_app_is_mouse_locked());
return 1;
} else {
bool b = luaL_checkboolean(L, 1);
d_app_set_mouse_locked(b);
return 0;
}
}
static int l_app_mouse_hidden(lua_State* L) {
if (lua_isnoneornil(L, 1)) {
lua_pushboolean(L, d_app_is_mouse_hidden());
return 1;
} else {
bool b = luaL_checkboolean(L, 1);
d_app_set_mouse_hidden(b);
return 0;
}
}
static int l_app_keyboard_shown(lua_State* L) {
if (lua_isnoneornil(L, 1)) {
lua_pushboolean(L, d_app_is_keyboard_shown());
return 1;
} else {
bool b = luaL_checkboolean(L, 1);
d_app_set_keyboard_shown(b);
return 0;
}
}
static int l_app_title(lua_State* L) {
if (lua_isnoneornil(L, 1)) {
return 0;
// lua_pushstring(L, d_app_title());
// return 1;
} else {
const char* title = luaL_checkstring(L, 1);
d_app_set_title(title);
return 0;
}
}
void l_app_cleanup(lua_State* L) {
luaL_unref(L, LUA_REGISTRYINDEX, l_app.init_ref);
luaL_unref(L, LUA_REGISTRYINDEX, l_app.frame_ref);
luaL_unref(L, LUA_REGISTRYINDEX, l_app.quit_ref);
luaL_unref(L, LUA_REGISTRYINDEX, l_app.err_ref);
}
static luaL_Reg app_funcs[] = {
{ "run", l_app_run, },
{ "quit", l_app_quit, },
{ "time", l_app_time, },
{ "dt", l_app_dt, },
{ "width", l_app_width, },
{ "height", l_app_height, },
{ "key_pressed", l_app_key_pressed, },
{ "key_rpressed", l_app_key_rpressed, },
{ "key_released", l_app_key_released, },
{ "key_down", l_app_key_down, },
{ "mouse_pressed", l_app_mouse_pressed, },
{ "mouse_released", l_app_mouse_released, },
{ "mouse_down", l_app_mouse_down, },
{ "mouse_pos", l_app_mouse_pos, },
{ "mouse_dpos", l_app_mouse_dpos, },
{ "mouse_moved", l_app_mouse_moved, },
{ "input", l_app_input, },
{ "fullscreen", l_app_fullscreen, },
{ "mouse_locked", l_app_mouse_locked, },
{ "mouse_hidden", l_app_mouse_hidden, },
{ "keyboard_shown", l_app_keyboard_shown, },
{ "title", l_app_title, },
{ NULL, NULL, },
};
static luaL_Reg gfx_funcs[] = {
{ NULL, NULL, },
};
static int l_vec2_len(lua_State *L) {
d_vec2* p = luaL_checkudata(L, 1, "vec2");
float l = d_vec2_len(*p);
lua_pushnumber(L, l);
return 1;
}
static int l_vec2_dist(lua_State *L) {
d_vec2* p1 = luaL_checkudata(L, 1, "vec2");
d_vec2* p2 = luaL_checkudata(L, 2, "vec2");
float d = d_vec2_dist(*p1, *p2);
lua_pushnumber(L, d);
return 1;
}
static int l_vec2_angle(lua_State *L) {
d_vec2* p1 = luaL_checkudata(L, 1, "vec2");
d_vec2* p2 = luaL_checkudata(L, 2, "vec2");
float d = d_vec2_angle(*p1, *p2);
lua_pushnumber(L, d);
return 1;
}
static int l_vec2_unit(lua_State *L) {
d_vec2* p1 = luaL_checkudata(L, 1, "vec2");
d_vec2 p2 = d_vec2_unit(*p1);
lua_pushudata(L, d_vec2, "vec2", &p2);
return 1;
}
static int l_vec2_normal(lua_State *L) {
d_vec2* p1 = luaL_checkudata(L, 1, "vec2");
d_vec2 p2 = d_vec2_normal(*p1);
lua_pushudata(L, d_vec2, "vec2", &p2);
return 1;
}
static int l_vec2_index(lua_State *L) {
d_vec2* p = luaL_checkudata(L, 1, "vec2");
const char* key = luaL_checkstring(L, 2);
if (strcmp(key, "x") == 0) {
lua_pushnumber(L, p->x);
} else if (strcmp(key, "y") == 0) {
lua_pushnumber(L, p->y);
} else if (strcmp(key, "unit") == 0) {
lua_pushcfunction(L, l_vec2_unit);
} else if (strcmp(key, "len") == 0) {
lua_pushcfunction(L, l_vec2_len);
} else if (strcmp(key, "dist") == 0) {
lua_pushcfunction(L, l_vec2_dist);
} else if (strcmp(key, "normal") == 0) {
lua_pushcfunction(L, l_vec2_normal);
} else if (strcmp(key, "angle") == 0) {
lua_pushcfunction(L, l_vec2_angle);
} else {
lua_pushnil(L);
}
return 1;
}
static int l_vec2_newindex(lua_State *L) {
d_vec2* p = luaL_checkudata(L, 1, "vec2");
const char* key = luaL_checkstring(L, 2);
float value = luaL_checknumber(L, 3);
if (strcmp(key, "x") == 0) {
p->x = value;
} else if (strcmp(key, "y") == 0) {
p->y = value;
}
return 0;
}
static int l_vec2_tostring(lua_State *L) {
d_vec2* p = luaL_checkudata(L, 1, "vec2");
char buf[64];
snprintf(buf, sizeof(buf), "vec2(%f, %f)", p->x, p->y);
lua_pushstring(L, buf);
return 1;
}
static int l_vec2_add(lua_State *L) {
d_vec2* p1 = luaL_checkudata(L, 1, "vec2");
d_vec2* p2 = luaL_checkudata(L, 2, "vec2");
d_vec2 p = d_vec2_add(*p1, *p2);
lua_pushudata(L, d_vec2, "vec2", &p);
return 1;
}
static int l_vec2_sub(lua_State *L) {
d_vec2* p1 = luaL_checkudata(L, 1, "vec2");
d_vec2* p2 = luaL_checkudata(L, 2, "vec2");
d_vec2 p = d_vec2_sub(*p1, *p2);
lua_pushudata(L, d_vec2, "vec2", &p);
return 1;
}
// TODO: support vec2 * vec2
static int l_vec2_mul(lua_State *L) {
d_vec2* p1 = luaL_checkudata(L, 1, "vec2");
float s = luaL_checknumber(L, 2);
d_vec2 p = d_vec2f(p1->x * s, p1->y * s);
lua_pushudata(L, d_vec2, "vec2", &p);
return 1;
}
static int l_vec2_div(lua_State *L) {
d_vec2* p1 = luaL_checkudata(L, 1, "vec2");
float s = luaL_checknumber(L, 2);
d_vec2 p = d_vec2f(p1->x / s, p1->y / s);
lua_pushudata(L, d_vec2, "vec2", &p);
return 1;
}
static luaL_Reg vec2_meta[] = {
{ "__index", l_vec2_index },
{ "__newindex", l_vec2_newindex },
{ "__tostring", l_vec2_tostring },
{ "__add", l_vec2_add },
{ "__sub", l_vec2_sub },
{ "__mul", l_vec2_mul },
{ "__div", l_vec2_div },
{ NULL, NULL, },
};
static int l_color_darken(lua_State *L) {
d_color* c1 = luaL_checkudata(L, 1, "color");
int s = luaL_checknumber(L, 2);
d_color c2 = d_color_darken(*c1, s);
lua_pushudata(L, d_color, "color", &c2);
return 1;
}
static int l_color_lighten(lua_State *L) {
d_color* c1 = luaL_checkudata(L, 1, "color");
int s = luaL_checknumber(L, 2);
d_color c2 = d_color_lighten(*c1, s);
lua_pushudata(L, d_color, "color", &c2);
return 1;
}
static int l_color_mix(lua_State *L) {
d_color* c1 = luaL_checkudata(L, 1, "color");
d_color* c2 = luaL_checkudata(L, 2, "color");
d_color c = d_color_mix(*c1, *c2);
lua_pushudata(L, d_color, "color", &c);
return 1;
}
static int l_color_index(lua_State *L) {
d_color* c = (d_color*)luaL_checkudata(L, 1, "color");
const char* key = luaL_checkstring(L, 2);
if (strcmp(key, "r") == 0) {
lua_pushnumber(L, c->r);
} else if (strcmp(key, "g") == 0) {
lua_pushnumber(L, c->g);
} else if (strcmp(key, "b") == 0) {
lua_pushnumber(L, c->b);
} else if (strcmp(key, "a") == 0) {
lua_pushnumber(L, c->a);
} else if (strcmp(key, "mix") == 0) {
lua_pushcfunction(L, l_color_mix);
} else if (strcmp(key, "darken") == 0) {
lua_pushcfunction(L, l_color_darken);
} else if (strcmp(key, "lighten") == 0) {
lua_pushcfunction(L, l_color_lighten);
} else {
lua_pushnil(L);
}
return 1;
}
static int l_color_newindex(lua_State *L) {
d_color* c = (d_color*)luaL_checkudata(L, 1, "color");
const char* key = luaL_checkstring(L, 2);
float value = luaL_checknumber(L, 3);
if (strcmp(key, "r") == 0) {
c->r = value;
} else if (strcmp(key, "g") == 0) {
c->g = value;
} else if (strcmp(key, "b") == 0) {
c->b = value;
} else if (strcmp(key, "a") == 0) {
c->a = value;
}
return 0;
}
static int l_color_tostring(lua_State *L) {
d_color* c = (d_color*)luaL_checkudata(L, 1, "color");
char buf[64];
snprintf(buf, sizeof(buf), "color(%d, %d, %d, %d)", c->r, c->g, c->b, c->a);
lua_pushstring(L, buf);
return 1;
}
static luaL_Reg color_meta[] = {
{ "__index", l_color_index },
{ "__newindex", l_color_newindex },
{ "__tostring", l_color_tostring },
{ "__add", l_color_lighten },
{ "__sub", l_color_darken },
{ "__mul", l_color_mix },
{ NULL, NULL, },
};
int main(int argc, char** argv) {
char* path = NULL;
for (int i = 1 ; i < argc; i++) {
char* arg = argv[i];
int len = strlen(arg);
if (len >= 2 && arg[0] == '-' && arg[1] != '-') {
switch (arg[1]) {
}
} else if (arg[0] != '-') {
path = arg;
break;
}
}
lua_State* L = luaL_newstate();
l_app.lua = L;
luaL_openlibs(L);
luaL_newlib(L, app_funcs);
lua_setglobal(L, "app");
luaL_newlib(L, gfx_funcs);
lua_setglobal(L, "gfx");
luaL_regtype(L, "vec2", vec2_meta);
luaL_regtype(L, "color", color_meta);
luaL_dofile(L, path);
lua_close(L);
return 0;
}