lua.d 22 KB

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