-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaBind.cpp
More file actions
1755 lines (1635 loc) · 48 KB
/
LuaBind.cpp
File metadata and controls
1755 lines (1635 loc) · 48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <string.h>
#include <apollo/Apollo.h>
#include <apollo/Scripting/Scripting.h>
#include <apollo/Utilities/ResourceManager.h>
#include <apollo/Sound/Sound.h>
#include <apollo/Graphics/Graphics.h>
#include <apollo/Graphics/TextRenderer.h>
#include <apollo/Preferences.h>
#include <apollo/Input.h>
#include <apollo/Modes/ModeManager.h>
#include "TinyXML/tinyxml.h"
#include <apollo/Net/Net.h>
#include <apollo/Utilities/GameTime.h>
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
namespace
{
vec2 luaL_checkvec2(lua_State *L, int narg);
void lua_pushvec2(lua_State *L, vec2 val);
int VEC_new (lua_State* L)
{
float x = luaL_checknumber(L, 1);
float y = luaL_checknumber(L, 2);
vec2 v = vec2(x,y);
lua_pushvec2(L, v);
return 1;
}
int VEC_add (lua_State* L)
{
vec2 left = luaL_checkvec2(L, 1);
vec2 right = luaL_checkvec2(L, 2);
vec2 result = left + right;
lua_pushvec2(L, result);
return 1;
}
int VEC_sub (lua_State* L)
{
vec2 left = luaL_checkvec2(L, 1);
vec2 right = luaL_checkvec2(L, 2);
vec2 result = left - right;
lua_pushvec2(L, result);
return 1;
}
int VEC_mul (lua_State* L)
{
vec2 left = luaL_checkvec2(L, 1);
if (lua_istable(L, 2))
{
vec2 right = luaL_checkvec2(L, 2);
float result = left * right;
lua_pushnumber(L, result);
}
else
{
float right = luaL_checknumber(L, 2);
vec2 result = left * right;
lua_pushvec2(L, result);
}
return 1;
}
int VEC_div (lua_State* L)
{
vec2 left = luaL_checkvec2(L, 1);
float right = luaL_checknumber(L, 2);
vec2 result = left / right;
lua_pushvec2(L, result);
return 1;
}
int VEC_unm (lua_State* L)
{
vec2 val = -luaL_checkvec2(L, 1);
lua_pushvec2(L, val);
return 1;
}
int VEC_tostring (lua_State* L)
{
vec2 v = luaL_checkvec2(L, 1);
char s[256];
sprintf(s, "%f, %f", v.X(), v.Y());
lua_pushstring(L, s);
return 1;
}
luaL_Reg registryObjectVector[] =
{
"__add", VEC_add,
"__sub", VEC_sub,
"__mul", VEC_mul,
"__div", VEC_div,
"__unm", VEC_unm,
"__tostring", VEC_tostring,
NULL, NULL
};
vec2 luaL_checkvec2(lua_State* L, int narg)
{
if (!lua_istable(L, narg))
{
luaL_argerror(L, narg, "must pass a vector table (not a table)");
}
float x, y;
lua_getfield(L, narg, "x");
if (!lua_isnumber(L, -1))
{
luaL_argerror(L, narg, "must pass a vector table (bad x value)");
}
x = lua_tonumber(L, -1);
lua_getfield(L, narg, "y");
if (!lua_isnumber(L, -1))
{
luaL_argerror(L, narg, "must pass a vector table (bad y value)");
}
y = lua_tonumber(L, -1);
lua_pop(L, 2);
return vec2(x, y);
}
std::string FloatToString ( float val )
{
std::ostringstream o;
if (!(o << val))
{
printf("BAD CONVERSION?!?!?");
}
return o.str();
}
unsigned ToInt ( const std::string& value )
{
return atoi(value.c_str());
}
bool ToBool ( const std::string& value )
{
return value == "true";
}
vec2 luaL_optvec2(lua_State* L, int narg, vec2 defaultValue)
{
if (lua_isnoneornil(L, narg))
return defaultValue;
return luaL_checkvec2(L, narg);
}
void lua_pushvec2(lua_State* L, vec2 val)
{
lua_createtable(L, 0, 2);
lua_pushnumber(L, val.X());
lua_setfield(L, -2, "x");
lua_pushnumber(L, val.Y());
lua_setfield(L, -2, "y");
luaL_getmetatable(L, "Apollo.vec2");
lua_setmetatable(L, -2);
}
int Pref_Get ( lua_State* L )
{
const char* arg = luaL_checkstring(L, 1);
std::string push = Preferences::Get(arg, "nil");
// [TODO, ADAM] Make this not hardcoded... I think we have to recognize what type "push" is and format it accordingly
// lua_pushstring(L, push);
if (push == "true") // [HARDCODED]
{
lua_pushboolean(L, true);
} else
{
lua_pushboolean(L, false);
}
return 1;
}
int Pref_Set ( lua_State* L )
{
const char* arg = luaL_checkstring(L, 1);
const char* set = luaL_checkstring(L, 2);
Preferences::Set(arg, set);
Preferences::Save();
return 0;
}
/**
* @page lua_preferences The Lua Preferences Registry
* This page contains information about the Lua preferences registry.
*
* This registry currently only contains one function, used for retrieving
* preferences. In Lua, it is called called like so: "preferences.get(name)".
*
* @section pref_get get
* Finds and returns a particular preference.\n
* Parameters:\n
* name - The name of the preference to be fetched.\n
* Returns:\n
* boolean - The status of the requested preference. (currently, this is hardcoded)
*
* @todo Make @ref xml_get un-hardcoded.
*/
luaL_Reg registryPreferences[] =
{
"get", Pref_Get,
"set", Pref_Set,
NULL, NULL
};
int NetServer_Startup ( lua_State* L )
{
unsigned port = luaL_checkinteger(L, 1);
luaL_argcheck(L, port < 65536 && port > 0, 1, "Invalid port number");
const char* password = "";
if (lua_gettop(L) > 1)
{
password = luaL_checkstring(L, 2);
}
Net::Server::Startup(port, password);
return 0;
}
int NetServer_Shutdown ( lua_State* L )
{
Net::Server::Shutdown();
return 0;
}
int NetServer_Running ( lua_State* L )
{
lua_pushboolean(L, Net::Server::IsRunning() ? 1 : 0);
return 1;
}
int NetServer_ClientCount ( lua_State* L )
{
lua_pushinteger(L, Net::Server::ClientCount());
return 1;
}
int NetServer_KillClient ( lua_State* L )
{
unsigned clientID = luaL_checkinteger(L, 1);
Net::Server::KillClient(clientID);
return 0;
}
int NetServer_Connected ( lua_State* L )
{
unsigned clientID = luaL_checkinteger(L, 1);
bool isConnected = Net::Server::IsConnected(clientID);
lua_pushboolean(L, isConnected ? 1 : 0);
return 1;
}
int NetServer_SendMessage ( lua_State* L )
{
int nargs = lua_gettop(L);
unsigned clientID = luaL_checkinteger(L, 1);
const char* message = luaL_checkstring(L, 2);
size_t len = 0;
const void* data = NULL;
if (nargs > 2)
{
data = luaL_checklstring(L, 3, &len);
}
Net::Message messageObject ( message, data, len );
Net::Server::SendMessage ( clientID, messageObject );
return 0;
}
int NetServer_BroadcastMessage ( lua_State* L )
{
int nargs = lua_gettop(L);
const char* message = luaL_checkstring(L, 1);
size_t len = 0;
const void* data = NULL;
if (nargs > 1)
{
data = luaL_checklstring(L, 2, &len);
}
Net::Message messageObject ( message, data, len );
Net::Server::BroadcastMessage ( messageObject );
return 0;
}
int NetServer_GetMessage ( lua_State* L )
{
Net::Message* msg = Net::Server::GetMessage();
if (msg)
{
lua_pushlstring(L, msg->message.data(), msg->message.length());
if (msg->data)
{
lua_pushlstring(L, (const char*)msg->data, msg->dataLength);
}
else
{
lua_pushnil(L);
}
lua_pushinteger(L, msg->clientID);
delete msg;
}
else
{
lua_pushnil(L);
lua_pushnil(L);
lua_pushnil(L);
}
return 3;
}
/**
* @page lua_net_server The Lua Net Server Registry
* This page contains information about the Lua net server registry.
*
* This registry contains functions related to running a multiplayer server. In
* Lua, they are all called like so: "net_server.function_name()" (for example:
* "startup" becomes "net_server.startup(port, password)").
*
* Note: Somebody else will need to complete this registry, I don't know
* anything about it right now.
*
* @section startup
*
* @section shutdown
*
* @section running
*
* @section client_count
*
* @section kill
*
* @section net_server_connected connected
*
* @section net_server_send send
*
* @section broadcast
*
* @section net_server_get get
*
* @todo Complete the @ref lua_net_server registry.
*
*/
luaL_Reg registryNetServer[] =
{
"startup", NetServer_Startup,
"shutdown", NetServer_Shutdown,
"running", NetServer_Running,
"client_count", NetServer_ClientCount,
"kill", NetServer_KillClient,
"connected", NetServer_Connected,
"send", NetServer_SendMessage,
"broadcast", NetServer_BroadcastMessage,
"get", NetServer_GetMessage,
NULL, NULL
};
// XML code based on code from lua-users.org
void XML_ParseNode (lua_State* L, TiXmlNode* xmlNode)
{
if (!xmlNode) return;
// resize stack if neccessary
luaL_checkstack(L, 5, "XML_ParseNode : recursion too deep");
TiXmlElement* xmlElement = xmlNode->ToElement();
if (xmlElement)
{
// element name
lua_pushstring(L, "name");
lua_pushstring(L, xmlElement->Value());
lua_settable(L, -3);
// parse attributes
TiXmlAttribute* xmlAttribute = xmlElement->FirstAttribute();
if (xmlAttribute)
{
lua_pushstring(L, "attr");
lua_newtable(L);
for (; xmlAttribute; xmlAttribute = xmlAttribute->Next())
{
lua_pushstring(L, xmlAttribute->Name());
lua_pushstring(L, xmlAttribute->Value());
lua_settable(L, -3);
}
lua_settable(L, -3);
}
}
// children
TiXmlNode *child = xmlNode->FirstChild();
if (child)
{
int childCount = 0;
for(; child; child = child->NextSibling())
{
switch (child->Type())
{
case TiXmlNode::DOCUMENT:
break;
case TiXmlNode::ELEMENT:
// normal element, parse recursive
lua_newtable(L);
XML_ParseNode(L, child);
lua_rawseti(L, -2, ++childCount);
break;
case TiXmlNode::COMMENT: break;
case TiXmlNode::TEXT:
// plaintext, push raw
lua_pushstring(L, child->Value());
lua_rawseti(L, -2, ++childCount);
break;
case TiXmlNode::DECLARATION: break;
case TiXmlNode::UNKNOWN: break;
};
}
lua_pushstring(L, "n");
lua_pushnumber(L, childCount);
lua_settable(L, -3);
}
}
static int XML_ParseFile (lua_State *L)
{
const char* fileName = luaL_checkstring(L, 1);
SDL_RWops* ops = ResourceManager::OpenFile(fileName);
size_t len;
void* dataPointer = ResourceManager::ReadFull(&len, ops, 1);
char* fullDataPointer = (char*)malloc(len + 1);
fullDataPointer[len] = 0;
memcpy(fullDataPointer, dataPointer, len);
free(dataPointer);
TiXmlDocument doc ( fileName );
doc.Parse(fullDataPointer);
lua_newtable(L);
XML_ParseNode(L, &doc);
free((void*)fullDataPointer);
return 1;
}
/**
* @page lua_xml The Lua XML Registry
* This page contains information about the Lua XML registry.
*
* This registry currently only contains one function. In Lua, it is called
* called like so: "xml.load(file)".
*
* @section load
* Loads and parses an XML file\n
* Parameters:\n
* name - The name of the mode that the game is currently in.\n
* Returns:\n
* A table with the contents of the file in it.\n
*/
luaL_Reg registryXML[] =
{
"load", XML_ParseFile,
NULL, NULL
};
int WIND_IsFullscreen ( lua_State* L )
{
lua_pushboolean(L, ToBool(Preferences::Get("Screen/Fullscreen")));
return 1;
}
int WIND_SetFullscreen ( lua_State* L )
{
Preferences::Set("Screen/Fullscreen", luaL_checkstring(L, 1) );
Graphics::Init(ToInt(Preferences::Get("Screen/Width")), ToInt(Preferences::Get("Screen/Height")), ToBool(Preferences::Get("Screen/Fullscreen")));
return 0;
}
int WIND_ToggleFullscreen ( lua_State* L )
{
Preferences::Set("Screen/Fullscreen", Preferences::Get("Screen/Fullscreen") == "true" ? "false" : "true");
Graphics::Init(ToInt(Preferences::Get("Screen/Width")), ToInt(Preferences::Get("Screen/Height")), ToBool(Preferences::Get("Screen/Fullscreen")));
return 0;
}
int WIND_WindowSize ( lua_State* L )
{
lua_pushnumber(L, ToInt(Preferences::Get("Screen/Width")));
lua_pushnumber(L, ToInt(Preferences::Get("Screen/Height")));
return 2;
}
int WIND_SetWindow ( lua_State* L )
{
vec2 newSize = luaL_checkvec2(L, 1);
std::string width = FloatToString(newSize.X());
std::string height = FloatToString(newSize.Y());
Preferences::Set("Screen/Width", width);
Preferences::Set("Screen/Height", height);
Graphics::Init(ToInt(Preferences::Get("Screen/Width")), ToInt(Preferences::Get("Screen/Height")), ToBool(Preferences::Get("Screen/Fullscreen")));
return 0;
}
int WIND_MouseToggle ( lua_State* L )
{
if (SDL_ShowCursor(SDL_QUERY) == SDL_ENABLE)
SDL_ShowCursor(SDL_DISABLE);
else
SDL_ShowCursor(SDL_ENABLE);
return 0;
}
/**
* @page lua_window The Lua Window Registry
* This page contains information about the Lua Window registry.
*
* This registry contains miscellaneous functions for modifying the SDL window's
* properties.
*
* @section is_fullscreen
* Checks the status of the SDL window with regards to fullscreen or windowed
* mode.\n
* Returns:\n
* A boolean value of true or false - true for fullscreen, false for windowed.
*
* @section set_fullscreen
* Sets the fullscreen status of the SDL window based upon the argument given.\n
* Parameters:\n
* fullscreen - a string equivalent of the boolean value of whether or not the
* window should be set to fullscreen.\n
* Returns:\n
* Initially this function will return nothing, but there are plans to allow for
* it to return the SDL status given (in case there's a problem with setting it
* to fullscreen).
*
* @section toggle_fullscreen
* Changes the fullscreen status of the SDL window. No parameters or returns.\n
*
* @section size
* Gives the size of the screen in pixels.\n
* Returns:\n
* size - a vectorized size table containing the dimensions of the window in
* pixels. (currently returns two values, I hope to make it a table soon)
*
* @todo Make @ref size return a vectorized table instead of two values
*
* @section set
* Sets the size of the screen in pixels.\n
* Parameters:\n
* A table of a coordinate pair, representing the size of the screen (in pixels)
* .\n
*/
luaL_Reg registryWindowManager[] =
{
"is_fullscreen", WIND_IsFullscreen,
"set_fullscreen", WIND_SetFullscreen,
"toggle_fullscreen", WIND_ToggleFullscreen,
"size", WIND_WindowSize,
"set", WIND_SetWindow,
"mouse_toggle", WIND_MouseToggle,
NULL, NULL
};
int MM_Switch ( lua_State* L )
{
int nargs = lua_gettop(L);
const char* newmode = luaL_checkstring(L, 1);
if (nargs > 1)
{
SwitchMode(std::string(newmode), luaL_ref(L, 2));
}
else
{
SwitchMode(std::string(newmode));
}
return 0;
}
int MM_Time ( lua_State* L )
{
lua_pushnumber(L, GameTime());
return 1;
}
int MM_Query ( lua_State* L )
{
lua_pushstring(L, QueryMode());
return 1;
}
int MM_Release ( lua_State* L )
{
#ifdef NDEBUG
lua_pushboolean(L, true);
return 1;
#else
lua_pushboolean(L, false);
return 1;
#endif
}
int MM_Quit ( lua_State* L )
{
QuitEngine();
// unreachable!
return 0;
}
/**
* @page lua_mode_manager The Lua Mode Manager Registry
* This page contains information about the Lua mode manager registry.
*
* This small registry contains functions for dealing with modes. Modes are the
* states in which Lua runs, containing functions triggered by certain states of
* Apollo (like mouse movement, keyboard presses, etc). In Lua, they are all
* called like so: "mode_manager.function_name()" (for example: "switch" becomes
* "mode_manager.switch(mode)").
*
* @section switch
* Switches the game mode. If the mode cannot be switched to the given name, an
* error occurs.\n
* Parameters:\n
* mode - the name of the mode you want to switch to (without the suffix
* ".lua"). For example, to switch to "MainMenu.lua", enter "MainMenu" for the
* parameter.
*
* @section time
* Gives the game's time, in seconds, since the game's start. This function has
* no parameters.\n
* Returns:\n
* number - The amount of seconds (accurate to miliseconds) since the game's
* start.
*
* @section query
* Returns the game's current mode. This function has no parameters.\n
* Returns:\n
* string - The name of the mode that the game is currently in.
*
* @section is_release
* Returns the game's current build mode. This function has no parameters.\n
* Returns:\n
* bool - True if the game's current build is a release build, false if not.
*/
luaL_Reg registryModeManager[] =
{
"switch", MM_Switch,
"time", MM_Time,
"query", MM_Query,
"is_release", MM_Release,
"quit", MM_Quit,
NULL, NULL
};
int RM_Load ( lua_State* L )
{
const char* file = luaL_checkstring(L, 1);
SDL_RWops* ptr = ResourceManager::OpenFile(file);
if (ptr)
{
size_t len;
void* data = ResourceManager::ReadFull(&len, ptr, 1);
lua_pushlstring(L, (const char*)data, len);
free(data);
return 1;
}
else
{
lua_pushliteral(L, "file not found");
return lua_error(L);
}
}
int RM_FileExists ( lua_State* L )
{
const char* file = luaL_checkstring(L, 1);
bool exists = ResourceManager::FileExists(file);
lua_pushboolean(L, exists ? 1 : 0);
return 1;
}
int RM_Write ( lua_State* L )
{
const char* file = luaL_checkstring(L, 1);
size_t len;
const char* data = luaL_checklstring(L, 2, &len);
ResourceManager::WriteFile(file, (const void*)data, len);
return 0;
}
/**
* @page lua_resource_manager The Lua Resource Manager Registry
* This page contains information about the Lua resource manager registry.
*
* This small registry contains a few simple tools for manipulating files. In
* Lua, they are all called like so: "resource_manager.function_name()" (for
* example: "file_exists" becomes "resource_manager.file_exists(file)").
*
* @section file_exists
* Ensures that the given file exists, then returns a boolean of whether it does
* or not.\n
* Parameters:\n
* file - The name of the file\n
* Returns:\n
* Boolean - true if file exists, false if it does not\n
*
* @section load
* Loads a given file. Will return error statement along with error if the file
* does not exist or cannot be opened.\n
* Parameters:\n
* file - The name of the file to be loaded\n
* Returns:\n
* data - If the load is successful, data will be returned from the file
* literal - If the load is unsuccessful, a string will be returned ("file not
* found")
*
* @section write
* Writes to a given file. No error checking implemented.
* Parameters:\n
* file - The name of the file\n
* data - The data to be written to the file\n
*/
luaL_Reg registryResourceManager[] =
{
"file_exists", RM_FileExists,
"load", RM_Load,
"write", RM_Write,
NULL, NULL
};
int GFX_BeginFrame ( lua_State* L )
{
Graphics::BeginFrame();
return 0;
}
int GFX_EndFrame ( lua_State* L )
{
Graphics::EndFrame();
return 0;
}
int GFX_SetCamera ( lua_State* L )
{
float ll_x, ll_y, tr_x, tr_y, rot = 0.0f;
int nargs = lua_gettop(L);
ll_x = luaL_checknumber(L, 1);
ll_y = luaL_checknumber(L, 2);
tr_x = luaL_checknumber(L, 3);
tr_y = luaL_checknumber(L, 4);
if (nargs > 4)
rot = luaL_checknumber(L, 5);
Graphics::SetCamera(vec2(ll_x, ll_y), vec2(tr_x, tr_y), rot);
return 0;
}
static colour LoadColour ( lua_State* L, int index )
{
float r = 1.0f, g = 1.0f, b = 1.0f, a = 1.0f;
lua_getfield(L, index, "r");
r = luaL_checknumber(L, -1);
lua_getfield(L, index, "g");
g = luaL_checknumber(L, -1);
lua_getfield(L, index, "b");
b = luaL_checknumber(L, -1);
lua_getfield(L, index, "a");
a = luaL_checknumber(L, -1);
return colour(r, g, b, a);
}
int GFX_DrawText ( lua_State* L )
{
int nargs = lua_gettop(L);
const char* text = luaL_checkstring(L, 1);
const char* font = luaL_checkstring(L, 2);
const char* justify = luaL_checkstring(L, 3);
vec2 location = luaL_checkvec2(L, 4);
float height = luaL_checknumber(L, 5);
float rotation = 0.0f;
if (nargs >= 7)
{
rotation = luaL_checknumber(L, 7);
}
if (nargs >= 6)
{
luaL_argcheck(L, lua_istable(L, 6), 6, "bad colour");
Graphics::DrawTextSDL(text, font, justify, location, height, LoadColour(L, 6), rotation);
}
else
{
Graphics::DrawTextSDL(text, font, justify, location, height, colour(1.0f, 1.0f, 1.0f, 1.0f), rotation);
}
return 0;
}
int GFX_TextLength (lua_State* L )
{
const char* text = luaL_checkstring(L, 1);
const char* font = luaL_checkstring(L, 2);
float height = luaL_checknumber(L, 3);
vec2 dims = Graphics::TextRenderer::TextDimensions(font, text, height);
// printf("[%f, %f]\n", dims.X(), dims.Y());
dims = dims * (height / dims.Y());
// printf("[%f, %f]\n", dims.X(), dims.Y());
lua_pushnumber(L, dims.X());
return 1;
}
int GFX_DrawLine ( lua_State* L )
{
int nargs = lua_gettop(L);
float width;
vec2 point1 = luaL_checkvec2(L, 1);
vec2 point2 = luaL_checkvec2(L, 2);
width = luaL_checknumber(L, 3);
if (nargs > 3)
{
Graphics::DrawLine(point1, point2, width, LoadColour(L, 4));
}
else
{
Graphics::DrawLine(point1, point2, width, colour(0.0f, 1.0f, 0.0f, 1.0f));
}
return 0;
}
int GFX_DrawLightning ( lua_State* L )
{
int nargs = lua_gettop(L);
float width, chaos;
bool tailed;
vec2 point1 = luaL_checkvec2(L, 1);
vec2 point2 = luaL_checkvec2(L, 2);
width = luaL_checknumber(L, 3);
chaos = luaL_checknumber(L, 4);
tailed = lua_tonumber(L, 5);
if (nargs > 5)
{
Graphics::DrawLightning(point1, point2, width, chaos, LoadColour(L, 6), tailed);
}
else
{
Graphics::DrawLightning(point1, point2, width, chaos, colour(0.93f, 0.88f, 1.0f, 1.0f), tailed);
}
return 0;
}
int GFX_DrawBox ( lua_State* L )
{
int nargs = lua_gettop(L);
float top, left, bottom, right, width;
top = luaL_checknumber(L, 1);
left = luaL_checknumber(L, 2);
bottom = luaL_checknumber(L, 3);
right = luaL_checknumber(L, 4);
width = luaL_checknumber(L, 5);
colour col = colour(0.0f, 1.0f, 0.0f, 1.0f);
if (nargs > 5)
{
col = LoadColour(L, 6);
}
Graphics::DrawBox(top, left, bottom, right, width, col);
return 0;
}
int GFX_DrawPoint ( lua_State* L )
{
int nargs = lua_gettop(L);
float size = 1;
vec2 location = luaL_checkvec2(L, 1);
colour col = colour(0.0f, 1.0f, 0.0f, 1.0f);
if (nargs > 1)
{
size = luaL_checknumber(L, 2);
}
if (nargs > 2)
{
col = LoadColour(L, 3);
}
Graphics::DrawPoint(location, size, col);
return 0;
}
int GFX_DrawRadarTriangle ( lua_State* L )
{
int nargs = lua_gettop(L);
vec2 coordinates = luaL_checkvec2(L, 1);
float varsize = luaL_checknumber(L, 2);
if (nargs > 2)
{
Graphics::DrawTriangle(vec2(coordinates.X(), coordinates.Y() + varsize),
vec2(coordinates.X() - varsize, coordinates.Y() - varsize),
vec2(coordinates.X() + varsize, coordinates.Y() - varsize),
LoadColour(L, 3));
}
else
{
Graphics::DrawTriangle(vec2(coordinates.X(), coordinates.Y() + varsize),
vec2(coordinates.X() - varsize, coordinates.Y() - varsize),
vec2(coordinates.X() + varsize, coordinates.Y() - varsize),
colour(0.0f, 1.0f, 0.0f, 1.0f));
}
return 0;
}
int GFX_DrawRadarPlus ( lua_State* L )
{
int nargs = lua_gettop(L);
vec2 coordinates = luaL_checkvec2(L, 1);
float varsize = luaL_checknumber(L, 2);
if (nargs > 2)
{
Graphics::DrawBox(coordinates.Y() + varsize, coordinates.X() - 0.3 * varsize, coordinates.Y() - varsize, coordinates.X() + 0.3 * varsize, 0, LoadColour(L, 3));
Graphics::DrawBox(coordinates.Y() + 0.3 * varsize, coordinates.X() - varsize, coordinates.Y() - 0.3 * varsize, coordinates.X() + varsize, 0, LoadColour(L, 3));
}
else
{
Graphics::DrawBox(coordinates.Y() + varsize, coordinates.X() - 0.3 * varsize, coordinates.Y() - varsize, coordinates.X() + 0.3 * varsize, 0, colour(0, 1, 0, 1));
Graphics::DrawBox(coordinates.Y() + 0.3 * varsize, coordinates.X() - varsize, coordinates.Y() - 0.3 * varsize, coordinates.X() + varsize, 0, colour(0, 1, 0, 1));
}
return 0;
}
int GFX_DrawRadarBox ( lua_State* L )
{
int nargs = lua_gettop(L);
vec2 coordinates = luaL_checkvec2(L, 1);
float varsize = luaL_checknumber(L, 2);
if (nargs > 2)
{
Graphics::DrawBox(coordinates.Y() + varsize, coordinates.X() - varsize, coordinates.Y() - varsize, coordinates.X() + varsize, 0, LoadColour(L, 3));
}
else
{
Graphics::DrawBox(coordinates.Y() + varsize, coordinates.X() - varsize, coordinates.Y() - varsize, coordinates.X() + varsize, 0, colour(0, 1, 0, 1));
}
return 0;
}
int GFX_DrawRadarDiamond ( lua_State* L )
{
int nargs = lua_gettop(L);
vec2 coordinates = luaL_checkvec2(L, 1);
float varsize = luaL_checknumber(L, 2);
if (nargs > 2)
{
Graphics::DrawDiamond(coordinates.Y() + varsize, coordinates.X() - varsize, coordinates.Y() - varsize, coordinates.X() + varsize, LoadColour(L, 3));
}
else
{
Graphics::DrawDiamond(coordinates.Y() + varsize, coordinates.X() - varsize, coordinates.Y() - varsize, coordinates.X() + varsize, colour(0, 1, 0, 1));
}
return 0;
}
int GFX_DrawObject3DAmbient ( lua_State* L )
{
std::string object = luaL_checkstring(L, 1);
vec2 location = luaL_checkvec2(L, 2);
float scale = luaL_checknumber(L, 3);
float angle = luaL_checknumber(L, 4);
float bank = luaL_optnumber(L, 5, 0.0);
Graphics::DrawObject3DAmbient(object, location, scale, angle, bank);
return 0;
}
int GFX_DrawParticles ( lua_State* L )
{
Graphics::DrawParticles();
return 0;
}
int GFX_ClearParticles ( lua_State* L )
{
Graphics::ClearParticles();
}
int GFX_AddParticles ( lua_State* L )
{
const char* name = luaL_checkstring(L, 1);
unsigned long pcount = luaL_checkinteger(L, 2);
vec2 location = luaL_checkvec2(L, 3);
vec2 velocity = luaL_checkvec2(L, 4);
vec2 velocityVar = luaL_checkvec2(L, 5);
vec2 acc = luaL_checkvec2(L, 6);
float size = luaL_checknumber(L, 7);
float lifetime = luaL_checknumber(L, 8);
Graphics::AddParticles(name, pcount, location, velocity, velocityVar, acc, size, lifetime);
return 0;