wrap_SpriteBatch.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. Quad *quad = 0;
  36. int startidx = 2;
  37. if (luax_istype(L, 2, GRAPHICS_QUAD_T))
  38. {
  39. quad = luax_totype<Quad>(L, 2, "Quad", GRAPHICS_QUAD_T);
  40. startidx = 3;
  41. }
  42. float x = (float) luaL_optnumber(L, startidx + 0, 0.0);
  43. float y = (float) luaL_optnumber(L, startidx + 1, 0.0);
  44. float a = (float) luaL_optnumber(L, startidx + 2, 0.0);
  45. float sx = (float) luaL_optnumber(L, startidx + 3, 1.0);
  46. float sy = (float) luaL_optnumber(L, startidx + 4, sx);
  47. float ox = (float) luaL_optnumber(L, startidx + 5, 0.0);
  48. float oy = (float) luaL_optnumber(L, startidx + 6, 0.0);
  49. float kx = (float) luaL_optnumber(L, startidx + 7, 0.0);
  50. float ky = (float) luaL_optnumber(L, startidx + 8, 0.0);
  51. int id = 0;
  52. EXCEPT_GUARD(
  53. if (quad)
  54. id = t->addq(quad, x, y, a, sx, sy, ox, oy, kx, ky);
  55. else
  56. id = t->add(x, y, a, sx, sy, ox, oy, kx, ky);
  57. )
  58. lua_pushinteger(L, id);
  59. return 1;
  60. }
  61. int w_SpriteBatch_set(lua_State *L)
  62. {
  63. SpriteBatch *t = luax_checkspritebatch(L, 1);
  64. int id = luaL_checkinteger(L, 2);
  65. Quad *quad = 0;
  66. int startidx = 3;
  67. if (luax_istype(L, 3, GRAPHICS_QUAD_T))
  68. {
  69. quad = luax_totype<Quad>(L, 3, "Quad", GRAPHICS_QUAD_T);
  70. startidx = 4;
  71. }
  72. float x = (float) luaL_optnumber(L, startidx + 0, 0.0);
  73. float y = (float) luaL_optnumber(L, startidx + 1, 0.0);
  74. float a = (float) luaL_optnumber(L, startidx + 2, 0.0);
  75. float sx = (float) luaL_optnumber(L, startidx + 3, 1.0);
  76. float sy = (float) luaL_optnumber(L, startidx + 4, sx);
  77. float ox = (float) luaL_optnumber(L, startidx + 5, 0.0);
  78. float oy = (float) luaL_optnumber(L, startidx + 6, 0.0);
  79. float kx = (float) luaL_optnumber(L, startidx + 7, 0.0);
  80. float ky = (float) luaL_optnumber(L, startidx + 8, 0.0);
  81. EXCEPT_GUARD(
  82. if (quad)
  83. t->addq(quad, x, y, a, sx, sy, ox, oy, kx, ky, id);
  84. else
  85. t->add(x, y, a, sx, sy, ox, oy, kx, ky, id);
  86. )
  87. return 0;
  88. }
  89. int w_SpriteBatch_clear(lua_State *L)
  90. {
  91. SpriteBatch *t = luax_checkspritebatch(L, 1);
  92. t->clear();
  93. return 0;
  94. }
  95. int w_SpriteBatch_bind(lua_State *L)
  96. {
  97. SpriteBatch *t = luax_checkspritebatch(L, 1);
  98. EXCEPT_GUARD(t->lock();)
  99. return 0;
  100. }
  101. int w_SpriteBatch_unbind(lua_State *L)
  102. {
  103. SpriteBatch *t = luax_checkspritebatch(L, 1);
  104. t->unlock();
  105. return 0;
  106. }
  107. int w_SpriteBatch_setImage(lua_State *L)
  108. {
  109. SpriteBatch *t = luax_checkspritebatch(L, 1);
  110. Image *image = luax_checktype<Image>(L, 2, "Image", GRAPHICS_IMAGE_T);
  111. t->setImage(image);
  112. return 0;
  113. }
  114. int w_SpriteBatch_getImage(lua_State *L)
  115. {
  116. SpriteBatch *t = luax_checkspritebatch(L, 1);
  117. Image *image = t->getImage();
  118. image->retain();
  119. luax_pushtype(L, "Image", GRAPHICS_IMAGE_T, image);
  120. return 1;
  121. }
  122. int w_SpriteBatch_setColor(lua_State *L)
  123. {
  124. SpriteBatch *t = luax_checkspritebatch(L, 1);
  125. Color c;
  126. if (lua_gettop(L) <= 1)
  127. {
  128. t->setColor();
  129. return 0;
  130. }
  131. else if (lua_istable(L, 2))
  132. {
  133. for (int i = 1; i <= 4; i++)
  134. lua_rawgeti(L, 2, i);
  135. c.r = (unsigned char) luaL_checkinteger(L, -4);
  136. c.g = (unsigned char) luaL_checkinteger(L, -3);
  137. c.b = (unsigned char) luaL_checkinteger(L, -2);
  138. c.a = (unsigned char) luaL_optinteger(L, -1, 255);
  139. lua_pop(L, 4);
  140. }
  141. else
  142. {
  143. c.r = (unsigned char)luaL_checkinteger(L, 2);
  144. c.g = (unsigned char)luaL_checkinteger(L, 3);
  145. c.b = (unsigned char)luaL_checkinteger(L, 4);
  146. c.a = (unsigned char)luaL_optinteger(L, 5, 255);
  147. }
  148. t->setColor(c);
  149. return 0;
  150. }
  151. int w_SpriteBatch_getColor(lua_State *L)
  152. {
  153. SpriteBatch *t = luax_checkspritebatch(L, 1);
  154. const Color *color = t->getColor();
  155. // getColor returns NULL if no color is set.
  156. if (!color)
  157. return 0;
  158. lua_pushinteger(L, (lua_Integer) color->r);
  159. lua_pushinteger(L, (lua_Integer) color->g);
  160. lua_pushinteger(L, (lua_Integer) color->b);
  161. lua_pushinteger(L, (lua_Integer) color->a);
  162. return 4;
  163. }
  164. int w_SpriteBatch_getCount(lua_State *L)
  165. {
  166. SpriteBatch *t = luax_checkspritebatch(L, 1);
  167. lua_pushinteger(L, t->getCount());
  168. return 1;
  169. }
  170. int w_SpriteBatch_setBufferSize(lua_State *L)
  171. {
  172. SpriteBatch *t = luax_checkspritebatch(L, 1);
  173. int size = luaL_checkint(L, 2);
  174. EXCEPT_GUARD(t->setBufferSize(size);)
  175. return 0;
  176. }
  177. int w_SpriteBatch_getBufferSize(lua_State *L)
  178. {
  179. SpriteBatch *t = luax_checkspritebatch(L, 1);
  180. lua_pushinteger(L, t->getBufferSize());
  181. return 1;
  182. }
  183. static const luaL_Reg functions[] =
  184. {
  185. { "add", w_SpriteBatch_add },
  186. { "set", w_SpriteBatch_set },
  187. { "clear", w_SpriteBatch_clear },
  188. { "bind", w_SpriteBatch_bind },
  189. { "unbind", w_SpriteBatch_unbind },
  190. { "setImage", w_SpriteBatch_setImage },
  191. { "getImage", w_SpriteBatch_getImage },
  192. { "setColor", w_SpriteBatch_setColor },
  193. { "getColor", w_SpriteBatch_getColor },
  194. { "getCount", w_SpriteBatch_getCount },
  195. { "setBufferSize", w_SpriteBatch_setBufferSize },
  196. { "getBufferSize", w_SpriteBatch_getBufferSize },
  197. { 0, 0 }
  198. };
  199. extern "C" int luaopen_spritebatch(lua_State *L)
  200. {
  201. return luax_register_type(L, "SpriteBatch", functions);
  202. }
  203. } // opengl
  204. } // graphics
  205. } // love