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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
|
#include <iostream>
#include <fstream>
#include "libjin/jin.h"
#include "common/je_lua_object.h"
#include "common/je_lua_common.h"
#include "je_lua_canvas.h"
#include "je_lua_sprite.h"
#include "je_lua_spritesheet.h"
#include "je_lua_bitmap.h"
#include "je_lua_mesh.h"
#include "je_lua_ttf.h"
#include "je_lua_ttf_data.h"
#include "je_lua_texture.h"
#include "je_lua_shader.h"
#include "je_lua_text.h"
#include "je_lua_texture_font.h"
#include "je_lua_page.h"
#include "je_lua_sprite.h"
#include "je_lua_animation.h"
#include "je_lua_animator.h"
#include "je_lua_particle_system.h"
using namespace std;
using namespace JinEngine;
using namespace JinEngine::Math;
using namespace JinEngine::Graphics;
using namespace JinEngine::Graphics::Fonts;
using namespace JinEngine::Graphics::Shaders;
using namespace JinEngine::Graphics::Animations;
using namespace JinEngine::Graphics::Particles;
using namespace JinEngine::Filesystem;
namespace JinEngine
{
namespace Lua
{
#include "../../resources/font.ttf.h"
static struct
{
Color curRenderColor;
Color curClearColor;
Font* defaultFont = nullptr;
bool initialized = false;
} context;
LUA_IMPLEMENT int l_init(lua_State* L)
{
if (context.initialized)
{
luax_pushboolean(L, true);
return 1;
}
Window* wnd = Window::get();
Window::Setting setting;
setting.width = luax_getfieldinteger(L, 1, "width");
setting.height = luax_getfieldinteger(L, 1, "height");
setting.title = luax_getfieldstring(L, 1, "title");
setting.icon = luax_getfieldstring(L, 1, "icon");
setting.vsync = luax_getfieldbool(L, 1, "vsync");
setting.fullscreen = luax_getfieldbool(L, 1, "fullscreen");
setting.resizable = luax_getfieldbool(L, 1, "resizable");
context.initialized = wnd->start(&setting);
if (!context.initialized)
{
luax_pushboolean(L, context.initialized);
return 1;
}
/* load default font */
Bitmap* bitmap = new Bitmap(default_font_bitmap, sizeof(default_font_bitmap));
TextureFont* tf = new TextureFont(bitmap, Text(Encode::UTF8, default_charset), default_font_split, bitmap->getHeight());
delete bitmap;
context.defaultFont = tf;
gl.setFont(tf);
luax_pushboolean(L, context.initialized);
return 1;
}
LUA_IMPLEMENT int l_setTitle(lua_State* L)
{
Window* wnd = Window::get();
const char* title = luax_checkstring(L, 1);
wnd->setTitle(title);
return 0;
}
LUA_IMPLEMENT int l_destroy(lua_State* L)
{
Window* wnd = Window::get();
wnd->quit();
return 0;
}
LUA_IMPLEMENT int l_showWindow(lua_State* L)
{
Window* wnd = Window::get();
wnd->show();
return 0;
}
LUA_IMPLEMENT int l_hideWindow(lua_State* L)
{
Window* wnd = Window::get();
wnd->hide();
return 0;
}
LUA_IMPLEMENT int l_getSize(lua_State* L)
{
Window* wnd = Window::get();
luax_pushnumber(L, wnd->getW());
luax_pushnumber(L, wnd->getH());
return 2;
}
LUA_IMPLEMENT int l_getWidth(lua_State* L)
{
Window* wnd = Window::get();
luax_pushnumber(L, wnd->getW());
return 1;
}
LUA_IMPLEMENT int l_getHeight(lua_State* L)
{
Window* wnd = Window::get();
luax_pushnumber(L, wnd->getH());
return 1;
}
LUA_IMPLEMENT int l_newBitmap(lua_State* L)
{
Bitmap* bitmap = nullptr;
if (luax_gettop(L) == 2)
{
int w = luax_checkinteger(L, 1);
int h = luax_checkinteger(L, 2);
bitmap = new Bitmap(w, h);
}
else if (luax_gettop(L) == 3)
{
int w = luax_checkinteger(L, 1);
int h = luax_checkinteger(L, 2);
if (luax_istable(L, 3))
{
unsigned int r = luax_rawgetnumber(L, 3, 1);
unsigned int g = luax_rawgetnumber(L, 3, 2);
unsigned int b = luax_rawgetnumber(L, 3, 3);
unsigned int a = luax_rawgetnumber(L, 3, 4);
bitmap = new Bitmap(w, h, Color(r, g, b, a));
}
else if (luax_isfunction(L, 3))
{
std::function<Color(int, int, int, int)> drawer = [=](int w, int h, int x, int y)->Color{
luax_pushvalue(L, 3);
luax_pushnumber(L, w);
luax_pushnumber(L, h);
luax_pushnumber(L, x);
luax_pushnumber(L, y);
// Call drawer function.
luax_call(L, 4, 1);
// Get result color.
if (!luax_istable(L, -1))
luax_error(L, "Return value of bitmap drawer is wrong, should be a color table.");
Color c;
c.r = luax_rawgetnumberthenpop(L, -1, 1);
c.g = luax_rawgetnumberthenpop(L, -1, 2);
c.b = luax_rawgetnumberthenpop(L, -1, 3);
c.a = luax_rawgetnumberthenpop(L, -1, 4);
// Pop return value.
luax_pop(L, 1);
return c;
};
bitmap = new Bitmap(w, h, drawer);
}
else
{
luax_typerror(L, 3, "color table or color setter");
return 1;
}
}
else
{
const char* f = luax_checkstring(L, 1);
AssetDatabase* fs = AssetDatabase::get();
Buffer b;
try
{
if (!fs->exists(f))
throw Exception("No such image file %s.", f);
fs->read(f, b);
}
catch (Exception& e)
{
luax_errorf(L, "Failed to read image %s", f);
luax_pushnil(L);
return 1;
}
bitmap = new Bitmap(&b, b.size());
if (bitmap == nullptr)
{
luax_errorf(L, "Failed to decode image file %s", f);
luax_pushnil(L);
return 1;
}
}
LuaObject* luaObj = luax_newinstance(L, Jin_Lua_Bitmap, new Shared(bitmap));
return 1;
}
/* jin.graphics.newTexture(bitmap) */
LUA_IMPLEMENT int l_newTexture(lua_State* L)
{
Texture* texture = nullptr;
if (luax_istype(L, 1, Jin_Lua_Bitmap))
{
LuaObject* luaObj = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Bitmap);
Bitmap* bitmap = luaObj->getObject<Bitmap>();
texture = new Texture(bitmap);
}
else if (luax_isstring(L, 1))
{
const char* path = luax_checkstring(L, 1);
texture = new Texture(path);
}
LuaObject* luaObj = luax_newinstance(L, Jin_Lua_Texture, new Shared(texture));
return 1;
}
// See embed graphics.lua.
LUA_IMPLEMENT int l_newShader(lua_State* L)
{
const char* program = luax_checkstring(L, 1);
Shader* jsl = nullptr;
try
{
jsl = new Shader(program);
}
catch (JinEngine::Exception& e)
{
//luax_errorf(L, "Failed to compile shader");
luax_pushnil(L);
return 1;
}
LuaObject* luaObj = luax_newinstance(L, Jin_Lua_Shader, new Shared(jsl));
return 1;
}
// See embed graphics.lua
LUA_IMPLEMENT int l_newShaderf(lua_State* L)
{
const char* path = luax_checkstring(L, 1);
AssetDatabase* fs = AssetDatabase::get();
if (!fs->exists(path))
{
//luax_errorf(L, "No such shader file \"%s\"", path);
luax_pushnil(L);
return 1;
}
Buffer b;
fs->read(path, b);
Shader* jsl = nullptr;
try
{
jsl = new Shader((char*)&b);
}
catch (JinEngine::Exception& e)
{
//luax_errorf(L, "Failed to compile shader");
luax_pushnil(L);
return 1;
}
LuaObject* luaObj = luax_newinstance(L, Jin_Lua_Shader, new Shared(jsl));
return 1;
}
LUA_IMPLEMENT int l_newCanvas(lua_State* L)
{
int w = luax_checknumber(L, 1);
int h = luax_checknumber(L, 2);
Canvas* cvs = new Canvas(w, h);
LuaObject* luaObj = luax_newinstance(L, Jin_Lua_Canvas, new Shared(cvs));
return 1;
}
LUA_IMPLEMENT int l_clear(lua_State* L)
{
glClear(GL_COLOR_BUFFER_BIT);
return 0;
}
LUA_IMPLEMENT int l_setClearColor(lua_State* L)
{
if (luax_gettop(L) == 0)
{
glClearColor(0, 0, 0, 1);
return 0;
}
context.curClearColor.r = luax_checknumber(L, 1);
context.curClearColor.g = luax_checknumber(L, 2);
context.curClearColor.b = luax_checknumber(L, 3);
context.curClearColor.a = luax_checknumber(L, 4);
gl.setClearColor(context.curClearColor.r,
context.curClearColor.g,
context.curClearColor.b,
context.curClearColor.a);
return 0;
}
LUA_IMPLEMENT int l_present(lua_State* L)
{
Window::get()->present();
return 0;
}
LUA_IMPLEMENT void l_draw_texture(lua_State* L)
{
if (!luax_istype(L, 1, Jin_Lua_Texture))
return;
int x = luax_optnumber(L, 2, 0);
int y = luax_optnumber(L, 3, 0);
float sx = luax_optnumber(L, 4, 1);
float sy = luax_optnumber(L, 5, 1);
float r = luax_optnumber(L, 6, 0);
float ox = luax_optnumber(L, 7, 0);
float oy = luax_optnumber(L, 8, 0);
LuaObject* luaObj = (LuaObject*)luax_toudata(L, 1);
Texture* tex = luaObj->getObject<Texture>();
tex->render(x, y, sx, sy, r, ox, oy);
}
LUA_IMPLEMENT void l_draw_canvas(lua_State* L)
{
if (!luax_istype(L, 1, Jin_Lua_Canvas))
return;
int x = luax_optnumber(L, 2, 0);
int y = luax_optnumber(L, 3, 0);
float sx = luax_optnumber(L, 4, 1);
float sy = luax_optnumber(L, 5, 1);
float r = luax_optnumber(L, 6, 0);
float ox = luax_optnumber(L, 7, 0);
float oy = luax_optnumber(L, 8, 0);
LuaObject* luaObj = (LuaObject*)luax_toudata(L, 1);
Canvas* canvas = luaObj->getObject<Canvas>();
canvas->render(x, y, sx, sy, r, ox, oy);
}
/* jin.graphics.draw(text, font, x, y) */
LUA_IMPLEMENT void l_draw_text(lua_State* L)
{
if (!luax_istype(L, 1, Jin_Lua_Text))
return;
LuaObject* p = (LuaObject*)luax_toudata(L, 1);
Text* text = p->getObject<Text>();
int x = luax_optnumber(L, 3, 0);
int y = luax_optnumber(L, 4, 0);
int spacing = luax_optnumber(L, 6, 0);
Font* font = nullptr;
LuaObject* p2 = (LuaObject*)luax_toudata(L, 2);
if (luax_istype(L, 2, Jin_Lua_TextureFont))
{
TextureFont* tf = p2->getObject<TextureFont>();
font = tf;
}
else if (luax_istype(L, 2, Jin_Lua_TTF))
{
TTF* ttf = p2->getObject<TTF>();
font = ttf;
}
else
{
font = context.defaultFont;
}
int lineheight = luax_optnumber(L, 5, font->getFontSize());
font->render(*text, x, y, lineheight, spacing);
}
/* jin.graphics.draw(page, x, y) */
LUA_IMPLEMENT void l_draw_page(lua_State* L)
{
if (!luax_istype(L, 1, Jin_Lua_Page))
return;
int x = luax_optnumber(L, 2, 0);
int y = luax_optnumber(L, 3, 0);
LuaObject* p = (LuaObject*)luax_toudata(L, 1);
Page* page = p->getObject<Page>();
Font* font = page->font;
font->render(page, x, y);
}
LUA_IMPLEMENT void l_draw_sprite(lua_State* L)
{
if (!luax_istype(L, 1, Jin_Lua_Sprite))
return;
LuaObject* luaSprite = (LuaObject*)luax_toudata(L, 1);
Sprite* sprite = luaSprite->getObject<Sprite>();
float x = luax_checknumber(L, 2);
float y = luax_checknumber(L, 3);
float sx = luax_checknumber(L, 4);
float sy = luax_checknumber(L, 5);
float r = luax_checknumber(L, 6);
sprite->render(x, y, sx, sy, r);
}
LUA_IMPLEMENT void l_draw_mesh(lua_State* L)
{
if (!luax_istype(L, 1, Jin_Lua_Mesh))
return;
LuaObject* obj= (LuaObject*)luax_toudata(L, 1);
Mesh* mesh = obj->getObject<Mesh>();
int x = luax_optnumber(L, 2, 0);
int y = luax_optnumber(L, 3, 0);
float sx = luax_optnumber(L, 4, 1);
float sy = luax_optnumber(L, 5, 1);
float r = luax_optnumber(L, 6, 0);
float ox = luax_optnumber(L, 7, 0);
float oy = luax_optnumber(L, 8, 0);
mesh->render(x, y, sx, sy, r, ox, oy);
}
LUA_IMPLEMENT int l_draw(lua_State* L)
{
if (luax_istype(L, 1, Jin_Lua_Texture))
l_draw_texture(L);
else if (luax_istype(L, 1, Jin_Lua_Canvas))
l_draw_canvas(L);
else if (luax_istype(L, 1, Jin_Lua_Text))
l_draw_text(L);
else if (luax_istype(L, 1, Jin_Lua_Page))
l_draw_page(L);
else if (luax_istype(L, 1, Jin_Lua_Sprite))
l_draw_sprite(L);
else if (luax_istype(L, 1, Jin_Lua_Mesh))
l_draw_mesh(L);
else
{
luax_typerror(L, 1, "texture or canvas");
return 1;
}
return 0;
}
// draw(tex, quad, x, y, sx, sy, r, ax, ay)
LUA_IMPLEMENT int l_drawq(lua_State* L)
{
if (!luax_istable(L, 2))
{
luax_typerror(L, 2, "table");
return 1;
}
Math::Quad q;
q.x = luax_rawgetnumber(L, 2, 1);
q.y = luax_rawgetnumber(L, 2, 2);
q.w = luax_rawgetnumber(L, 2, 3);
q.h = luax_rawgetnumber(L, 2, 4);
luax_pop(L, 4);
int x = luax_optnumber(L, 3, 0);
int y = luax_optnumber(L, 4, 0);
float sx = luax_optnumber(L, 5, 1);
float sy = luax_optnumber(L, 6, 1);
float r = luax_optnumber(L, 7, 0);
float ox = luax_optnumber(L, 8, 0);
float oy = luax_optnumber(L, 9, 0);
if (luax_istype(L, 1, Jin_Lua_Texture))
{
LuaObject* luaObj = (LuaObject*)luax_toudata(L, 1);
Texture* tex = luaObj->getObject<Texture>();
tex->render(q, x, y, sx, sy, r, ox, oy);
}
else if (luax_istype(L, 1, Jin_Lua_Canvas))
{
LuaObject* luaObj = (LuaObject*)luax_toudata(L, 1);
Canvas* canvas = luaObj->getObject<Canvas>();
canvas->render(q, x, y, sx, sy, r, ox, oy);
}
else
{
luax_typerror(L, 1, "texture or canvas");
return 1;
}
return 0;
}
/* print(string, x, y, lineheight, spacing) */
/* need set font */
LUA_IMPLEMENT int l_print(lua_State* L)
{
Font* font = gl.getFont();
if (font == nullptr)
return 0;
unsigned length;
const char* str = luax_checklstring(L, 1, &length);
Text text(Encode::UTF8, str, length);
int x = luax_optnumber(L, 2, 0);
int y = luax_optnumber(L, 3, 0);
int lineheight = luax_optnumber(L, 4, font->getFontSize());
int spacing = luax_optnumber(L, 5, 0);
font->render(text, x, y, lineheight, spacing);
return 0;
}
LUA_IMPLEMENT int l_setColor(lua_State* L)
{
if (luax_gettop(L) == 0)
{
gl.setColor(Color(255, 255, 255, 255));
return 0;
}
context.curRenderColor.r = luax_checknumber(L, 1);
context.curRenderColor.g = luax_checknumber(L, 2);
context.curRenderColor.b = luax_checknumber(L, 3);
if (luax_gettop(L) == 4)
context.curRenderColor.a = luax_checknumber(L, 4);
else
context.curRenderColor.a = 255;
gl.setColor(context.curRenderColor);
return 0;
}
LUA_IMPLEMENT int l_getColor(lua_State * L)
{
luax_pushnumber(L, context.curRenderColor.r);
luax_pushnumber(L, context.curRenderColor.g);
luax_pushnumber(L, context.curRenderColor.b);
luax_pushnumber(L, context.curRenderColor.a);
return 4;
}
LUA_IMPLEMENT int l_bindCanvas(lua_State* L)
{
if (luax_gettop(L) == 0)
{
// bind to default canvas
gl.unbindCanvas();
return 0;
}
LuaObject* luaObj = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Canvas);
Canvas* canvas = luaObj->getObject<Canvas>();
gl.bindCanvas(canvas);
return 0;
}
LUA_IMPLEMENT int l_unbindCanvas(lua_State* L)
{
gl.unbindCanvas();
return 0;
}
LUA_IMPLEMENT int l_useShader(lua_State* L)
{
if (luax_gettop(L) == 0)
{
gl.unuseShader();
return 0;
}
if (luax_istype(L, 1, Jin_Lua_Shader))
{
LuaObject* luaObj = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Shader);
Shader* shader = luaObj->getObject<Shader>();
gl.useShader(shader);
}
else
{
luax_typerror(L, 1, "JSL shader");
}
return 0;
}
LUA_IMPLEMENT int l_setBlendMode(lua_State* L)
{
int mode = luax_checkinteger(L, 1);
gl.setBlendMode(static_cast<OpenGL::BlendMode>(mode));
return 0;
}
LUA_IMPLEMENT int l_getBlendMode(lua_State* L)
{
int mode = static_cast<int>(gl.getBlendMode());
luax_pushinteger(L, mode);
return 1;
}
LUA_IMPLEMENT int l_point(lua_State* L)
{
int x = luax_checknumber(L, 1);
int y = luax_checknumber(L, 2);
JinEngine::Graphics::point(x, y);
return 0;
}
LUA_IMPLEMENT int l_line(lua_State* L)
{
int x1 = luax_checknumber(L, 1);
int y1 = luax_checknumber(L, 2);
int x2 = luax_checknumber(L, 3);
int y2 = luax_checknumber(L, 4);
JinEngine::Graphics::line(x1, y1, x2, y2);
return 0;
}
LUA_IMPLEMENT int l_rect(lua_State* L)
{
RenderMode mode = static_cast<RenderMode>(luax_checkinteger(L, 1));
if (mode != RenderMode::NONE)
{
int x = luax_checknumber(L, 2);
int y = luax_checknumber(L, 3);
int w = luax_checknumber(L, 4);
int h = luax_checknumber(L, 5);
rect(mode, x, y, w, h);
}
else
{
luax_typerror(L, 1, "'fill' or 'line'");
return 1;
}
return 0;
}
LUA_IMPLEMENT int l_circle(lua_State* L)
{
RenderMode mode = static_cast<RenderMode>(luax_checkinteger(L, 1));
if (mode != RenderMode::NONE)
{
int x = luax_checknumber(L, 2);
int y = luax_checknumber(L, 3);
float r = luax_checknumber(L, 4);
circle(mode, x, y, r);
}
else
{
luax_typerror(L, 1, "'fill' or 'line'");
return 1;
}
return 0;
}
LUA_IMPLEMENT int l_triangle(lua_State* L)
{
RenderMode mode = static_cast<RenderMode>(luax_checkinteger(L, 1));
if (mode != RenderMode::NONE)
{
int x = luax_checknumber(L, 2);
int y = luax_checknumber(L, 3);
int x2 = luax_checknumber(L, 3);
int y2 = luax_checknumber(L, 4);
int x3 = luax_checknumber(L, 5);
int y3 = luax_checknumber(L, 6);
triangle(mode, x, y, x2, y2, x3, y3);
}
else
{
luax_typerror(L, 1, "'fill' or 'line'");
return 1;
}
return 0;
}
LUA_IMPLEMENT int l_polygon(lua_State* L)
{
RenderMode mode = static_cast<RenderMode>(luax_checkinteger(L, 1));
int n = luax_checknumber(L, 2);
if (mode != RenderMode::NONE)
{
if (!luax_istable(L, 3))
{
luax_typerror(L, 3, "table");
return 1;
}
int tn = luax_tableidxlen(L, 3);
if (tn != n * 2)
{
LUA_IMPLEMENT char* emsg = \
"number of polygon vertices doesn't match " \
"provided n, expect %d numbers but get %d";
luax_error(L, emsg, n * 2, tn);
return 1;
}
float* p = (float*)alloca(2 * n * sizeof(float));
for (int i = 1; i <= 2 * n; ++i)
p[i - 1] = luax_rawgetnumber(L, 3, i);
polygon(mode, p, n);
}
else
{
luax_typerror(L, 1, "'fill' or 'line'");
return 1;
}
return 0;
}
LUA_IMPLEMENT int l_newTTFData(lua_State* L)
{
TTFData* fd = nullptr;
{
const char* path = luax_checkstring(L, 1);
AssetDatabase* fs = AssetDatabase::get();
if (!fs->exists(path))
{
luax_errorf(L, "No such font \"%s\"", path);
luax_pushnil(L);
return 1;
}
Buffer b;
fs->read(path, b);
fd = new TTFData(&b, b.size());
}
LuaObject* luaObj = luax_newinstance(L, Jin_Lua_TTFData, new Shared(fd));
return 1;
}
/* newText(str[, encode]) */
LUA_IMPLEMENT int l_newText(lua_State* L)
{
Encode encode = Encode::UTF8;
if (luax_gettop(L) == 2)
{
const char* e = luax_checkstring(L, 2);
if (strcmp(e, "UTF8") == 0) encode = Encode::UTF8;
//else if (strcmp(e, "UTF16") == 0) encode = Encode::UTF16;
else if (strcmp(e, "ASCII") == 0) encode = Encode::ASCII;
else
{
luax_error(L, "wrong text encode %s", e);
return 0;
}
}
unsigned length;
const char* data = luax_checklstring(L, 1, &length);
Text* text = new Text(encode, data, length);
LuaObject* luaObj = luax_newinstance(L, Jin_Lua_Text, new Shared(text));
return 1;
}
// newSprite(Texture tex, Quad quad, Origin origin)
// newSprite(Texture tex, Quad quad, Number ox, Number oy)
// newSprite(Texture tex, Origin origin)
// newSprite(Texture tex, Number ox, Number oy)
LUA_IMPLEMENT int l_newSprite(lua_State* L)
{
int n = luax_gettop(L);
LuaObject* objGraphic = nullptr;
if (luax_istype(L, 1, Jin_Lua_Texture))
objGraphic = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Texture);
else if (luax_istype(L, 1, Jin_Lua_Canvas))
objGraphic = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Canvas);
Graphic* graphic = objGraphic->getObject<Graphic>();
if (objGraphic != nullptr)
{
if (n == 3 && luax_istable(L, 2))
{
Quad quad;
quad.x = luax_rawgetnumberthenpop(L, 2, 1);
quad.y = luax_rawgetnumberthenpop(L, 2, 2);
quad.w = luax_rawgetnumberthenpop(L, 2, 3);
quad.h = luax_rawgetnumberthenpop(L, 2, 4);
int o = luax_checkinteger(L, 3);
Origin origin = static_cast<Origin>(o);
LuaObject* p = luax_newinstance(L, Jin_Lua_Sprite, new Shared(new Sprite(graphic, quad, origin)));
p->setDependency((int)SpriteDependency::DEP_GRAPHIC, objGraphic);
}
else if (n == 4)
{
Quad quad;
quad.x = luax_rawgetnumberthenpop(L, 2, 1);
quad.y = luax_rawgetnumberthenpop(L, 2, 2);
quad.w = luax_rawgetnumberthenpop(L, 2, 3);
quad.h = luax_rawgetnumberthenpop(L, 2, 4);
int ox = luax_checkinteger(L, 3);
int oy = luax_checkinteger(L, 4);
LuaObject* p = luax_newinstance(L, Jin_Lua_Sprite, new Shared(new Sprite(graphic, quad, ox, oy)));
p->setDependency((int)SpriteDependency::DEP_GRAPHIC, objGraphic);
}
else if (n == 2)
{
int o = luax_checkinteger(L, 2);
Origin origin = static_cast<Origin>(o);
LuaObject* p = luax_newinstance(L, Jin_Lua_Sprite, new Shared(new Sprite(graphic, origin)));
p->setDependency((int)SpriteDependency::DEP_GRAPHIC, objGraphic);
}
else if (n == 3)
{
int ox = luax_checkinteger(L, 2);
int oy = luax_checkinteger(L, 3);
LuaObject* p = luax_newinstance(L, Jin_Lua_Sprite, new Shared(new Sprite(graphic, ox, oy)));
p->setDependency((int)SpriteDependency::DEP_GRAPHIC, objGraphic);
}
else
{
luax_error(L, "No matched overloaded functions.");
return 1;
}
}
return 1;
}
LUA_IMPLEMENT int l_newSpriteSheet(lua_State* L)
{
LuaObject* objGraphic = nullptr;
if (luax_istype(L, 1, Jin_Lua_Texture))
objGraphic = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Texture);
else if(luax_istype(L, 1, Jin_Lua_Canvas))
objGraphic = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Canvas);
if (objGraphic != nullptr)
{
Graphic* graphic = objGraphic->getObject<Graphic>();
Shared* shrSSheet = new Shared(new SpriteSheet(graphic));
LuaObject* luaSSheet = luax_newinstance(L, Jin_Lua_SpriteSheet, shrSSheet);
luaSSheet->setDependency((int)SpriteSheetDependency::DEP_GRAPHIC, objGraphic);
return 1;
}
else
return 0;
}
// newAnimation([frames table, loop, speed])
LUA_IMPLEMENT int l_newAnimation(lua_State* L)
{
int argc = luax_gettop(L);
Animation* animation = new Animation();
Shared* shrAnimation = new Shared(animation);
LuaObject* luaAnimation = luax_newinstance(L, Jin_Lua_Animation, shrAnimation);
if (argc >= 3)
{
if (!luax_istable(L, 1))
{
luax_typerror(L, 1, "frames table");
return 1;
}
bool loop = luax_checkbool(L, 2);
float speed = luax_checknumber(L, 3);
int n = luax_tableidxlen(L, 1);
for (int i = 1; i <= n; ++i)
{
luax_rawgeti(L, 1, i);
LuaObject* luaSprite = (LuaObject*)luax_checktype(L, -1, Jin_Lua_Sprite);
animation->addFrame(luaSprite->getObject<Sprite>());
int index = animation->getFrameCount() - 1;
luaAnimation->setDependency((int)AnimationDependency::DEP_SPRITES + index, luaSprite);
}
animation->setLoop(loop);
animation->setSpeed(speed);
}
luax_pushvalue(L, argc + 1);
return 1;
}
// newAnimator([animation])
LUA_IMPLEMENT int l_newAnimator(lua_State* L)
{
int argc = luax_gettop(L);
Animator* animator = new Animator();
Shared* shrAniamtor = new Shared(animator);
if (argc >= 1)
{
LuaObject* animation = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Animation);
animator->setAnimation(animation->getObject<Animation>());
LuaObject* luaAnimator = luax_newinstance(L, Jin_Lua_Animator, shrAniamtor);
luaAnimator->setDependency((int)AnimatorDependency::DEP_ANIMATION, animation);
return 1;
}
LuaObject* luaAnimator = luax_newinstance(L, Jin_Lua_Animator, shrAniamtor);
return 1;
}
/* newTextureFont(bitmap, text, color | cellw, cellh) */
LUA_IMPLEMENT int l_newTextureFont(lua_State* L)
{
LuaObject* p = (LuaObject*)luax_checktype(L, 1, Jin_Lua_Bitmap);
Bitmap* bitmap = p->getObject<Bitmap>();
Text* text;
if (luax_istype(L, 2, Jin_Lua_Text))
{
LuaObject* pt = (LuaObject*)luax_checktype(L, 2, Jin_Lua_Text);
text = pt->getObject<Text>();
}
else if (luax_isstring(L, 2))
{
unsigned len;
const char* str = luax_checklstring(L, 2, &len);
text = new Text(Encode::UTF8, str, len);
}
else
{
luax_typerror(L, 2, "Text or string");
return 1;
}
float cellh = luax_checknumber(L, 4);
TextureFont* textureFont = nullptr;
if (luax_istable(L, 3))
{
unsigned int r = luax_rawgetnumber(L, 3, 1);
unsigned int g = luax_rawgetnumber(L, 3, 2);
unsigned int b = luax_rawgetnumber(L, 3, 3);
unsigned int a = luax_rawgetnumber(L, 3, 4);
textureFont = new TextureFont(bitmap, *text, Color(r, g, b, a), cellh);
}
else if (luax_isnumber(L, 3))
{
float cellw = luax_checknumber(L, 3);
textureFont = new TextureFont(bitmap, *text, cellw, cellh);
}
else
{
luax_error(L, "bad arguments #3 to 'newTextureFont', need to be table or number");
return 0;
}
if (luax_isstring(L, 2))
{
// Delete temporary text.
delete text;
}
LuaObject* luaObj = luax_newinstance(L, Jin_Lua_TextureFont, new Shared(textureFont));
return 1;
}
LUA_IMPLEMENT int l_newParticleSystem(lua_State* L)
{
LuaObject* luaObj = luax_newinstance(L, Jin_Lua_ParticleSystem, new Shared(new ParticleSystem()));
return 1;
}
LUA_IMPLEMENT int l_newMesh(lua_State* L)
{
LuaObject* luaObj = luax_newinstance(L, Jin_Lua_Mesh, new Shared(new Mesh()));
return 1;
}
/* setFont(font) */
LUA_IMPLEMENT int l_setFont(lua_State* L)
{
if (luax_istype(L, 1, Jin_Lua_TTF))
{
LuaObject* p = (LuaObject*)luax_checktype(L, 1, Jin_Lua_TTF);
TTF* ttf = p->getObject<TTF>();
gl.setFont(ttf);
}
else if (luax_istype(L, 1, Jin_Lua_TextureFont))
{
LuaObject* p = (LuaObject*)luax_checktype(L, 1, Jin_Lua_TextureFont);
TextureFont* tf = p->getObject<TextureFont>();
gl.setFont(tf);
}
return 0;
}
LUA_IMPLEMENT int l_unsetFont(lua_State* L)
{
gl.setFont(context.defaultFont);
return 0;
}
LUA_IMPLEMENT int l_clearMatrix(lua_State* L)
{
gl.clearMatrix();
return 0;
}
LUA_IMPLEMENT int l_pushMatrix(lua_State* L)
{
gl.pushMatrix();
return 0;
}
LUA_IMPLEMENT int l_popMatrix(lua_State* L)
{
gl.popMatrix();
return 0;
}
LUA_IMPLEMENT int l_scale(lua_State* L)
{
float sx = luax_checknumber(L, 1);
float sy = luax_checknumber(L, 2);
gl.scale(sx, sy);
return 0;
}
LUA_IMPLEMENT int l_translate(lua_State* L)
{
float x = luax_checknumber(L, 1);
float y = luax_checknumber(L, 2);
gl.translate(x, y);
return 0;
}
LUA_IMPLEMENT int l_rotate(lua_State* L)
{
float r = luax_checknumber(L, 1);
gl.rotate(r);
return 0;
}
LUA_IMPLEMENT int l_getStats(lua_State* L)
{
OpenGL::Stats stats = gl.getStats();
luax_newtable(L);
luax_setfieldinteger(L, "drawCalls", stats.drawCalls);
luax_setfieldinteger(L, "canvasSwitches", stats.canvasSwitches);
luax_setfieldinteger(L, "shaderSwitches", stats.shaderSwitches);
luax_setfieldinteger(L, "fontSwitches", stats.fontSwitches);
luax_setfieldinteger(L, "textures", stats.textures);
luax_setfieldinteger(L, "canvases", stats.canvases);
luax_setfieldinteger(L, "fonts", stats.fonts);
return 1;
}
LUA_EXPORT int luaopen_graphics(lua_State* L)
{
luaopen_Bitmap(L);
luaopen_Texture(L);
luaopen_Canvas(L);
luaopen_TTFData(L);
luaopen_TTF(L);
luaopen_Text(L);
luaopen_TextureFont(L);
luaopen_Page(L);
luaopen_Shader(L);
luaopen_Sprite(L);
luaopen_SpriteSheet(L);
luaopen_Animation(L);
luaopen_Animator(L);
luaopen_ParticleSystem(L);
luaopen_Mesh(L);
luaL_Reg methods[] = {
{ "getStats", l_getStats },
/* window */
{ "init", l_init },
{ "setTitle", l_setTitle },
{ "getSize", l_getSize },
{ "getWidth", l_getWidth },
{ "getHeight", l_getHeight },
{ "destroy", l_destroy },
{ "hideWindow", l_hideWindow },
{ "showWindow", l_showWindow },
/* creators */
{ "newBitmap", l_newBitmap },
{ "newTexture", l_newTexture },
{ "newShader", l_newShader },
{ "newShaderf", l_newShaderf },
{ "newCanvas", l_newCanvas },
{ "newTTFData", l_newTTFData },
{ "newText", l_newText },
{ "newTextureFont", l_newTextureFont },
{ "newSprite", l_newSprite },
{ "newSpriteSheet", l_newSpriteSheet },
{ "newAnimation", l_newAnimation },
{ "newAnimator", l_newAnimator },
{ "newParticleSystem", l_newParticleSystem },
{ "newMesh", l_newMesh },
/* render */
{ "setClearColor", l_setClearColor },
{ "clear", l_clear },
{ "draw", l_draw },
{ "print", l_print },
{ "drawq", l_drawq },
{ "setColor", l_setColor },
{ "getColor", l_getColor },
{ "present", l_present },
{ "setBlendMode", l_setBlendMode },
{ "getBlendMode", l_getBlendMode },
/* canvas */
{ "bindCanvas", l_bindCanvas },
{ "unbindCanvas", l_unbindCanvas },
/* shader */
{ "useShader", l_useShader },
/* shapes */
{ "point", l_point },
{ "line", l_line },
{ "rect", l_rect },
{ "circle", l_circle },
{ "triangle", l_triangle },
{ "polygon", l_polygon },
/* font */
{ "setFont", l_setFont },
{ "unsetFont", l_unsetFont },
/* transform */
{ "pushMatrix", l_pushMatrix },
{ "clearMatrix", l_clearMatrix },
{ "popMatrix", l_popMatrix },
{ "translate", l_translate },
{ "rotate", l_rotate },
{ "scale", l_scale },
{ 0, 0 }
};
// Load whole lib.
luax_newlib(L, methods);
return 1;
}
} // namespace Lua
} // namespace JinEngine
|