wrap_SpriteBatch.cpp 6.6 KB

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