lua.d 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. // quantumde1 developed software, licensed under BSD-0-Clause license.
  2. module scripts.lua;
  3. import bindbc.lua;
  4. import raylib;
  5. import variables;
  6. import graphics.effects;
  7. import std.conv;
  8. import system.abstraction;
  9. import system.config;
  10. import std.string;
  11. import std.algorithm;
  12. import graphics.engine;
  13. import graphics.playback;
  14. import std.file;
  15. /*
  16. * This module provides Lua bindings for various engine functionalities.
  17. * Functions are built on top of engine built-in functions for execution from scripts.
  18. * Not all engine functions usable for scripting are yet implemented.
  19. */
  20. /* text window */
  21. extern (C) nothrow int luaL_dialogBox(lua_State* L)
  22. {
  23. showDialog = true;
  24. // parse table with text pages
  25. luaL_checktype(L, 1, LUA_TTABLE);
  26. int textTableLength = cast(int) lua_objlen(L, 1);
  27. messageGlobal = new string[](textTableLength);
  28. foreach (i; 0..textTableLength) {
  29. lua_rawgeti(L, 1, i + 1);
  30. messageGlobal[i] = luaL_checkstring(L, -1).to!string;
  31. lua_pop(L, 1);
  32. }
  33. //parse table with choices
  34. luaL_checktype(L, 2, LUA_TTABLE);
  35. int choicesLength = cast(int) lua_objlen(L, 2);
  36. choices = new string[choicesLength];
  37. foreach (i; 0..choicesLength)
  38. {
  39. lua_rawgeti(L, 2, i + 1);
  40. choices[i] = luaL_checkstring(L, -1).to!string;
  41. lua_pop(L, 1);
  42. }
  43. //if provided, get page on which choices must be shown
  44. if (lua_gettop(L) >= 3) {
  45. choicePage = cast(int)luaL_checkinteger(L, 3);
  46. }
  47. if (lua_gettop(L) >= 4 && !lua_istable(L, 4)) {
  48. typingSpeed = cast(float) luaL_checknumber(L, 4);
  49. }
  50. return 0;
  51. }
  52. extern (C) nothrow int luaL_getAnswerValue(lua_State* L)
  53. {
  54. lua_pushinteger(L, selectedChoice);
  55. return 1;
  56. }
  57. extern (C) nothrow int luaL_isDialogExecuted(lua_State *L) {
  58. lua_pushboolean(L, showDialog);
  59. return 1;
  60. }
  61. /* background drawing and loading */
  62. extern (C) nothrow int luaL_loadBackground(lua_State* L)
  63. {
  64. try
  65. {
  66. int count = cast(int)luaL_checkinteger(L, 2);
  67. if (count >= backgroundTextures.length) {
  68. backgroundTextures.length = count + 1;
  69. }
  70. if (backgroundTextures[count].texture.id != 0) {
  71. UnloadTexture(backgroundTextures[count].texture);
  72. }
  73. char* filename = cast(char*)luaL_checkstring(L, 1);
  74. backgroundTextures[count].texture = LoadTexture(filename);
  75. SetTextureFilter(backgroundTextures[count].texture, TextureFilter.TEXTURE_FILTER_BILINEAR);
  76. }
  77. catch (Exception e)
  78. {
  79. debugWriteln(e.msg);
  80. }
  81. return 0;
  82. }
  83. extern (C) nothrow int luaL_drawBackground(lua_State* L)
  84. {
  85. try
  86. {
  87. int count = cast(int)luaL_checkinteger(L, 4);
  88. debugWriteln(backgroundTextures[count]);
  89. backgroundTextures[count].height = backgroundTextures[count].texture.height;
  90. if (backgroundTextures.length < count) {
  91. backgroundTextures.length = backgroundTextures.length;
  92. }
  93. backgroundTextures[count].width = backgroundTextures[count].texture.width;
  94. backgroundTextures[count].x = luaL_checknumber(L, 1);
  95. backgroundTextures[count].y = luaL_checknumber(L, 2);
  96. backgroundTextures[count].scale = luaL_checknumber(L, 3) * scale;
  97. backgroundTextures[count].drawTexture = true;
  98. debugWriteln(backgroundTextures[count]);
  99. }
  100. catch (Exception e)
  101. {
  102. debugWriteln(e.msg);
  103. }
  104. return 0;
  105. }
  106. extern (C) nothrow int luaL_stopDrawBackground(lua_State* L)
  107. {
  108. try
  109. {
  110. int count = cast(int)luaL_checkinteger(L, 1);
  111. if (count >= backgroundTextures.length) {
  112. debugWriteln("stop draw not loaded background unavailable");
  113. } else {
  114. backgroundTextures[count].drawTexture = false;
  115. }
  116. }
  117. catch (Exception e)
  118. {
  119. debugWriteln(e.msg);
  120. }
  121. return 0;
  122. }
  123. extern (C) nothrow int luaL_unloadBackground(lua_State* L)
  124. {
  125. int count = cast(int)luaL_checkinteger(L, 1);
  126. if (count >= backgroundTextures.length) {
  127. debugWriteln("Unloading non-loaded background unavailable");
  128. } else {
  129. backgroundTextures[count].drawTexture = false;
  130. UnloadTexture(backgroundTextures[count].texture);
  131. }
  132. return 0;
  133. }
  134. /* character textures */
  135. extern (C) nothrow int luaL_loadCharacter(lua_State *L) {
  136. try
  137. {
  138. int count = cast(int) luaL_checkinteger(L, 2);
  139. if (count >= characterTextures.length) {
  140. characterTextures.length = count + 1;
  141. }
  142. if (characterTextures.length < characterTextures.length) {
  143. characterTextures.length = characterTextures.length;
  144. }
  145. if (count < characterTextures.length && characterTextures[count].texture.id != 0) {
  146. UnloadTexture(characterTextures[count].texture);
  147. }
  148. characterTextures[count].texture = LoadTexture(luaL_checkstring(L, 1));
  149. SetTextureFilter(characterTextures[count].texture, TextureFilter.TEXTURE_FILTER_BILINEAR);
  150. characterTextures[count].width = characterTextures[count].texture.width;
  151. characterTextures[count].height = characterTextures[count].texture.height;
  152. characterTextures[count].drawTexture = false;
  153. }
  154. catch (Exception e) {
  155. debugWriteln(e.msg);
  156. }
  157. return 0;
  158. }
  159. extern (C) nothrow int luaL_drawCharacter(lua_State* L)
  160. {
  161. try {
  162. //configuring needed parameters in characterTextures like coordinates, scale and drawTexture(its a boolean value which checks need this texture to be drawn or not)
  163. int count = to!int(luaL_checkinteger(L, 4));
  164. characterTextures[count].scale = luaL_checknumber(L, 3) * scale;
  165. characterTextures[count].y = cast(int) luaL_checknumber(L, 2);
  166. characterTextures[count].x = cast(int) luaL_checknumber(L, 1);
  167. characterTextures[count].drawTexture = true;
  168. characterTextures[count].justDrawn = true;
  169. if (lua_gettop(L) == 5) {
  170. lua_getfield(L, 5, "r");
  171. characterTextures[count].color.r = cast(ubyte)lua_tointeger(L, -1);
  172. lua_pop(L, 1);
  173. lua_getfield(L, 5, "g");
  174. characterTextures[count].color.g = cast(ubyte)lua_tointeger(L, -1);
  175. lua_pop(L, 1);
  176. lua_getfield(L, 5, "b");
  177. characterTextures[count].color.b = cast(ubyte)lua_tointeger(L, -1);
  178. lua_pop(L, 1);
  179. lua_getfield(L, 5, "a");
  180. //if empty, reset to default
  181. if (!lua_isnil(L, -1)) {
  182. characterTextures[count].color.a = cast(ubyte)lua_tointeger(L, -1);
  183. }
  184. lua_pop(L, 1);
  185. } else {
  186. characterTextures[count].color = Colors.WHITE;
  187. }
  188. debugWriteln("Count: ", count, " drawTexture cond: ", characterTextures[count].drawTexture);
  189. debugWriteln("arguments count: ", lua_gettop(L));
  190. } catch (Exception e) {
  191. debugWriteln(e.msg);
  192. }
  193. return 0;
  194. }
  195. extern (C) nothrow int luaL_stopDrawCharacter(lua_State* L)
  196. {
  197. int count = cast(int) luaL_checkinteger(L, 1);
  198. if (count >= characterTextures.length) {
  199. debugWriteln("error stop draw not loaded character");
  200. } else {
  201. characterTextures[count].drawTexture = false;
  202. }
  203. return 0;
  204. }
  205. extern (C) nothrow int luaL_unloadCharacter(lua_State *L) {
  206. int count = cast(int) luaL_checkinteger(L, 1);
  207. if (count >= characterTextures.length) {
  208. debugWriteln("error unloading not loaded character");
  209. } else {
  210. characterTextures[count].drawTexture = false;
  211. UnloadTexture(characterTextures[count].texture);
  212. }
  213. return 0;
  214. }
  215. /* music and video */
  216. extern (C) nothrow int luaL_loadMusic(lua_State* L)
  217. {
  218. try
  219. {
  220. char* musicPath = cast(char*)luaL_checkstring(L, 1);
  221. music = LoadMusicStream(musicPath);
  222. }
  223. catch (Exception e)
  224. {
  225. debugWriteln(e.msg);
  226. }
  227. return 0;
  228. }
  229. extern (C) nothrow int luaL_playMusic(lua_State* L)
  230. {
  231. PlayMusicStream(music);
  232. return 0;
  233. }
  234. extern (C) nothrow int luaL_stopMusic(lua_State* L)
  235. {
  236. StopMusicStream(music);
  237. return 0;
  238. }
  239. extern (C) nothrow int luaL_unloadMusic(lua_State* L)
  240. {
  241. UnloadMusicStream(music);
  242. music = Music();
  243. return 0;
  244. }
  245. extern (C) nothrow int luaL_playSfx(lua_State *L) {
  246. try {
  247. playSfx(to!string(luaL_checkstring(L, 1)));
  248. } catch (Exception e) {
  249. debugWriteln(e.msg);
  250. }
  251. return 0;
  252. }
  253. extern (C) nothrow int luaL_stopSfx(lua_State *L) {
  254. StopSound(sfx);
  255. return 0;
  256. }
  257. extern (C) nothrow int luaL_playVideo(lua_State* L)
  258. {
  259. try
  260. {
  261. videoFinished = false;
  262. playVideo(luaL_checkstring(L, 1).to!string);
  263. }
  264. catch (Exception e)
  265. {
  266. debugWriteln(e.msg);
  267. }
  268. return 0;
  269. }
  270. /* ui animations */
  271. extern (C) nothrow int luaL_loadUIAnimation(lua_State *L) {
  272. try {
  273. //loads from uifx folder HPFF files, in which png textures are stored
  274. framesUI = loadAnimationFramesUI(to!string(luaL_checkstring(L, 1)), to!string(luaL_checkstring(L, 2)));
  275. if (lua_gettop(L) == 3) {
  276. frameDuration = luaL_checknumber(L, 3);
  277. debug debugWriteln("frameDuration: ", frameDuration);
  278. }
  279. } catch (Exception e) {
  280. debugWriteln(e.msg);
  281. }
  282. return 0;
  283. }
  284. extern (C) nothrow int luaL_playUIAnimation(lua_State *L) {
  285. debug debugWriteln("Animation UI start");
  286. try {
  287. if (lua_gettop(L) == 0) playAnimation = true;
  288. else {
  289. debugWriteln("animationAlpha before reconfig: ", animationAlpha);
  290. animationAlpha = luaL_checkinteger(L, 1).to!ubyte;
  291. debugWriteln("animationAlpha after reconfig: ", animationAlpha);
  292. playAnimation = true;
  293. }
  294. } catch (Exception e) {
  295. debugWriteln(e.msg);
  296. }
  297. return 0;
  298. }
  299. extern (C) nothrow int luaL_stopUIAnimation(lua_State *L) {
  300. playAnimation = false;
  301. debug debugWriteln("Animation UI stop");
  302. frameDuration = 0.016f;
  303. currentFrame = 0;
  304. return 0;
  305. }
  306. extern (C) nothrow int luaL_unloadUIAnimation(lua_State *L) {
  307. try {
  308. for (int i = 0; i < framesUI.length; i++) {
  309. UnloadTexture(framesUI[i]);
  310. }
  311. } catch (Exception e) {
  312. debugWriteln(e.msg);
  313. }
  314. return 0;
  315. }
  316. extern (C) nothrow int luaL_setDialogBoxBackground(lua_State *L) {
  317. string filename = luaL_checkstring(L, 1).to!string;
  318. debug debugWriteln("Set dialog background to: ", filename);
  319. UnloadTexture(dialogBackgroundTex);
  320. dialogBackgroundTex = Texture2D();
  321. dialogBackgroundTex = LoadTexture(filename.toStringz());
  322. return 0;
  323. }
  324. extern (C) nothrow int luaL_setDialogBoxEndIndicatorTexture(lua_State *L) {
  325. char* filename = cast(char*)luaL_checkstring(L, 1);
  326. UnloadTexture(circle);
  327. circle = Texture2D();
  328. circle = LoadTexture(filename);
  329. return 0;
  330. }
  331. /* system */
  332. extern (C) nothrow int luaL_getScreenWidth(lua_State* L)
  333. {
  334. lua_pushinteger(L, GetScreenWidth());
  335. return 1;
  336. }
  337. extern (C) nothrow int luaL_getScreenHeight(lua_State* L)
  338. {
  339. lua_pushinteger(L, GetScreenHeight());
  340. return 1;
  341. }
  342. extern (C) nothrow int luaL_unloadFont(lua_State *L) {
  343. UnloadFont(textFont);
  344. return 0;
  345. }
  346. extern (C) nothrow int luaL_loadFont(lua_State* L)
  347. {
  348. const char* x = luaL_checkstring(L, 1);
  349. debugWriteln("Setting custom font: ", x.to!string);
  350. int[512] codepoints = 0;
  351. //configuring both cyrillic and latin fonts if available
  352. foreach (i; 0 .. 95)
  353. {
  354. codepoints[i] = 32 + i;
  355. }
  356. foreach (i; 0 .. 255)
  357. {
  358. codepoints[96 + i] = 0x400 + i;
  359. }
  360. int fontSize = max(10, cast(int)(40 * scale));
  361. textFont = LoadFontEx(x, fontSize, null, 0);
  362. return 0;
  363. }
  364. extern (C) nothrow int luaL_getTime(lua_State* L)
  365. {
  366. //getTime() returns current time.
  367. lua_pushnumber(L, GetTime());
  368. return 1;
  369. }
  370. extern (C) nothrow int luaL_getDeltaTime(lua_State* L)
  371. {
  372. //getDeltaTime
  373. lua_pushnumber(L, GetFrameTime());
  374. return 1;
  375. }
  376. float lastKeyPressTime = 0.0;
  377. immutable float keyPressCooldown = 0.2;
  378. extern (C) nothrow int luaL_isKeyPressed(lua_State* L)
  379. {
  380. try
  381. {
  382. int keyCode = cast(int)luaL_checkinteger(L, 1);
  383. bool isPressed = IsKeyPressed(keyCode).to!bool;
  384. double currentTime = GetTime();
  385. if (isPressed && (currentTime - lastKeyPressTime) < keyPressCooldown)
  386. {
  387. lua_pushboolean(L, false);
  388. }
  389. else if (isPressed)
  390. {
  391. lastKeyPressTime = currentTime;
  392. lua_pushboolean(L, true);
  393. }
  394. else
  395. {
  396. lua_pushboolean(L, false);
  397. }
  398. return 1;
  399. }
  400. catch (Exception e)
  401. {
  402. debugWriteln(e.msg);
  403. lua_pushboolean(L, false);
  404. return 1;
  405. }
  406. }
  407. extern (C) nothrow int luaL_isKeyDown(lua_State *L) {
  408. return IsKeyDown(cast(int)luaL_checkinteger(L, 1));
  409. }
  410. extern (C) nothrow int luaL_isMouseButtonPressed(lua_State* L)
  411. {
  412. try
  413. {
  414. int keyCode = cast(int)luaL_checkinteger(L, 1);
  415. bool isPressed = IsMouseButtonPressed(keyCode);
  416. double currentTime = GetTime();
  417. if (isPressed && (currentTime - lastKeyPressTime) < keyPressCooldown)
  418. {
  419. lua_pushboolean(L, false);
  420. }
  421. else if (isPressed)
  422. {
  423. lastKeyPressTime = currentTime;
  424. lua_pushboolean(L, true);
  425. }
  426. else
  427. {
  428. lua_pushboolean(L, false);
  429. }
  430. return 1;
  431. }
  432. catch (Exception e)
  433. {
  434. debugWriteln(e.msg);
  435. lua_pushboolean(L, false);
  436. return 1;
  437. }
  438. }
  439. extern (C) nothrow int luaL_getMouseX(lua_State *L) {
  440. lua_pushnumber(L, GetMousePosition.x);
  441. return 1;
  442. }
  443. extern (C) nothrow int luaL_getMouseY(lua_State *L) {
  444. lua_pushnumber(L, GetMousePosition.y);
  445. return 1;
  446. }
  447. extern (C) nothrow int luaL_loadScript(lua_State* L)
  448. {
  449. try
  450. {
  451. luaExec = luaL_checkstring(L, 1).to!string;
  452. }
  453. catch (Exception e)
  454. {
  455. debugWriteln(e.msg);
  456. }
  457. luaReload = true;
  458. return 0;
  459. }
  460. extern (C) nothrow int luaL_setGameState(lua_State *L) {
  461. currentGameState = cast(int)luaL_checkinteger(L, 1);
  462. return 0;
  463. }
  464. extern (C) nothrow int luaL_setCamera(lua_State *L) {
  465. camera.position = Vector3(
  466. luaL_checknumber(L, 1),
  467. luaL_checknumber(L, 2),
  468. luaL_checknumber(L, 3)
  469. );
  470. camera.target = Vector3(
  471. luaL_checknumber(L, 4),
  472. luaL_checknumber(L, 5),
  473. luaL_checknumber(L, 6)
  474. );
  475. return 0;
  476. }
  477. ModelAnimation *modelAnimations;
  478. int animationCurrentFrame = 0;
  479. int animsCount = 0;
  480. extern (C) nothrow int luaL_loadModelAnimations(lua_State *L) {
  481. modelAnimations = LoadModelAnimations(luaL_checkstring(L, 1), &animsCount);
  482. return 0;
  483. }
  484. extern (C) nothrow int luaL_updateModelAnimation(lua_State *L) {
  485. ModelAnimation anim = modelAnimations[cast(int)luaL_checkinteger(L, 2)];
  486. int speedMultipler;
  487. if (lua_gettop(L) == 3) {
  488. speedMultipler = cast(int)luaL_checkinteger(L, 3);
  489. } else {
  490. speedMultipler = 1;
  491. }
  492. animationCurrentFrame = (animationCurrentFrame + speedMultipler)%anim.frameCount;
  493. Model *model = cast(Model*)luaL_checkudata(L, 1, "Model");
  494. UpdateModelAnimation(*model, anim, animationCurrentFrame);
  495. return 0;
  496. }
  497. extern (C) nothrow int luaL_resetModelAnimation(lua_State *L) {
  498. animationCurrentFrame = 0;
  499. return 0;
  500. }
  501. /* raylib direct bindings for graphics */
  502. /* basic */
  503. extern (C) nothrow int luaL_loadTexture(lua_State *L) {
  504. const char* fileName = luaL_checkstring(L, 1);
  505. Texture2D texture = LoadTexture(fileName);
  506. Texture2D* texturePtr = cast(Texture2D*)lua_newuserdata(L, Texture2D.sizeof);
  507. *texturePtr = texture;
  508. if (luaL_newmetatable(L, "Texture")) {
  509. lua_pushcfunction(L, &luaL_textureGC);
  510. lua_setfield(L, -2, "__gc");
  511. }
  512. SetTextureFilter(texture, TextureFilter.TEXTURE_FILTER_BILINEAR);
  513. lua_setmetatable(L, -2);
  514. return 1;
  515. }
  516. extern (C) nothrow int luaL_textureGC(lua_State *L) {
  517. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  518. UnloadTexture(*texture);
  519. return 0;
  520. }
  521. extern (C) nothrow int luaL_unloadTexture(lua_State *L) {
  522. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  523. UnloadTexture(*texture);
  524. return 0;
  525. }
  526. extern (C) nothrow int luaL_drawTexture(lua_State *L) {
  527. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  528. int x = cast(int)luaL_checkinteger(L, 2);
  529. int y = cast(int)luaL_checkinteger(L, 3);
  530. Color color = Colors.WHITE;
  531. if (lua_gettop(L) >= 4 && lua_istable(L, 4)) {
  532. lua_getfield(L, 4, "r");
  533. color.r = cast(ubyte)lua_tointeger(L, -1);
  534. lua_pop(L, 1);
  535. lua_getfield(L, 4, "g");
  536. color.g = cast(ubyte)lua_tointeger(L, -1);
  537. lua_pop(L, 1);
  538. lua_getfield(L, 4, "b");
  539. color.b = cast(ubyte)lua_tointeger(L, -1);
  540. lua_pop(L, 1);
  541. lua_getfield(L, 4, "a");
  542. color.a = cast(ubyte)lua_tointeger(L, -1);
  543. lua_pop(L, 1);
  544. }
  545. DrawTextureEx(*texture, Vector2(x, y), 0.0f, scale, color);
  546. return 0;
  547. }
  548. extern (C) nothrow int luaL_getTextureWidth(lua_State *L) {
  549. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  550. lua_pushnumber(L, texture.width*variables.scale);
  551. return 1;
  552. }
  553. extern (C) nothrow int luaL_getTextureHeight(lua_State *L) {
  554. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  555. lua_pushnumber(L, texture.height*variables.scale);
  556. return 1;
  557. }
  558. extern (C) nothrow int luaL_drawText(lua_State *L) {
  559. const char* text = luaL_checkstring(L, 1);
  560. int x = cast(int)luaL_checkinteger(L, 2);
  561. int y = cast(int)luaL_checkinteger(L, 3);
  562. int fontSize = cast(int)luaL_optinteger(L, 4, 20);
  563. Color color = Colors.WHITE;
  564. if (lua_istable(L, 5)) {
  565. lua_getfield(L, 5, "r");
  566. color.r = cast(ubyte)lua_tointeger(L, -1);
  567. lua_pop(L, 1);
  568. lua_getfield(L, 5, "g");
  569. color.g = cast(ubyte)lua_tointeger(L, -1);
  570. lua_pop(L, 1);
  571. lua_getfield(L, 5, "b");
  572. color.b = cast(ubyte)lua_tointeger(L, -1);
  573. lua_pop(L, 1);
  574. lua_getfield(L, 5, "a");
  575. color.a = cast(ubyte)lua_tointeger(L, -1);
  576. lua_pop(L, 1);
  577. }
  578. DrawTextEx(textFont, text, Vector2(x, y), fontSize, 1.0f, color);
  579. return 0;
  580. }
  581. extern (C) nothrow int luaL_measureTextX(lua_State *L) {
  582. lua_pushinteger(L, cast(int)MeasureTextEx(textFont, luaL_checkstring(L, 1), cast(int)luaL_checkinteger(L, 2), 1.0f).x);
  583. return 1;
  584. }
  585. extern (C) nothrow int luaL_measureTextY(lua_State *L) {
  586. lua_pushinteger(L, cast(int)MeasureTextEx(textFont, luaL_checkstring(L, 1), cast(int)luaL_checkinteger(L, 2), 1.0f).y);
  587. return 1;
  588. }
  589. /* extended */
  590. extern (C) nothrow int luaL_drawTextureEx(lua_State *L) {
  591. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  592. float x = luaL_checknumber(L, 2);
  593. float y = luaL_checknumber(L, 3);
  594. float rotation = luaL_optnumber(L, 4, 0);
  595. float scale = luaL_optnumber(L, 5, 1);
  596. Color color = Colors.WHITE;
  597. if (lua_istable(L, 6)) {
  598. lua_getfield(L, 6, "r");
  599. color.r = cast(ubyte)lua_tointeger(L, -1);
  600. lua_pop(L, 1);
  601. lua_getfield(L, 6, "g");
  602. color.g = cast(ubyte)lua_tointeger(L, -1);
  603. lua_pop(L, 1);
  604. lua_getfield(L, 6, "b");
  605. color.b = cast(ubyte)lua_tointeger(L, -1);
  606. lua_pop(L, 1);
  607. lua_getfield(L, 6, "a");
  608. color.a = cast(ubyte)lua_tointeger(L, -1);
  609. lua_pop(L, 1);
  610. }
  611. DrawTextureEx(*texture, Vector2(x, y), rotation, scale*variables.scale, color);
  612. return 0;
  613. }
  614. extern (C) nothrow int luaL_loadModel(lua_State *L) {
  615. const char* fileName = luaL_checkstring(L, 1);
  616. Model model = LoadModel(fileName);
  617. Model* modelPtr = cast(Model*)lua_newuserdata(L, Model.sizeof);
  618. *modelPtr = model;
  619. if (luaL_newmetatable(L, "Model")) {
  620. lua_pushcfunction(L, &luaL_textureGC);
  621. lua_setfield(L, -2, "__gc");
  622. }
  623. lua_setmetatable(L, -2);
  624. return 1;
  625. }
  626. extern (C) nothrow int luaL_drawModel(lua_State *L) {
  627. Model* model = cast(Model*)luaL_checkudata(L, 1, "Model");
  628. float x = luaL_checknumber(L, 2);
  629. float y = luaL_checknumber(L, 3);
  630. float z = luaL_checknumber(L, 4);
  631. float rotation = luaL_optnumber(L, 5, 0);
  632. float scale = luaL_optnumber(L, 6, 1);
  633. Color color = Colors.WHITE;
  634. if (lua_istable(L, 6)) {
  635. lua_getfield(L, 6, "r");
  636. color.r = cast(ubyte)lua_tointeger(L, -1);
  637. lua_pop(L, 1);
  638. lua_getfield(L, 6, "g");
  639. color.g = cast(ubyte)lua_tointeger(L, -1);
  640. lua_pop(L, 1);
  641. lua_getfield(L, 6, "b");
  642. color.b = cast(ubyte)lua_tointeger(L, -1);
  643. lua_pop(L, 1);
  644. lua_getfield(L, 6, "a");
  645. color.a = cast(ubyte)lua_tointeger(L, -1);
  646. lua_pop(L, 1);
  647. }
  648. DrawModelEx(
  649. *model,
  650. Vector3(x, y, z),
  651. Vector3(0.0f, 1.0f, 0.0f),
  652. rotation,
  653. Vector3(scale, scale, scale),
  654. color
  655. );
  656. return 0;
  657. }
  658. extern (C) nothrow int luaL_showCursor(lua_State *L) {
  659. ShowCursor();
  660. return 0;
  661. }
  662. extern (C) nothrow int luaL_hideCursor(lua_State *L) {
  663. HideCursor();
  664. return 0;
  665. }
  666. extern (C) nothrow int luaL_setMousePosition(lua_State *L) {
  667. SetMousePosition(cast(int)luaL_checkinteger(L, 1), cast(int)luaL_checkinteger(L, 2));
  668. return 0;
  669. }
  670. /* Register functions */
  671. extern (C) nothrow void luaL_loader(lua_State* L)
  672. {
  673. lua_register(L, "dialogBox", &luaL_dialogBox);
  674. lua_register(L, "isDialogExecuted", &luaL_isDialogExecuted);
  675. lua_register(L, "getAnswerValue", &luaL_getAnswerValue);
  676. lua_register(L, "loadAnimationUI", &luaL_loadUIAnimation);
  677. lua_register(L, "playAnimationUI", &luaL_playUIAnimation);
  678. lua_register(L, "stopAnimationUI", &luaL_stopUIAnimation);
  679. lua_register(L, "unloadAnimationUI", &luaL_unloadUIAnimation);
  680. lua_register(L, "setDialogBoxBackground", &luaL_setDialogBoxBackground);
  681. lua_register(L, "setDialogEndIndicator", &luaL_setDialogBoxEndIndicatorTexture);
  682. lua_register(L, "playVideo", &luaL_playVideo);
  683. lua_register(L, "loadMusic", &luaL_loadMusic);
  684. lua_register(L, "playMusic", &luaL_playMusic);
  685. lua_register(L, "stopMusic", &luaL_stopMusic);
  686. lua_register(L, "unloadMusic", &luaL_unloadMusic);
  687. lua_register(L, "playSfx", &luaL_playSfx);
  688. lua_register(L, "stopSfx", &luaL_stopSfx);
  689. lua_register(L, "loadCharacter", &luaL_loadCharacter);
  690. lua_register(L, "drawCharacter", &luaL_drawCharacter);
  691. lua_register(L, "stopDrawCharacter", &luaL_stopDrawCharacter);
  692. lua_register(L, "unloadCharacter", &luaL_unloadCharacter);
  693. lua_register(L, "loadBackground", &luaL_loadBackground);
  694. lua_register(L, "drawBackground", &luaL_drawBackground);
  695. lua_register(L, "stopDrawBackground", &luaL_stopDrawBackground);
  696. lua_register(L, "unloadBackground", &luaL_unloadBackground);
  697. lua_register(L, "loadScript", &luaL_loadScript);
  698. lua_register(L, "getScreenHeight", &luaL_getScreenHeight);
  699. lua_register(L, "getScreenWidth", &luaL_getScreenWidth);
  700. lua_register(L, "isKeyPressed", &luaL_isKeyPressed);
  701. lua_register(L, "isKeyDown", &luaL_isKeyDown);
  702. lua_register(L, "isMouseButtonPressed", &luaL_isMouseButtonPressed);
  703. lua_register(L, "getMouseX", &luaL_getMouseX);
  704. lua_register(L, "getMouseY", &luaL_getMouseY);
  705. lua_register(L, "setGameState", &luaL_setGameState);
  706. lua_register(L, "setCamera", &luaL_setCamera);
  707. lua_register(L, "loadModelAnimations", &luaL_loadModelAnimations);
  708. lua_register(L, "updateModelAnimation", &luaL_updateModelAnimation);
  709. lua_register(L, "resetModelAnimation", &luaL_resetModelAnimation);
  710. //raylib direct bindings
  711. lua_register(L, "unloadFont", &luaL_unloadFont);
  712. lua_register(L, "loadFont", &luaL_loadFont);
  713. lua_register(L, "getTime", &luaL_getTime);
  714. lua_register(L, "getDeltaTime", &luaL_getDeltaTime);
  715. lua_register(L, "loadTexture", &luaL_loadTexture);
  716. lua_register(L, "drawTexture", &luaL_drawTexture);
  717. lua_register(L, "drawTextureEx", &luaL_drawTextureEx);
  718. lua_register(L, "unloadTexture", &luaL_unloadTexture);
  719. lua_register(L, "drawText", &luaL_drawText);
  720. lua_register(L, "measureTextX", &luaL_measureTextX);
  721. lua_register(L, "measureTextY", &luaL_measureTextY);
  722. lua_register(L, "getTextureWidth", &luaL_getTextureWidth);
  723. lua_register(L, "getTextureHeight", &luaL_getTextureHeight);
  724. lua_register(L, "loadModel", &luaL_loadModel);
  725. lua_register(L, "drawModel", &luaL_drawModel);
  726. lua_register(L, "showCursor", &luaL_showCursor);
  727. lua_register(L, "hideCursor", &luaL_hideCursor);
  728. lua_register(L, "setMousePosition", &luaL_setMousePosition);
  729. //compat
  730. lua_register(L, "setFont", &luaL_loadFont);
  731. debugWriteln("strict mode enabled");
  732. const char* strict_lua =
  733. `local mt = {
  734. __index = function(_, name)
  735. error("attempt to call undefined function: " .. tostring(name), 2)
  736. end
  737. }
  738. setmetatable(_G, mt)`;
  739. if (luaL_dostring(L, strict_lua) != LUA_OK) {
  740. debugWriteln("Failed to set strict mode: ", to!string(lua_tostring(L, -1)));
  741. lua_pop(L, 1); // Pop the error message
  742. }
  743. }
  744. int luaInit(string luaExec)
  745. {
  746. debugWriteln("loading Lua");
  747. L = luaL_newstate();
  748. luaL_openlibs(L);
  749. luaL_loader(L);
  750. debugWriteln("Executing next Lua file: ", luaExec);
  751. if (std.file.exists(luaExec) == false) {
  752. debugWriteln("Script file not found! Exiting.");
  753. return EngineExitCodes.EXIT_FILE_NOT_FOUND;
  754. }
  755. if (luaL_dofile(L, toStringz(luaExec)) != LUA_OK) {
  756. debugWriteln("Lua error: ", to!string(lua_tostring(L, -1)));
  757. return EngineExitCodes.EXIT_SCRIPT_ERROR;
  758. }
  759. return EngineExitCodes.EXIT_OK;
  760. }
  761. void luaEventLoop()
  762. {
  763. lua_getglobal(L, "EventLoop");
  764. if (lua_pcall(L, 0, 0, 0) != LUA_OK)
  765. {
  766. debug debugWriteln("Error in EventLoop: ", to!string(lua_tostring(L, -1)));
  767. }
  768. lua_pop(L, 0);
  769. }