-
Notifications
You must be signed in to change notification settings - Fork 13
/
raylib-lua.h
5782 lines (5191 loc) · 165 KB
/
raylib-lua.h
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
/**********************************************************************************************
*
* raylib-lua v2.0 - raylib Lua bindings for raylib v2.0
*
* NOTES:
*
* The following types are treated as objects with named fields, same as in C.
* Color, Vector2, Vector3, Rectangle, Ray, Camera, Camera2D
*
* Lua defines utility functions to create those objects.
*
* USAGE EXAMPLE:
* local cl = Color(255,255,255,255)
* local rec = Rectangle(10, 10, 100, 100)
* local ray = Ray(Vector3(20, 20, 20), Vector3(50, 50, 50))
* local x2 = rec.x + rec.width
*
* The following types are immutable, and you can only read their non-pointer arguments.
* Image, Texture2D, RenderTexture2D, Font
*
* All other object types are opaque, that is, you cannot access or change their fields directly.
*
* Remember that ALL raylib types have REFERENCE SEMANTICS in Lua.
* There is currently no way to create a copy of an opaque object.
*
* Some raylib functions take a pointer to an array, and the size of that array.
* The equivalent Lua functions take only an array table of the specified type UNLESS
* it's a pointer to a large char array (e.g. for images), then it takes (and potentially returns)
* a Lua string (without the size argument, as Lua strings are sized by default).
*
* Some raylib functions take pointers to objects to modify (e.g. ImageToPOT(), etc.)
* In Lua, these functions take values and return a new changed value, instead.
*
* So, in C: ImageToPOT(&image, BLACK);
* In Lua becomes: image = ImageToPOT(image, BLACK)
*
* Remember that Lua functions can return multiple values.
* This is to preserve value semantics of raylib objects.
*
* CONTRIBUTORS:
* Ghassan Al-Mashareqa (ghassan@ghassan.pl): Original binding creation (for raylib 1.3)
* Ramon Santamaria (@raysan5): Review, update and maintenance
*
*
* LICENSE: zlib/libpng
*
* Copyright (c) 2015-2017 Ghassan Al-Mashareqa and Ramon Santamaria (@raysan5)
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you
* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
* as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
**********************************************************************************************/
#pragma once
#include "raylib.h"
#define RLUA_STATIC
#ifdef RLUA_STATIC
#define RLUADEF static // Functions just visible to module including this file
#else
#ifdef __cplusplus
#define RLUADEF extern "C" // Functions visible from other files (no name mangling of functions in C++)
#else
#define RLUADEF extern // Functions visible from other files
#endif
#endif
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
// ...
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
RLUADEF void rLuaInitDevice(void); // Initialize Lua system
RLUADEF void rLuaExecuteCode(const char *code); // Execute raylib Lua code
RLUADEF void rLuaExecuteFile(const char *filename); // Execute raylib Lua script
RLUADEF void rLuaCloseDevice(void); // De-initialize Lua system
/***********************************************************************************
*
* RLUA IMPLEMENTATION
*
************************************************************************************/
#if defined(RLUA_IMPLEMENTATION)
#include "raylib.h"
#include "raymath.h"
#define PHYSAC_IMPLEMENTATION
#include "physac.h"
#include <string.h>
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
#define LuaPush_int(L, value) lua_pushinteger(L, value)
#define LuaPush_float(L, value) lua_pushnumber(L, value);
#define LuaPush_bool(L, value) lua_pushboolean(L, value)
#define LuaPush_string(L, value) lua_pushstring(L, value)
#define LuaPush_Image(L, img) LuaPushOpaqueTypeWithMetatable(L, img, Image)
#define LuaPush_Texture2D(L, tex) LuaPushOpaqueTypeWithMetatable(L, tex, Texture2D)
#define LuaPush_RenderTexture2D(L, tex) LuaPushOpaqueTypeWithMetatable(L, tex, RenderTexture2D)
#define LuaPush_Font(L, sf) LuaPushOpaqueTypeWithMetatable(L, sf, Font)
#define LuaPush_Mesh(L, vd) LuaPushOpaqueType(L, vd)
#define LuaPush_Shader(L, s) LuaPushOpaqueType(L, s)
#define LuaPush_Sound(L, snd) LuaPushOpaqueType(L, snd)
#define LuaPush_Wave(L, wav) LuaPushOpaqueType(L, wav)
#define LuaPush_Music(L, mus) LuaPushOpaqueType(L, mus)
#define LuaPush_AudioStream(L, aud) LuaPushOpaqueType(L, aud)
#define LuaPush_PhysicsBody(L, body) LuaPushOpaqueType(L, body)
#define LuaGetArgument_ptr (void *)luaL_checkinteger
#define LuaGetArgument_int (int)luaL_checkinteger
#define LuaGetArgument_unsigned (unsigned)luaL_checkinteger
#define LuaGetArgument_char (char)luaL_checkinteger
#define LuaGetArgument_float (float)luaL_checknumber
#define LuaGetArgument_double luaL_checknumber
#define LuaGetArgument_string luaL_checkstring
#define LuaGetArgument_Image(L, img) *(Image *)LuaGetArgumentOpaqueTypeWithMetatable(L, img, "Image")
#define LuaGetArgument_Texture2D(L, tex) *(Texture2D *)LuaGetArgumentOpaqueTypeWithMetatable(L, tex, "Texture2D")
#define LuaGetArgument_RenderTexture2D(L, rtex) *(RenderTexture2D *)LuaGetArgumentOpaqueTypeWithMetatable(L, rtex, "RenderTexture2D")
#define LuaGetArgument_Font(L, sf) *(Font *)LuaGetArgumentOpaqueTypeWithMetatable(L, sf, "Font")
#define LuaGetArgument_Mesh(L, vd) *(Mesh *)LuaGetArgumentOpaqueType(L, vd)
#define LuaGetArgument_Shader(L, s) *(Shader *)LuaGetArgumentOpaqueType(L, s)
#define LuaGetArgument_Sound(L, snd) *(Sound *)LuaGetArgumentOpaqueType(L, snd)
#define LuaGetArgument_Wave(L, wav) *(Wave *)LuaGetArgumentOpaqueType(L, wav)
#define LuaGetArgument_Music(L, mus) *(Music *)LuaGetArgumentOpaqueType(L, mus)
#define LuaGetArgument_AudioStream(L, aud) *(AudioStream *)LuaGetArgumentOpaqueType(L, aud)
#define LuaGetArgument_PhysicsBody(L, body) *(PhysicsBody *)LuaGetArgumentOpaqueType(L, body)
#define LuaPushOpaqueType(L, str) LuaPushOpaque(L, &str, sizeof(str))
#define LuaPushOpaqueTypeWithMetatable(L, str, meta) LuaPushOpaqueWithMetatable(L, &str, sizeof(str), #meta)
//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
static lua_State* mainLuaState = 0;
static lua_State* L = 0;
//----------------------------------------------------------------------------------
// Module specific Functions Declaration
//----------------------------------------------------------------------------------
static void LuaPush_Color(lua_State* L, Color color);
static void LuaPush_Vector2(lua_State* L, Vector2 vec);
static void LuaPush_Vector3(lua_State* L, Vector3 vec);
static void LuaPush_Vector4(lua_State* L, Vector4 vec);
static void LuaPush_Quaternion(lua_State* L, Quaternion vec);
static void LuaPush_Matrix(lua_State* L, Matrix *matrix);
static void LuaPush_Rectangle(lua_State* L, Rectangle rect);
static void LuaPush_Camera(lua_State* L, Camera cam);
static void LuaPush_Camera2D(lua_State* L, Camera2D cam);
static void LuaPush_Model(lua_State* L, Model mdl);
static void LuaPush_Ray(lua_State* L, Ray ray);
static void LuaPush_RayHitInfo(lua_State* L, RayHitInfo hit);
static Color LuaGetArgument_Color(lua_State* L, int index);
static Vector2 LuaGetArgument_Vector2(lua_State* L, int index);
static Vector3 LuaGetArgument_Vector3(lua_State* L, int index);
static Vector4 LuaGetArgument_Vector4(lua_State* L, int index);
static Quaternion LuaGetArgument_Quaternion(lua_State* L, int index);
static Matrix LuaGetArgument_Matrix(lua_State* L, int index);
static Rectangle LuaGetArgument_Rectangle(lua_State* L, int index);
static Camera LuaGetArgument_Camera(lua_State* L, int index);
static Camera2D LuaGetArgument_Camera2D(lua_State* L, int index);
static Model LuaGetArgument_Model(lua_State* L, int index);
static Ray LuaGetArgument_Ray(lua_State* L, int index);
//----------------------------------------------------------------------------------
// rlua Helper Functions
//----------------------------------------------------------------------------------
static void LuaStartEnum(void)
{
lua_newtable(L);
}
static void LuaSetEnum(const char *name, int value)
{
LuaPush_int(L, value);
lua_setfield(L, -2, name);
}
static void LuaSetEnumColor(const char *name, Color color)
{
LuaPush_Color(L, color);
lua_setfield(L, -2, name);
}
static void LuaEndEnum(const char *name)
{
lua_setglobal(L, name);
}
static void LuaPushOpaque(lua_State* L, void *ptr, size_t size)
{
void *ud = lua_newuserdata(L, size);
memcpy(ud, ptr, size);
}
static void LuaPushOpaqueWithMetatable(lua_State* L, void *ptr, size_t size, const char *metatable_name)
{
void *ud = lua_newuserdata(L, size);
memcpy(ud, ptr, size);
luaL_setmetatable(L, metatable_name);
}
static void* LuaGetArgumentOpaqueType(lua_State* L, int index)
{
return lua_touserdata(L, index);
}
static void* LuaGetArgumentOpaqueTypeWithMetatable(lua_State* L, int index, const char *metatable_name)
{
return luaL_checkudata(L, index, metatable_name);
}
//----------------------------------------------------------------------------------
// LuaIndex* functions
//----------------------------------------------------------------------------------
static int LuaIndexImage(lua_State* L)
{
Image img = LuaGetArgument_Image(L, 1);
const char *key = luaL_checkstring(L, 2);
if (!strcmp(key, "width")) LuaPush_int(L, img.width);
else if (!strcmp(key, "height")) LuaPush_int(L, img.height);
else if (!strcmp(key, "mipmaps")) LuaPush_int(L, img.mipmaps);
else if (!strcmp(key, "format")) LuaPush_int(L, img.format);
else return 0;
return 1;
}
static int LuaIndexTexture2D(lua_State* L)
{
Texture2D img = LuaGetArgument_Texture2D(L, 1);
const char *key = luaL_checkstring(L, 2);
if (!strcmp(key, "width")) LuaPush_int(L, img.width);
else if (!strcmp(key, "height")) LuaPush_int(L, img.height);
else if (!strcmp(key, "mipmaps")) LuaPush_int(L, img.mipmaps);
else if (!strcmp(key, "format")) LuaPush_int(L, img.format);
else if (!strcmp(key, "id")) LuaPush_int(L, img.id);
else return 0;
return 1;
}
static int LuaIndexRenderTexture2D(lua_State* L)
{
RenderTexture2D img = LuaGetArgument_RenderTexture2D(L, 1);
const char *key = luaL_checkstring(L, 2);
if (!strcmp(key, "texture")) LuaPush_Texture2D(L, img.texture);
else if (!strcmp(key, "depth")) LuaPush_Texture2D(L, img.depth);
else return 0;
return 1;
}
static int LuaIndexFont(lua_State* L)
{
Font img = LuaGetArgument_Font(L, 1);
const char *key = luaL_checkstring(L, 2);
if (!strcmp(key, "baseSize")) LuaPush_int(L, img.baseSize);
else if (!strcmp(key, "texture")) LuaPush_Texture2D(L, img.texture);
else if (!strcmp(key, "charsCount")) LuaPush_int(L, img.charsCount);
else return 0;
return 1;
}
static void LuaBuildOpaqueMetatables(void)
{
luaL_newmetatable(L, "Image");
lua_pushcfunction(L, &LuaIndexImage);
lua_setfield(L, -2, "__index");
lua_pop(L, 1);
luaL_newmetatable(L, "Texture2D");
lua_pushcfunction(L, &LuaIndexTexture2D);
lua_setfield(L, -2, "__index");
lua_pop(L, 1);
luaL_newmetatable(L, "RenderTexture2D");
lua_pushcfunction(L, &LuaIndexRenderTexture2D);
lua_setfield(L, -2, "__index");
lua_pop(L, 1);
luaL_newmetatable(L, "Font");
lua_pushcfunction(L, &LuaIndexFont);
lua_setfield(L, -2, "__index");
lua_pop(L, 1);
}
//----------------------------------------------------------------------------------
// LuaGetArgument functions
//----------------------------------------------------------------------------------
// Vector2 type
static Vector2 LuaGetArgument_Vector2(lua_State *L, int index)
{
Vector2 result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "x") == LUA_TNUMBER, index, "Expected Vector2.x");
result.x = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "y") == LUA_TNUMBER, index, "Expected Vector2.y");
result.y = LuaGetArgument_float(L, -1);
lua_pop(L, 2);
return result;
}
// Vector3 type
static Vector3 LuaGetArgument_Vector3(lua_State *L, int index)
{
Vector3 result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "x") == LUA_TNUMBER, index, "Expected Vector3.x");
result.x = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "y") == LUA_TNUMBER, index, "Expected Vector3.y");
result.y = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "z") == LUA_TNUMBER, index, "Expected Vector3.z");
result.z = LuaGetArgument_float(L, -1);
lua_pop(L, 3);
return result;
}
// Vector4 type
static Vector4 LuaGetArgument_Vector4(lua_State *L, int index)
{
Vector4 result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "x") == LUA_TNUMBER, index, "Expected Vector4.x");
result.x = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "y") == LUA_TNUMBER, index, "Expected Vector4.y");
result.y = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "z") == LUA_TNUMBER, index, "Expected Vector4.z");
result.z = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "w") == LUA_TNUMBER, index, "Expected Vector4.w");
result.w = LuaGetArgument_float(L, -1);
lua_pop(L, 4);
return result;
}
static Quaternion LuaGetArgument_Quaternion(lua_State* L, int index)
{
Quaternion result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "x") == LUA_TNUMBER, index, "Expected Quaternion.x");
result.x = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "y") == LUA_TNUMBER, index, "Expected Quaternion.y");
result.y = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "z") == LUA_TNUMBER, index, "Expected Quaternion.z");
result.z = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "w") == LUA_TNUMBER, index, "Expected Quaternion.w");
result.w = LuaGetArgument_float(L, -1);
lua_pop(L, 4);
return result;
}
// Matrix type
static Matrix LuaGetArgument_Matrix(lua_State* L, int index)
{
Matrix result = { 0 };
float* ptr = &result.m0;
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
for (int i = 0; i < 16; i++)
{
lua_geti(L, index, i+1);
ptr[i] = luaL_checknumber(L, -1);
}
lua_pop(L, 16);
return result;
}
// Color type, RGBA (32bit)
static Color LuaGetArgument_Color(lua_State *L, int index)
{
Color result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "r") == LUA_TNUMBER, index, "Expected Color.r");
result.r = LuaGetArgument_unsigned(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "g") == LUA_TNUMBER, index, "Expected Color.g");
result.g = LuaGetArgument_unsigned(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "b") == LUA_TNUMBER, index, "Expected Color.b");
result.b = LuaGetArgument_unsigned(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "a") == LUA_TNUMBER, index, "Expected Color.a");
result.a = LuaGetArgument_unsigned(L, -1);
lua_pop(L, 4);
return result;
}
// Rectangle type
static Rectangle LuaGetArgument_Rectangle(lua_State *L, int index)
{
Rectangle result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "x") == LUA_TNUMBER, index, "Expected Rectangle.x");
result.x = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "y") == LUA_TNUMBER, index, "Expected Rectangle.y");
result.y = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "width") == LUA_TNUMBER, index, "Expected Rectangle.width");
result.width = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "height") == LUA_TNUMBER, index, "Expected Rectangle.height");
result.height = LuaGetArgument_float(L, -1);
lua_pop(L, 4);
return result;
}
// Image type -> Opaque
// Texture2D type -> Opaque
// RenderTexture2D type -> Opaque
// Font character info type
static CharInfo LuaGetArgument_CharInfo(lua_State *L, int index)
{
CharInfo result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "value") == LUA_TNUMBER, index, "Expected CharInfo.value");
result.value = LuaGetArgument_int(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "rec") == LUA_TNUMBER, index, "Expected CharInfo.rec");
result.rec = LuaGetArgument_Rectangle(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "offsetX") == LUA_TNUMBER, index, "Expected CharInfo.offsetX");
result.offsetX = LuaGetArgument_int(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "offsetY") == LUA_TNUMBER, index, "Expected CharInfo.offsetY");
result.offsetY = LuaGetArgument_int(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "advanceX") == LUA_TNUMBER, index, "Expected CharInfo.advanceX");
result.advanceX = LuaGetArgument_int(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "char *data") == LUA_TNUMBER, index, "Expected CharInfo.char *data");
result.char *data = LuaGetArgument_unsigned(L, -1);
lua_pop(L, 6);
return result;
}
// Font type -> Opaque
// Camera type, defines a camera position/orientation in 3d space
static Camera LuaGetArgument_Camera(lua_State *L, int index)
{
Camera result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "position") == LUA_TNUMBER, index, "Expected Camera.position");
result.position = LuaGetArgument_Vector3(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "target") == LUA_TNUMBER, index, "Expected Camera.target");
result.target = LuaGetArgument_Vector3(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "up") == LUA_TNUMBER, index, "Expected Camera.up");
result.up = LuaGetArgument_Vector3(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "fovy") == LUA_TNUMBER, index, "Expected Camera.fovy");
result.fovy = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "type") == LUA_TNUMBER, index, "Expected Camera.type");
result.type = LuaGetArgument_int(L, -1);
lua_pop(L, 5);
return result;
}
// Camera2D type, defines a 2d camera
static Camera2D LuaGetArgument_Camera2D(lua_State *L, int index)
{
Camera2D result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "offset") == LUA_TNUMBER, index, "Expected Camera2D.offset");
result.offset = LuaGetArgument_Vector2(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "target") == LUA_TNUMBER, index, "Expected Camera2D.target");
result.target = LuaGetArgument_Vector2(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "rotation") == LUA_TNUMBER, index, "Expected Camera2D.rotation");
result.rotation = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "zoom") == LUA_TNUMBER, index, "Expected Camera2D.zoom");
result.zoom = LuaGetArgument_float(L, -1);
lua_pop(L, 4);
return result;
}
// Bounding box type
static BoundingBox LuaGetArgument_BoundingBox(lua_State *L, int index)
{
BoundingBox result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "min") == LUA_TNUMBER, index, "Expected BoundingBox.min");
result.min = LuaGetArgument_Vector3(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "max") == LUA_TNUMBER, index, "Expected BoundingBox.max");
result.max = LuaGetArgument_Vector3(L, -1);
lua_pop(L, 2);
return result;
}
// Mesh type -> Opaque
// Shader type -> Opaque
// Material texture map
static MaterialMap LuaGetArgument_MaterialMap(lua_State *L, int index)
{
MaterialMap result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "texture") == LUA_TNUMBER, index, "Expected MaterialMap.texture");
result.texture = LuaGetArgument_Texture2D(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "color") == LUA_TNUMBER, index, "Expected MaterialMap.color");
result.color = LuaGetArgument_Color(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "value") == LUA_TNUMBER, index, "Expected MaterialMap.value");
result.value = LuaGetArgument_float(L, -1);
lua_pop(L, 3);
return result;
}
// Material type -> REVIEW
/*
static Material LuaGetArgument_Material(lua_State *L, int index)
{
Material result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "shader") == LUA_TNUMBER, index, "Expected Material.shader");
result.shader = LuaGetArgument_Shader(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "maps[MAX_MATERIAL_MAPS]") == LUA_TNUMBER, index, "Expected Material.maps[MAX_MATERIAL_MAPS]");
result.maps[MAX_MATERIAL_MAPS] = LuaGetArgument_MaterialMap(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "*params") == LUA_TNUMBER, index, "Expected Material.*params");
result.*params = LuaGetArgument_float(L, -1);
lua_pop(L, 3);
return result;
}
static Material LuaGetArgument_Material(lua_State* L, int index)
{
Material result;
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "shader") == LUA_TUSERDATA, index, "Expected Material");
result.shader = LuaGetArgument_Shader(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "texDiffuse") == LUA_TUSERDATA, index, "Expected Material");
result.texDiffuse = LuaGetArgument_Texture2D(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "texNormal") == LUA_TUSERDATA, index, "Expected Material");
result.texNormal = LuaGetArgument_Texture2D(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "texSpecular") == LUA_TUSERDATA, index, "Expected Material");
result.texSpecular = LuaGetArgument_Texture2D(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "colDiffuse") == LUA_TTABLE, index, "Expected Material");
result.colDiffuse = LuaGetArgument_Color(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "colAmbient") == LUA_TTABLE, index, "Expected Material");
result.colAmbient = LuaGetArgument_Color(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "colSpecular") == LUA_TTABLE, index, "Expected Material");
result.colSpecular = LuaGetArgument_Color(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "glossiness") == LUA_TNUMBER, index, "Expected Material");
result.glossiness = LuaGetArgument_float(L, -1);
lua_pop(L, 8);
return result;
}
*/
// Model type
static Model LuaGetArgument_Model(lua_State *L, int index)
{
Model result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "mesh") == LUA_TNUMBER, index, "Expected Model.mesh");
result.mesh = LuaGetArgument_Mesh(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "transform") == LUA_TNUMBER, index, "Expected Model.transform");
result.transform = LuaGetArgument_Matrix(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "material") == LUA_TNUMBER, index, "Expected Model.material");
result.material = LuaGetArgument_Material(L, -1);
lua_pop(L, 3);
return result;
}
// Ray type (useful for raycast)
static Ray LuaGetArgument_Ray(lua_State *L, int index)
{
Ray result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "position") == LUA_TNUMBER, index, "Expected Ray.position");
result.position = LuaGetArgument_Vector3(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "direction") == LUA_TNUMBER, index, "Expected Ray.direction");
result.direction = LuaGetArgument_Vector3(L, -1);
lua_pop(L, 2);
return result;
}
// Raycast hit information
static RayHitInfo LuaGetArgument_RayHitInfo(lua_State *L, int index)
{
RayHitInfo result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "hit") == LUA_TNUMBER, index, "Expected RayHitInfo.hit");
result.hit = LuaGetArgument_bool(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "distance") == LUA_TNUMBER, index, "Expected RayHitInfo.distance");
result.distance = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "position") == LUA_TNUMBER, index, "Expected RayHitInfo.position");
result.position = LuaGetArgument_Vector3(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "normal") == LUA_TNUMBER, index, "Expected RayHitInfo.normal");
result.normal = LuaGetArgument_Vector3(L, -1);
lua_pop(L, 4);
return result;
}
// Wave type -> Opaque
// Sound type -> Opaque
// MusicData type -> Opaque
// Head-Mounted-Display device parameters
static VrDeviceInfo LuaGetArgument_VrDeviceInfo(lua_State *L, int index)
{
VrDeviceInfo result = { 0 };
index = lua_absindex(L, index); // Makes sure we use absolute indices because we push multiple values
luaL_argcheck(L, lua_getfield(L, index, "hResolution") == LUA_TNUMBER, index, "Expected VrDeviceInfo.hResolution");
result.hResolution = LuaGetArgument_int(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "vResolution") == LUA_TNUMBER, index, "Expected VrDeviceInfo.vResolution");
result.vResolution = LuaGetArgument_int(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "hScreenSize") == LUA_TNUMBER, index, "Expected VrDeviceInfo.hScreenSize");
result.hScreenSize = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "vScreenSize") == LUA_TNUMBER, index, "Expected VrDeviceInfo.vScreenSize");
result.vScreenSize = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "vScreenCenter") == LUA_TNUMBER, index, "Expected VrDeviceInfo.vScreenCenter");
result.vScreenCenter = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "eyeToScreenDistance") == LUA_TNUMBER, index, "Expected VrDeviceInfo.eyeToScreenDistance");
result.eyeToScreenDistance = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "lensSeparationDistance") == LUA_TNUMBER, index, "Expected VrDeviceInfo.lensSeparationDistance");
result.lensSeparationDistance = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "interpupillaryDistance") == LUA_TNUMBER, index, "Expected VrDeviceInfo.interpupillaryDistance");
result.interpupillaryDistance = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "lensDistortionValues[4]") == LUA_TNUMBER, index, "Expected VrDeviceInfo.lensDistortionValues[4]");
result.lensDistortionValues[4] = LuaGetArgument_float(L, -1);
luaL_argcheck(L, lua_getfield(L, index, "chromaAbCorrection[4]") == LUA_TNUMBER, index, "Expected VrDeviceInfo.chromaAbCorrection[4]");
result.chromaAbCorrection[4] = LuaGetArgument_float(L, -1);
lua_pop(L, 10);
return result;
}
//----------------------------------------------------------------------------------
// LuaPush functions
//----------------------------------------------------------------------------------
static void LuaPush_Color(lua_State* L, Color color)
{
lua_createtable(L, 0, 4);
LuaPush_int(L, color.r);
lua_setfield(L, -2, "r");
LuaPush_int(L, color.g);
lua_setfield(L, -2, "g");
LuaPush_int(L, color.b);
lua_setfield(L, -2, "b");
LuaPush_int(L, color.a);
lua_setfield(L, -2, "a");
}
static void LuaPush_Vector2(lua_State* L, Vector2 vec)
{
lua_createtable(L, 0, 2);
LuaPush_float(L, vec.x);
lua_setfield(L, -2, "x");
LuaPush_float(L, vec.y);
lua_setfield(L, -2, "y");
}
static void LuaPush_Vector3(lua_State* L, Vector3 vec)
{
lua_createtable(L, 0, 3);
LuaPush_float(L, vec.x);
lua_setfield(L, -2, "x");
LuaPush_float(L, vec.y);
lua_setfield(L, -2, "y");
LuaPush_float(L, vec.z);
lua_setfield(L, -2, "z");
}
static void LuaPush_Vector4(lua_State* L, Vector4 vec)
{
lua_createtable(L, 0, 4);
LuaPush_float(L, vec.x);
lua_setfield(L, -2, "x");
LuaPush_float(L, vec.y);
lua_setfield(L, -2, "y");
LuaPush_float(L, vec.z);
lua_setfield(L, -2, "z");
LuaPush_float(L, vec.w);
lua_setfield(L, -2, "w");
}
static void LuaPush_Quaternion(lua_State* L, Quaternion vec)
{
lua_createtable(L, 0, 4);
LuaPush_float(L, vec.x);
lua_setfield(L, -2, "x");
LuaPush_float(L, vec.y);
lua_setfield(L, -2, "y");
LuaPush_float(L, vec.z);
lua_setfield(L, -2, "z");
LuaPush_float(L, vec.w);
lua_setfield(L, -2, "w");
}
static void LuaPush_Matrix(lua_State* L, Matrix *matrix)
{
int i;
lua_createtable(L, 16, 0);
float* num = (&matrix->m0);
for (i = 0; i < 16; i++)
{
LuaPush_float(L, num[i]);
lua_rawseti(L, -2, i + 1);
}
}
static void LuaPush_Rectangle(lua_State* L, Rectangle rect)
{
lua_createtable(L, 0, 4);
LuaPush_int(L, rect.x);
lua_setfield(L, -2, "x");
LuaPush_int(L, rect.y);
lua_setfield(L, -2, "y");
LuaPush_int(L, rect.width);
lua_setfield(L, -2, "width");
LuaPush_int(L, rect.height);
lua_setfield(L, -2, "height");
}
static void LuaPush_Ray(lua_State* L, Ray ray)
{
lua_createtable(L, 0, 2);
LuaPush_Vector3(L, ray.position);
lua_setfield(L, -2, "position");
LuaPush_Vector3(L, ray.direction);
lua_setfield(L, -2, "direction");
}
static void LuaPush_RayHitInfo(lua_State* L, RayHitInfo hit)
{
lua_createtable(L, 0, 4);
LuaPush_int(L, hit.hit);
lua_setfield(L, -2, "hit");
LuaPush_float(L, hit.distance);
lua_setfield(L, -2, "distance");
LuaPush_Vector3(L, hit.position);
lua_setfield(L, -2, "position");
LuaPush_Vector3(L, hit.normal);
lua_setfield(L, -2, "normal");
}
static void LuaPush_BoundingBox(lua_State* L, BoundingBox bb)
{
lua_createtable(L, 0, 2);
LuaPush_Vector3(L, bb.min);
lua_setfield(L, -2, "min");
LuaPush_Vector3(L, bb.max);
lua_setfield(L, -2, "max");
}
static void LuaPush_Camera(lua_State* L, Camera cam)
{
lua_createtable(L, 0, 4);
LuaPush_Vector3(L, cam.position);
lua_setfield(L, -2, "position");
LuaPush_Vector3(L, cam.target);
lua_setfield(L, -2, "target");
LuaPush_Vector3(L, cam.up);
lua_setfield(L, -2, "up");
LuaPush_float(L, cam.fovy);
lua_setfield(L, -2, "fovy");
}
static void LuaPush_Camera2D(lua_State* L, Camera2D cam)
{
lua_createtable(L, 0, 4);
LuaPush_Vector2(L, cam.offset);
lua_setfield(L, -2, "offset");
LuaPush_Vector2(L, cam.target);
lua_setfield(L, -2, "target");
LuaPush_float(L, cam.rotation);
lua_setfield(L, -2, "rotation");
LuaPush_float(L, cam.zoom);
lua_setfield(L, -2, "zoom");
}
// REVIEW!!!
/*
static void LuaPush_Material(lua_State* L, Material mat)
{
lua_createtable(L, 0, 8);
LuaPush_Shader(L, mat.shader);
lua_setfield(L, -2, "shader");
LuaPush_Texture2D(L, mat.texDiffuse);
lua_setfield(L, -2, "texDiffuse");
LuaPush_Texture2D(L, mat.texNormal);
lua_setfield(L, -2, "texNormal");
LuaPush_Texture2D(L, mat.texSpecular);
lua_setfield(L, -2, "texSpecular");
LuaPush_Color(L, mat.colDiffuse);
lua_setfield(L, -2, "colDiffuse");
LuaPush_Color(L, mat.colAmbient);
lua_setfield(L, -2, "colAmbient");
LuaPush_Color(L, mat.colSpecular);
lua_setfield(L, -2, "colSpecular");
LuaPush_float(L, mat.glossiness);
lua_setfield(L, -2, "glossiness");
}
*/
static void LuaPush_Model(lua_State* L, Model mdl)
{
lua_createtable(L, 0, 3);
LuaPush_Mesh(L, mdl.mesh);
lua_setfield(L, -2, "mesh");
LuaPush_Matrix(L, &mdl.transform);
lua_setfield(L, -2, "transform");
LuaPush_Material(L, mdl.material);
lua_setfield(L, -2, "material");
}
//----------------------------------------------------------------------------------
// raylib Lua Structure constructors
//----------------------------------------------------------------------------------
static int lua_Color(lua_State* L)
{
LuaPush_Color(L, (Color) { (unsigned char)luaL_checkinteger(L, 1), (unsigned char)luaL_checkinteger(L, 2), (unsigned char)luaL_checkinteger(L, 3), (unsigned char)luaL_checkinteger(L, 4) });
return 1;
}
static int lua_Vector2(lua_State* L)
{
LuaPush_Vector2(L, (Vector2) { (float)luaL_checknumber(L, 1), (float)luaL_checknumber(L, 2) });
return 1;
}
static int lua_Vector3(lua_State* L)
{
LuaPush_Vector3(L, (Vector3) { (float)luaL_checknumber(L, 1), (float)luaL_checknumber(L, 2), (float)luaL_checknumber(L, 3) });
return 1;
}
static int lua_Vector4(lua_State* L)
{
LuaPush_Vector4(L, (Vector4) { (float)luaL_checknumber(L, 1), (float)luaL_checknumber(L, 2), (float)luaL_checknumber(L, 3), (float)luaL_checknumber(L, 4) });
return 1;
}
static int lua_Quaternion(lua_State* L)
{
LuaPush_Quaternion(L, (Quaternion) { (float)luaL_checknumber(L, 1), (float)luaL_checknumber(L, 2), (float)luaL_checknumber(L, 3), (float)luaL_checknumber(L, 4) });
return 1;
}
static int lua_Rectangle(lua_State* L)
{
LuaPush_Rectangle(L, (Rectangle) { (float)luaL_checknumber(L, 1), (float)luaL_checknumber(L, 2), (float)luaL_checknumber(L, 3), (float)luaL_checknumber(L, 4) });
return 1;
}
static int lua_Ray(lua_State* L)
{
Vector3 pos = LuaGetArgument_Vector3(L, 1);
Vector3 dir = LuaGetArgument_Vector3(L, 2);
LuaPush_Ray(L, (Ray) { { pos.x, pos.y, pos.z }, { dir.x, dir.y, dir.z } });
return 1;
}
static int lua_RayHitInfo(lua_State* L)
{
int hit = LuaGetArgument_int(L, 1);
float dis = LuaGetArgument_float(L, 2);
Vector3 pos = LuaGetArgument_Vector3(L, 3);
Vector3 norm = LuaGetArgument_Vector3(L, 4);
LuaPush_RayHitInfo(L, (RayHitInfo) { hit, dis, { pos.x, pos.y, pos.z }, { norm.x, norm.y, norm.z } });
return 1;
}
static int lua_BoundingBox(lua_State* L)
{
Vector3 min = LuaGetArgument_Vector3(L, 1);
Vector3 max = LuaGetArgument_Vector3(L, 2);
LuaPush_BoundingBox(L, (BoundingBox) { { min.x, min.y, min.z }, { max.x, max.y, max.z } });
return 1;
}
static int lua_Camera(lua_State* L)
{
Vector3 pos = LuaGetArgument_Vector3(L, 1);
Vector3 tar = LuaGetArgument_Vector3(L, 2);
Vector3 up = LuaGetArgument_Vector3(L, 3);
float fovy = LuaGetArgument_float(L, 4);
LuaPush_Camera(L, (Camera) { { pos.x, pos.y, pos.z }, { tar.x, tar.y, tar.z }, { up.x, up.y, up.z }, fovy });
return 1;
}
static int lua_Camera2D(lua_State* L)
{
Vector2 off = LuaGetArgument_Vector2(L, 1);
Vector2 tar = LuaGetArgument_Vector2(L, 2);
float rot = LuaGetArgument_float(L, 3);
float zoom = LuaGetArgument_float(L, 4);
LuaPush_Camera2D(L, (Camera2D) { { off.x, off.y }, { tar.x, tar.y }, rot, zoom });
return 1;
}
/*************************************************************************************
*
* raylib Lua Functions Bindings
*
**************************************************************************************/
//------------------------------------------------------------------------------------
// raylib [core] module functions - Window and Graphics Device
//------------------------------------------------------------------------------------
// Initialize window and OpenGL context
int lua_InitWindow(lua_State *L)
{
int width = LuaGetArgument_int(L, 1);
int height = LuaGetArgument_int(L, 2);
const char *title = LuaGetArgument_string(L, 3);
InitWindow(width, height, title);
return 0;
}
// Close window and unload OpenGL context
int lua_CloseWindow(lua_State *L)
{
CloseWindow();
return 0;
}
// Check if window has been initialized successfully
int lua_IsWindowReady(lua_State *L)
{
bool result = IsWindowReady();
LuaPush_bool(L, result);
return 1;
}
// Check if KEY_ESCAPE pressed or Close icon pressed
int lua_WindowShouldClose(lua_State *L)
{
bool result = WindowShouldClose();
LuaPush_bool(L, result);
return 1;
}
// Check if window has been minimized (or lost focus)
int lua_IsWindowMinimized(lua_State *L)
{
bool result = IsWindowMinimized();
LuaPush_bool(L, result);
return 1;
}
// Toggle fullscreen mode (only PLATFORM_DESKTOP)
int lua_ToggleFullscreen(lua_State *L)
{
ToggleFullscreen();
return 0;
}
// Set icon for window (only PLATFORM_DESKTOP)
int lua_SetWindowIcon(lua_State *L)
{
Image image = LuaGetArgument_Image(L, 1);
SetWindowIcon(image);
return 0;
}
// Set title for window (only PLATFORM_DESKTOP)
int lua_SetWindowTitle(lua_State *L)
{
const char *title = LuaGetArgument_string(L, 1);
SetWindowTitle(title);
return 0;
}
// Set window position on screen (only PLATFORM_DESKTOP)
int lua_SetWindowPosition(lua_State *L)
{
int x = LuaGetArgument_int(L, 1);
int y = LuaGetArgument_int(L, 2);
SetWindowPosition(x, y);
return 0;
}
// Set monitor for the current window (fullscreen mode)