wrap_SpriteBatch.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * Copyright (c) 2006-2013 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 "Image.h"
  21. #include "wrap_SpriteBatch.h"
  22. namespace love
  23. {
  24. namespace graphics
  25. {
  26. namespace opengl
  27. {
  28. SpriteBatch *luax_checkspritebatch(lua_State *L, int idx)
  29. {
  30. return luax_checktype<SpriteBatch>(L, idx, "SpriteBatch", GRAPHICS_SPRITE_BATCH_T);
  31. }
  32. int w_SpriteBatch_add(lua_State *L)
  33. {
  34. SpriteBatch *t = luax_checkspritebatch(L, 1);
  35. float x = (float)luaL_optnumber(L, 2, 0.0f);
  36. float y = (float)luaL_optnumber(L, 3, 0.0f);
  37. float angle = (float)luaL_optnumber(L, 4, 0.0f);
  38. float sx = (float)luaL_optnumber(L, 5, 1.0f);
  39. float sy = (float)luaL_optnumber(L, 6, sx);
  40. float ox = (float)luaL_optnumber(L, 7, 0);
  41. float oy = (float)luaL_optnumber(L, 8, 0);
  42. float kx = (float)luaL_optnumber(L, 9, 0);
  43. float ky = (float)luaL_optnumber(L, 10, 0);
  44. lua_pushnumber(L, t->add(x, y, angle, sx, sy, ox, oy, kx, ky));
  45. return 1;
  46. }
  47. int w_SpriteBatch_addq(lua_State *L)
  48. {
  49. SpriteBatch *t = luax_checkspritebatch(L, 1);
  50. Quad *q = luax_checktype<Quad>(L, 2, "Quad", GRAPHICS_QUAD_T);
  51. float x = (float)luaL_optnumber(L, 3, 0.0f);
  52. float y = (float)luaL_optnumber(L, 4, 0.0f);
  53. float angle = (float)luaL_optnumber(L, 5, 0.0f);
  54. float sx = (float)luaL_optnumber(L, 6, 1.0f);
  55. float sy = (float)luaL_optnumber(L, 7, sx);
  56. float ox = (float)luaL_optnumber(L, 8, 0);
  57. float oy = (float)luaL_optnumber(L, 9, 0);
  58. float kx = (float)luaL_optnumber(L, 10, 0);
  59. float ky = (float)luaL_optnumber(L, 11, 0);
  60. lua_pushnumber(L, t->addq(q, x, y, angle, sx, sy, ox, oy, kx, ky));
  61. return 1;
  62. }
  63. int w_SpriteBatch_set(lua_State *L)
  64. {
  65. SpriteBatch *t = luax_checkspritebatch(L, 1);
  66. int index = luaL_checkinteger(L, 2);
  67. float x = (float)luaL_optnumber(L, 3, 0.0f);
  68. float y = (float)luaL_optnumber(L, 4, 0.0f);
  69. float angle = (float)luaL_optnumber(L, 5, 0.0f);
  70. float sx = (float)luaL_optnumber(L, 6, 1.0f);
  71. float sy = (float)luaL_optnumber(L, 7, sx);
  72. float ox = (float)luaL_optnumber(L, 8, 0);
  73. float oy = (float)luaL_optnumber(L, 9, 0);
  74. float kx = (float)luaL_optnumber(L, 10, 0);
  75. float ky = (float)luaL_optnumber(L, 11, 0);
  76. t->add(x, y, angle, sx, sy, ox, oy, kx, ky, index);
  77. return 0;
  78. }
  79. int w_SpriteBatch_setq(lua_State *L)
  80. {
  81. SpriteBatch *t = luax_checkspritebatch(L, 1);
  82. int index = luaL_checkinteger(L, 2);
  83. Quad *q = luax_checktype<Quad>(L, 3, "Quad", GRAPHICS_QUAD_T);
  84. float x = (float)luaL_optnumber(L, 4, 0.0f);
  85. float y = (float)luaL_optnumber(L, 5, 0.0f);
  86. float angle = (float)luaL_optnumber(L, 6, 0.0f);
  87. float sx = (float)luaL_optnumber(L, 7, 1.0f);
  88. float sy = (float)luaL_optnumber(L, 8, sx);
  89. float ox = (float)luaL_optnumber(L, 9, 0);
  90. float oy = (float)luaL_optnumber(L, 10, 0);
  91. float kx = (float)luaL_optnumber(L, 11, 0);
  92. float ky = (float)luaL_optnumber(L, 12, 0);
  93. t->addq(q, x, y, angle, sx, sy, ox, oy, kx, ky, index);
  94. return 0;
  95. }
  96. int w_SpriteBatch_clear(lua_State *L)
  97. {
  98. SpriteBatch *t = luax_checkspritebatch(L, 1);
  99. t->clear();
  100. return 0;
  101. }
  102. int w_SpriteBatch_bind(lua_State *L)
  103. {
  104. SpriteBatch *t = luax_checkspritebatch(L, 1);
  105. try
  106. {
  107. t->lock();
  108. }
  109. catch (love::Exception &e)
  110. {
  111. return luaL_error(L, "%s", e.what());
  112. }
  113. return 0;
  114. }
  115. int w_SpriteBatch_unbind(lua_State *L)
  116. {
  117. SpriteBatch *t = luax_checkspritebatch(L, 1);
  118. t->unlock();
  119. return 0;
  120. }
  121. int w_SpriteBatch_setImage(lua_State *L)
  122. {
  123. SpriteBatch *t = luax_checkspritebatch(L, 1);
  124. Image *image = luax_checktype<Image>(L, 2, "Image", GRAPHICS_IMAGE_T);
  125. t->setImage(image);
  126. return 0;
  127. }
  128. int w_SpriteBatch_getImage(lua_State *L)
  129. {
  130. SpriteBatch *t = luax_checkspritebatch(L, 1);
  131. Image *image = t->getImage();
  132. image->retain();
  133. luax_newtype(L, "Image", GRAPHICS_IMAGE_T, (void *)image);
  134. return 1;
  135. }
  136. int w_SpriteBatch_setColor(lua_State *L)
  137. {
  138. SpriteBatch *t = luax_checkspritebatch(L, 1);
  139. Color c;
  140. if (lua_gettop(L) <= 1)
  141. {
  142. t->setColor();
  143. return 0;
  144. }
  145. else if (lua_istable(L, 2))
  146. {
  147. for (int i = 1; i <= 4; i++)
  148. lua_rawgeti(L, 2, i);
  149. c.r = (unsigned char) luaL_checkint(L, -4);
  150. c.g = (unsigned char) luaL_checkint(L, -3);
  151. c.b = (unsigned char) luaL_checkint(L, -2);
  152. c.a = (unsigned char) luaL_optint(L, -1, 255);
  153. lua_pop(L, 4);
  154. }
  155. else
  156. {
  157. c.r = (unsigned char)luaL_checkint(L, 2);
  158. c.g = (unsigned char)luaL_checkint(L, 3);
  159. c.b = (unsigned char)luaL_checkint(L, 4);
  160. c.a = (unsigned char)luaL_optint(L, 5, 255);
  161. }
  162. t->setColor(c);
  163. return 0;
  164. }
  165. int w_SpriteBatch_isEmpty(lua_State *L)
  166. {
  167. SpriteBatch *t = luax_checkspritebatch(L, 1);
  168. luax_pushboolean(L, t->isEmpty());
  169. return 1;
  170. }
  171. int w_SpriteBatch_isFull(lua_State *L)
  172. {
  173. SpriteBatch *t = luax_checkspritebatch(L, 1);
  174. luax_pushboolean(L, t->isFull());
  175. return 1;
  176. }
  177. static const luaL_Reg functions[] =
  178. {
  179. { "add", w_SpriteBatch_add },
  180. { "addq", w_SpriteBatch_addq },
  181. { "set", w_SpriteBatch_set },
  182. { "setq", w_SpriteBatch_setq },
  183. { "clear", w_SpriteBatch_clear },
  184. { "bind", w_SpriteBatch_bind },
  185. { "unbind", w_SpriteBatch_unbind },
  186. { "setImage", w_SpriteBatch_setImage },
  187. { "getImage", w_SpriteBatch_getImage },
  188. { "setColor", w_SpriteBatch_setColor },
  189. { "isEmpty", w_SpriteBatch_isEmpty },
  190. { "isFull", w_SpriteBatch_isFull },
  191. { 0, 0 }
  192. };
  193. extern "C" int luaopen_spritebatch(lua_State *L)
  194. {
  195. return luax_register_type(L, "SpriteBatch", functions);
  196. }
  197. } // opengl
  198. } // graphics
  199. } // love