wrap_Transform.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * Copyright (c) 2006-2023 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "wrap_Transform.h"
  21. namespace love
  22. {
  23. namespace math
  24. {
  25. Transform *luax_checktransform(lua_State *L, int idx)
  26. {
  27. return luax_checktype<Transform>(L, idx, Transform::type);
  28. }
  29. int w_Transform_clone(lua_State *L)
  30. {
  31. Transform *t = luax_checktransform(L, 1);
  32. Transform *newtransform = t->clone();
  33. luax_pushtype(L, newtransform);
  34. newtransform->release();
  35. return 1;
  36. }
  37. int w_Transform_inverse(lua_State *L)
  38. {
  39. Transform *t = luax_checktransform(L, 1);
  40. Transform *inverse = t->inverse();
  41. luax_pushtype(L, inverse);
  42. inverse->release();
  43. return 1;
  44. }
  45. int w_Transform_apply(lua_State *L)
  46. {
  47. Transform *t = luax_checktransform(L, 1);
  48. Transform *other = luax_checktransform(L, 2);
  49. t->apply(other);
  50. lua_pushvalue(L, 1);
  51. return 1;
  52. }
  53. int w_Transform_isAffine2DTransform(lua_State *L)
  54. {
  55. Transform *t = luax_checktransform(L, 1);
  56. luax_pushboolean(L, t->getMatrix().isAffine2DTransform());
  57. return 1;
  58. }
  59. int w_Transform_translate(lua_State *L)
  60. {
  61. Transform *t = luax_checktransform(L, 1);
  62. float x = (float) luaL_checknumber(L, 2);
  63. float y = (float) luaL_checknumber(L, 3);
  64. t->translate(x, y);
  65. lua_pushvalue(L, 1);
  66. return 1;
  67. }
  68. int w_Transform_rotate(lua_State *L)
  69. {
  70. Transform *t = luax_checktransform(L, 1);
  71. float angle = (float) luaL_checknumber(L, 2);
  72. t->rotate(angle);
  73. lua_pushvalue(L, 1);
  74. return 1;
  75. }
  76. int w_Transform_scale(lua_State *L)
  77. {
  78. Transform *t = luax_checktransform(L, 1);
  79. float sx = (float) luaL_checknumber(L, 2);
  80. float sy = (float) luaL_optnumber(L, 3, sx);
  81. t->scale(sx, sy);
  82. lua_pushvalue(L, 1);
  83. return 1;
  84. }
  85. int w_Transform_shear(lua_State *L)
  86. {
  87. Transform *t = luax_checktransform(L, 1);
  88. float kx = (float) luaL_checknumber(L, 2);
  89. float ky = (float) luaL_checknumber(L, 3);
  90. t->shear(kx, ky);
  91. lua_pushvalue(L, 1);
  92. return 1;
  93. }
  94. int w_Transform_reset(lua_State *L)
  95. {
  96. Transform *t = luax_checktransform(L, 1);
  97. t->reset();
  98. lua_pushvalue(L, 1);
  99. return 1;
  100. }
  101. int w_Transform_setTransformation(lua_State *L)
  102. {
  103. Transform *t = luax_checktransform(L, 1);
  104. float x = (float) luaL_optnumber(L, 2, 0.0);
  105. float y = (float) luaL_optnumber(L, 3, 0.0);
  106. float a = (float) luaL_optnumber(L, 4, 0.0);
  107. float sx = (float) luaL_optnumber(L, 5, 1.0);
  108. float sy = (float) luaL_optnumber(L, 6, sx);
  109. float ox = (float) luaL_optnumber(L, 7, 0.0);
  110. float oy = (float) luaL_optnumber(L, 8, 0.0);
  111. float kx = (float) luaL_optnumber(L, 9, 0.0);
  112. float ky = (float) luaL_optnumber(L, 10, 0.0);
  113. t->setTransformation(x, y, a, sx, sy, ox, oy, kx, ky);
  114. lua_pushvalue(L, 1);
  115. return 1;
  116. }
  117. int w_Transform_setMatrix(lua_State *L)
  118. {
  119. Transform *t = luax_checktransform(L, 1);
  120. bool columnmajor = false;
  121. int idx = 2;
  122. if (lua_type(L, idx) == LUA_TSTRING)
  123. {
  124. const char *layoutstr = lua_tostring(L, idx);
  125. Transform::MatrixLayout layout;
  126. if (!Transform::getConstant(layoutstr, layout))
  127. return luax_enumerror(L, "matrix layout", Transform::getConstants(layout), layoutstr);
  128. columnmajor = (layout == Transform::MATRIX_COLUMN_MAJOR);
  129. idx++;
  130. }
  131. float elements[16];
  132. if (lua_istable(L, idx))
  133. {
  134. lua_rawgeti(L, idx, 1);
  135. bool tableoftables = lua_istable(L, -1);
  136. lua_pop(L, 1);
  137. if (tableoftables)
  138. {
  139. if (columnmajor)
  140. {
  141. for (int column = 0; column < 4; column++)
  142. {
  143. lua_rawgeti(L, idx, column + 1);
  144. for (int row = 0; row < 4; row++)
  145. {
  146. lua_rawgeti(L, -(row + 1), row + 1);
  147. elements[column * 4 + row] = (float) luaL_checknumber(L, -1);
  148. }
  149. lua_pop(L, 4 + 1);
  150. }
  151. }
  152. else
  153. {
  154. for (int row = 0; row < 4; row++)
  155. {
  156. lua_rawgeti(L, idx, row + 1);
  157. for (int column = 0; column < 4; column++)
  158. {
  159. // The table has the matrix elements laid out in row-major
  160. // order, but we need to store them column-major in memory.
  161. lua_rawgeti(L, -(column + 1), column + 1);
  162. elements[column * 4 + row] = (float) luaL_checknumber(L, -1);
  163. }
  164. lua_pop(L, 4 + 1);
  165. }
  166. }
  167. }
  168. else
  169. {
  170. if (columnmajor)
  171. {
  172. for (int column = 0; column < 4; column++)
  173. {
  174. for (int row = 0; row < 4; row++)
  175. {
  176. lua_rawgeti(L, idx, column * 4 + row + 1);
  177. elements[column * 4 + row] = (float) luaL_checknumber(L, -1);
  178. }
  179. }
  180. }
  181. else
  182. {
  183. for (int column = 0; column < 4; column++)
  184. {
  185. for (int row = 0; row < 4; row++)
  186. {
  187. // The table has the matrix elements laid out in row-major
  188. // order, but we need to store them column-major in memory.
  189. lua_rawgeti(L, idx, row * 4 + column + 1);
  190. elements[column * 4 + row] = (float) luaL_checknumber(L, -1);
  191. }
  192. }
  193. }
  194. lua_pop(L, 16);
  195. }
  196. }
  197. else
  198. {
  199. if (columnmajor)
  200. {
  201. for (int i = 0; i < 16; i++)
  202. elements[i] = (float) luaL_checknumber(L, idx + i);
  203. }
  204. else
  205. {
  206. for (int column = 0; column < 4; column++)
  207. {
  208. for (int row = 0; row < 4; row++)
  209. elements[column * 4 + row] = (float) luaL_checknumber(L, row * 4 + column + idx);
  210. }
  211. }
  212. }
  213. t->setMatrix(Matrix4(elements));
  214. lua_pushvalue(L, 1);
  215. return 1;
  216. }
  217. int w_Transform_getMatrix(lua_State *L)
  218. {
  219. Transform *t = luax_checktransform(L, 1);
  220. const float *elements = t->getMatrix().getElements();
  221. // We want to push elements in row-major order, but they're stored column-
  222. // major.
  223. for (int row = 0; row < 4; row++)
  224. {
  225. for (int column = 0; column < 4; column++)
  226. lua_pushnumber(L, elements[column * 4 + row]);
  227. }
  228. return 16;
  229. }
  230. int w_Transform_transformPoint(lua_State *L)
  231. {
  232. Transform *t = luax_checktransform(L, 1);
  233. love::Vector2 p;
  234. p.x = (float) luaL_checknumber(L, 2);
  235. p.y = (float) luaL_checknumber(L, 3);
  236. p = t->transformPoint(p);
  237. lua_pushnumber(L, p.x);
  238. lua_pushnumber(L, p.y);
  239. return 2;
  240. }
  241. int w_Transform_inverseTransformPoint(lua_State *L)
  242. {
  243. Transform *t = luax_checktransform(L, 1);
  244. love::Vector2 p;
  245. p.x = (float) luaL_checknumber(L, 2);
  246. p.y = (float) luaL_checknumber(L, 3);
  247. p = t->inverseTransformPoint(p);
  248. lua_pushnumber(L, p.x);
  249. lua_pushnumber(L, p.y);
  250. return 2;
  251. }
  252. int w_Transform__mul(lua_State *L)
  253. {
  254. Transform *t1 = luax_checktransform(L, 1);
  255. Transform *t2 = luax_checktransform(L, 2);
  256. Transform *t3 = new Transform(t1->getMatrix() * t2->getMatrix());
  257. luax_pushtype(L, t3);
  258. t3->release();
  259. return 1;
  260. }
  261. static const luaL_Reg functions[] =
  262. {
  263. { "clone", w_Transform_clone },
  264. { "inverse", w_Transform_inverse },
  265. { "apply", w_Transform_apply },
  266. { "isAffine2DTransform", w_Transform_isAffine2DTransform },
  267. { "translate", w_Transform_translate },
  268. { "rotate", w_Transform_rotate },
  269. { "scale", w_Transform_scale },
  270. { "shear", w_Transform_shear },
  271. { "reset", w_Transform_reset },
  272. { "setTransformation", w_Transform_setTransformation },
  273. { "setMatrix", w_Transform_setMatrix },
  274. { "getMatrix", w_Transform_getMatrix },
  275. { "transformPoint", w_Transform_transformPoint },
  276. { "inverseTransformPoint", w_Transform_inverseTransformPoint },
  277. { "__mul", w_Transform__mul },
  278. { 0, 0 }
  279. };
  280. extern "C" int luaopen_transform(lua_State *L)
  281. {
  282. return luax_register_type(L, &Transform::type, functions, nullptr);
  283. }
  284. } // math
  285. } // love