wrap_SpriteBatch.cpp 6.6 KB

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