wrap_SpriteBatch.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * Copyright (c) 2006-2016 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 "graphics/wrap_Texture.h"
  25. // C++
  26. #include <typeinfo>
  27. namespace love
  28. {
  29. namespace graphics
  30. {
  31. namespace opengl
  32. {
  33. SpriteBatch *luax_checkspritebatch(lua_State *L, int idx)
  34. {
  35. return luax_checktype<SpriteBatch>(L, idx, GRAPHICS_SPRITE_BATCH_ID);
  36. }
  37. static inline int w_SpriteBatch_add_or_set(lua_State *L, SpriteBatch *t, int startidx, int index)
  38. {
  39. Quad *quad = nullptr;
  40. if (luax_istype(L, startidx, GRAPHICS_QUAD_ID))
  41. {
  42. quad = luax_totype<Quad>(L, startidx, GRAPHICS_QUAD_ID);
  43. startidx++;
  44. }
  45. else if (lua_isnil(L, startidx) && !lua_isnoneornil(L, startidx + 1))
  46. return luax_typerror(L, startidx, "Quad");
  47. float x = (float) luaL_optnumber(L, startidx + 0, 0.0);
  48. float y = (float) luaL_optnumber(L, startidx + 1, 0.0);
  49. float a = (float) luaL_optnumber(L, startidx + 2, 0.0);
  50. float sx = (float) luaL_optnumber(L, startidx + 3, 1.0);
  51. float sy = (float) luaL_optnumber(L, startidx + 4, sx);
  52. float ox = (float) luaL_optnumber(L, startidx + 5, 0.0);
  53. float oy = (float) luaL_optnumber(L, startidx + 6, 0.0);
  54. float kx = (float) luaL_optnumber(L, startidx + 7, 0.0);
  55. float ky = (float) luaL_optnumber(L, startidx + 8, 0.0);
  56. luax_catchexcept(L, [&]() {
  57. if (quad)
  58. index = t->addq(quad, x, y, a, sx, sy, ox, oy, kx, ky, index);
  59. else
  60. index = t->add(x, y, a, sx, sy, ox, oy, kx, ky, index);
  61. });
  62. return index;
  63. }
  64. int w_SpriteBatch_add(lua_State *L)
  65. {
  66. SpriteBatch *t = luax_checkspritebatch(L, 1);
  67. int index = w_SpriteBatch_add_or_set(L, t, 2, -1);
  68. lua_pushinteger(L, index + 1);
  69. return 1;
  70. }
  71. int w_SpriteBatch_set(lua_State *L)
  72. {
  73. SpriteBatch *t = luax_checkspritebatch(L, 1);
  74. int index = (int) luaL_checknumber(L, 2) - 1;
  75. w_SpriteBatch_add_or_set(L, t, 3, index);
  76. return 0;
  77. }
  78. int w_SpriteBatch_clear(lua_State *L)
  79. {
  80. SpriteBatch *t = luax_checkspritebatch(L, 1);
  81. t->clear();
  82. return 0;
  83. }
  84. int w_SpriteBatch_flush(lua_State *L)
  85. {
  86. SpriteBatch *t = luax_checkspritebatch(L, 1);
  87. t->flush();
  88. return 0;
  89. }
  90. int w_SpriteBatch_setTexture(lua_State *L)
  91. {
  92. SpriteBatch *t = luax_checkspritebatch(L, 1);
  93. Texture *tex = luax_checktexture(L, 2);
  94. t->setTexture(tex);
  95. return 0;
  96. }
  97. int w_SpriteBatch_getTexture(lua_State *L)
  98. {
  99. SpriteBatch *t = luax_checkspritebatch(L, 1);
  100. Texture *tex = t->getTexture();
  101. // FIXME: big hack right here.
  102. if (typeid(*tex) == typeid(Image))
  103. luax_pushtype(L, GRAPHICS_IMAGE_ID, tex);
  104. else if (typeid(*tex) == typeid(Canvas))
  105. luax_pushtype(L, GRAPHICS_CANVAS_ID, tex);
  106. else
  107. return luaL_error(L, "Unable to determine texture type.");
  108. return 1;
  109. }
  110. int w_SpriteBatch_setColor(lua_State *L)
  111. {
  112. SpriteBatch *t = luax_checkspritebatch(L, 1);
  113. Color c;
  114. if (lua_gettop(L) <= 1)
  115. {
  116. t->setColor();
  117. return 0;
  118. }
  119. else if (lua_istable(L, 2))
  120. {
  121. for (int i = 1; i <= 4; i++)
  122. lua_rawgeti(L, 2, i);
  123. c.r = (unsigned char) (luaL_checknumber(L, -4) * 255.0);
  124. c.g = (unsigned char) (luaL_checknumber(L, -3) * 255.0);
  125. c.b = (unsigned char) (luaL_checknumber(L, -2) * 255.0);
  126. c.a = (unsigned char) (luaL_optnumber(L, -1, 1.0) * 255.0);
  127. lua_pop(L, 4);
  128. }
  129. else
  130. {
  131. c.r = (unsigned char) (luaL_checknumber(L, 2) * 255.0);
  132. c.g = (unsigned char) (luaL_checknumber(L, 3) * 255.0);
  133. c.b = (unsigned char) (luaL_checknumber(L, 4) * 255.0);
  134. c.a = (unsigned char) (luaL_optnumber(L, 5, 1.0) * 255.0);
  135. }
  136. t->setColor(c);
  137. return 0;
  138. }
  139. int w_SpriteBatch_getColor(lua_State *L)
  140. {
  141. SpriteBatch *t = luax_checkspritebatch(L, 1);
  142. const Color *color = t->getColor();
  143. // getColor returns null if no color is set.
  144. if (!color)
  145. return 0;
  146. lua_pushnumber(L, (lua_Number) color->r / 255.0);
  147. lua_pushnumber(L, (lua_Number) color->g / 255.0);
  148. lua_pushnumber(L, (lua_Number) color->b / 255.0);
  149. lua_pushnumber(L, (lua_Number) color->a / 255.0);
  150. return 4;
  151. }
  152. int w_SpriteBatch_getCount(lua_State *L)
  153. {
  154. SpriteBatch *t = luax_checkspritebatch(L, 1);
  155. lua_pushinteger(L, t->getCount());
  156. return 1;
  157. }
  158. int w_SpriteBatch_getBufferSize(lua_State *L)
  159. {
  160. SpriteBatch *t = luax_checkspritebatch(L, 1);
  161. lua_pushinteger(L, t->getBufferSize());
  162. return 1;
  163. }
  164. int w_SpriteBatch_attachAttribute(lua_State *L)
  165. {
  166. SpriteBatch *t = luax_checkspritebatch(L, 1);
  167. const char *name = luaL_checkstring(L, 2);
  168. Mesh *m = luax_checktype<Mesh>(L, 3, GRAPHICS_MESH_ID);
  169. luax_catchexcept(L, [&](){ t->attachAttribute(name, m); });
  170. return 0;
  171. }
  172. static const luaL_Reg w_SpriteBatch_functions[] =
  173. {
  174. { "add", w_SpriteBatch_add },
  175. { "set", w_SpriteBatch_set },
  176. { "clear", w_SpriteBatch_clear },
  177. { "flush", w_SpriteBatch_flush },
  178. { "setTexture", w_SpriteBatch_setTexture },
  179. { "getTexture", w_SpriteBatch_getTexture },
  180. { "setColor", w_SpriteBatch_setColor },
  181. { "getColor", w_SpriteBatch_getColor },
  182. { "getCount", w_SpriteBatch_getCount },
  183. { "getBufferSize", w_SpriteBatch_getBufferSize },
  184. { "attachAttribute", w_SpriteBatch_attachAttribute },
  185. { 0, 0 }
  186. };
  187. extern "C" int luaopen_spritebatch(lua_State *L)
  188. {
  189. return luax_register_type(L, GRAPHICS_SPRITE_BATCH_ID, "SpriteBatch", w_SpriteBatch_functions, nullptr);
  190. }
  191. } // opengl
  192. } // graphics
  193. } // love