lua.d 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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_unloadFont(lua_State *L) {
  358. UnloadFont(textFont);
  359. return 0;
  360. }
  361. extern (C) nothrow int luaL_loadFont(lua_State* L)
  362. {
  363. const char* x = luaL_checkstring(L, 1);
  364. debugWriteln("Setting custom font: ", x.to!string);
  365. int[512] codepoints = 0;
  366. //configuring both cyrillic and latin fonts if available
  367. foreach (i; 0 .. 95)
  368. {
  369. codepoints[i] = 32 + i;
  370. }
  371. foreach (i; 0 .. 255)
  372. {
  373. codepoints[96 + i] = 0x400 + i;
  374. }
  375. textFont = LoadFontEx(x, 40, codepoints.ptr, codepoints.length);
  376. return 0;
  377. }
  378. extern (C) nothrow int luaL_getTime(lua_State* L)
  379. {
  380. //getTime() returns current time.
  381. lua_pushnumber(L, GetTime());
  382. return 1;
  383. }
  384. extern (C) nothrow int luaL_getDeltaTime(lua_State* L)
  385. {
  386. //getDeltaTime
  387. lua_pushnumber(L, GetFrameTime());
  388. return 1;
  389. }
  390. float lastKeyPressTime = 0.0;
  391. immutable float keyPressCooldown = 0.2;
  392. extern (C) nothrow int luaL_isKeyPressed(lua_State* L)
  393. {
  394. try
  395. {
  396. int keyCode = cast(int)luaL_checkinteger(L, 1);
  397. bool isPressed = IsKeyPressed(keyCode).to!bool;
  398. double currentTime = GetTime();
  399. if (isPressed && (currentTime - lastKeyPressTime) < keyPressCooldown)
  400. {
  401. lua_pushboolean(L, false);
  402. }
  403. else if (isPressed)
  404. {
  405. lastKeyPressTime = currentTime;
  406. lua_pushboolean(L, true);
  407. }
  408. else
  409. {
  410. lua_pushboolean(L, false);
  411. }
  412. return 1;
  413. }
  414. catch (Exception e)
  415. {
  416. debugWriteln(e.msg);
  417. lua_pushboolean(L, false);
  418. return 1;
  419. }
  420. }
  421. extern (C) nothrow int luaL_isMouseButtonPressed(lua_State* L)
  422. {
  423. try
  424. {
  425. int keyCode = cast(int)luaL_checkinteger(L, 1);
  426. bool isPressed = IsMouseButtonPressed(keyCode);
  427. double currentTime = GetTime();
  428. if (isPressed && (currentTime - lastKeyPressTime) < keyPressCooldown)
  429. {
  430. lua_pushboolean(L, false);
  431. }
  432. else if (isPressed)
  433. {
  434. lastKeyPressTime = currentTime;
  435. lua_pushboolean(L, true);
  436. }
  437. else
  438. {
  439. lua_pushboolean(L, false);
  440. }
  441. return 1;
  442. }
  443. catch (Exception e)
  444. {
  445. debugWriteln(e.msg);
  446. lua_pushboolean(L, false);
  447. return 1;
  448. }
  449. }
  450. extern (C) nothrow int luaL_loadScript(lua_State* L)
  451. {
  452. try
  453. {
  454. luaExec = luaL_checkstring(L, 1).to!string;
  455. }
  456. catch (Exception e)
  457. {
  458. debugWriteln(e.msg);
  459. }
  460. luaReload = true;
  461. return 0;
  462. }
  463. extern (C) nothrow int luaL_setGameState(lua_State *L) {
  464. currentGameState = cast(int)luaL_checkinteger(L, 1);
  465. return 0;
  466. }
  467. /* raylib direct bindings for graphics */
  468. /* basic */
  469. extern (C) nothrow int luaL_loadTexture(lua_State *L) {
  470. const char* fileName = luaL_checkstring(L, 1);
  471. Texture2D texture = LoadTexture(fileName);
  472. Texture2D* texturePtr = cast(Texture2D*)lua_newuserdata(L, Texture2D.sizeof);
  473. *texturePtr = texture;
  474. if (luaL_newmetatable(L, "Texture")) {
  475. lua_pushcfunction(L, &luaL_textureGC);
  476. lua_setfield(L, -2, "__gc");
  477. }
  478. lua_setmetatable(L, -2);
  479. return 1;
  480. }
  481. extern (C) nothrow int luaL_textureGC(lua_State *L) {
  482. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  483. UnloadTexture(*texture);
  484. return 0;
  485. }
  486. extern (C) nothrow int luaL_unloadTexture(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_drawTexture(lua_State *L) {
  492. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  493. int x = cast(int)luaL_checkinteger(L, 2);
  494. int y = cast(int)luaL_checkinteger(L, 3);
  495. Color color = Colors.WHITE;
  496. if (lua_gettop(L) >= 4 && lua_istable(L, 4)) {
  497. lua_getfield(L, 4, "r");
  498. color.r = cast(ubyte)lua_tointeger(L, -1);
  499. lua_pop(L, 1);
  500. lua_getfield(L, 4, "g");
  501. color.g = cast(ubyte)lua_tointeger(L, -1);
  502. lua_pop(L, 1);
  503. lua_getfield(L, 4, "b");
  504. color.b = cast(ubyte)lua_tointeger(L, -1);
  505. lua_pop(L, 1);
  506. lua_getfield(L, 4, "a");
  507. color.a = cast(ubyte)lua_tointeger(L, -1);
  508. lua_pop(L, 1);
  509. }
  510. DrawTexture(*texture, x, y, color);
  511. return 0;
  512. }
  513. extern (C) nothrow int luaL_getTextureWidth(lua_State *L) {
  514. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  515. lua_pushinteger(L, texture.width);
  516. return 1;
  517. }
  518. extern (C) nothrow int luaL_getTextureHeight(lua_State *L) {
  519. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  520. lua_pushinteger(L, texture.height);
  521. return 1;
  522. }
  523. extern (C) nothrow int luaL_drawText(lua_State *L) {
  524. const char* text = luaL_checkstring(L, 1);
  525. int x = cast(int)luaL_checkinteger(L, 2);
  526. int y = cast(int)luaL_checkinteger(L, 3);
  527. int fontSize = cast(int)luaL_optinteger(L, 4, 20);
  528. Color color = Colors.WHITE;
  529. if (lua_istable(L, 5)) {
  530. lua_getfield(L, 5, "r");
  531. color.r = cast(ubyte)lua_tointeger(L, -1);
  532. lua_pop(L, 1);
  533. lua_getfield(L, 5, "g");
  534. color.g = cast(ubyte)lua_tointeger(L, -1);
  535. lua_pop(L, 1);
  536. lua_getfield(L, 5, "b");
  537. color.b = cast(ubyte)lua_tointeger(L, -1);
  538. lua_pop(L, 1);
  539. lua_getfield(L, 5, "a");
  540. color.a = cast(ubyte)lua_tointeger(L, -1);
  541. lua_pop(L, 1);
  542. }
  543. DrawTextEx(textFont, text, Vector2(x, y), fontSize, 1.0f, color);
  544. return 0;
  545. }
  546. extern (C) nothrow int luaL_measureTextX(lua_State *L) {
  547. lua_pushinteger(L, cast(int)MeasureTextEx(textFont, luaL_checkstring(L, 1), cast(int)luaL_checkinteger(L, 2), 1.0f).x);
  548. return 1;
  549. }
  550. extern (C) nothrow int luaL_measureTextY(lua_State *L) {
  551. lua_pushinteger(L, cast(int)MeasureTextEx(textFont, luaL_checkstring(L, 1), cast(int)luaL_checkinteger(L, 2), 1.0f).y);
  552. return 1;
  553. }
  554. /* extended */
  555. extern (C) nothrow int luaL_drawTextureEx(lua_State *L) {
  556. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  557. float x = luaL_checknumber(L, 2);
  558. float y = luaL_checknumber(L, 3);
  559. float rotation = luaL_optnumber(L, 4, 0);
  560. float scale = luaL_optnumber(L, 5, 1);
  561. Color color = Colors.WHITE;
  562. if (lua_istable(L, 6)) {
  563. lua_getfield(L, 6, "r");
  564. color.r = cast(ubyte)lua_tointeger(L, -1);
  565. lua_pop(L, 1);
  566. lua_getfield(L, 6, "g");
  567. color.g = cast(ubyte)lua_tointeger(L, -1);
  568. lua_pop(L, 1);
  569. lua_getfield(L, 6, "b");
  570. color.b = cast(ubyte)lua_tointeger(L, -1);
  571. lua_pop(L, 1);
  572. lua_getfield(L, 6, "a");
  573. color.a = cast(ubyte)lua_tointeger(L, -1);
  574. lua_pop(L, 1);
  575. }
  576. DrawTextureEx(*texture, Vector2(x, y), rotation, scale, color);
  577. return 0;
  578. }
  579. /* Register functions */
  580. extern (C) nothrow void luaL_loader(lua_State* L)
  581. {
  582. lua_register(L, "dialogBox", &luaL_dialogBox);
  583. lua_register(L, "isDialogExecuted", &luaL_isDialogExecuted);
  584. lua_register(L, "getAnswerValue", &luaL_getAnswerValue);
  585. lua_register(L, "loadAnimationUI", &luaL_loadUIAnimation);
  586. lua_register(L, "playAnimationUI", &luaL_playUIAnimation);
  587. lua_register(L, "stopAnimationUI", &luaL_stopUIAnimation);
  588. lua_register(L, "unloadAnimationUI", &luaL_unloadUIAnimation);
  589. lua_register(L, "moveCamera", &luaL_moveCamera);
  590. lua_register(L, "restoreCamera", &luaL_restoreCamera);
  591. lua_register(L, "isCameraMoving", &luaL_isCameraMoving);
  592. lua_register(L, "playVideo", &luaL_playVideo);
  593. lua_register(L, "loadMusic", &luaL_loadMusic);
  594. lua_register(L, "playMusic", &luaL_playMusic);
  595. lua_register(L, "stopMusic", &luaL_stopMusic);
  596. lua_register(L, "unloadMusic", &luaL_unloadMusic);
  597. lua_register(L, "playSfx", &luaL_playSfx);
  598. lua_register(L, "stopSfx", &luaL_stopSfx);
  599. lua_register(L, "Begin2D", &luaL_2dModeEnable);
  600. lua_register(L, "End2D", &luaL_2dModeDisable);
  601. lua_register(L, "loadCharacter", &luaL_load2Dcharacter);
  602. lua_register(L, "drawCharacter", &luaL_draw2Dcharacter);
  603. lua_register(L, "stopDrawCharacter", &luaL_stopDraw2Dcharacter);
  604. lua_register(L, "unloadCharacter", &luaL_unload2Dcharacter);
  605. lua_register(L, "loadBackground", &luaL_load2Dbackground);
  606. lua_register(L, "drawBackground", &luaL_draw2Dbackground);
  607. lua_register(L, "stopDrawBackground", &luaL_stopDraw2Dbackground);
  608. lua_register(L, "unloadBackground", &luaL_unload2Dbackground);
  609. lua_register(L, "getTime", &luaL_getTime);
  610. lua_register(L, "getDeltaTime", &luaL_getDeltaTime);
  611. lua_register(L, "loadScript", &luaL_loadScript);
  612. lua_register(L, "loadFont", &luaL_loadFont);
  613. lua_register(L, "unloadFont", &luaL_unloadFont);
  614. lua_register(L, "getScreenHeight", &luaL_getScreenHeight);
  615. lua_register(L, "getScreenWidth", &luaL_getScreenWidth);
  616. lua_register(L, "isKeyPressed", &luaL_isKeyPressed);
  617. lua_register(L, "isMouseButtonPressed", &luaL_isMouseButtonPressed);
  618. lua_register(L, "getLanguage", &luaL_getUsedLanguage);
  619. lua_register(L, "setGameState", &luaL_setGameState);
  620. //raylib direct bindings
  621. lua_register(L, "loadTexture", &luaL_loadTexture);
  622. lua_register(L, "drawTexture", &luaL_drawTexture);
  623. lua_register(L, "drawTextureEx", &luaL_drawTextureEx);
  624. lua_register(L, "unloadTexture", &luaL_unloadTexture);
  625. lua_register(L, "drawText", &luaL_drawText);
  626. lua_register(L, "measureTextX", &luaL_measureTextX);
  627. lua_register(L, "measureTextY", &luaL_measureTextY);
  628. lua_register(L, "getTextureWidth", &luaL_getTextureWidth);
  629. lua_register(L, "getTextureHeight", &luaL_getTextureHeight);
  630. //compat
  631. lua_register(L, "setFont", &luaL_loadFont);
  632. debugWriteln("strict mode enabled");
  633. const char* strict_lua =
  634. `local mt = {
  635. __index = function(_, name)
  636. error("attempt to call undefined function: " .. tostring(name), 2)
  637. end
  638. }
  639. setmetatable(_G, mt)`;
  640. if (luaL_dostring(L, strict_lua) != LUA_OK) {
  641. debugWriteln("Failed to set strict mode: ", to!string(lua_tostring(L, -1)));
  642. lua_pop(L, 1); // Pop the error message
  643. }
  644. }
  645. int luaInit(string luaExec)
  646. {
  647. debugWriteln("loading Lua");
  648. L = luaL_newstate();
  649. luaL_openlibs(L);
  650. luaL_loader(L);
  651. debugWriteln("Executing next Lua file: ", luaExec);
  652. if (std.file.exists(luaExec) == false) {
  653. debugWriteln("Script file not found! Exiting.");
  654. return EngineExitCodes.EXIT_FILE_NOT_FOUND;
  655. }
  656. if (luaL_dofile(L, toStringz(luaExec)) != LUA_OK) {
  657. debugWriteln("Lua error: ", to!string(lua_tostring(L, -1)));
  658. return EngineExitCodes.EXIT_SCRIPT_ERROR;
  659. }
  660. return EngineExitCodes.EXIT_OK;
  661. }
  662. void luaEventLoop()
  663. {
  664. lua_getglobal(L, "EventLoop");
  665. if (lua_pcall(L, 0, 0, 0) != LUA_OK)
  666. {
  667. debug debugWriteln("Error in EventLoop: ", to!string(lua_tostring(L, -1)));
  668. }
  669. lua_pop(L, 0);
  670. }