textures.d 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. module scripts.lua.textures;
  2. import bindbc.lua;
  3. import raylib;
  4. import variables;
  5. import graphics.effects;
  6. import std.conv;
  7. import system.debugwriteln;
  8. import system.config;
  9. import std.string;
  10. import std.algorithm;
  11. import system.init;
  12. import graphics.playback;
  13. import std.file;
  14. import graphics.collision;
  15. import audio.sfx;
  16. import system.hpf;
  17. extern (C) nothrow int luaL_loadTexture(lua_State *L) {
  18. int textureIndex = cast(int)luaL_checkinteger(L, 1);
  19. char* textureFilename = cast(char*)luaL_checkstring(L, 2);
  20. if (textureIndex >= textures.length) {
  21. textures.length = textureIndex + 1;
  22. }
  23. if (textures.length < textures.length) {
  24. textures.length = textures.length;
  25. }
  26. if (textureIndex < textures.length && textures[textureIndex].id != 0) {
  27. UnloadTexture(textures[textureIndex]);
  28. }
  29. textures[textureIndex] = LoadTexture(textureFilename);
  30. return 0;
  31. }
  32. extern (C) nothrow int luaL_loadTextureFromHPF(lua_State *L) {
  33. try {
  34. int indexOfArchive = cast(int)luaL_checkinteger(L, 1);
  35. int textureIndexInArchive = cast(int)luaL_checkinteger(L, 2);
  36. int textureIndex = cast(int)luaL_checkinteger(L, 3);
  37. ubyte[] textureFile = loadFileFromHPF(archivesNames[indexOfArchive], parsedChunks[indexOfArchive], textureIndexInArchive);
  38. if (textureIndex >= textures.length) {
  39. textures.length = textureIndex + 1;
  40. }
  41. if (textures.length < textures.length) {
  42. textures.length = textures.length;
  43. }
  44. if (textureIndex < textures.length && textures[textureIndex].id != 0) {
  45. UnloadTexture(textures[textureIndex]);
  46. }
  47. Image textureImage = LoadImageFromMemory(
  48. ".png", cast(const(ubyte)*)textureFile, cast(int)textureFile.length
  49. );
  50. textures[textureIndex] = LoadTextureFromImage(textureImage);
  51. UnloadImage(textureImage);
  52. } catch (Exception e) {
  53. debugWriteln("exception in luaL_loadTextureFromHPF");
  54. }
  55. return 0;
  56. }
  57. extern (C) nothrow int luaL_unloadTexture(lua_State *L) {
  58. int textureIndex = cast(int)luaL_checkinteger(L, 1);
  59. UnloadTexture(textures[textureIndex]);
  60. return 0;
  61. }
  62. extern (C) nothrow int luaL_drawTexture(lua_State *L) {
  63. int textureIndex = cast(int)luaL_checkinteger(L, 1);
  64. int x = cast(int)luaL_checkinteger(L, 2);
  65. int y = cast(int)luaL_checkinteger(L, 3);
  66. float scale = luaL_checknumber(L, 4)*variables.scale;
  67. Color color = Colors.WHITE;
  68. if (lua_gettop(L) >= 5 && lua_istable(L, 5)) {
  69. lua_getfield(L, 5, "r");
  70. color.r = cast(ubyte)lua_tointeger(L, -1);
  71. lua_pop(L, 1);
  72. lua_getfield(L, 5, "g");
  73. color.g = cast(ubyte)lua_tointeger(L, -1);
  74. lua_pop(L, 1);
  75. lua_getfield(L, 5, "b");
  76. color.b = cast(ubyte)lua_tointeger(L, -1);
  77. lua_pop(L, 1);
  78. lua_getfield(L, 5, "a");
  79. color.a = cast(ubyte)lua_tointeger(L, -1);
  80. lua_pop(L, 1);
  81. }
  82. DrawTextureEx(textures[textureIndex], Vector2(x, y), 0.0f, scale, color);
  83. return 0;
  84. }
  85. extern (C) nothrow int luaL_getTextureWidth(lua_State *L) {
  86. int textureIndex = cast(int)luaL_checkinteger(L, 1);
  87. float scalel = textures[textureIndex].width*variables.scale;
  88. //debugWriteln(scalel);
  89. lua_pushnumber(L, scalel);
  90. return 1;
  91. }
  92. extern (C) nothrow int luaL_getTextureHeight(lua_State *L) {
  93. int textureIndex = cast(int)luaL_checkinteger(L, 1);
  94. float scalel = textures[textureIndex].height*variables.scale;
  95. //debugWriteln(scalel);
  96. lua_pushnumber(L, scalel);
  97. return 1;
  98. }
  99. extern (C) nothrow int luaL_drawTextureEx(lua_State *L) {
  100. int textureIndex = cast(int)luaL_checkinteger(L, 1);
  101. float x = luaL_checknumber(L, 2);
  102. float y = luaL_checknumber(L, 3);
  103. float rotation = luaL_optnumber(L, 4, 0);
  104. float scale = luaL_optnumber(L, 5, 1);
  105. Color color = Colors.WHITE;
  106. if (lua_istable(L, 6)) {
  107. lua_getfield(L, 6, "r");
  108. color.r = cast(ubyte)lua_tointeger(L, -1);
  109. lua_pop(L, 1);
  110. lua_getfield(L, 6, "g");
  111. color.g = cast(ubyte)lua_tointeger(L, -1);
  112. lua_pop(L, 1);
  113. lua_getfield(L, 6, "b");
  114. color.b = cast(ubyte)lua_tointeger(L, -1);
  115. lua_pop(L, 1);
  116. lua_getfield(L, 6, "a");
  117. color.a = cast(ubyte)lua_tointeger(L, -1);
  118. lua_pop(L, 1);
  119. }
  120. DrawTextureEx(textures[textureIndex], Vector2(x, y), rotation, scale*variables.scale, color);
  121. return 0;
  122. }