wrap_SpriteBatch.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. // LOVE
  21. #include "wrap_SpriteBatch.h"
  22. #include "Image.h"
  23. #include "Canvas.h"
  24. #include "wrap_Texture.h"
  25. namespace love
  26. {
  27. namespace graphics
  28. {
  29. SpriteBatch *luax_checkspritebatch(lua_State *L, int idx)
  30. {
  31. return luax_checktype<SpriteBatch>(L, idx);
  32. }
  33. static inline int w_SpriteBatch_add_or_set(lua_State *L, SpriteBatch *t, int startidx, int index)
  34. {
  35. Quad *quad = nullptr;
  36. if (luax_istype(L, startidx, Quad::type))
  37. {
  38. quad = luax_totype<Quad>(L, startidx);
  39. startidx++;
  40. }
  41. else if (lua_isnil(L, startidx) && !lua_isnoneornil(L, startidx + 1))
  42. return luax_typerror(L, startidx, "Quad");
  43. luax_checkstandardtransform(L, startidx, [&](const Matrix4 &m)
  44. {
  45. luax_catchexcept(L, [&]()
  46. {
  47. if (quad)
  48. index = t->add(quad, m, index);
  49. else
  50. index = t->add(m, index);
  51. });
  52. });
  53. return index;
  54. }
  55. static int w_SpriteBatch_addLayer_or_setLayer(lua_State *L, SpriteBatch *t, int startidx, int index)
  56. {
  57. Quad *quad = nullptr;
  58. int layer = (int) luaL_checkinteger(L, startidx) - 1;
  59. startidx++;
  60. if (luax_istype(L, startidx, Quad::type))
  61. {
  62. quad = luax_totype<Quad>(L, startidx);
  63. startidx++;
  64. }
  65. else if (lua_isnil(L, startidx) && !lua_isnoneornil(L, startidx + 1))
  66. return luax_typerror(L, startidx, "Quad");
  67. luax_checkstandardtransform(L, startidx, [&](const Matrix4 &m)
  68. {
  69. luax_catchexcept(L, [&]()
  70. {
  71. if (quad)
  72. index = t->addLayer(layer, quad, m, index);
  73. else
  74. index = t->addLayer(layer, m, index);
  75. });
  76. });
  77. return index;
  78. }
  79. int w_SpriteBatch_add(lua_State *L)
  80. {
  81. SpriteBatch *t = luax_checkspritebatch(L, 1);
  82. int index = w_SpriteBatch_add_or_set(L, t, 2, -1);
  83. lua_pushinteger(L, index + 1);
  84. return 1;
  85. }
  86. int w_SpriteBatch_set(lua_State *L)
  87. {
  88. SpriteBatch *t = luax_checkspritebatch(L, 1);
  89. int index = (int) luaL_checkinteger(L, 2) - 1;
  90. w_SpriteBatch_add_or_set(L, t, 3, index);
  91. return 0;
  92. }
  93. int w_SpriteBatch_addLayer(lua_State *L)
  94. {
  95. SpriteBatch *t = luax_checkspritebatch(L, 1);
  96. int index = w_SpriteBatch_addLayer_or_setLayer(L, t, 2, -1);
  97. lua_pushinteger(L, index + 1);
  98. return 1;
  99. }
  100. int w_SpriteBatch_setLayer(lua_State *L)
  101. {
  102. SpriteBatch *t = luax_checkspritebatch(L, 1);
  103. int index = (int) luaL_checkinteger(L, 2) - 1;
  104. w_SpriteBatch_addLayer_or_setLayer(L, t, 3, index);
  105. return 0;
  106. }
  107. int w_SpriteBatch_clear(lua_State *L)
  108. {
  109. SpriteBatch *t = luax_checkspritebatch(L, 1);
  110. t->clear();
  111. return 0;
  112. }
  113. int w_SpriteBatch_flush(lua_State *L)
  114. {
  115. SpriteBatch *t = luax_checkspritebatch(L, 1);
  116. t->flush();
  117. return 0;
  118. }
  119. int w_SpriteBatch_setTexture(lua_State *L)
  120. {
  121. SpriteBatch *t = luax_checkspritebatch(L, 1);
  122. Texture *tex = luax_checktexture(L, 2);
  123. luax_catchexcept(L, [&](){ t->setTexture(tex); });
  124. return 0;
  125. }
  126. int w_SpriteBatch_getTexture(lua_State *L)
  127. {
  128. SpriteBatch *t = luax_checkspritebatch(L, 1);
  129. Texture *tex = t->getTexture();
  130. // FIXME: big hack right here.
  131. if (dynamic_cast<Image *>(tex) != nullptr)
  132. luax_pushtype(L, Image::type, tex);
  133. else if (dynamic_cast<Canvas *>(tex) != nullptr)
  134. luax_pushtype(L, Canvas::type, tex);
  135. else
  136. return luaL_error(L, "Unable to determine texture type.");
  137. return 1;
  138. }
  139. int w_SpriteBatch_setColor(lua_State *L)
  140. {
  141. SpriteBatch *t = luax_checkspritebatch(L, 1);
  142. Colorf c;
  143. if (lua_gettop(L) <= 1)
  144. {
  145. t->setColor();
  146. return 0;
  147. }
  148. else if (lua_istable(L, 2))
  149. {
  150. for (int i = 1; i <= 4; i++)
  151. lua_rawgeti(L, 2, i);
  152. c.r = (float) luaL_checknumber(L, -4);
  153. c.g = (float) luaL_checknumber(L, -3);
  154. c.b = (float) luaL_checknumber(L, -2);
  155. c.a = (float) luaL_optnumber(L, -1, 1.0);
  156. lua_pop(L, 4);
  157. }
  158. else
  159. {
  160. c.r = (float) luaL_checknumber(L, 2);
  161. c.g = (float) luaL_checknumber(L, 3);
  162. c.b = (float) luaL_checknumber(L, 4);
  163. c.a = (float) luaL_optnumber(L, 5, 1.0);
  164. }
  165. t->setColor(c);
  166. return 0;
  167. }
  168. int w_SpriteBatch_getColor(lua_State *L)
  169. {
  170. SpriteBatch *t = luax_checkspritebatch(L, 1);
  171. bool active = false;
  172. Colorf color = t->getColor(active);
  173. // getColor returns null if no color is set.
  174. if (!active)
  175. return 0;
  176. lua_pushnumber(L, color.r);
  177. lua_pushnumber(L, color.g);
  178. lua_pushnumber(L, color.b);
  179. lua_pushnumber(L, color.a);
  180. return 4;
  181. }
  182. int w_SpriteBatch_getCount(lua_State *L)
  183. {
  184. SpriteBatch *t = luax_checkspritebatch(L, 1);
  185. lua_pushinteger(L, t->getCount());
  186. return 1;
  187. }
  188. int w_SpriteBatch_getBufferSize(lua_State *L)
  189. {
  190. SpriteBatch *t = luax_checkspritebatch(L, 1);
  191. lua_pushinteger(L, t->getBufferSize());
  192. return 1;
  193. }
  194. int w_SpriteBatch_attachAttribute(lua_State *L)
  195. {
  196. SpriteBatch *t = luax_checkspritebatch(L, 1);
  197. const char *name = luaL_checkstring(L, 2);
  198. Mesh *m = luax_checktype<Mesh>(L, 3);
  199. luax_catchexcept(L, [&](){ t->attachAttribute(name, m); });
  200. return 0;
  201. }
  202. int w_SpriteBatch_setDrawRange(lua_State *L)
  203. {
  204. SpriteBatch *t = luax_checkspritebatch(L, 1);
  205. if (lua_isnoneornil(L, 2))
  206. t->setDrawRange();
  207. else
  208. {
  209. int start = (int) luaL_checkinteger(L, 2) - 1;
  210. int count = (int) luaL_checkinteger(L, 3);
  211. luax_catchexcept(L, [&](){ t->setDrawRange(start, count); });
  212. }
  213. return 0;
  214. }
  215. int w_SpriteBatch_getDrawRange(lua_State *L)
  216. {
  217. SpriteBatch *t = luax_checkspritebatch(L, 1);
  218. int start = 0;
  219. int count = 1;
  220. if (!t->getDrawRange(start, count))
  221. return 0;
  222. lua_pushnumber(L, start + 1);
  223. lua_pushnumber(L, count);
  224. return 2;
  225. }
  226. static const luaL_Reg w_SpriteBatch_functions[] =
  227. {
  228. { "add", w_SpriteBatch_add },
  229. { "set", w_SpriteBatch_set },
  230. { "addLayer", w_SpriteBatch_addLayer },
  231. { "setLayer", w_SpriteBatch_setLayer },
  232. { "clear", w_SpriteBatch_clear },
  233. { "flush", w_SpriteBatch_flush },
  234. { "setTexture", w_SpriteBatch_setTexture },
  235. { "getTexture", w_SpriteBatch_getTexture },
  236. { "setColor", w_SpriteBatch_setColor },
  237. { "getColor", w_SpriteBatch_getColor },
  238. { "getCount", w_SpriteBatch_getCount },
  239. { "getBufferSize", w_SpriteBatch_getBufferSize },
  240. { "attachAttribute", w_SpriteBatch_attachAttribute },
  241. { "setDrawRange", w_SpriteBatch_setDrawRange },
  242. { "getDrawRange", w_SpriteBatch_getDrawRange },
  243. { 0, 0 }
  244. };
  245. extern "C" int luaopen_spritebatch(lua_State *L)
  246. {
  247. return luax_register_type(L, &SpriteBatch::type, w_SpriteBatch_functions, nullptr);
  248. }
  249. } // graphics
  250. } // love