lua.d 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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_moveCamera(lua_State *L) {
  272. try {
  273. oldCamera = camera;
  274. float targetX = cast(float) luaL_checknumber(L, 1);
  275. float targetY = cast(float) luaL_checknumber(L, 2);
  276. float zoom = cast(float) luaL_optnumber(L, 3, 1.0f);
  277. float speed = cast(float) luaL_optnumber(L, 4, 5.0f);
  278. debugWriteln(targetX, targetY, zoom, speed);
  279. cameraTargetX = targetX;
  280. cameraTargetY = targetY;
  281. cameraTargetZoom = zoom;
  282. cameraMoveSpeed = speed;
  283. isCameraMoving = true;
  284. } catch (Exception e) {
  285. debugWriteln(e.msg);
  286. }
  287. return 0;
  288. }
  289. extern (C) nothrow int luaL_restoreCamera(lua_State *L) {
  290. camera = oldCamera;
  291. return 0;
  292. }
  293. extern (C) nothrow int luaL_isCameraMoving(lua_State *L) {
  294. lua_pushboolean(L, isCameraMoving);
  295. return 1;
  296. }
  297. extern (C) nothrow int luaL_loadUIAnimation(lua_State *L) {
  298. try {
  299. //loads from uifx folder HPFF files, in which png textures are stored
  300. framesUI = loadAnimationFramesUI(to!string(luaL_checkstring(L, 1)), to!string(luaL_checkstring(L, 2)));
  301. if (lua_gettop(L) == 3) {
  302. frameDuration = luaL_checknumber(L, 3);
  303. debug debugWriteln("frameDuration: ", frameDuration);
  304. }
  305. } catch (Exception e) {
  306. debugWriteln(e.msg);
  307. }
  308. return 0;
  309. }
  310. extern (C) nothrow int luaL_playUIAnimation(lua_State *L) {
  311. debug debugWriteln("Animation UI start");
  312. try {
  313. if (lua_gettop(L) == 0) playAnimation = true;
  314. else {
  315. debugWriteln("animationAlpha before reconfig: ", animationAlpha);
  316. animationAlpha = luaL_checkinteger(L, 1).to!ubyte;
  317. debugWriteln("animationAlpha after reconfig: ", animationAlpha);
  318. playAnimation = true;
  319. }
  320. } catch (Exception e) {
  321. debugWriteln(e.msg);
  322. }
  323. return 0;
  324. }
  325. extern (C) nothrow int luaL_stopUIAnimation(lua_State *L) {
  326. playAnimation = false;
  327. debug debugWriteln("Animation UI stop");
  328. frameDuration = 0.016f;
  329. currentFrame = 0;
  330. return 0;
  331. }
  332. extern (C) nothrow int luaL_unloadUIAnimation(lua_State *L) {
  333. try {
  334. for (int i = 0; i < framesUI.length; i++) {
  335. UnloadTexture(framesUI[i]);
  336. }
  337. } catch (Exception e) {
  338. debugWriteln(e.msg);
  339. }
  340. return 0;
  341. }
  342. extern (C) nothrow int luaL_setDialogBoxBackground(lua_State *L) {
  343. string filename = luaL_checkstring(L, 1).to!string;
  344. debug debugWriteln("Set dialog background to: ", filename);
  345. UnloadTexture(dialogBackgroundTex);
  346. dialogBackgroundTex = Texture2D();
  347. dialogBackgroundTex = LoadTexture(filename.toStringz());
  348. return 0;
  349. }
  350. extern (C) nothrow int luaL_setDialogBoxEndIndicatorTexture(lua_State *L) {
  351. char* filename = cast(char*)luaL_checkstring(L, 1);
  352. UnloadTexture(circle);
  353. circle = Texture2D();
  354. circle = LoadTexture(filename);
  355. return 0;
  356. }
  357. /* system */
  358. extern (C) nothrow int luaL_getScreenWidth(lua_State* L)
  359. {
  360. lua_pushinteger(L, GetScreenWidth());
  361. return 1;
  362. }
  363. extern (C) nothrow int luaL_getScreenHeight(lua_State* L)
  364. {
  365. lua_pushinteger(L, GetScreenHeight());
  366. return 1;
  367. }
  368. extern (C) nothrow int luaL_unloadFont(lua_State *L) {
  369. UnloadFont(textFont);
  370. return 0;
  371. }
  372. extern (C) nothrow int luaL_loadFont(lua_State* L)
  373. {
  374. const char* x = luaL_checkstring(L, 1);
  375. debugWriteln("Setting custom font: ", x.to!string);
  376. int[512] codepoints = 0;
  377. //configuring both cyrillic and latin fonts if available
  378. foreach (i; 0 .. 95)
  379. {
  380. codepoints[i] = 32 + i;
  381. }
  382. foreach (i; 0 .. 255)
  383. {
  384. codepoints[96 + i] = 0x400 + i;
  385. }
  386. int fontSize = max(10, cast(int)(40 * scale));
  387. textFont = LoadFontEx(x, fontSize, null, 0);
  388. return 0;
  389. }
  390. extern (C) nothrow int luaL_getTime(lua_State* L)
  391. {
  392. //getTime() returns current time.
  393. lua_pushnumber(L, GetTime());
  394. return 1;
  395. }
  396. extern (C) nothrow int luaL_getDeltaTime(lua_State* L)
  397. {
  398. //getDeltaTime
  399. lua_pushnumber(L, GetFrameTime());
  400. return 1;
  401. }
  402. float lastKeyPressTime = 0.0;
  403. immutable float keyPressCooldown = 0.2;
  404. extern (C) nothrow int luaL_isKeyPressed(lua_State* L)
  405. {
  406. try
  407. {
  408. int keyCode = cast(int)luaL_checkinteger(L, 1);
  409. bool isPressed = IsKeyPressed(keyCode).to!bool;
  410. double currentTime = GetTime();
  411. if (isPressed && (currentTime - lastKeyPressTime) < keyPressCooldown)
  412. {
  413. lua_pushboolean(L, false);
  414. }
  415. else if (isPressed)
  416. {
  417. lastKeyPressTime = currentTime;
  418. lua_pushboolean(L, true);
  419. }
  420. else
  421. {
  422. lua_pushboolean(L, false);
  423. }
  424. return 1;
  425. }
  426. catch (Exception e)
  427. {
  428. debugWriteln(e.msg);
  429. lua_pushboolean(L, false);
  430. return 1;
  431. }
  432. }
  433. extern (C) nothrow int luaL_isMouseButtonPressed(lua_State* L)
  434. {
  435. try
  436. {
  437. int keyCode = cast(int)luaL_checkinteger(L, 1);
  438. bool isPressed = IsMouseButtonPressed(keyCode);
  439. double currentTime = GetTime();
  440. if (isPressed && (currentTime - lastKeyPressTime) < keyPressCooldown)
  441. {
  442. lua_pushboolean(L, false);
  443. }
  444. else if (isPressed)
  445. {
  446. lastKeyPressTime = currentTime;
  447. lua_pushboolean(L, true);
  448. }
  449. else
  450. {
  451. lua_pushboolean(L, false);
  452. }
  453. return 1;
  454. }
  455. catch (Exception e)
  456. {
  457. debugWriteln(e.msg);
  458. lua_pushboolean(L, false);
  459. return 1;
  460. }
  461. }
  462. extern (C) nothrow int luaL_loadScript(lua_State* L)
  463. {
  464. try
  465. {
  466. luaExec = luaL_checkstring(L, 1).to!string;
  467. }
  468. catch (Exception e)
  469. {
  470. debugWriteln(e.msg);
  471. }
  472. luaReload = true;
  473. return 0;
  474. }
  475. extern (C) nothrow int luaL_setGameState(lua_State *L) {
  476. currentGameState = cast(int)luaL_checkinteger(L, 1);
  477. return 0;
  478. }
  479. /* raylib direct bindings for graphics */
  480. /* basic */
  481. extern (C) nothrow int luaL_loadTexture(lua_State *L) {
  482. const char* fileName = luaL_checkstring(L, 1);
  483. Texture2D texture = LoadTexture(fileName);
  484. Texture2D* texturePtr = cast(Texture2D*)lua_newuserdata(L, Texture2D.sizeof);
  485. *texturePtr = texture;
  486. if (luaL_newmetatable(L, "Texture")) {
  487. lua_pushcfunction(L, &luaL_textureGC);
  488. lua_setfield(L, -2, "__gc");
  489. }
  490. SetTextureFilter(texture, TextureFilter.TEXTURE_FILTER_BILINEAR);
  491. lua_setmetatable(L, -2);
  492. return 1;
  493. }
  494. extern (C) nothrow int luaL_textureGC(lua_State *L) {
  495. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  496. UnloadTexture(*texture);
  497. return 0;
  498. }
  499. extern (C) nothrow int luaL_unloadTexture(lua_State *L) {
  500. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  501. UnloadTexture(*texture);
  502. return 0;
  503. }
  504. extern (C) nothrow int luaL_drawTexture(lua_State *L) {
  505. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  506. int x = cast(int)luaL_checkinteger(L, 2);
  507. int y = cast(int)luaL_checkinteger(L, 3);
  508. Color color = Colors.WHITE;
  509. if (lua_gettop(L) >= 4 && lua_istable(L, 4)) {
  510. lua_getfield(L, 4, "r");
  511. color.r = cast(ubyte)lua_tointeger(L, -1);
  512. lua_pop(L, 1);
  513. lua_getfield(L, 4, "g");
  514. color.g = cast(ubyte)lua_tointeger(L, -1);
  515. lua_pop(L, 1);
  516. lua_getfield(L, 4, "b");
  517. color.b = cast(ubyte)lua_tointeger(L, -1);
  518. lua_pop(L, 1);
  519. lua_getfield(L, 4, "a");
  520. color.a = cast(ubyte)lua_tointeger(L, -1);
  521. lua_pop(L, 1);
  522. }
  523. DrawTextureEx(*texture, Vector2(x, y), 0.0f, scale, color);
  524. return 0;
  525. }
  526. extern (C) nothrow int luaL_getTextureWidth(lua_State *L) {
  527. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  528. lua_pushnumber(L, texture.width*variables.scale);
  529. return 1;
  530. }
  531. extern (C) nothrow int luaL_getTextureHeight(lua_State *L) {
  532. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  533. lua_pushnumber(L, texture.height*variables.scale);
  534. return 1;
  535. }
  536. extern (C) nothrow int luaL_drawText(lua_State *L) {
  537. const char* text = luaL_checkstring(L, 1);
  538. int x = cast(int)luaL_checkinteger(L, 2);
  539. int y = cast(int)luaL_checkinteger(L, 3);
  540. int fontSize = cast(int)luaL_optinteger(L, 4, 20);
  541. Color color = Colors.WHITE;
  542. if (lua_istable(L, 5)) {
  543. lua_getfield(L, 5, "r");
  544. color.r = cast(ubyte)lua_tointeger(L, -1);
  545. lua_pop(L, 1);
  546. lua_getfield(L, 5, "g");
  547. color.g = cast(ubyte)lua_tointeger(L, -1);
  548. lua_pop(L, 1);
  549. lua_getfield(L, 5, "b");
  550. color.b = cast(ubyte)lua_tointeger(L, -1);
  551. lua_pop(L, 1);
  552. lua_getfield(L, 5, "a");
  553. color.a = cast(ubyte)lua_tointeger(L, -1);
  554. lua_pop(L, 1);
  555. }
  556. DrawTextEx(textFont, text, Vector2(x, y), fontSize, 1.0f, color);
  557. return 0;
  558. }
  559. extern (C) nothrow int luaL_measureTextX(lua_State *L) {
  560. lua_pushinteger(L, cast(int)MeasureTextEx(textFont, luaL_checkstring(L, 1), cast(int)luaL_checkinteger(L, 2), 1.0f).x);
  561. return 1;
  562. }
  563. extern (C) nothrow int luaL_measureTextY(lua_State *L) {
  564. lua_pushinteger(L, cast(int)MeasureTextEx(textFont, luaL_checkstring(L, 1), cast(int)luaL_checkinteger(L, 2), 1.0f).y);
  565. return 1;
  566. }
  567. /* extended */
  568. extern (C) nothrow int luaL_drawTextureEx(lua_State *L) {
  569. Texture2D* texture = cast(Texture2D*)luaL_checkudata(L, 1, "Texture");
  570. float x = luaL_checknumber(L, 2);
  571. float y = luaL_checknumber(L, 3);
  572. float rotation = luaL_optnumber(L, 4, 0);
  573. float scale = luaL_optnumber(L, 5, 1);
  574. Color color = Colors.WHITE;
  575. if (lua_istable(L, 6)) {
  576. lua_getfield(L, 6, "r");
  577. color.r = cast(ubyte)lua_tointeger(L, -1);
  578. lua_pop(L, 1);
  579. lua_getfield(L, 6, "g");
  580. color.g = cast(ubyte)lua_tointeger(L, -1);
  581. lua_pop(L, 1);
  582. lua_getfield(L, 6, "b");
  583. color.b = cast(ubyte)lua_tointeger(L, -1);
  584. lua_pop(L, 1);
  585. lua_getfield(L, 6, "a");
  586. color.a = cast(ubyte)lua_tointeger(L, -1);
  587. lua_pop(L, 1);
  588. }
  589. DrawTextureEx(*texture, Vector2(x, y), rotation, scale*variables.scale, color);
  590. return 0;
  591. }
  592. /* Register functions */
  593. extern (C) nothrow void luaL_loader(lua_State* L)
  594. {
  595. lua_register(L, "dialogBox", &luaL_dialogBox);
  596. lua_register(L, "isDialogExecuted", &luaL_isDialogExecuted);
  597. lua_register(L, "getAnswerValue", &luaL_getAnswerValue);
  598. lua_register(L, "loadAnimationUI", &luaL_loadUIAnimation);
  599. lua_register(L, "playAnimationUI", &luaL_playUIAnimation);
  600. lua_register(L, "stopAnimationUI", &luaL_stopUIAnimation);
  601. lua_register(L, "unloadAnimationUI", &luaL_unloadUIAnimation);
  602. lua_register(L, "setDialogBoxBackground", &luaL_setDialogBoxBackground);
  603. lua_register(L, "setDialogEndIndicator", &luaL_setDialogBoxEndIndicatorTexture);
  604. lua_register(L, "moveCamera", &luaL_moveCamera);
  605. lua_register(L, "restoreCamera", &luaL_restoreCamera);
  606. lua_register(L, "isCameraMoving", &luaL_isCameraMoving);
  607. lua_register(L, "playVideo", &luaL_playVideo);
  608. lua_register(L, "loadMusic", &luaL_loadMusic);
  609. lua_register(L, "playMusic", &luaL_playMusic);
  610. lua_register(L, "stopMusic", &luaL_stopMusic);
  611. lua_register(L, "unloadMusic", &luaL_unloadMusic);
  612. lua_register(L, "playSfx", &luaL_playSfx);
  613. lua_register(L, "stopSfx", &luaL_stopSfx);
  614. lua_register(L, "loadCharacter", &luaL_loadCharacter);
  615. lua_register(L, "drawCharacter", &luaL_drawCharacter);
  616. lua_register(L, "stopDrawCharacter", &luaL_stopDrawCharacter);
  617. lua_register(L, "unloadCharacter", &luaL_unloadCharacter);
  618. lua_register(L, "loadBackground", &luaL_loadBackground);
  619. lua_register(L, "drawBackground", &luaL_drawBackground);
  620. lua_register(L, "stopDrawBackground", &luaL_stopDrawBackground);
  621. lua_register(L, "unloadBackground", &luaL_unloadBackground);
  622. lua_register(L, "loadScript", &luaL_loadScript);
  623. lua_register(L, "getScreenHeight", &luaL_getScreenHeight);
  624. lua_register(L, "getScreenWidth", &luaL_getScreenWidth);
  625. lua_register(L, "isKeyPressed", &luaL_isKeyPressed);
  626. lua_register(L, "isMouseButtonPressed", &luaL_isMouseButtonPressed);
  627. lua_register(L, "setGameState", &luaL_setGameState);
  628. //raylib direct bindings
  629. lua_register(L, "unloadFont", &luaL_unloadFont);
  630. lua_register(L, "loadFont", &luaL_loadFont);
  631. lua_register(L, "getTime", &luaL_getTime);
  632. lua_register(L, "getDeltaTime", &luaL_getDeltaTime);
  633. lua_register(L, "loadTexture", &luaL_loadTexture);
  634. lua_register(L, "drawTexture", &luaL_drawTexture);
  635. lua_register(L, "drawTextureEx", &luaL_drawTextureEx);
  636. lua_register(L, "unloadTexture", &luaL_unloadTexture);
  637. lua_register(L, "drawText", &luaL_drawText);
  638. lua_register(L, "measureTextX", &luaL_measureTextX);
  639. lua_register(L, "measureTextY", &luaL_measureTextY);
  640. lua_register(L, "getTextureWidth", &luaL_getTextureWidth);
  641. lua_register(L, "getTextureHeight", &luaL_getTextureHeight);
  642. //compat
  643. lua_register(L, "setFont", &luaL_loadFont);
  644. debugWriteln("strict mode enabled");
  645. const char* strict_lua =
  646. `local mt = {
  647. __index = function(_, name)
  648. error("attempt to call undefined function: " .. tostring(name), 2)
  649. end
  650. }
  651. setmetatable(_G, mt)`;
  652. if (luaL_dostring(L, strict_lua) != LUA_OK) {
  653. debugWriteln("Failed to set strict mode: ", to!string(lua_tostring(L, -1)));
  654. lua_pop(L, 1); // Pop the error message
  655. }
  656. }
  657. int luaInit(string luaExec)
  658. {
  659. debugWriteln("loading Lua");
  660. L = luaL_newstate();
  661. luaL_openlibs(L);
  662. luaL_loader(L);
  663. debugWriteln("Executing next Lua file: ", luaExec);
  664. if (std.file.exists(luaExec) == false) {
  665. debugWriteln("Script file not found! Exiting.");
  666. return EngineExitCodes.EXIT_FILE_NOT_FOUND;
  667. }
  668. if (luaL_dofile(L, toStringz(luaExec)) != LUA_OK) {
  669. debugWriteln("Lua error: ", to!string(lua_tostring(L, -1)));
  670. return EngineExitCodes.EXIT_SCRIPT_ERROR;
  671. }
  672. return EngineExitCodes.EXIT_OK;
  673. }
  674. void luaEventLoop()
  675. {
  676. lua_getglobal(L, "EventLoop");
  677. if (lua_pcall(L, 0, 0, 0) != LUA_OK)
  678. {
  679. debug debugWriteln("Error in EventLoop: ", to!string(lua_tostring(L, -1)));
  680. }
  681. lua_pop(L, 0);
  682. }