lua.d 22 KB

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